Merge branch 'master' into bug-4403-remove-polyfill
[maxima.git] / interfaces / xmaxima / Tkmaxima / Proxy.tcl
blobfb9b02eb93f0db4437e7a072600f32158ba13379
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
15 # an error.
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
20 # Side Effects:
22 #----------------------------------------------------------------
24 proc openSocketAndSend { host port msg { verify 0}} {
25 global maxima_priv pdata
26 dtrace
27 if { [info exists maxima_priv(proxy,http)] } {
28 global pdata
29 set magic "billy-[clock clicks]"
30 debugsend "sendViaProxy $msg $host $port $magic"
32 set sock [sendViaProxy $msg $host $port $magic]
34 if { $verify } {
35 fconfigure $sock -blocking 1 -translation {crlf binary}
36 gets $sock tem
37 if { [regexp "503" $tem] } {
38 error [concat [mc "Could not connect"] "$host $port"]
40 while { 1 } {
41 gets $sock tem
42 if { [string length $tem] == 0 } { break }
45 set pdata($sock,proxyto) [list $host $port $magic]
46 fconfigure $sock -blocking 0
47 return $sock
50 } else {
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
57 flush $sock
58 return $sock
67 #-----------------------------------------------------------------
69 # proxyPuts -- send the MESSAGE to SOCK, not appending a newline.
71 # Results: none
73 # Side Effects: message sent
75 #----------------------------------------------------------------
77 proc proxyPuts { sock message } {
78 global pdata
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]
83 } else {
84 puts -nonewline $sock $message
85 flush $sock
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
101 # filename
103 #----------------------------------------------------------------
105 proc sendViaProxy { message host port magic } {
106 global maxima_priv
107 dtrace
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>"
113 puts $ss $request
114 flush $ss
115 return $ss
118 ## endsource proxy.tcl