1 # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
3 # $Id: Proxy.tcl,v 1.4 2004-10-13 12:08:58 vvzhy Exp $
5 ###### Proxy.tcl ######
10 #-----------------------------------------------------------------
12 # openSocketAndSend -- open a Socket to HOST on PORT and then
13 # send the message MSG to it. If verify is non 0, then read
14 # up through the end of the http header and verify this is not
17 # Results: returns a socket which you can read from using ordinary
18 # read and write, but to which you should write only using s
22 #----------------------------------------------------------------
24 proc openSocketAndSend
{ host port msg
{ verify
0}} {
25 global maxima_priv pdata
27 if { [info exists maxima_priv
(proxy
,http)] } {
29 set magic
"billy-[clock clicks]"
30 debugsend
"sendViaProxy $msg $host $port $magic"
32 set sock
[sendViaProxy
$msg $host $port $magic]
35 fconfigure $sock -blocking 1 -translation {crlf
binary}
37 if { [regexp "503" $tem] } {
38 error [concat [mc
"Could not connect"] "$host $port"]
42 if { [string length
$tem] == 0 } { break }
45 set pdata
($sock,proxyto
) [list $host $port $magic]
46 fconfigure $sock -blocking 0
51 set sock
[socket $host $port]
52 if {[info exists pdata
($sock,proxyto
)]} {
53 unset pdata
($sock,proxyto
)
55 fconfigure $sock -blocking 0
56 puts -nonewline $sock $msg
67 #-----------------------------------------------------------------
69 # proxyPuts -- send the MESSAGE to SOCK, not appending a newline.
73 # Side Effects: message sent
75 #----------------------------------------------------------------
77 proc proxyPuts
{ sock
message } {
79 debugsend
"proxyPuts $sock $message useproxy=[info exists pdata($sock,proxyto)]"
80 if { [info exists pdata
($sock,proxyto
)] } {
81 desetq
"host port magic" $pdata($sock,proxyto
)
82 close [sendViaProxy
$message $host $port $magic]
84 puts -nonewline $sock $message
91 #-----------------------------------------------------------------
93 # sendViaProxy -- send a message.
94 # this is a private function.
96 # Results: a socket one can read the answer from.
97 # Caller is responsible for closing the socket.
99 # Side Effects: socket opened and message sent as the body
100 # of a post. The magic is put in the http header request as the
103 #----------------------------------------------------------------
105 proc sendViaProxy
{ message host port magic
} {
108 set ss
[eval socket $maxima_priv(proxy
,http)]
109 fconfigure $ss -blocking 0
110 fconfigure $ss -translation {crlf
binary}
111 set request
[getURLrequest
http://$host:$port/$magic $host $port "" $message]
112 debugsend
"<$ss request=$request>"
118 ## endsource proxy.tcl