Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2022-09-10
12:17 • Ticket [3102850fff] HTTP does not follow redirects status still Open with 6 ... artifact: ba356d478b user: kjnash
2012-10-30
10:27 • Ticket [3102850fff]: 4 changes artifact: 6434700b5c user: kjnash
2010-11-04
20:36 • Ticket [3102850fff]: 5 changes artifact: faedd1c104 user: dkf
20:35 • Ticket [3102850fff]: 1 change artifact: 934f0a36b1 user: dkf
18:43 • Ticket [3102850fff]: 1 change artifact: 8575676e99 user: hm2k
18:43 • Ticket [3102850fff]: 1 change artifact: 8d43b710ff user: hm2k
11:42 • New ticket [3102850fff]. artifact: 0462769e46 user: hm2k

HTTP does not follow redirects

Open3102850fffffffffffffffffffffffffffffffffCreated 16 years agoUpdated 4 years ago
Descriptionplain2010-11-04 11:42:27
I am referring to redirects such as the 301 HTTP redirect which is when a page has moved permanently.

At present the http package does not appear to follow such redirects.

This is not the nature of which http was intended to be used and should be fixed accordingly.
hm2k Ticket [3102850fff]: 1 change [History diff]2010-11-04 18:43:08
hm2k Ticket [3102850fff]: 1 change [History diff]2010-11-04 18:43:26
dkf Ticket [3102850fff]: 1 change [History diff]2010-11-04 20:35:20
dkfplain2010-11-04 20:36:09
Redirect handling ought to be a configurable feature (default: on)
kjnashplain2012-10-30 10:27:22
I suggest that following redirects is not a suitable feature for the http::geturl which implements a single HTTP request.

The redirect problem is not simple because RFC 2616 insists we MUST have user confirmation in certain cases: a POST when the response code is 301, 302, or 307.  This means that http::geturl must either refuse these redirects, or provide hooks for user interaction.

I suggest that Tcllib is a better place for redirection code than the http package itself.

I have a wrapper library for package http that provides the necessary code to follow redirects, and handle authorization (401), including hooks for user interaction.  I intend to offer this code to Tcllib when it is finished (I am still fixing bugs right now).
kjnashplain2022-09-10 12:17:07
Bug [3612478] is a duplicate of this one.  I have closed that bug, and copied Harald's comments below:
(kjnash)

IMHO it would be helpful, if http::geturl would follow redirects automatically (or with an option).

Following http://wiki.tcl.tk/1475

Thus 
::http::geturl $url -followredirect 1
would do:
proc geturl_followRedirects {url args} {
     while {1} {
        set token [eval [list http::geturl $url] $args]
        switch -glob [http::ncode $token] {
           30[1237] { ### redirect - see below ### }
           default  { return $token }
        }
        upvar #0 $token state
        array set meta [set ${token}(meta)]
        if {![info exist meta(Location)]} {
           return $token
        }
        set url $meta(Location)
        unset meta
     }
  }

Thank you,
- Harald
Log in (anonymous is fine) to add a comment.