if {[string length $data] == 0} {
close $fromSock
catch { close $toSock }
- insertData meta "----- closed connection -----"
+ INFO "----- closed connection $fromSock \[eof\] -----"
INFO "waiting for new connection..."
return
}
- if {$toSock == ""} { ;# Not connected yet
- ProxyConnect $fromSock $data ;# Do proxy forwarding
+ if {$toSock == ""} { ;# Make proxy connection
+ if {[catch {ProxyConnect $fromSock $data} err]} {
+ INFO $err
+ close $fromSock
+ INFO "----- closed connection $fromSock -----"
+ INFO "waiting for new connection..."
+ return 0
+ }
} else {
catch { puts -nonewline $toSock $data } ;# Forward if we have a socket
}
insertData $who $data
- update
+ update idletasks
}
##+##########################################################################
#
# to forward to.
#
proc ProxyConnect {fromSock data} {
- set line1 [lindex [split $data \r] 0]
- set bad [regexp -nocase {(http:[^ ]+)} $line1 => uri]
+ set line1 [string range $data 0 [string first "\n" $data]]
+ set line1 [string trimright $line1 "\r\n"]
+ INFO "proxy request \"$line1\"" meta2
+ set bad [regexp {^([^ ]+) +([^ ]+)} $line1 -> method uri]
if {$bad == 0} {
- INFO "ERROR: cannot extract URI from '$line1'"
- close $fromSock
- insertData meta "----- closed connection -----"
- insertData meta "waiting for new connection..."
+ return -code error "failed to parse a uri from '$line1'"
}
set state(uri) $uri ;# For debugging
array set URI [::uri::split $uri]
+ if {$URI(scheme) != "http"} {
+ return -code error "cannot proxy the '$URI(scheme)' protocol"
+ }
if {$URI(port) == ""} { set URI(port) 80 }
set bad [catch {set sockServ [socket $URI(host) $URI(port)]} reason]
if {$bad} {
- set msg "cannot connect to $URI(host):$URI(port) => $reason"
- INFO $msg
- close $fromSock
- ERROR $msg
- insertData meta "----- closed connection -----"
- insertData meta "waiting for new connection..."
- return
+ return -code error "cannot connect to $URI(host):$URI(port) => $reason"
}
- INFO "fowarding to $URI(host):$URI(port)" meta2
+ INFO "fowarding $method request to $URI(host):$URI(port)" meta2
fileevent $fromSock readable \
[list sockReadable $fromSock $sockServ client]
fconfigure $sockServ -blocking 0 -buffering none -translation binary
fileevent $sockServ readable \
[list sockReadable $sockServ $fromSock server]
puts -nonewline $sockServ $data
+ return
}
+
##+##########################################################################
#
# clntConnect -- Called when we get a new client connection