Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2022-10-26
12:56 • Closed ticket [1173760fff]: geturl fails on https requests if a proxy server is ... artifact: 2e2db1be48 user: kjnash
2009-12-13
01:52 • Ticket [1173760fff]: 1 change artifact: 602af6169f user: dkf
2005-03-31
01:40 • New ticket [1173760fff]. artifact: e33e69f6c1 user: melissachawla

geturl fails on https requests if a proxy server is used

Closed / Fixed1173760fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 4 years ago
Descriptionplain2005-03-31 01:40:24
Package require http
I tried to use http::geturl on an https url with a proxy 
server, and I kept getting timeouts (or ran forever when 
no timeout was specified).

package require http
package require tls
http::config -proxyhost <my-squid-host> -proxyport <my-
squid-port> 
http::register https 443 ::tls::socket http::geturl 
https://<server>:443/<path> -timeout 30000

Currently, the http package will use a GET request via 
HTTPS to a web server outside my firewall via a web 
proxy server inside my firewall.  I am sure that the web
proxy server works because I can use it to make the 
same request through my browser.  The problem is that 
http package needs to make a "CONNECT" request to 
the proxy before making the GET request.  I believe that 
http version 2.4 is affected in the same way.  Here's a 
description of the fix that I used:

1) replace the following line in http.tcl:

        set conStat [catch {eval $defcmd $async {$phost 
$pport}} s]

with:

        if {[string equal -nocase $proto "https"] \
                && [string match "*tls::socket" $defcmd]} {
            # Use "connect" to initiate https connection via 
web proxy. 
            set conStat [catch {SecureProxyConnect 
$phost $pport $host $port} s]
        } else {
            set conStat [catch {eval $defcmd $async 
{$phost $pport}} s]
        }

2) Add the SecureProxyConnect procedure:

# Internal procedure to initiate https connection via 
proxy server.
proc http::SecureProxyConnect {proxyserver proxyport 
webserver webserverport} {
     set sock [socket $proxyserver $proxyport]
     fconfigure $sock -blocking 0 -buffering line -
translation crlf

     puts $sock "CONNECT $webserver:$webserverport 
HTTP/1.1"
     puts $sock "Host: $webserver"
     puts $sock "User-Agent: [http::config -useragent]"
     puts $sock "Proxy-Connection: keep-alive"
     puts $sock "Connection: keep-alive"
     puts $sock ""

     fileevent $sock readable {set proxy_readable ""}
     global proxy_readable
     vwait proxy_readable
     fileevent $sock readable {}

     set block [read $sock]
     set result [lindex [split $block \n] 0]
     set code [lindex [split $result { }] 1]
     fconfigure $sock -blocking 1 -translation binary -
buffering none

     if {$code >= 200 && $code < 300} {
         tls::import $sock
     } else {
         error "proxy connect failed: $block"
     }
     return $sock
}
dkf Ticket [1173760fff]: 1 change [History diff]2009-12-13 01:52:31
kjnashplain2022-10-26 12:56:58
This is now fixed in Tcl 8.7, fossil branch core-8-branch, commit [f0cf5f7866].  The fix is based on the changes proposed above, plus proxy authentication as used in (Tcllib) autoproxy, and handling of proxy errors by returning the proxy's HTTP response.
Log in (anonymous is fine) to add a comment.