alias urlget.test {
urlget.listen
var %url = $iif($1,$1,http://localhost:8080/relative)
var %id = $urlget(%url,gb,&target,urlget.callback,)
}
alias urlget.callback {
var %id = $1
echo -ag -
echo -agi9 url $urlget(%id).url
echo -agi9 redirect $urlget(%id).redirect
echo -agi9 method $urlget(%id).method
echo -agi9 type $urlget(%id).type
echo -agi9 target $urlget(%id).target
echo -agi9 alias $urlget(%id).alias
echo -agi9 id $urlget(%id).id
echo -agi9 state $urlget(%id).state
echo -agi9 size $urlget(%id).size
echo -agi9 resume $urlget(%id).resume
echo -agi9 rcvd $urlget(%id).rcvd
echo -agi9 time $urlget(%id).time
echo -agi9 reply $urlget(%id).reply
if ($urlget(%id).type == binvar) && ($bvar($urlget(%id).target,0)) {
echo -agi9 response $bvar($urlget(%id).target,1-3000).text
}
}
alias urlget.listen {
if (!$sock(urlget.listen)) socklisten -d 127.0.0.1 urlget.listen 80
if (!$sock(urlget.listen2)) socklisten -d 127.0.0.1 urlget.listen2 8080
}
on *:socklisten:urlget.listen*:{
echo -ag -
echo 4 -ag Request on $iif($sockname == urlget.listen,80,8080)
var %sockname = urlget.client. $+ $ticks
if ($sock(%sockname)) return
sockaccept %sockname
}
on *:sockread:urlget.client.*:{
var %header
if (!$sock($sockname).mark) {
sockread %header
while (%header != $null) {
if ($regex(%header,/GET (\S+)/)) {
var %request = $regml(1)
}
echo 3 -ag > %header
if (sub isin %header) %x = $true
if ($regex(%header,Content-Length: (\d+))) {
hadd -m $sockname content-length $regml(1)
}
sockread %header
}
if ($sockbr) sockmark $sockname $true
}
if ($sock($sockname).mark) && ($sock($sockname).rq) {
sockread &read
while ($sockbr) {
hinc $sockname content-read $sockbr
echo 6 -agi2 > $bvar(&read,1-3000).text
sockread &read
}
}
if ($hget($sockname,content-length) == 0) || ($v1 == $hget($sockname,content-read)) {
var %redirect, %data
if (/relative/sub2 == %request) %data = success
else if (/relative/sub isin %request) %redirect = ./sub2
else if (/relative isin %request) %redirect = /relative/sub
else %data = failure
noop $socket.respond($sockname,%data,%redirect)
}
}
alias -l sockwrite {
echo 12 -ag < $3-
sockwrite $1-
}
alias -l socket.respond {
var %sockname = $$1, %data = $2, %redirect = $3
if ($3) sockwrite -n %sockname HTTP/1.1 302 Temporary Redirect
else sockwrite -n %sockname HTTP/1.1 200 OK
if ($3) sockwrite -n %sockname Location: %redirect
sockwrite -n %sockname Connection: close
sockwrite -n %sockname Content-Length: $len(%data)
sockwrite -n %sockname $+($crlf,%data)
}