3 # Client-side HTTP for GET, POST, and HEAD commands. These routines can
4 # be used in untrusted code that uses the Safesock security policy.
5 # These procedures use a callback interface to avoid using vwait, which
6 # is not defined in the safe base.
8 # See the file "license.terms" for information on usage and redistribution of
9 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 # RCS: @(#) $Id: http.tcl,v 1.67.2.6 2009/04/09 17:05:39 dgp Exp $
13 package require Tcl 8.4
14 # Keep this in sync with pkgIndex.tcl and with the install directories in
16 package provide http 2.7.3
19 # Allow resourcing to not clobber existing data
22 if {![info exists http]} {
27 -proxyfilter http::ProxyRequired
30 set http(-useragent) "Tcl http client package [package provide http]"
34 # Set up the map for quoting chars. RFC3986 Section 2.3 say percent
35 # encode all except: "... percent-encoded octets in the ranges of
36 # ALPHA (%41-%5A and %61-%7A), DIGIT (%30-%39), hyphen (%2D), period
37 # (%2E), underscore (%5F), or tilde (%7E) should not be created by URI
39 for {set i 0} {$i <= 256} {incr i} {
41 if {![string match {[-._~a-zA-Z0-9]} $c]} {
42 set map($c) %[format %.2x $i]
45 # These are handled specially
47 variable formMap [array get map]
49 # Create a map for HTTP/1.1 open sockets
51 if {[info exists socketmap]} {
52 # Close but don't remove open sockets on re-init
53 foreach {url sock} [array get socketmap] {
57 array set socketmap {}
62 if {![info exists urlTypes]} {
63 set urlTypes(http) [list 80 ::socket]
66 variable encodings [string tolower [encoding names]]
67 # This can be changed, but iso8859-1 is the RFC standard.
68 variable defaultCharset
69 if {![info exists defaultCharset]} {
70 set defaultCharset "iso8859-1"
73 # Force RFC 3986 strictness in geturl url verification?
75 if {![info exists strict]} {
79 # Let user control default keepalive for compatibility
80 variable defaultKeepalive
81 if {![info exists defaultKeepalive]} {
82 set defaultKeepalive 0
85 namespace export geturl config reset wait formatQuery register unregister
86 # Useful, but not exported: data size status code
91 # Debugging output -- define this to observe HTTP/1.1 socket usage.
92 # Should echo any args received.
95 # msg Message to output
97 proc http::Log {args} {}
101 # See documentation for details.
104 # proto URL protocol prefix, e.g. https
105 # port Default port for protocol
106 # command Command to use to create socket
108 # list of port and command that was registered.
110 proc http::register {proto port command} {
112 set urlTypes($proto) [list $port $command]
115 # http::unregister --
117 # Unregisters URL protocol handler
120 # proto URL protocol prefix, e.g. https
122 # list of port and command that was unregistered.
124 proc http::unregister {proto} {
126 if {![info exists urlTypes($proto)]} {
127 return -code error "unsupported url type \"$proto\""
129 set old $urlTypes($proto)
130 unset urlTypes($proto)
136 # See documentation for details.
139 # args Options parsed by the procedure.
143 proc http::config {args} {
145 set options [lsort [array names http -*]]
146 set usage [join $options ", "]
147 if {[llength $args] == 0} {
149 foreach name $options {
150 lappend result $name $http($name)
154 set options [string map {- ""} $options]
155 set pat ^-(?:[join $options |])$
156 if {[llength $args] == 1} {
157 set flag [lindex $args 0]
158 if {![regexp -- $pat $flag]} {
159 return -code error "Unknown option $flag, must be: $usage"
163 foreach {flag value} $args {
164 if {![regexp -- $pat $flag]} {
165 return -code error "Unknown option $flag, must be: $usage"
167 set http($flag) $value
174 # Clean up the socket and eval close time callbacks
177 # token Connection token.
178 # errormsg (optional) If set, forces status to error.
179 # skipCB (optional) If set, don't call the -command callback. This
180 # is useful when geturl wants to throw an exception instead
181 # of calling the callback. That way, the same error isn't
182 # reported to two places.
187 proc http::Finish {token {errormsg ""} {skipCB 0}} {
190 global errorInfo errorCode
191 if {$errormsg ne ""} {
192 set state(error) [list $errormsg $errorInfo $errorCode]
193 set state(status) "error"
196 ($state(status) eq "timeout") || ($state(status) eq "error") ||
197 ([info exists state(connection)] && ($state(connection) eq "close"))
199 CloseSocket $state(sock) $token
201 if {[info exists state(after)]} {
202 after cancel $state(after)
204 if {[info exists state(-command)] && !$skipCB} {
205 if {[catch {eval $state(-command) {$token}} err]} {
206 if {$errormsg eq ""} {
207 set state(error) [list $err $errorInfo $errorCode]
208 set state(status) error
211 # Command callback may already have unset our state
212 unset -nocomplain state(-command)
216 # http::CloseSocket -
218 # Close a socket and remove it from the persistent sockets table. If
219 # possible an http token is included here but when we are called from a
220 # fileevent on remote closure we need to find the correct entry - hence
221 # the second section.
223 proc ::http::CloseSocket {s {token {}}} {
225 catch {fileevent $s readable {}}
230 if {[info exists state(socketinfo)]} {
231 set conn_id $state(socketinfo)
234 set map [array get socketmap]
235 set ndx [lsearch -exact $map $s]
238 set conn_id [lindex $map $ndx]
241 if {$conn_id eq {} || ![info exists socketmap($conn_id)]} {
242 Log "Closing socket $s (no connection info)"
243 if {[catch {close $s} err]} {
247 if {[info exists socketmap($conn_id)]} {
248 Log "Closing connection $conn_id (sock $socketmap($conn_id))"
249 if {[catch {close $socketmap($conn_id)} err]} {
252 unset socketmap($conn_id)
254 Log "Cannot close connection $conn_id - no socket in socket map"
261 # See documentation for details.
264 # token Connection token.
270 proc http::reset {token {why reset}} {
273 set state(status) $why
274 catch {fileevent $state(sock) readable {}}
275 catch {fileevent $state(sock) writable {}}
277 if {[info exists state(error)]} {
278 set errorlist $state(error)
280 eval ::error $errorlist
286 # Establishes a connection to a remote url via http.
289 # url The http URL to goget.
290 # args Option value pairs. Valid options include:
291 # -blocksize, -validate, -headers, -timeout
293 # Returns a token for this connection. This token is the name of an
294 # array that the caller should unset to garbage collect the state.
296 proc http::geturl {url args} {
299 variable defaultCharset
300 variable defaultKeepalive
303 # Initialize the state variable, an array. We'll return the name of this
304 # array as the token for the transaction.
306 if {![info exists http(uid)]} {
309 set token [namespace current]::[incr http(uid)]
314 # Process command options.
323 -type application/x-www-form-urlencoded
340 set state(-keepalive) $defaultKeepalive
341 set state(-strict) $strict
342 # These flags have their types verified [Bug 811170]
346 -queryblocksize integer
351 set state(charset) $defaultCharset
353 -binary -blocksize -channel -command -handler -headers -keepalive
354 -method -myaddr -progress -protocol -query -queryblocksize
355 -querychannel -queryprogress -strict -timeout -type -validate
357 set usage [join [lsort $options] ", "]
358 set options [string map {- ""} $options]
359 set pat ^-(?:[join $options |])$
360 foreach {flag value} $args {
361 if {[regexp -- $pat $flag]} {
364 [info exists type($flag)] &&
365 ![string is $type($flag) -strict $value]
369 "Bad value for $flag ($value), must be $type($flag)"
371 set state($flag) $value
374 return -code error "Unknown option $flag, can be: $usage"
378 # Make sure -query and -querychannel aren't both specified
380 set isQueryChannel [info exists state(-querychannel)]
381 set isQuery [info exists state(-query)]
382 if {$isQuery && $isQueryChannel} {
384 return -code error "Can't combine -query and -querychannel options!"
387 # Validate URL, determine the server host and port, and check proxy case
388 # Recognize user:pass@host URLs also, although we do not do anything with
391 # URLs have basically four parts.
392 # First, before the colon, is the protocol scheme (e.g. http)
393 # Second, for HTTP-like protocols, is the authority
394 # The authority is preceded by // and lasts up to (but not including)
395 # the following / and it identifies up to four parts, of which only one,
396 # the host, is required (if an authority is present at all). All other
397 # parts of the authority (user name, password, port number) are optional.
398 # Third is the resource name, which is split into two parts at a ?
399 # The first part (from the single "/" up to "?") is the path, and the
400 # second part (from that "?" up to "#") is the query. *HOWEVER*, we do
401 # not need to separate them; we send the whole lot to the server.
402 # Fourth is the fragment identifier, which is everything after the first
403 # "#" in the URL. The fragment identifier MUST NOT be sent to the server
404 # and indeed, we don't bother to validate it (it could be an error to
405 # pass it in here, but it's cheap to strip).
407 # An example of a URL that has all the parts:
409 # http://jschmoe:xyzzy@www.bogus.net:8000/foo/bar.tml?q=foo#changes
411 # The "http" is the protocol, the user is "jschmoe", the password is
412 # "xyzzy", the host is "www.bogus.net", the port is "8000", the path is
413 # "/foo/bar.tml", the query is "q=foo", and the fragment is "changes".
415 # Note that the RE actually combines the user and password parts, as
416 # recommended in RFC 3986. Indeed, that RFC states that putting passwords
417 # in URLs is a Really Bad Idea, something with which I would agree utterly.
418 # Also note that we do not currently support IPv6 addresses.
420 # From a validation perspective, we need to ensure that the parts of the
421 # URL that are going to the server are correctly encoded. This is only
422 # done if $state(-strict) is true (inherited from $::http::strict).
424 set URLmatcher {(?x) # this is _expanded_ syntax
426 (?: (\w+) : ) ? # <protocol scheme>
430 [^@/\#?]+ # <userinfo part of authority>
433 ( [^/:\#?]+ ) # <host part of authority>
434 (?: : (\d+) )? # <port part of authority>
436 ( / [^\#?]* (?: \? [^\#?]* )?)? # <path> (including query)
437 (?: \# (.*) )? # <fragment>
442 if {![regexp -- $URLmatcher $url -> proto user host port srvurl]} {
444 return -code error "Unsupported URL: $url"
446 # Phase two: validate
448 # Caller has to provide a host name; we do not have a "default host"
449 # that would enable us to handle relative URLs.
451 return -code error "Missing host part: $url"
452 # Note that we don't check the hostname for validity here; if it's
453 # invalid, we'll simply fail to resolve it later on.
455 if {$port ne "" && $port > 65535} {
457 return -code error "Invalid port number: $port"
459 # The user identification and resource identification parts of the URL can
460 # have encoded characters in them; take care!
462 # Check for validity according to RFC 3986, Appendix A
463 set validityRE {(?xi)
465 (?: [-\w.~!$&'()*+,;=:] | %[0-9a-f][0-9a-f] )+
468 if {$state(-strict) && ![regexp -- $validityRE $user]} {
470 # Provide a better error message in this error case
471 if {[regexp {(?i)%(?![0-9a-f][0-9a-f]).?.?} $user bad]} {
473 "Illegal encoding character usage \"$bad\" in URL user"
475 return -code error "Illegal characters in URL user"
479 # Check for validity according to RFC 3986, Appendix A
480 set validityRE {(?xi)
482 # Path part (already must start with / character)
483 (?: [-\w.~!$&'()*+,;=:@/] | %[0-9a-f][0-9a-f] )*
484 # Query part (optional, permits ? characters)
485 (?: \? (?: [-\w.~!$&'()*+,;=:@/?] | %[0-9a-f][0-9a-f] )* )?
488 if {$state(-strict) && ![regexp -- $validityRE $srvurl]} {
490 # Provide a better error message in this error case
491 if {[regexp {(?i)%(?![0-9a-f][0-9a-f])..} $srvurl bad]} {
493 "Illegal encoding character usage \"$bad\" in URL path"
495 return -code error "Illegal characters in URL path"
503 if {![info exists urlTypes($proto)]} {
505 return -code error "Unsupported URL type \"$proto\""
507 set defport [lindex $urlTypes($proto) 0]
508 set defcmd [lindex $urlTypes($proto) 1]
513 if {![catch {$http(-proxyfilter) $host} proxy]} {
514 set phost [lindex $proxy 0]
515 set pport [lindex $proxy 1]
518 # OK, now reassemble into a full URL
525 if {$port != $defport} {
529 # Don't append the fragment!
532 # If a timeout is specified we set up the after event and arrange for an
533 # asynchronous socket connection.
536 if {$state(-timeout) > 0} {
537 set state(after) [after $state(-timeout) \
538 [list http::reset $token timeout]]
539 lappend sockopts -async
542 # If we are using the proxy, we must pass in the full URL that includes
545 if {[info exists phost] && ($phost ne "")} {
547 set targetAddr [list $phost $pport]
549 set targetAddr [list $host $port]
551 # Proxy connections aren't shared among different hosts.
552 set state(socketinfo) $host:$port
554 # See if we are supposed to use a previously opened channel.
555 if {$state(-keepalive)} {
557 if {[info exists socketmap($state(socketinfo))]} {
558 if {[catch {fconfigure $socketmap($state(socketinfo))}]} {
559 Log "WARNING: socket for $state(socketinfo) was closed"
560 unset socketmap($state(socketinfo))
562 set sock $socketmap($state(socketinfo))
563 Log "reusing socket $sock for $state(socketinfo)"
564 catch {fileevent $sock writable {}}
565 catch {fileevent $sock readable {}}
568 # don't automatically close this connection socket
569 set state(connection) {}
571 if {![info exists sock]} {
572 # Pass -myaddr directly to the socket command
573 if {[info exists state(-myaddr)]} {
574 lappend sockopts -myaddr $state(-myaddr)
576 if {[catch {eval $defcmd $sockopts $targetAddr} sock]} {
577 # something went wrong while trying to establish the connection.
578 # Clean up after events and such, but DON'T call the command
579 # callback (if available) because we're going to throw an
580 # exception from here instead.
582 set state(sock) $sock
585 return -code error $sock
588 set state(sock) $sock
589 Log "Using $sock for $state(socketinfo)" \
590 [expr {$state(-keepalive)?"keepalive":""}]
591 if {$state(-keepalive)} {
592 set socketmap($state(socketinfo)) $sock
595 # Wait for the connection to complete.
597 if {$state(-timeout) > 0} {
598 fileevent $sock writable [list http::Connect $token]
601 if {![info exists state]} {
602 # If we timed out then Finish has been called and the users
603 # command callback may have cleaned up the token. If so we end up
604 # here with nothing left to do.
606 } elseif {$state(status) eq "error"} {
607 # Something went wrong while trying to establish the connection.
608 # Clean up after events and such, but DON'T call the command
609 # callback (if available) because we're going to throw an
610 # exception from here instead.
611 set err [lindex $state(error) 0]
613 return -code error $err
614 } elseif {$state(status) ne "connect"} {
615 # Likely to be connection timeout
621 # Send data in cr-lf format, but accept any line terminators
623 fconfigure $sock -translation {auto crlf} -buffersize $state(-blocksize)
625 # The following is disallowed in safe interpreters, but the socket is
626 # already in non-blocking mode in that case.
628 catch {fconfigure $sock -blocking off}
631 set state(querylength) [string length $state(-query)]
632 if {$state(querylength) > 0} {
636 # There's no query data.
640 } elseif {$state(-validate)} {
642 } elseif {$isQueryChannel} {
644 # The query channel must be blocking for the async Write to
646 fconfigure $state(-querychannel) -blocking 1 -translation binary
649 if {[info exists state(-method)] && $state(-method) ne ""} {
650 set how $state(-method)
654 puts $sock "$how $srvurl HTTP/$state(-protocol)"
655 puts $sock "Accept: $http(-accept)"
656 array set hdrs $state(-headers)
657 if {[info exists hdrs(Host)]} {
658 # Allow Host spoofing. [Bug 928154]
659 puts $sock "Host: $hdrs(Host)"
660 } elseif {$port == $defport} {
661 # Don't add port in this case, to handle broken servers. [Bug
663 puts $sock "Host: $host"
665 puts $sock "Host: $host:$port"
668 puts $sock "User-Agent: $http(-useragent)"
669 if {$state(-protocol) == 1.0 && $state(-keepalive)} {
670 puts $sock "Connection: keep-alive"
672 if {$state(-protocol) > 1.0 && !$state(-keepalive)} {
673 puts $sock "Connection: close" ;# RFC2616 sec 8.1.2.1
675 if {[info exists phost] && ($phost ne "") && $state(-keepalive)} {
676 puts $sock "Proxy-Connection: Keep-Alive"
678 set accept_encoding_seen 0
679 foreach {key value} $state(-headers) {
680 if {[string equal -nocase $key "host"]} {
683 if {[string equal -nocase $key "accept-encoding"]} {
684 set accept_encoding_seen 1
686 set value [string map [list \n "" \r ""] $value]
687 set key [string trim $key]
688 if {[string equal -nocase $key "content-length"]} {
690 set state(querylength) $value
692 if {[string length $key]} {
693 puts $sock "$key: $value"
696 # Soft zlib dependency check - no package require
698 !$accept_encoding_seen &&
699 ([package vsatisfies [package provide Tcl] 8.6]
700 || [llength [package provide zlib]]) &&
701 !([info exists state(-channel)] || [info exists state(-handler)])
703 puts $sock "Accept-Encoding: gzip, identity, *;q=0.1"
705 if {$isQueryChannel && $state(querylength) == 0} {
706 # Try to determine size of data in channel. If we cannot seek, the
707 # surrounding catch will trap us
709 set start [tell $state(-querychannel)]
710 seek $state(-querychannel) 0 end
711 set state(querylength) \
712 [expr {[tell $state(-querychannel)] - $start}]
713 seek $state(-querychannel) $start
716 # Flush the request header and set up the fileevent that will either
717 # push the POST data or read the response.
721 # It is possible to have both the read and write fileevents active at
722 # this point. The only scenario it seems to affect is a server that
723 # closes the connection without reading the POST data. (e.g., early
724 # versions TclHttpd in various error cases). Depending on the
725 # platform, the client may or may not be able to get the response from
726 # the server because of the error it will get trying to write the post
727 # data. Having both fileevents active changes the timing and the
728 # behavior, but no two platforms (among Solaris, Linux, and NT) behave
729 # the same, and none behave all that well in any case. Servers should
730 # always read their POST data if they expect the client to read their
733 if {$isQuery || $isQueryChannel} {
734 puts $sock "Content-Type: $state(-type)"
736 puts $sock "Content-Length: $state(querylength)"
739 fconfigure $sock -translation {auto binary}
740 fileevent $sock writable [list http::Write $token]
744 fileevent $sock readable [list http::Event $sock $token]
747 if {![info exists state(-command)]} {
748 # geturl does EVERYTHING asynchronously, so if the user calls it
749 # synchronously, we just do a wait here.
752 if {$state(status) eq "error"} {
753 # Something went wrong, so throw the exception, and the
754 # enclosing catch will do cleanup.
755 return -code error [lindex $state(error) 0]
759 # The socket probably was never connected, or the connection dropped
762 # Clean up after events and such, but DON'T call the command callback
763 # (if available) because we're going to throw an exception from here
766 # if state(status) is error, it means someone's already called Finish
767 # to do the above-described clean up.
768 if {$state(status) ne "error"} {
772 return -code error $err
778 # Data access functions:
779 # Data - the URL data
780 # Status - the transaction status: ok, reset, eof, timeout
781 # Code - the HTTP transaction code, e.g., 200
782 # Size - the size of the URL data
784 proc http::data {token} {
789 proc http::status {token} {
790 if {![info exists $token]} {
795 return $state(status)
797 proc http::code {token} {
802 proc http::ncode {token} {
805 if {[regexp {[0-9]{3}} $state(http) numeric_code]} {
811 proc http::size {token} {
814 return $state(currentsize)
816 proc http::meta {token} {
821 proc http::error {token} {
824 if {[info exists state(error)]} {
832 # Garbage collect the state associated with a transaction
835 # token The token returned from http::geturl
838 # unsets the state array
840 proc http::cleanup {token} {
843 if {[info exists state]} {
850 # This callback is made when an asyncronous connection completes.
853 # token The token returned from http::geturl
856 # Sets the status of the connection, which unblocks
857 # the waiting geturl call
859 proc http::Connect {token} {
862 global errorInfo errorCode
864 [eof $state(sock)] ||
865 [string length [fconfigure $state(sock) -error]]
867 Finish $token "connect failed [fconfigure $state(sock) -error]" 1
869 set state(status) connect
870 fileevent $state(sock) writable {}
877 # Write POST query data to the socket
880 # token The token for the connection
883 # Write the socket and handle callbacks.
885 proc http::Write {token} {
888 set sock $state(sock)
890 # Output a block. Tcl will buffer this if the socket blocks
893 # Catch I/O errors on dead sockets
895 if {[info exists state(-query)]} {
896 # Chop up large query strings so queryprogress callback can give
899 puts -nonewline $sock \
900 [string range $state(-query) $state(queryoffset) \
901 [expr {$state(queryoffset) + $state(-queryblocksize) - 1}]]
902 incr state(queryoffset) $state(-queryblocksize)
903 if {$state(queryoffset) >= $state(querylength)} {
904 set state(queryoffset) $state(querylength)
909 # Copy blocks from the query channel
911 set outStr [read $state(-querychannel) $state(-queryblocksize)]
912 puts -nonewline $sock $outStr
913 incr state(queryoffset) [string length $outStr]
914 if {[eof $state(-querychannel)]} {
919 # Do not call Finish here, but instead let the read half of the socket
920 # process whatever server reply there is to get.
922 set state(posterror) $err
927 fileevent $sock writable {}
928 fileevent $sock readable [list http::Event $sock $token]
931 # Callback to the client after we've completely handled everything.
933 if {[string length $state(-queryprogress)]} {
934 eval $state(-queryprogress) \
935 [list $token $state(querylength) $state(queryoffset)]
941 # Handle input on the socket
944 # sock The socket receiving input.
945 # token The token returned from http::geturl
948 # Read the socket and handle callbacks.
950 proc http::Event {sock token} {
954 if {![info exists state]} {
955 Log "Event $sock with invalid token '$token' - remote close?"
957 if {[set d [read $sock]] ne ""} {
958 Log "WARNING: additional data left on closed socket"
964 if {$state(state) eq "connecting"} {
965 if {[catch {gets $sock state(http)} n]} {
966 return [Finish $token $n]
968 set state(state) "header"
970 } elseif {$state(state) eq "header"} {
971 if {[catch {gets $sock line} n]} {
972 return [Finish $token $n]
974 # We have now read all headers
975 # We ignore HTTP/1.1 100 Continue returns. RFC2616 sec 8.2.3
976 if {$state(http) == "" || [lindex $state(http) 1] == 100} {
980 set state(state) body
982 # If doing a HEAD, then we won't get any body
983 if {$state(-validate)} {
988 # For non-chunked transfer we may have no body - in this case we
989 # may get no further file event if the connection doesn't close
990 # and no more data is sent. We can tell and must finish up now -
993 !(([info exists state(connection)]
994 && ($state(connection) eq "close"))
995 || [info exists state(transfer)])
996 && ($state(totalsize) == 0)
998 Log "body size is 0 and no events likely - complete."
1003 # We have to use binary translation to count bytes properly.
1004 fconfigure $sock -translation binary
1007 $state(-binary) || ![string match -nocase text* $state(type)]
1009 # Turn off conversions for non-text data
1013 $state(binary) || [string match *gzip* $state(coding)] ||
1014 [string match *compress* $state(coding)]
1016 if {[info exists state(-channel)]} {
1017 fconfigure $state(-channel) -translation binary
1021 [info exists state(-channel)] &&
1022 ![info exists state(-handler)]
1024 # Initiate a sequence of background fcopies
1025 fileevent $sock readable {}
1026 CopyStart $sock $token
1030 # Process header lines
1031 if {[regexp -nocase {^([^:]+):(.+)$} $line x key value]} {
1032 switch -- [string tolower $key] {
1034 set state(type) [string trim [string tolower $value]]
1035 # grab the optional charset information
1036 regexp -nocase {charset\s*=\s*(\S+?);?} \
1037 $state(type) -> state(charset)
1040 set state(totalsize) [string trim $value]
1043 set state(coding) [string trim $value]
1046 set state(transfer) \
1047 [string trim [string tolower $value]]
1051 set state(connection) \
1052 [string trim [string tolower $value]]
1055 lappend state(meta) $key [string trim $value]
1061 if {[info exists state(-handler)]} {
1062 set n [eval $state(-handler) [list $sock $token]]
1063 } elseif {[info exists state(transfer_final)]} {
1064 set line [getTextLine $sock]
1065 set n [string length $line]
1067 Log "found $n bytes following final chunk"
1068 append state(transfer_final) $line
1070 Log "final chunk part"
1074 [info exists state(transfer)]
1075 && $state(transfer) eq "chunked"
1078 set chunk [getTextLine $sock]
1079 set n [string length $chunk]
1080 if {[string trim $chunk] ne ""} {
1083 set bl [fconfigure $sock -blocking]
1084 fconfigure $sock -blocking 1
1085 set chunk [read $sock $size]
1086 fconfigure $sock -blocking $bl
1087 set n [string length $chunk]
1089 append state(body) $chunk
1091 if {$size != [string length $chunk]} {
1092 Log "WARNING: mis-sized chunk:\
1093 was [string length $chunk], should be $size"
1097 set state(transfer_final) {}
1101 #Log "read non-chunk $state(currentsize) of $state(totalsize)"
1102 set block [read $sock $state(-blocksize)]
1103 set n [string length $block]
1105 append state(body) $block
1108 if {[info exists state]} {
1110 incr state(currentsize) $n
1112 # If Content-Length - check for end of data.
1114 ($state(totalsize) > 0)
1115 && ($state(currentsize) >= $state(totalsize))
1121 return [Finish $token $err]
1123 if {[info exists state(-progress)]} {
1124 eval $state(-progress) \
1125 [list $token $state(totalsize) $state(currentsize)]
1130 # catch as an Eof above may have closed the socket already
1131 if {![catch {eof $sock} eof] && $eof} {
1132 if {[info exists $token]} {
1133 set state(connection) close
1136 # open connection closed on a token that has been cleaned up.
1143 # http::getTextLine --
1145 # Get one line with the stream in blocking crlf mode
1148 # sock The socket receiving input.
1151 # The line of text, without trailing newline
1153 proc http::getTextLine {sock} {
1154 set tr [fconfigure $sock -translation]
1155 set bl [fconfigure $sock -blocking]
1156 fconfigure $sock -translation crlf -blocking 1
1158 fconfigure $sock -translation $tr -blocking $bl
1164 # Error handling wrapper around fcopy
1167 # sock The socket to copy from
1168 # token The token returned from http::geturl
1171 # This closes the connection upon error
1173 proc http::CopyStart {sock token} {
1175 upvar 0 $token state
1177 fcopy $sock $state(-channel) -size $state(-blocksize) -command \
1178 [list http::CopyDone $token]
1186 # fcopy completion callback
1189 # token The token returned from http::geturl
1190 # count The amount transfered
1195 proc http::CopyDone {token count {error {}}} {
1197 upvar 0 $token state
1198 set sock $state(sock)
1199 incr state(currentsize) $count
1200 if {[info exists state(-progress)]} {
1201 eval $state(-progress) \
1202 [list $token $state(totalsize) $state(currentsize)]
1204 # At this point the token may have been reset
1205 if {[string length $error]} {
1206 Finish $token $error
1207 } elseif {[catch {eof $sock} iseof] || $iseof} {
1210 CopyStart $sock $token
1216 # Handle eof on the socket
1219 # token The token returned from http::geturl
1222 # Clean up the socket
1224 proc http::Eof {token {force 0}} {
1226 upvar 0 $token state
1227 if {$state(state) eq "header"} {
1229 set state(status) eof
1231 set state(status) ok
1234 if {($state(coding) eq "gzip") && [string length $state(body)] > 0} {
1236 if {[package vsatisfies [package present Tcl] 8.6]} {
1237 # The zlib integration into 8.6 includes proper gzip support
1238 set state(body) [zlib gunzip $state(body)]
1240 set state(body) [Gunzip $state(body)]
1243 return [Finish $token $err]
1247 if {!$state(binary)} {
1248 # If we are getting text, set the incoming channel's encoding
1249 # correctly. iso8859-1 is the RFC default, but this could be any IANA
1250 # charset. However, we only know how to convert what we have
1253 set enc [CharsetToEncoding $state(charset)]
1254 if {$enc ne "binary"} {
1255 set state(body) [encoding convertfrom $enc $state(body)]
1258 # Translate text line endings.
1259 set state(body) [string map {\r\n \n \r \n} $state(body)]
1267 # See documentation for details.
1270 # token Connection token.
1273 # The status after the wait.
1275 proc http::wait {token} {
1277 upvar 0 $token state
1279 if {![info exists state(status)] || $state(status) eq ""} {
1280 # We must wait on the original variable name, not the upvar alias
1281 vwait ${token}(status)
1284 return [status $token]
1287 # http::formatQuery --
1289 # See documentation for details. Call http::formatQuery with an even
1290 # number of arguments, where the first is a name, the second is a value,
1291 # the third is another name, and so on.
1294 # args A list of name-value pairs.
1299 proc http::formatQuery {args} {
1303 append result $sep [mapReply $i]
1315 # Do x-www-urlencoded character mapping
1318 # string The string the needs to be encoded
1321 # The encoded string
1323 proc http::mapReply {string} {
1327 # The spec says: "non-alphanumeric characters are replaced by '%HH'". Use
1328 # a pre-computed map and [string map] to do the conversion (much faster
1329 # than [regsub]/[subst]). [Bug 1020491]
1331 if {$http(-urlencoding) ne ""} {
1332 set string [encoding convertto $http(-urlencoding) $string]
1333 return [string map $formMap $string]
1335 set converted [string map $formMap $string]
1336 if {[string match "*\[\u0100-\uffff\]*" $converted]} {
1337 regexp {[\u0100-\uffff]} $converted badChar
1338 # Return this error message for maximum compatability... :^/
1339 return -code error \
1340 "can't read \"formMap($badChar)\": no such element in array"
1345 # http::ProxyRequired --
1346 # Default proxy filter.
1349 # host The destination host
1352 # The current proxy settings
1354 proc http::ProxyRequired {host} {
1356 if {[info exists http(-proxyhost)] && [string length $http(-proxyhost)]} {
1358 ![info exists http(-proxyport)] ||
1359 ![string length $http(-proxyport)]
1361 set http(-proxyport) 8080
1363 return [list $http(-proxyhost) $http(-proxyport)]
1367 # http::CharsetToEncoding --
1369 # Tries to map a given IANA charset to a tcl encoding. If no encoding
1370 # can be found, returns binary.
1373 proc http::CharsetToEncoding {charset} {
1376 set charset [string tolower $charset]
1377 if {[regexp {iso-?8859-([0-9]+)} $charset -> num]} {
1378 set encoding "iso8859-$num"
1379 } elseif {[regexp {iso-?2022-(jp|kr)} $charset -> ext]} {
1380 set encoding "iso2022-$ext"
1381 } elseif {[regexp {shift[-_]?js} $charset]} {
1382 set encoding "shiftjis"
1383 } elseif {[regexp {(?:windows|cp)-?([0-9]+)} $charset -> num]} {
1384 set encoding "cp$num"
1385 } elseif {$charset eq "us-ascii"} {
1386 set encoding "ascii"
1387 } elseif {[regexp {(?:iso-?)?lat(?:in)?-?([0-9]+)} $charset -> num]} {
1389 5 {set encoding "iso8859-9"}
1391 set encoding "iso8859-$num"
1395 # other charset, like euc-xx, utf-8,... may directly map to encoding
1396 set encoding $charset
1398 set idx [lsearch -exact $encodings $encoding]
1408 # Decompress data transmitted using the gzip transfer coding.
1411 # FIX ME: redo using zlib sinflate
1412 proc http::Gunzip {data} {
1413 binary scan $data Scb5icc magic method flags time xfl os
1415 if {$magic != 0x1f8b} {
1416 return -code error "invalid data: supplied data is not in gzip format"
1419 return -code error "invalid compression method"
1422 # lassign [split $flags ""] f_text f_crc f_extra f_name f_comment
1423 foreach {f_text f_crc f_extra f_name f_comment} [split $flags ""] break
1426 binary scan $data @${pos}S xlen
1428 set extra [string range $data $pos $xlen]
1434 set ndx [string first \0 $data $pos]
1435 set name [string range $data $pos $ndx]
1441 set ndx [string first \0 $data $pos]
1442 set comment [string range $data $pos $ndx]
1448 set fcrc [string range $data $pos [incr pos]]
1452 binary scan [string range $data end-7 end] ii crc size
1453 set inflated [zlib inflate [string range $data $pos end-8]]
1454 set chk [zlib crc32 $inflated]
1455 if {($crc & 0xffffffff) != ($chk & 0xffffffff)} {
1456 return -code error "invalid data: checksum mismatch $crc != $chk"
1462 # indent-tabs-mode: t