1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
###
# Tool to download file from the web
# Enhancements to http
###
package provide http::wget 0.2.1
package require http
::namespace eval ::http {}
###
# topic: 1ed971e03ae89415e2f25d20e59b765c
# description: this proc contributed by Donal Fellows
###
proc ::http::_followRedirects {url args} {
while 1 {
set token [geturl $url -validate 1]
set ncode [ncode $token]
if { $ncode eq "404" } {
error "URL Not found"
}
switch -glob $ncode {
30[1237] {
### redirect - see below ###
}
default {
cleanup $token
return $url
}
}
upvar #0 $token state
array set meta [set ${token}(meta)]
cleanup $token
if {[info exists meta(Location)]} {
set url $meta(Location)
} elseif {[info exists meta(location)]} {
set url $meta(location)
} else {
return $url
}
unset meta
}
return $url
}
###
# topic: fced7bc395596569ac225a719c686dcc
###
proc ::http::wget {url destfile {verbose 1}} {
set tmpchan [open $destfile w]
fconfigure $tmpchan -translation binary
if { $verbose } {
puts [list GETTING [file tail $destfile] from $url]
}
set real_url [_followRedirects $url]
set token [geturl $real_url -channel $tmpchan -binary yes]
if {[ncode $token] != "200"} {
error "DOWNLOAD FAILED"
}
cleanup $token
close $tmpchan
}
|