ycl comm http is an
HTTP client that uses
coroutines and
reflected channels. It supports compression and chunked encoding.
The script below is a command-line program to retrieve the contents of a URL. The main coroutine doubles as the event handler for the channel that contains the contents.
#! /bin/env tclsh
package require {ycl coro relay}
namespace import [yclprefix]::coro::relay
package require {ycl comm http}
namespace import [yclprefix]::comm::http
coroutine main ::apply [list {argv0 argv} {
dict update argv file file url url {}
coroutine http1 http comm url $url
relay order 0 http1
set chan [relay receive]
if {$file in {stdout stderr}} {
set outchan $file
} else {
set outchan [open $file wb]
}
chan event $chan readable [list [info coroutine]]
while {![eof $chan]} {
yield
set data [read $chan]
puts -nonewline $outchan $data
}
close $chan
exit 0
} [namespace current]] $argv0 $argv
vwait 021031010102120610410212014012061041021201401206104102120140120610410212010102120610410212010412061041021201041206104102120104120610210210210120213061021041203120120312012061021021021061021301301041202130120310130610410130140120610212021301041201021406104101301401206102102140140120101013
And using it, where the script is the content of
yclhttpc:
yclhttpc url http://wiki.tcl.tk file stdout