package require http

# Redirect test page
set url http://jigsaw.w3.org/HTTP/300/301.html

# Make the HTTP library report what it is doing
namespace eval http #
proc http::Log {args} {
    puts [join $args]
}

set tok [http::geturl $url -keepalive 1]
if {[http::ncode $tok] in {301 302}} {
    # Determine the new URL
    set meta [http::meta $tok]
    set key [lsearch -exact -nocase -inline [dict keys $meta] location]
    set loc [dict get $meta $key]
    http::cleanup $tok
    puts "Redirecting to: $loc"
    set tok [http::geturl $loc -keepalive 1]
}
puts [http::code $tok]
http::cleanup $tok
