HTTP Socket Tester

By Wims on Jun 12, 2015

This is a first version of a dialog to quickly make an HTTP request with socket, I'll improve it later.
Load the code and type /http_tester to open the dialog

alias HTTP_tester {
  if ($dialog(test_sock)) dialog -v test_sock
  else dialog -m test_sock test_sock
}
dialog test_sock1 {
  title "HTTP Socket Tester"
  size -1 -1 336 268
  option dbu
  edit "", 1, 13 18 283 84, read multi hsbar vsbar
  box "Headers", 2, 11 6 286 98
  box "Content", 3, 2 108 332 128
  edit "", 4, 4 120 328 114, read multi hsbar vsbar
}
dialog Test_Sock {
  title "HTTP Socket Tester"
  size -1 -1 206 266
  option dbu
  text "URL", 1, 6 5 12 8
  edit "", 2, 25 3 168 10, autohs
  radio "GET", 3, 30 51 50 10, group
  radio "POST", 4, 116 52 50 10
  list 5, 7 87 95 90, size hsbar vsbar extsel multsel
  box "Name", 6, 5 76 99 103
  box "Headers", 7, 5 68 196 127
  list 8, 105 87 94 90, size hsbar vsbar extsel multsel
  box "Value", 9, 103 76 98 103
  button "Add", 10, 9 180 37 12
  button "Move Up", 11, 54 180 37 12, disable
  button "Move Down", 12, 100 180 37 12, disable
  box "Data", 13, 5 196 196 48
  radio "HTTP 1.1", 14, 30 27 50 10, group
  radio "HTTP 1.0", 15, 116 28 50 10
  box "Protocol", 16, 5 17 196 24
  box "Method", 17, 5 42 196 24
  edit "", 18, 8 206 191 34, multi return hsbar vsbar disable
  button "Go", 19, 80 250 37 12, disable
}
on *:dialog:test_sock:*:*:{
  if ($devent == init) {
    did -c $dname 3,14
  }
  elseif ($devent == close) {
    if ($hget(test_sock)) hfree test_sock
    sockclose test_sock
    if ($dialog(test_sock1)) dialog -x test_sock1
  }
  elseif ($devent == dclick) {
    if ($did == 5) {
      did -d $dname 5,8 $did(5).sel
      did -b $dname 11,12
    }
    elseif ($did == 8) {
      did -d $dname 5,8 $did(8).sel
      did -b $dname 11,12
    }
  }
  elseif ($devent == sclick) {
    if ($did == 3) did -b $dname 18
    elseif ($did == 19) test_sock_go
    elseif ($did == 4) did -e $dname 18
    elseif ($did == 11) {
      if ($did(5).sel > 1) {
        var %p $did(5,$calc($v1 - 1)),%pp $did(8,$calc($v1 - 1))
        did -d $dname 5,8 $calc($v1 - 1)
        did -i $dname 5 $v1 %p
        did -i $dname 8 $v1 %pp
      }
    }
    elseif ($did == 12) {
      if ($did(5).sel < $did(5).lines) {
        var %p $did(5,$calc($v1 + 1)),%pp $did(8,$calc($v1 + 1))
        did -d $dname 5,8 $calc($v1 + 1)
        did -i $dname 5 $v1 %p
        did -i $dname 8 $v1 %pp
      }
    }
    elseif ($did == 5) {
      if ($did(5,0).sel == 1) && ($did(5).lines > 1) did -e $dname 11,12
      else did -b $dname 11,12
      did -u $dname 8
      var %a 1
      while ($did(5,%a).sel) {
        did -ck $dname 8 $v1
        inc %a
      }
    }
    elseif ($did == 8) {
      if ($did(8,0).sel == 1) && ($did(8).lines > 1) did -e $dname 11,12
      else did -b $dname 11,12
      did -u $dname 5
      var %a 1
      while ($did(8,%a).sel) {
        did -ck $dname 5 $v1
        inc %a
      }
    }
    elseif ($did == 10) {
      var %h Accept,Accept-Charset,Accept-Encoding,Accept-Language,Accept-Datetime,Authorization,Cache-Control,Connection,Cookie,Content-Length,Content-MD5,Content-Type,Date,Expect,From,Host,If-Match,If-Modified-Since,If-None-Match,If-Range,If-Unmodified-Since,Max-Forwards,Origin,Pragma,Proxy-Authorization,Range,Referer,TE,User-Agent,Upgrade,Via,Warning
      if ($input($+(Select the header,$crlf,Leave the field empty for a non standard header),fmu,HTTP Socket tester,1, [ %h ] ) != $false) {
        if ($v1 == $null) {
          if ($input(Enter the non standard header,euf,HTTP Socket tester) == $false) || ($v1 == $null) return
          var %header $v1
        }
        else var %header $v1
      }
      if (%header != $null) {
        if ($input(Enter the value for that header,euf,HTTP Socket tester) == $false) || ($v1 == $null) return
        else var %value $v1
        if ($didwm(test_sock,5,%header)) {
          if ($input(You already have an header named $qt(%header) $+ $chr(44) would you like to add it anyway?,uyw) == $false) return
        }
        did -a test_sock 5 %header
        did -a test_sock 8 %value
      }
    }
  }
  elseif ($devent == edit) {
    if ($did == 2) {
      var %p m@^(https?://)?((?:www\.)?[^/]+)(/.*|)$@
      if ($regex($did(2),%p)) {
        if (http* iswm $regml(1)) {
          if (https* iswm $regml(1)) hadd -m test_sock SSL 1
          else hadd -m test_sock SSL 0
          hadd -m test_sock host $regml(2)
          hadd test_sock page $regml(3)
        }
        else {
          hadd -m test_sock SSL 0
          hadd test_sock host $regml(1)
          hadd test_sock page $regml(2)
        }
        if ($did(14).state) {
          if ($didwm(test_sock,5,Host)) did -d test_sock 5,8 $v1
          if ($hget(test_sock,host) != $null) {
            did -i test_sock 5 1 Host
            did -i test_sock 8 1 $hget(test_sock,host)
          }
        }
        if ($didwm(test_sock,5,Connection)) did -d test_sock 5,8 $v1
         did -i test_sock 5 2 Connection
          did -i test_sock 8 2 close
        ;  echo -a $regml(1) -- $regml(2) -- $regml(3) 
      }
      else {
        if ($hget(test_sock)) hfree test_sock
        if ($didwm(test_sock,5,Host)) did -d test_sock 5,8 $v1
        if ($didwm(test_sock,5,Connection)) did -d test_sock 5,8 $v1
      }
    }
    elseif ($did == 18) {
      if ($did(18) != $null) {
        if ($didwm(test_sock,5,Content-Length)) did -d test_sock 5,8 $v1
        did -a test_sock 5 Content-Length
        var %a 1,%n 
        while (%a <= $did(18).lines) {
          inc %n $calc($len($did(18,%a)) + 2)
          inc %a
        }
        did -a test_sock 8 %n
      }

      elseif ($didwm(test_sock,5,Content-Length)) did -d test_sock 5,8 $v1
    }
  }
  if ($hget(test_sock,host)) did -e $dname 19
  else did -b $dname 19
}
alias test_sock_go {
  .remove test_sock
  sockclose test_sock
  sockopen $iif($hget(test_sock,SSL),-e) test_sock $hget(test_sock,host) $iif($v1,443,80)
}
on *:sockopen:test_sock:{
  if ($sockerr) {
    echo -a error
  }
  else {
    if ($dialog(test_sock1)) {
      dialog -v test_sock1
      did -r test_sock1 1,2
    }
    else {
      dialog -m test_sock1 test_sock1
    }
    var %s sockwrite -n $sockname
    %s $iif($did(test_sock,3).state,GET,POST) $iif($hget(test_sock,page),$v1,/) $iif($did(test_sock,14).state,HTTP/1.1,HTTP/1.0)
    var %a 1
    while ($did(test_sock,5,%a) != $null) {
      %s $v1 $+ : $did(test_sock,8,%a)
      inc %a
    }
    %s
    if ($did(test_sock,18) != $null) {
      var %a 1
      while (%a <= $did(test_sock,18).lines) {
        sockwrite $sockname $did(test_sock,18,%a)
        inc %a
      }
    }
  }
}

on *:sockread:test_sock:{
  if ($sockerr) {
    echo -a error sockread
  }
  else {
    if (!$sock($sockname).mark) {
      var %a
      sockread %a
      if ($sockbr) {
        if (%a == $null) {
          sockmark $sockname 1
          if ($sock($sockname).rq) goto read
        }
        else {
          did -a test_sock1 1 %a $+ $crlf
          if (Content-Length:* iswmcs %a) hadd test_sock len $gettok(%a,2,32)
        }
      } 
    }
    else {
      :read
      sockread -f $sock($sockname).rq &t
      bwrite test_sock -1 -1 &t
      hdec test_sock len $sockbr
      if (!$hget(test_sock,len)) {
        loadbuf -o test_sock1 4 test_sock
        .remove test_sock
      }
    }
  }
}

on *:dialog:test_sock1:close:0:sockclose test_sock

Comments

Sign in to comment.
^WeSt   -  Jun 13, 2015

Good work and very helpful..

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.