tk_sample.tcl

Go to the documentation of this file.
00001 /*  tk-sample.tcl - Copyright (C) 2002 Pat Thoyts <patthoyts@users.sourceforge.net>*/
00002 /* */
00003 /*  Derived from Neil Madden's browser sig :)*/
00004 /* */
00005 /*  Note that this doesn't work for sites using virtual hosting and is dubious for*/
00006 /*  multi-homed sites too. This is only to illustrate the resolver usage. What we*/
00007 /*  should be doing is connecting a socket to the resolved address and then requesting*/
00008 /*  the original URL. Useless if there is a proxy between you as well.*/
00009 /* */
00010 /*  $Id: tk_sample.tcl,v 1.2 2004/01/15 06:36:12 andreas_kupries Exp $*/
00011 
00012 package require Tkhtml
00013 package require http
00014 package require dns
00015 
00016  Sample = (URL) http://mini.net/tcl/976.html
00017  Sample = (nameserver) localhost
00018 
00019 /*  Description:*/
00020 /*   Construct a simple web browser interface.*/
00021 /* */
00022 ret  gui () {
00023     frame .f -bd 0 -relief flat
00024     label .f.l1 -text "Nameserver" -underline 0
00025     entry .f.e1 -textvariable ::Sample(nameserver)
00026     label .f.l2 -text "URL" -underline 0
00027     entry .f.e2 -textvariable ::Sample(URL)
00028     button .f.b -text Go -underline 0 -command {get $::Sample(URL)}
00029     button .f.x -text Exit -underline 1 -command {bye}
00030     
00031     scrollbar .v -orient v -command {.h yv}
00032     html .h -yscrollcommand {.v set}
00033     
00034     pack .f.l1 -side left -fill y
00035     pack .f.e1 -side left -fill both -expand 1
00036     pack .f.x -side right -fill y
00037     pack .f.b -side right -fill y
00038     pack .f.l2 -side left -fill y
00039     pack .f.e2 -side right -fill both -expand 1
00040 
00041     pack .f -side top -fill x
00042     pack .v -side right -fill y
00043     pack .h -fill both -expand 1
00044     
00045     bind .h.x <1> {eval get [.h href %x %y]}
00046 }
00047 
00048 ret  bye () {
00049     destroy .f .v .h
00050 }
00051 
00052 ret  bgerror (type args) {
00053 }
00054 
00055 /*  Description:*/
00056 /*   Rewrite the URL by looking up the domain name and replacing with the */
00057 /*   IP address.*/
00058 /* */
00059 ret  resolve (type url) {
00060     global Sample
00061     if {![catch {array set URL [uri::split $url]} msg]} {
00062         set tok [dns::resolve $URL(host) -server $Sample(nameserver)]
00063         if {[dns::status $tok] == "ok"} {
00064             set URL(host) [dns::address $tok]
00065             set url [eval uri::join [array get URL]]
00066         }
00067         dns::cleanup $tok
00068     }
00069     log::log debug "resolved to $url"
00070     return $url
00071 }
00072 
00073 /*  Description:*/
00074 /*   Fetch an HTTP URL and display.*/
00075 /* */
00076 ret  get (type url) {
00077     global Sample
00078     set url [resolve $url]
00079     set Sample(URL) $url
00080     set tok [http::geturl $url -headers $::auth]
00081     .h clear
00082     .h parse [http::data $tok]
00083     http::cleanup $tok
00084     .h configure -base $url
00085 }
00086 
00087 gui
00088 get $::Sample(URL)
00089 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1