Playing HTTP

Richard Suchenwirth 2004-02-14 - As I'm now online at home, I felt I had to learn some more about the Hypertext Transfer Protocol (HTTP) that drives the Web. Tcl has good support with the http package, so I didn't need to walk the last steps to the socket invocation myself, but concentrate on what comes back after a HTTP GET, especially the headers. So here's my HTTP plaything, the ugliest browser I've seen in a long time, because it does not try to render HTML, just displays the plain text that came. However, it is still interesting, for looking behind the glittering facades of "real" browsers...

WikiDbImage myhttp.jpg

You have an entry on top, in which to type an URL, and the text at bottom displays what came back after you hit <Return>, images being rendered via the great Img extension, everything else as plain text.

 package require http
 package require Img
 package require base64

 pack [entry .e -textvar e] -fill x
 bind .e <Return> {go .f.t $e}
 pack [frame .f] -fill both -expand 1
 pack [scrollbar .f.y -command ".f.t yview"] -fill y -side right
 pack [text .f.t -wrap word -yscrollc ".f.y set"] \
    -fill both -expand 1 -side right
 foreach i {red blue green3} {.f.t tag config $i -foreground $i}
 focus .e
 raise .

 proc go {w url} {
    set token [http::geturl $url]
    upvar #0 $token arr
    $w insert end \n$arr(url)   blue
    if [info exists arr(error)] {$w insert end \n$arr(error) red}
    foreach {tag value} $arr(meta) {
        $w insert end \n$tag\t$value green3
    }
    $w see end
    if [regexp {Content-Type image/([^ ]+)} $arr(meta) -> format] {
        set im [image create photo -data [base64::encode $arr(body)]]
        $w insert end \n
        $w image create end -image $im
    } else {
        $w insert end \n$arr(body)
    }
    http::cleanup $token
 }
 bind . <Escape> {exec wish $argv0 &; exit}

WJR - I'm getting this error when I try to use the scrollbar:

 bad option "yscroll": must be bbox, cget, compare, configure, debug, delete, dlineinfo, dump, edit,
 get, image, index, insert, mark, scan, search, see, tag, window, xview, or yview

RS apologizes - should have been yview... But as my box is mouseless when online, I didn't test that... Sorry!