Tcl Source Code

View Ticket
Login
Ticket UUID: 3102850
Title: HTTP does not follow redirects
Type: Bug Created on: 2010-11-04 11:42:27
Submitter: hm2k Assigned to: patthoyts
Subsystem: 29. http Package Severity: Minor
Priority: 7 High Last modified: 2022-09-10 12:17:07
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: None
Description:
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.
User Comments:
dkf added on 2010-11-04 20:36:09:
Redirect handling ought to be a configurable feature (default: on)

kjnash added on 2012-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).

kjnash added on 2022-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