Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Added demos directory with example scripts to download web pages and files using TLS. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk | main |
Files: | files | file ages | folders |
SHA3-256: |
0ebb44402a4536ebe882809b042e0604 |
User & Date: | bohagan 2025-01-01 22:38:30 |
Context
2025-01-01
| ||
23:32 | Moved debug script examples from docs to demos directory check-in: 65f827b5f9 user: bohagan tags: trunk, main | |
22:38 | Added demos directory with example scripts to download web pages and files using TLS. check-in: 0ebb44402a user: bohagan tags: trunk, main | |
2024-12-31
| ||
04:12 | More comment updates and changes for fast path option check-in: c61a46b561 user: bohagan tags: trunk, main | |
Changes
Added demos/README.txt.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | This directory contain example files for how to use the TLS package to perform common functions. These are just a few of the possibilities. gets_blocking_no_variable.tcl Download a webpage using gets, no variable arg, and blocking I/O. gets_blocking_with_variable.tcl Download a webpage using gets, variable arg, and blocking I/O. gets_nonblocking_no_variable.tcl Download a webpage using gets, no variable arg, and non-blocking I/O. gets_nonblocking_with_variable.tcl Download a webpage using gets, variable arg, and non-blocking I/O. http_get_file.tcl Download a webpage using the http package. http_get_webpage.tcl Download a file using the http package. http_get_webpage_proxy.tcl Download a file using the http and autoproxy packages. read_blocking_webpage.tcl Download a webpage using read and blocking I/O. read_nonblocking_webpage.tcl Download a webpage using read and non-blocking I/O. |
Added demos/gets_blocking_no_variable.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 63 64 65 66 67 | ################################################# # # Example 1: Blocking channel gets with no variable # ################################################# package require Tcl 8.6- package require tls set host "www.google.com" set port 443 set path "/" set protocol "http/1.1" # # Send HTTP Get Request # proc http_get {ch host path protocol} { puts $ch [format "GET %s %s" $path [string toupper $protocol]] puts $ch [format "User-Agent: Mozilla/4.0 (compatible; %s)" $::tcl_platform(os)] puts $ch [format "Host: %s" $host] puts $ch [format "Connection: close"] puts $ch "" flush $ch } # Save returned data to file proc save_file {filename data} { if {[catch {open $filename wb} ch]} { return -code error $ch } fconfigure $ch -buffersize 16384 -encoding utf-8 -translation crlf puts $ch $data close $ch } proc gets_blocking_no_variable {host port path protocol} { set result "" # Open socket set ch [::tls::socket -servername $host -request 1 -require 1 -alpn [list [string tolower $protocol]] $host $port] chan configure $ch -blocking 1 -buffering line -buffersize 16384 -encoding utf-8 -translation {auto crlf} # Initiate handshake ::tls::handshake $ch after 1000 # Send get request http_get $ch $host $path $protocol after 1000 # Get data while {1} { set line [gets $ch] if {!([string length $line] == 0 && [eof $ch])} { append result $line "\n" } elseif {[eof $ch]} { close $ch break } } return $result } save_file "gets_blocking_no_variable.txt" [gets_blocking_no_variable $host $port $path $protocol] |
Added demos/gets_blocking_with_variable.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 63 64 65 66 | ################################################# # # Example 2: Blocking channel gets with variable # ################################################# package require Tcl 8.6- package require tls set host "www.google.com" set port 443 set path "/" set protocol "http/1.1" # # Send HTTP Get Request # proc http_get {ch host path protocol} { puts $ch [format "GET %s %s" $path [string toupper $protocol]] puts $ch [format "User-Agent: Mozilla/4.0 (compatible; %s)" $::tcl_platform(os)] puts $ch [format "Host: %s" $host] puts $ch [format "Connection: close"] puts $ch "" flush $ch } # Save returned data to file proc save_file {filename data} { if {[catch {open $filename wb} ch]} { return -code error $ch } fconfigure $ch -buffersize 16384 -encoding utf-8 -translation crlf puts $ch $data close $ch } proc gets_blocking_with_variable {host port path protocol} { set result "" # Open socket set ch [::tls::socket -servername $host -request 1 -require 1 -alpn [list [string tolower $protocol]] $host $port] chan configure $ch -blocking 1 -buffering line -buffersize 16384 -encoding utf-8 -translation {auto crlf} # Initiate handshake ::tls::handshake $ch after 1000 # Send get request http_get $ch $host $path $protocol after 1000 # Get data while {1} { if {[gets $ch line] > -1} { append result $line "\n" } elseif {[eof $ch]} { close $ch break } } return $result } save_file "gets_blocking_with_variable.txt" [gets_blocking_with_variable $host $port $path $protocol] |
Added demos/gets_nonblocking_no_variable.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | ################################################# # # Example 3: Non-blocking channel gets with no variable # ################################################# package require Tcl 8.6- package require tls set host "www.google.com" set port 443 set path "/" set protocol "http/1.1" # # Send HTTP Get Request # proc http_get {ch host path protocol} { puts $ch [format "GET %s %s" $path [string toupper $protocol]] puts $ch [format "User-Agent: Mozilla/4.0 (compatible; %s)" $::tcl_platform(os)] puts $ch [format "Host: %s" $host] puts $ch [format "Connection: close"] puts $ch "" flush $ch } # Save returned data to file proc save_file {filename data} { if {[catch {open $filename wb} ch]} { return -code error $ch } fconfigure $ch -buffersize 16384 -encoding utf-8 -translation crlf puts $ch $data close $ch } proc handler {ch} { set line [gets $ch] if {[eof $ch]} { # EOF close $ch set ::wait 1 return } elseif {![fblocked $ch]} { # Full or empty line append ::data $line "\n" } else { # Partial line append ::data $line } } proc gets_non_blocking_no_variable {host port path protocol} { set ::wait 0 # Open socket set ch [::tls::socket -servername $host -request 1 -require 1 -alpn [list [string tolower $protocol]] $host $port] chan configure $ch -blocking 0 -buffering line -buffersize 16384 -encoding utf-8 -translation {auto crlf} fileevent $ch readable [list handler $ch] # Initiate handshake ::tls::handshake $ch after 1000 # Send get request after 5000 [list set ::wait 1] http_get $ch $host $path $protocol vwait ::wait catch {close $ch} } set data "" gets_non_blocking_no_variable $host $port $path $protocol save_file "gets_non_blocking_no_variable.txt" $data |
Added demos/gets_nonblocking_with_variable.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | ################################################# # # Example 4: Non-blocking channel gets with variable # ################################################# package require Tcl 8.6- package require tls set host "www.google.com" set port 443 set path "/" set protocol "http/1.1" # # Send HTTP Get Request # proc http_get {ch host path protocol} { puts $ch [format "GET %s %s" $path [string toupper $protocol]] puts $ch [format "User-Agent: Mozilla/4.0 (compatible; %s)" $::tcl_platform(os)] puts $ch [format "Host: %s" $host] puts $ch [format "Connection: close"] puts $ch "" flush $ch } # Save returned data to file proc save_file {filename data} { if {[catch {open $filename wb} ch]} { return -code error $ch } fconfigure $ch -buffersize 16384 -encoding utf-8 -translation crlf puts $ch $data close $ch } proc handler {ch} { if {[gets $ch line] < 0 && [eof $ch]} { # EOF close $ch set ::wait 1 return } elseif {![fblocked $ch]} { # Full or empty line append ::data $line "\n" } else { # Partial line append ::data $line } } proc gets_non_blocking_with_variable {host port path protocol} { set ::wait 0 # Open socket set ch [::tls::socket -servername $host -request 1 -require 1 -alpn [list [string tolower $protocol]] $host $port] chan configure $ch -blocking 0 -buffering line -buffersize 16384 -encoding utf-8 -translation {auto crlf} fileevent $ch readable [list handler $ch] # Initiate handshake ::tls::handshake $ch after 1000 # Send get request after 5000 [list set ::wait 1] http_get $ch $host $path $protocol vwait ::wait catch {close $ch} } set data "" gets_non_blocking_with_variable $host $port $path $protocol save_file "gets_non_blocking_with_variable.txt" $data |
Added demos/http_get_file.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | ################################################# # # Download file using HTTP package # ################################################# package require Tcl 8.6- package require tls package require http set url "https://wiki.tcl-lang.org/sitemap.xml" set protocol "http/1.1" set filename [file tail $url] # Register https protocol handler with http package http::register https 443 [list ::tls::socket -autoservername 1 -require 1 -alpn [list [string tolower $protocol]]] # Open output file set ch [open $filename wb] # Get webpage set token [::http::geturl $url -blocksize 16384 -channel $ch] if {[http::status $token] ne "ok"} { puts [format "Error %s" [http::status $token]] } # Cleanup ::http::cleanup $token close $ch |
Added demos/http_get_webpage.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | ################################################# # # Download webpage using HTTP package # ################################################# package require Tcl 8.6- package require tls package require http set url "https://www.tcl.tk/" set port 443 set protocol "http/1.1" # Register https protocol handler with http package http::register https 443 [list ::tls::socket -autoservername 1 -require 1 -alpn [list [string tolower $protocol]]] # Get webpage set token [::http::geturl $url -blocksize 16384] if {[http::status $token] ne "ok"} { puts [format "Error: \"%s\"" [http::status $token]] ::http::cleanup $token exit } # Get web page set data [http::data $token] # Cleanup ::http::cleanup $token # Save data to file set ch [open "tcl_tk_home.html" wb] puts $ch $data close $ch |
Added demos/http_get_webpage_proxy.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | ################################################# # # Download webpage using HTTP and proxy packages. # # Process: # - Connect to the proxy # - Send HTTP "CONNECT $targeturl HTTP/1.1". # - Proxy responds with HTTP protocol response. # - Do tls::import # - Start handdshaking # ################################################# package require Tcl 8.6- package require tls package require http package require autoproxy autoproxy::init set url "https://www.tcl.tk/" set port 443 set protocol "http/1.1" # Set these if not set by OS/Platform if 0 { autoproxy::configure -basic -proxy_host example.com -proxy_port 880 -username user -password password } # Register https protocol handler and proxy with http package ::http::register https 443 [list ::autoproxy::tls_socket -autoservername 1 -require 1 \ -alpn [list [string tolower $protocol]]] # Get webpage set token [::http::geturl $url -blocksize 16384] if {[http::status $token] ne "ok"} { puts [format "Error: \"%s\"" [http::status $token]] ::http::cleanup $token exit } # Get web page set data [http::data $token] # Cleanup ::http::cleanup $token # Open output file set ch [open "tcl_tk_home.html" wb] puts $ch $data close $ch |
Added demos/read_blocking_webpage.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 63 64 65 66 | ################################################# # # Read using blocking channel # ################################################# package require Tcl 8.6- package require tls set host "www.google.com" set port 443 set path "/" set protocol "http/1.1" # # Send HTTP Get Request # proc http_get {ch host path protocol} { puts $ch [format "GET %s %s" $path [string toupper $protocol]] puts $ch [format "User-Agent: Mozilla/4.0 (compatible; %s)" $::tcl_platform(os)] puts $ch [format "Host: %s" $host] puts $ch [format "Connection: close"] puts $ch "" flush $ch } # Save returned data to file proc save_file {filename data} { if {[catch {open $filename wb} ch]} { return -code error $ch } fconfigure $ch -buffersize 16384 -encoding utf-8 -translation crlf puts $ch $data close $ch } proc read_blocking {host port path protocol} { set result "" # Open socket set ch [::tls::socket -servername $host -request 1 -require 1 -alpn [list [string tolower $protocol]] $host $port] chan configure $ch -blocking 1 -buffering line -buffersize 16384 -encoding utf-8 -translation {auto crlf} # Initiate handshake ::tls::handshake $ch after 1000 # Send get request http_get $ch $host $path $protocol after 1000 # Get data while {1} { append result [read $ch 4096] if {[eof $ch]} { close $ch break } } return $result } save_file "read_blocking_webpage.txt" [read_blocking $host $port $path $protocol] |
Added demos/read_nonblocking_webpage.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 63 64 65 66 67 68 69 70 | ################################################# # # Read using blocking channel # ################################################# package require Tcl 8.6- package require tls set host "www.google.com" set port 443 set path "/" set protocol "http/1.1" # # Send HTTP Get Request # proc http_get {ch host path protocol} { puts $ch [format "GET %s %s" $path [string toupper $protocol]] puts $ch [format "User-Agent: Mozilla/4.0 (compatible; %s)" $::tcl_platform(os)] puts $ch [format "Host: %s" $host] puts $ch [format "Connection: close"] puts $ch "" flush $ch } # Save returned data to file proc save_file {filename data} { if {[catch {open $filename wb} ch]} { return -code error $ch } fconfigure $ch -buffersize 16384 -encoding utf-8 -translation crlf puts $ch $data close $ch } proc handler {ch} { append ::data [read $ch 4096] if {[eof $ch]} { close $ch set ::wait 1 } } proc read_nonblocking {host port path protocol} { set result "" # Open socket set ch [::tls::socket -servername $host -request 1 -require 1 -alpn [list [string tolower $protocol]] $host $port] chan configure $ch -blocking 1 -buffering line -buffersize 16384 -encoding utf-8 -translation {auto crlf} fileevent $ch readable [list handler $ch] # Initiate handshake ::tls::handshake $ch after 1000 # Send get request after 5000 [list set ::wait 1] http_get $ch $host $path $protocol vwait ::wait catch {close $ch} } set data "" read_nonblocking $host $port $path $protocol save_file "read_nonblocking_webpage.txt" $data |
Changes to doc/tls.html.
︙ | ︙ | |||
768 769 770 771 772 773 774 | <b class="option">-validatecommand</b> option is set to <b class="cmd">tls::validate_command</b>.</p> <p><em>The use of the variable <b class="variable">tls::debug</b> is not recommended. It may be removed from future releases.</em></p> </div> <div id="section6" class="doctools_section"><h2><a name="section6">Debug Examples</a></h2> <p>These examples use the default Unix platform SSL certificates. For standard installations, -cadir and -cafile should not be needed. If your certificates | | > | 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 | <b class="option">-validatecommand</b> option is set to <b class="cmd">tls::validate_command</b>.</p> <p><em>The use of the variable <b class="variable">tls::debug</b> is not recommended. It may be removed from future releases.</em></p> </div> <div id="section6" class="doctools_section"><h2><a name="section6">Debug Examples</a></h2> <p>These examples use the default Unix platform SSL certificates. For standard installations, -cadir and -cafile should not be needed. If your certificates are in non-standard locations, specify -cadir or -cafile as needed. See the demos directory for more elaborate examples.</p> <p>Example #1: Use HTTP package</p> <pre class="doctools_example"> package require http package require tls set url "https://www.tcl.tk/" http::register https 443 [list ::tls::socket -autoservername 1 -require 1 -command ::tls::callback -password ::tls::password -validatecommand ::tls::validate_command] # Check for error |
︙ | ︙ |
Changes to doc/tls.man.
︙ | ︙ | |||
860 861 862 863 864 865 866 | [emph "The use of the variable [var tls::debug] is not recommended. It may be removed from future releases."] [section "Debug Examples"] These examples use the default Unix platform SSL certificates. For standard installations, -cadir and -cafile should not be needed. If your certificates | | > | 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 | [emph "The use of the variable [var tls::debug] is not recommended. It may be removed from future releases."] [section "Debug Examples"] These examples use the default Unix platform SSL certificates. For standard installations, -cadir and -cafile should not be needed. If your certificates are in non-standard locations, specify -cadir or -cafile as needed. See the demos directory for more elaborate examples. [para] Example #1: Use HTTP package [example { |
︙ | ︙ |
Changes to doc/tls.n.
︙ | ︙ | |||
1053 1054 1055 1056 1057 1058 1059 | \fB-validatecommand\fR option is set to \fBtls::validate_command\fR\&. .PP \fIThe use of the variable \fBtls::debug\fR is not recommended\&. It may be removed from future releases\&.\fR .SH "DEBUG EXAMPLES" These examples use the default Unix platform SSL certificates\&. For standard installations, -cadir and -cafile should not be needed\&. If your certificates | | > | 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 | \fB-validatecommand\fR option is set to \fBtls::validate_command\fR\&. .PP \fIThe use of the variable \fBtls::debug\fR is not recommended\&. It may be removed from future releases\&.\fR .SH "DEBUG EXAMPLES" These examples use the default Unix platform SSL certificates\&. For standard installations, -cadir and -cafile should not be needed\&. If your certificates are in non-standard locations, specify -cadir or -cafile as needed\&. See the demos directory for more elaborate examples\&. .PP Example #1: Use HTTP package .CS package require http |
︙ | ︙ |
Changes to win/makefile.vc.
︙ | ︙ | |||
136 137 138 139 140 141 142 | @copy $(WIN_DIR)\tlsUuid.h.in+$(TMP_DIR)\manifest.uuid $(TMP_DIR)\tlsUuid.h @echo: >>$(TMP_DIR)\tlsUuid.h # The default install target only installs binaries and scripts so add # an additional target for our documentation. Note this *adds* a target # since no commands are listed after it. The original targets for # install (from targets.vc) will remain. | | | 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | @copy $(WIN_DIR)\tlsUuid.h.in+$(TMP_DIR)\manifest.uuid $(TMP_DIR)\tlsUuid.h @echo: >>$(TMP_DIR)\tlsUuid.h # The default install target only installs binaries and scripts so add # an additional target for our documentation. Note this *adds* a target # since no commands are listed after it. The original targets for # install (from targets.vc) will remain. install: pkgindex default-install default-install-docs-html default-install-demos !IF EXIST($(SSL_INSTALL_FOLDER)\bin\libcrypto-*-x64.dll) @xcopy /c /y "$(SSL_INSTALL_FOLDER)\bin\libcrypto-*-x64.dll" "$(PRJ_INSTALL_DIR)" !ENDIF !IF EXIST($(SSL_INSTALL_FOLDER)\bin\libssl-*-x64.dll) @xcopy /c /y "$(SSL_INSTALL_FOLDER)\bin\libssl-*-x64.dll" "$(PRJ_INSTALL_DIR)" !ENDIF |
︙ | ︙ |