http.tcl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. # http.tcl
  2. # Client-side HTTP for GET, POST, and HEAD commands.
  3. # These routines can be used in untrusted code that uses the Safesock
  4. # security policy.
  5. # These procedures use a callback interface to avoid using vwait,
  6. # which is not defined in the safe base.
  7. #
  8. # See the http.n man page for documentation
  9. package provide http 1.0
  10. array set http {
  11. -accept */*
  12. -proxyhost {}
  13. -proxyport {}
  14. -useragent {Tcl http client package 1.0}
  15. -proxyfilter httpProxyRequired
  16. }
  17. proc http_config {args} {
  18. global http
  19. set options [lsort [array names http -*]]
  20. set usage [join $options ", "]
  21. if {[llength $args] == 0} {
  22. set result {}
  23. foreach name $options {
  24. lappend result $name $http($name)
  25. }
  26. return $result
  27. }
  28. regsub -all -- - $options {} options
  29. set pat ^-([join $options |])$
  30. if {[llength $args] == 1} {
  31. set flag [lindex $args 0]
  32. if {[regexp -- $pat $flag]} {
  33. return $http($flag)
  34. } else {
  35. return -code error "Unknown option $flag, must be: $usage"
  36. }
  37. } else {
  38. foreach {flag value} $args {
  39. if {[regexp -- $pat $flag]} {
  40. set http($flag) $value
  41. } else {
  42. return -code error "Unknown option $flag, must be: $usage"
  43. }
  44. }
  45. }
  46. }
  47. proc httpFinish { token {errormsg ""} } {
  48. upvar #0 $token state
  49. global errorInfo errorCode
  50. if {[string length $errormsg] != 0} {
  51. set state(error) [list $errormsg $errorInfo $errorCode]
  52. set state(status) error
  53. }
  54. catch {close $state(sock)}
  55. catch {after cancel $state(after)}
  56. if {[info exists state(-command)]} {
  57. if {[catch {eval $state(-command) {$token}} err]} {
  58. if {[string length $errormsg] == 0} {
  59. set state(error) [list $err $errorInfo $errorCode]
  60. set state(status) error
  61. }
  62. }
  63. unset state(-command)
  64. }
  65. }
  66. proc http_reset { token {why reset} } {
  67. upvar #0 $token state
  68. set state(status) $why
  69. catch {fileevent $state(sock) readable {}}
  70. httpFinish $token
  71. if {[info exists state(error)]} {
  72. set errorlist $state(error)
  73. unset state(error)
  74. eval error $errorlist
  75. }
  76. }
  77. proc http_get { url args } {
  78. global http
  79. if {![info exists http(uid)]} {
  80. set http(uid) 0
  81. }
  82. set token http#[incr http(uid)]
  83. upvar #0 $token state
  84. http_reset $token
  85. array set state {
  86. -blocksize 8192
  87. -validate 0
  88. -headers {}
  89. -timeout 0
  90. state header
  91. meta {}
  92. currentsize 0
  93. totalsize 0
  94. type text/html
  95. body {}
  96. status ""
  97. }
  98. set options {-blocksize -channel -command -handler -headers \
  99. -progress -query -validate -timeout}
  100. set usage [join $options ", "]
  101. regsub -all -- - $options {} options
  102. set pat ^-([join $options |])$
  103. foreach {flag value} $args {
  104. if {[regexp $pat $flag]} {
  105. # Validate numbers
  106. if {[info exists state($flag)] && \
  107. [regexp {^[0-9]+$} $state($flag)] && \
  108. ![regexp {^[0-9]+$} $value]} {
  109. return -code error "Bad value for $flag ($value), must be integer"
  110. }
  111. set state($flag) $value
  112. } else {
  113. return -code error "Unknown option $flag, can be: $usage"
  114. }
  115. }
  116. if {! [regexp -nocase {^(http://)?([^/:]+)(:([0-9]+))?(/.*)?$} $url \
  117. x proto host y port srvurl]} {
  118. error "Unsupported URL: $url"
  119. }
  120. if {[string length $port] == 0} {
  121. set port 80
  122. }
  123. if {[string length $srvurl] == 0} {
  124. set srvurl /
  125. }
  126. if {[string length $proto] == 0} {
  127. set url http://$url
  128. }
  129. set state(url) $url
  130. if {![catch {$http(-proxyfilter) $host} proxy]} {
  131. set phost [lindex $proxy 0]
  132. set pport [lindex $proxy 1]
  133. }
  134. if {$state(-timeout) > 0} {
  135. set state(after) [after $state(-timeout) [list http_reset $token timeout]]
  136. }
  137. if {[info exists phost] && [string length $phost]} {
  138. set srvurl $url
  139. set s [socket $phost $pport]
  140. } else {
  141. set s [socket $host $port]
  142. }
  143. set state(sock) $s
  144. # Send data in cr-lf format, but accept any line terminators
  145. fconfigure $s -translation {auto crlf} -buffersize $state(-blocksize)
  146. # The following is disallowed in safe interpreters, but the socket
  147. # is already in non-blocking mode in that case.
  148. catch {fconfigure $s -blocking off}
  149. set len 0
  150. set how GET
  151. if {[info exists state(-query)]} {
  152. set len [string length $state(-query)]
  153. if {$len > 0} {
  154. set how POST
  155. }
  156. } elseif {$state(-validate)} {
  157. set how HEAD
  158. }
  159. puts $s "$how $srvurl HTTP/1.0"
  160. puts $s "Accept: $http(-accept)"
  161. puts $s "Host: $host"
  162. puts $s "User-Agent: $http(-useragent)"
  163. foreach {key value} $state(-headers) {
  164. regsub -all \[\n\r\] $value {} value
  165. set key [string trim $key]
  166. if {[string length $key]} {
  167. puts $s "$key: $value"
  168. }
  169. }
  170. if {$len > 0} {
  171. puts $s "Content-Length: $len"
  172. puts $s "Content-Type: application/x-www-form-urlencoded"
  173. puts $s ""
  174. fconfigure $s -translation {auto binary}
  175. puts -nonewline $s $state(-query)
  176. } else {
  177. puts $s ""
  178. }
  179. flush $s
  180. fileevent $s readable [list httpEvent $token]
  181. if {! [info exists state(-command)]} {
  182. http_wait $token
  183. }
  184. return $token
  185. }
  186. proc http_data {token} {
  187. upvar #0 $token state
  188. return $state(body)
  189. }
  190. proc http_status {token} {
  191. upvar #0 $token state
  192. return $state(status)
  193. }
  194. proc http_code {token} {
  195. upvar #0 $token state
  196. return $state(http)
  197. }
  198. proc http_size {token} {
  199. upvar #0 $token state
  200. return $state(currentsize)
  201. }
  202. proc httpEvent {token} {
  203. upvar #0 $token state
  204. set s $state(sock)
  205. if {[eof $s]} {
  206. httpEof $token
  207. return
  208. }
  209. if {$state(state) == "header"} {
  210. set n [gets $s line]
  211. if {$n == 0} {
  212. set state(state) body
  213. if {![regexp -nocase ^text $state(type)]} {
  214. # Turn off conversions for non-text data
  215. fconfigure $s -translation binary
  216. if {[info exists state(-channel)]} {
  217. fconfigure $state(-channel) -translation binary
  218. }
  219. }
  220. if {[info exists state(-channel)] &&
  221. ![info exists state(-handler)]} {
  222. # Initiate a sequence of background fcopies
  223. fileevent $s readable {}
  224. httpCopyStart $s $token
  225. }
  226. } elseif {$n > 0} {
  227. if {[regexp -nocase {^content-type:(.+)$} $line x type]} {
  228. set state(type) [string trim $type]
  229. }
  230. if {[regexp -nocase {^content-length:(.+)$} $line x length]} {
  231. set state(totalsize) [string trim $length]
  232. }
  233. if {[regexp -nocase {^([^:]+):(.+)$} $line x key value]} {
  234. lappend state(meta) $key $value
  235. } elseif {[regexp ^HTTP $line]} {
  236. set state(http) $line
  237. }
  238. }
  239. } else {
  240. if {[catch {
  241. if {[info exists state(-handler)]} {
  242. set n [eval $state(-handler) {$s $token}]
  243. } else {
  244. set block [read $s $state(-blocksize)]
  245. set n [string length $block]
  246. if {$n >= 0} {
  247. append state(body) $block
  248. }
  249. }
  250. if {$n >= 0} {
  251. incr state(currentsize) $n
  252. }
  253. } err]} {
  254. httpFinish $token $err
  255. } else {
  256. if {[info exists state(-progress)]} {
  257. eval $state(-progress) {$token $state(totalsize) $state(currentsize)}
  258. }
  259. }
  260. }
  261. }
  262. proc httpCopyStart {s token} {
  263. upvar #0 $token state
  264. if {[catch {
  265. fcopy $s $state(-channel) -size $state(-blocksize) -command \
  266. [list httpCopyDone $token]
  267. } err]} {
  268. httpFinish $token $err
  269. }
  270. }
  271. proc httpCopyDone {token count {error {}}} {
  272. upvar #0 $token state
  273. set s $state(sock)
  274. incr state(currentsize) $count
  275. if {[info exists state(-progress)]} {
  276. eval $state(-progress) {$token $state(totalsize) $state(currentsize)}
  277. }
  278. if {([string length $error] != 0)} {
  279. httpFinish $token $error
  280. } elseif {[eof $s]} {
  281. httpEof $token
  282. } else {
  283. httpCopyStart $s $token
  284. }
  285. }
  286. proc httpEof {token} {
  287. upvar #0 $token state
  288. if {$state(state) == "header"} {
  289. # Premature eof
  290. set state(status) eof
  291. } else {
  292. set state(status) ok
  293. }
  294. set state(state) eof
  295. httpFinish $token
  296. }
  297. proc http_wait {token} {
  298. upvar #0 $token state
  299. if {![info exists state(status)] || [string length $state(status)] == 0} {
  300. vwait $token\(status)
  301. }
  302. if {[info exists state(error)]} {
  303. set errorlist $state(error)
  304. unset state(error)
  305. eval error $errorlist
  306. }
  307. return $state(status)
  308. }
  309. # Call http_formatQuery with an even number of arguments, where the first is
  310. # a name, the second is a value, the third is another name, and so on.
  311. proc http_formatQuery {args} {
  312. set result ""
  313. set sep ""
  314. foreach i $args {
  315. append result $sep [httpMapReply $i]
  316. if {$sep != "="} {
  317. set sep =
  318. } else {
  319. set sep &
  320. }
  321. }
  322. return $result
  323. }
  324. # do x-www-urlencoded character mapping
  325. # The spec says: "non-alphanumeric characters are replaced by '%HH'"
  326. # 1 leave alphanumerics characters alone
  327. # 2 Convert every other character to an array lookup
  328. # 3 Escape constructs that are "special" to the tcl parser
  329. # 4 "subst" the result, doing all the array substitutions
  330. proc httpMapReply {string} {
  331. global httpFormMap
  332. set alphanumeric a-zA-Z0-9
  333. if {![info exists httpFormMap]} {
  334. for {set i 1} {$i <= 256} {incr i} {
  335. set c [format %c $i]
  336. if {![string match \[$alphanumeric\] $c]} {
  337. set httpFormMap($c) %[format %.2x $i]
  338. }
  339. }
  340. # These are handled specially
  341. array set httpFormMap {
  342. " " + \n %0d%0a
  343. }
  344. }
  345. regsub -all \[^$alphanumeric\] $string {$httpFormMap(&)} string
  346. regsub -all \n $string {\\n} string
  347. regsub -all \t $string {\\t} string
  348. regsub -all {[][{})\\]\)} $string {\\&} string
  349. return [subst $string]
  350. }
  351. # Default proxy filter.
  352. proc httpProxyRequired {host} {
  353. global http
  354. if {[info exists http(-proxyhost)] && [string length $http(-proxyhost)]} {
  355. if {![info exists http(-proxyport)] || ![string length $http(-proxyport)]} {
  356. set http(-proxyport) 8080
  357. }
  358. return [list $http(-proxyhost) $http(-proxyport)]
  359. } else {
  360. return {}
  361. }
  362. }