/usr/lib/aolserver4/modules/tcl/http.tcl is in aolserver4-daemon 4.5.1-15.
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | #
# The contents of this file are subject to the AOLserver Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://aolserver.com/.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is AOLserver Code and related documentation
# distributed by AOL.
#
# The Initial Developer of the Original Code is America Online,
# Inc. Portions created by AOL are Copyright (C) 1999 America Online,
# Inc. All Rights Reserved.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above. If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#
#
# $Header: /cvsroot/aolserver/aolserver/tcl/http.tcl,v 1.15 2009/01/19 12:21:08 gneumann Exp $
#
# http.tcl -
# Routines for opening non-blocking HTTP connections through
# the Tcl socket interface.
#
#
# ==========================================================================
# API procs
# ==========================================================================
#
# ns_httpopen -
# Fetch a web page; see docs for full details.
#
# Results:
# A tcl list {read file handle} {write file handle} {result headers set}
#
# Side effects:
# May throw an error on failure.
#
proc ns_httpopen {method url {rqset ""} {timeout 30} {pdata ""}} {
#
# Determine if url is local; prepend site address if so
#
if {[string match /* $url]} {
set host "http://[ns_config ns/server/[ns_info server]/module/nssock hostname]"
set port [ns_config ns/server/[ns_info server]/module/nssock port]
if { $port != 80 } {
append host ":$port"
}
set url "$host$url"
}
#
# Verify that the URL is an HTTP url.
#
if {![string match "http://*" $url]} {
return -code error "Invalid url \"$url\": "\
"ns_httpopen only supports HTTP"
}
#
# Find each element in the URL
#
set url [split $url /]
set hp [split [lindex $url 2] :]
set host [lindex $hp 0]
set port [lindex $hp 1]
if {$port eq ""} {
set port 80
}
set uri /[join [lrange $url 3 end] /]
#
# Open a TCP connection to the host:port
#
set fds [ns_sockopen -nonblock $host $port]
set rfd [lindex $fds 0]
set wfd [lindex $fds 1]
if {[catch {
#
# First write the request, then the headers if they exist.
#
_ns_http_puts $timeout $wfd "$method $uri HTTP/1.0\r"
if {$rqset ne ""} {
#
# There are request headers
#
for {set i 0} {$i < [ns_set size $rqset]} {incr i} {
set key [ns_set key $rqset $i]
set val [ns_set value $rqset $i]
_ns_http_puts $timeout $wfd "$key: $val\r"
}
} else {
#
# No headers were specified, so send a minimum set of
# required headers.
#
_ns_http_puts $timeout $wfd "Accept: */*\r"
_ns_http_puts $timeout $wfd \
"User-Agent: [ns_info name]-Tcl/[ns_info version]\r"
}
#
# Always send a Host: header because virtual hosting happens
# even with HTTP/1.0.
#
if { $port == 80 } {
set hostheader "Host: ${host}\r"
} else {
set hostheader "Host: ${host}:${port}\r"
}
_ns_http_puts $timeout $wfd $hostheader
#
# If optional content exists, then output that. Otherwise spit
# out a newline to end the headers.
#
if {$pdata ne ""} {
_ns_http_puts $timeout $wfd "\r\n$pdata\r"
} else {
_ns_http_puts $timeout $wfd "\r"
}
flush $wfd
#
# Create a new set; its name will be the result line from
# the server. Then read headers into the set.
#
set rpset [ns_set new [_ns_http_gets $timeout $rfd]]
while {1} {
set line [_ns_http_gets $timeout $rfd]
if {![string length $line]} {
break
}
ns_parseheader $rpset $line
}
} errMsg]} {
#
# Something went wrong during the request, so return an error.
#
global errorInfo
close $wfd
close $rfd
if {[info exists rpset]} {
ns_set free $rpset
}
return -code error -errorinfo $errorInfo $errMsg
}
#
# Return a list of read file, write file, and headers set.
#
return [list $rfd $wfd $rpset]
}
#
# ns_httppost -
# Perform a POST request. This wraps ns_httpopen.
#
# Results:
# The URL content.
#
# Side effects:
#
proc ns_httppost {url {rqset ""} {qsset ""} {type ""} {timeout 30}} {
#
# Build the request. Since we're posting, we have to set
# content-type and content-length ourselves. We'll add these to
# rqset, overwriting if they already existed, which they
# shouldn't.
#
if {$rqset eq ""} {
set rqset [ns_set new rqset]
ns_set put $rqset "Accept" "*/*\r"
ns_set put $rqset "User-Agent" "[ns_info name]-Tcl/[ns_info version]\r"
}
if {$type eq ""} {
ns_set put $rqset "Content-type" "application/x-www-form-urlencoded"
} else {
ns_set put $rqset "Content-type" "$type"
}
#
# Build the query string to POST with
#
set querystring ""
if {$qsset ne ""} {
for {set i 0} {$i < [ns_set size $qsset]} {incr i} {
set key [ns_set key $qsset $i]
set value [ns_set value $qsset $i]
if { $i > 0 } {
append querystring "&"
}
append querystring "$key=[ns_urlencode $value]"
}
ns_log debug "QS that will be sent is $querystring"
ns_set put $rqset "Content-length" [string length $querystring]
} else {
ns_log debug "QS string is empty"
ns_set put $rqset "Content-length" "0"
}
#
# Perform the actual request.
#
set http [ns_httpopen POST $url $rqset $timeout $querystring]
set rfd [lindex $http 0]
close [lindex $http 1]
set headers [lindex $http 2]
set length [ns_set iget $headers content-length]
if {$length eq ""} {
set length -1
}
set err [catch {
#
# Read the content.
#
while {1} {
set buf [_ns_http_read $timeout $rfd $length]
append page $buf
if {$buf eq ""} {
break
}
if {$length > 0} {
incr length -[string length $buf]
if {$length <= 0} {
break
}
}
}
} errMsg]
ns_set free $headers
close $rfd
if {$err} {
global errorInfo
return -code error -errorinfo $errorInfo $errMsg
}
return $page
}
#
# ns_httpget -
# Perform a GET request. This wraps ns_httpopen, but it also
# knows how to follow redirects and will read the content into
# a buffer.
#
# Results:
# The URL content.
#
# Side effects:
# Will only follow redirections 10 levels deep.
#
proc ns_httpget {url {timeout 30} {depth 0} {rqset ""}} {
if {[incr depth] > 10} {
return -code error "ns_httpget: Recursive redirection: $url"
}
#
# Perform the actual request.
#
set http [ns_httpopen GET $url $rqset $timeout]
set rfd [lindex $http 0]
close [lindex $http 1]
set headers [lindex $http 2]
set response [ns_set name $headers]
set status [lindex $response 1]
if {$status == 302} {
#
# The response was a redirect, so free the headers and
# recurse.
#
set location [ns_set iget $headers location]
if {$location ne ""} {
ns_set free $headers
close $rfd
if {[string first http:// $location] != 0} {
set url2 [split $url /]
set hp [split [lindex $url2 2] :]
set host [lindex $hp 0]
set port [lindex $hp 1]
if {$port eq ""} {
set port 80
}
regexp "^(.*)://" $url match method
set location "$method://$host:$port/$location"
}
return [ns_httpget $location $timeout $depth]
}
}
set length [ns_set iget $headers content-length]
if {$length eq ""} {
set length -1
}
set err [catch {
#
# Read the content.
#
while {1} {
set buf [_ns_http_read $timeout $rfd $length]
append page $buf
if {$buf eq ""} {
break
}
if {$length > 0} {
incr length -[string length $buf]
if {$length <= 0} {
break
}
}
}
} errMsg]
ns_set free $headers
close $rfd
if {$err} {
global errorInfo
return -code error -errorinfo $errorInfo $errMsg
}
return $page
}
# ==========================================================================
# Local procs
# ==========================================================================
#
# _ns_http_readable -
# Return the number of bytes available to read from a
# socket without blocking, waiting up to $timeout seconds for bytes to
# arrive if none are currently available.
#
# Results:
# Number of bytes that are waiting to be read
#
proc _ns_http_readable {timeout sock} {
set nread [ns_socknread $sock]
if {!$nread} {
set sel [ns_sockselect -timeout $timeout $sock {} {}]
if {"" eq [lindex $sel 0]} {
return -code error "ns_sockreadwait: Timeout waiting for remote"
}
set nread [ns_socknread $sock]
}
return $nread
}
#
# _ns_http_read -
# Read upto $length bytes from a socket without blocking.
#
# Results:
# Up to $length bytes that were read from the socket. May
# throw and error on EOF.
#
proc _ns_http_read {timeout sock length} {
set buf ""
set nread [_ns_http_readable $timeout $sock]
if {$nread > 0} {
if {$length > 0 && $length < $nread} {
set nread $length
}
set buf [read $sock $nread]
}
return $buf
}
#
# _ns_http_gets -
# Carefully read lines, one character at a time, from a
# socket to avoid blocking.
#
# Results:
# One line read from the socket
#
proc _ns_http_gets {timeout sock} {
set line ""
set done 0
while {!$done} {
set nline [_ns_http_readable $timeout $sock]
if {!$nline} {set done 1}
while {!$done && $nline > 0} {
set char [read $sock 1]
if {$char eq "\n"} {set done 1}
append line $char
incr nline -1
}
}
string trimright $line
}
#
# _ns_http_puts -
# Send a string out a socket. If the socket buffer is
# full, wait for up to $timeout seconds.
#
# Results:
# None.
#
# Side effects:
# May return with an error code on timeout.
#
proc _ns_http_puts {timeout sock string} {
if {[lindex [ns_sockselect -timeout $timeout {} $sock {}] 1] == ""} {
return -code error "ns_socksend: Timeout writing to socket"
}
puts $sock $string
}
|