/usr/share/elmerpost/help/http.tcl is in elmer-common 6.1.0.svn.5396.dfsg-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | # http.tcl
# Client-side HTTP for GET, POST, and HEAD commands.
# These routines can be used in untrusted code that uses the Safesock
# security policy.
# These procedures use a callback interface to avoid using vwait,
# which is not defined in the safe base.
#
# SCCS: @(#) http.tcl 1.2 97/01/22 13:12:02
#
# See the http.n man page for documentation
package provide http 1.0
if {[info commands "unsupported0"] == "unsupported0"} {
rename unsupported0 copychannel
}
array set http {
-proxyhost {}
-proxyport {}
-useragent {Tcl http client package 1.0}
-proxyfilter httpProxyRequired
}
proc http_config {args} {
global http
if {[llength $args] == 0} {
set result {}
foreach name [lsort [array names http -*]] {
lappend result $name $http($name)
}
return $result
} elseif {[llength $args] == 1} {
set flag [lindex $args 0]
if {[regexp -- {^-(proxyhost|proxyport|proxyfilter|agent)$} $flag]} {
return $http($flag)
} else {
return -code error "Unknown option $flag, must be -proxyfilter, -proxyhost, -proxyport, or -useragent"
}
} else {
foreach {flag value} $args {
switch -- $flag {
-proxyhost -
-proxyport -
-proxyfilter -
-useragent {
set http($flag) $value
}
default {
return -code error "Unknown option $flag, must be -proxyfilter, -proxyhost, -proxyport, or -useragent"
}
}
}
}
}
proc http_reset { token } {
upvar #0 $token state
set state(status) reset
catch {fileevent $state(sock) readable {}}
catch {eval $state(-command) {$token}}
catch {close $state(sock)}
catch {unset state}
}
proc http_get { url args } {
global http
if ![info exists http(uid)] {
set http(uid) 0
}
set token http#[incr http(uid)]
upvar #0 $token state
http_reset $token
array set state {
-command {# }
-blocksize 8192
-validate 0
-headers {}
state header
meta {}
currentsize 0
totalsize 0
type text/html
body {}
status ""
}
foreach {flag value} $args {
switch -- $flag {
-blocksize -
-channel -
-command -
-headers -
-progress -
-query -
-validate {
set state($flag) $value
}
default {
return -code error "Unknown option $flag: can be -blocksize, -channel, -command, -headers, -progress, -query, or -validate"
}
}
}
if {! [regexp -nocase {^(http://)?([^/:]+)(:([0-9]+))?(/.*)} $url \
x proto host y port srvurl]} {
error "Unsupported URL: $url"
}
if {[string length $port] == 0} {
set port 80
}
if {[string length $proto] == 0} {
set url http://$url
}
set state(url) $url
if {![catch {$http(-proxyfilter) $host} proxy]} {
set phost [lindex $proxy 0]
set pport [lindex $proxy 1]
}
if {[info exists phost] && [string length $phost]} {
set srvurl $url
set s [socket $phost $pport]
} else {
set s [socket $host $port]
}
set state(sock) $s
# Send data in cr-lf format, but accept any line terminators
fconfigure $s -translation {auto crlf} -buffersize $state(-blocksize)
# this is disallowed in safe interpreters, but the socket
# is already in non-blocking mode in that case.
catch {fconfigure $s -blocking off}
set len 0
set how GET
if {[info exists state(-query)]} {
set len [string length $state(-query)]
if {$len > 0} {
set how POST
}
} elseif {$state(-validate)} {
set how HEAD
}
puts $s "$how $srvurl HTTP/1.0"
puts $s "Accept: */*"
puts $s "Host: $host"
puts $s "User-Agent: $http(-useragent)"
foreach {key value} $state(-headers) {
regsub -all \[\n\r\] $value {} value
set key [string trim $key]
if {[string length $key]} {
puts $s "$key: $value"
}
}
if {$len > 0} {
puts $s "Content-Length: $len"
puts $s "Content-Transfer-Encoding: x-url-encoding"
puts $s ""
fconfigure $s -translation {auto binary}
puts $s $state(-query)
} else {
puts $s ""
}
flush $s
fileevent $s readable [list httpEvent $token]
return $token
}
proc httpEvent {token} {
upvar #0 $token state
set s $state(sock)
if [eof $s] then {
close $s
if {$state(state) == "header"} {
# Premature eof
set state(status) eof
} else {
set state(status) ok
}
set state(state) eof
eval $state(-command) {$token}
return
}
if {$state(state) == "header"} {
set n [gets $s line]
if {$n == 0} {
set state(state) body
if ![regexp -nocase ^text $state(type)] {
# Turn off conversions for non-text data
fconfigure $s -translation binary
}
} elseif {$n > 0} {
if [regexp -nocase {^content-type:(.+)$} $line x type] {
set state(type) [string trim $type]
}
if [regexp -nocase {^content-length:(.+)$} $line x length] {
set state(totalsize) [string trim $length]
}
if [regexp -nocase {^([^:]+):(.+)$} $line x key value] {
lappend state(meta) $key $value
} elseif {[regexp ^HTTP $line]} {
set state(http) $line
}
}
} else {
if [catch {
# if [info exists state(-image)] {
# $state(-image) config -channel $s
# } else
if {[info exists state(-channel)]} {
set n [copychannel $s $state(-channel) $state(-blocksize)]
} else {
set block [read $s $state(-blocksize)]
set n [string length $block]
if {$n >= 0} {
append state(body) $block
}
}
if {$n >= 0} {
incr state(currentsize) $n
}
} err] {
set state(error) $err
http_reset $token
} else {
if [info exists state(-progress)] {
eval $state(-progress) {$token $state(totalsize) $state(currentsize)}
}
}
}
}
proc http_wait {token} {
upvar #0 $token state
if {![info exists state(status)] || [string length $state(status)] == 0} {
vwait $token\(status)
}
return $state(status)
}
# Call http_formatQuery with an even number of arguments, where the first is
# a name, the second is a value, the third is another name, and so on.
proc http_formatQuery {args} {
set result ""
set sep ""
foreach i $args {
append result $sep [httpMapReply $i]
if {$sep != "="} {
set sep =
} else {
set sep &
}
}
return $result
}
# do x-www-urlencoded character mapping
# The spec says: "non-alphanumeric characters are replaced by '%HH'"
# 1 leave alphanumerics characters alone
# 2 Convert every other character to an array lookup
# 3 Escape constructs that are "special" to the tcl parser
# 4 "subst" the result, doing all the array substitutions
proc httpMapReply {string} {
global httpFormMap
set alphanumeric a-zA-Z0-9
if ![info exists httpFormMap] {
for {set i 1} {$i <= 256} {incr i} {
set c [format %c $i]
if {![string match \[$alphanumeric\] $c]} {
set httpFormMap($c) %[format %.2x $i]
}
}
# These are handled specially
array set httpFormMap {
" " + \n %0d%0a
}
}
regsub -all \[^$alphanumeric\] $string {$httpFormMap(&)} string
regsub -all \n $string {\\n} string
regsub -all \t $string {\\t} string
regsub -all {[][{})\\]\)} $string {\\&} string
return [subst $string]
}
# Default proxy filter.
proc httpProxyRequired {host} {
global http
if {[info exists http(-proxyhost)] && [string length $http(-proxyhost)]} {
if {![info exists http(-proxyport)] || ![string length $http(-proxyport)]} {
set http(-proxyport) 8080
}
return [list $http(-proxyhost) $http(-proxyport)]
} else {
return {}
}
}
|