All pastes #931337 Raw Edit

http fetch with redirect

public text v1 · immutable
·published 2008-03-07 02:45 UTC
rendered paste body
      set url "http://google.com"
      set timeout 5
      set token [::http::geturl "$url" -timeout [expr round(1000 * $timeout)]]
      upvar #0 $token state
      switch -exact [::http::status $token] {
        "timeout" {
          putlog "status = timeout (url = $state(url))"
        }
        "error" {
          putlog "status = error([::http::error $token]) (url = $state(url))"
        }
        "ok" {
          switch -glob [::http::ncode $token] {
            3* {
              array set meta $state(meta)
              putlog "status = ok (url = $state(url)), redirect to $meta(Location)"
              fetch_html $meta(Location)
            }
            200 {
              putlog "status = ok (url = $state(url)), do parsing"
              process $state(body)
            }
            default {
              putlog "status = default, error"
            }
          }
        }
        default {
          putlog "status = unknown, default, error"
        }
      }