http-2.9.0.tm 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427
  1. # http.tcl --
  2. #
  3. # Client-side HTTP for GET, POST, and HEAD commands. These routines can
  4. # be used in untrusted code that uses the Safesock security policy.
  5. # These procedures use a callback interface to avoid using vwait, which
  6. # is not defined in the safe base.
  7. #
  8. # See the file "license.terms" for information on usage and redistribution of
  9. # this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10. package require Tcl 8.6-
  11. # Keep this in sync with pkgIndex.tcl and with the install directories in
  12. # Makefiles
  13. package provide http 2.9.0
  14. namespace eval http {
  15. # Allow resourcing to not clobber existing data
  16. variable http
  17. if {![info exists http]} {
  18. array set http {
  19. -accept */*
  20. -pipeline 1
  21. -postfresh 0
  22. -proxyhost {}
  23. -proxyport {}
  24. -proxyfilter http::ProxyRequired
  25. -repost 0
  26. -urlencoding utf-8
  27. -zip 1
  28. }
  29. # We need a useragent string of this style or various servers will
  30. # refuse to send us compressed content even when we ask for it. This
  31. # follows the de-facto layout of user-agent strings in current browsers.
  32. # Safe interpreters do not have ::tcl_platform(os) or
  33. # ::tcl_platform(osVersion).
  34. if {[interp issafe]} {
  35. set http(-useragent) "Mozilla/5.0\
  36. (Windows; U;\
  37. Windows NT 10.0)\
  38. http/[package provide http] Tcl/[package provide Tcl]"
  39. } else {
  40. set http(-useragent) "Mozilla/5.0\
  41. ([string totitle $::tcl_platform(platform)]; U;\
  42. $::tcl_platform(os) $::tcl_platform(osVersion))\
  43. http/[package provide http] Tcl/[package provide Tcl]"
  44. }
  45. }
  46. proc init {} {
  47. # Set up the map for quoting chars. RFC3986 Section 2.3 say percent
  48. # encode all except: "... percent-encoded octets in the ranges of
  49. # ALPHA (%41-%5A and %61-%7A), DIGIT (%30-%39), hyphen (%2D), period
  50. # (%2E), underscore (%5F), or tilde (%7E) should not be created by URI
  51. # producers ..."
  52. for {set i 0} {$i <= 256} {incr i} {
  53. set c [format %c $i]
  54. if {![string match {[-._~a-zA-Z0-9]} $c]} {
  55. set map($c) %[format %.2X $i]
  56. }
  57. }
  58. # These are handled specially
  59. set map(\n) %0D%0A
  60. variable formMap [array get map]
  61. # Create a map for HTTP/1.1 open sockets
  62. variable socketMapping
  63. variable socketRdState
  64. variable socketWrState
  65. variable socketRdQueue
  66. variable socketWrQueue
  67. variable socketClosing
  68. variable socketPlayCmd
  69. if {[info exists socketMapping]} {
  70. # Close open sockets on re-init. Do not permit retries.
  71. foreach {url sock} [array get socketMapping] {
  72. unset -nocomplain socketClosing($url)
  73. unset -nocomplain socketPlayCmd($url)
  74. CloseSocket $sock
  75. }
  76. }
  77. # CloseSocket should have unset the socket* arrays, one element at
  78. # a time. Now unset anything that was overlooked.
  79. # Traces on "unset socketRdState(*)" will call CancelReadPipeline and
  80. # cancel any queued responses.
  81. # Traces on "unset socketWrState(*)" will call CancelWritePipeline and
  82. # cancel any queued requests.
  83. array unset socketMapping
  84. array unset socketRdState
  85. array unset socketWrState
  86. array unset socketRdQueue
  87. array unset socketWrQueue
  88. array unset socketClosing
  89. array unset socketPlayCmd
  90. array set socketMapping {}
  91. array set socketRdState {}
  92. array set socketWrState {}
  93. array set socketRdQueue {}
  94. array set socketWrQueue {}
  95. array set socketClosing {}
  96. array set socketPlayCmd {}
  97. }
  98. init
  99. variable urlTypes
  100. if {![info exists urlTypes]} {
  101. set urlTypes(http) [list 80 ::socket]
  102. }
  103. variable encodings [string tolower [encoding names]]
  104. # This can be changed, but iso8859-1 is the RFC standard.
  105. variable defaultCharset
  106. if {![info exists defaultCharset]} {
  107. set defaultCharset "iso8859-1"
  108. }
  109. # Force RFC 3986 strictness in geturl url verification?
  110. variable strict
  111. if {![info exists strict]} {
  112. set strict 1
  113. }
  114. # Let user control default keepalive for compatibility
  115. variable defaultKeepalive
  116. if {![info exists defaultKeepalive]} {
  117. set defaultKeepalive 0
  118. }
  119. namespace export geturl config reset wait formatQuery quoteString
  120. namespace export register unregister registerError
  121. # - Useful, but not exported: data, size, status, code, cleanup, error,
  122. # meta, ncode, mapReply, init. Comments suggest that "init" can be used
  123. # for re-initialisation, although the command is undocumented.
  124. # - Not exported, probably should be upper-case initial letter as part
  125. # of the internals: getTextLine, make-transformation-chunked.
  126. }
  127. # http::Log --
  128. #
  129. # Debugging output -- define this to observe HTTP/1.1 socket usage.
  130. # Should echo any args received.
  131. #
  132. # Arguments:
  133. # msg Message to output
  134. #
  135. if {[info command http::Log] eq {}} {proc http::Log {args} {}}
  136. # http::register --
  137. #
  138. # See documentation for details.
  139. #
  140. # Arguments:
  141. # proto URL protocol prefix, e.g. https
  142. # port Default port for protocol
  143. # command Command to use to create socket
  144. # Results:
  145. # list of port and command that was registered.
  146. proc http::register {proto port command} {
  147. variable urlTypes
  148. set urlTypes([string tolower $proto]) [list $port $command]
  149. }
  150. # http::unregister --
  151. #
  152. # Unregisters URL protocol handler
  153. #
  154. # Arguments:
  155. # proto URL protocol prefix, e.g. https
  156. # Results:
  157. # list of port and command that was unregistered.
  158. proc http::unregister {proto} {
  159. variable urlTypes
  160. set lower [string tolower $proto]
  161. if {![info exists urlTypes($lower)]} {
  162. return -code error "unsupported url type \"$proto\""
  163. }
  164. set old $urlTypes($lower)
  165. unset urlTypes($lower)
  166. return $old
  167. }
  168. # http::config --
  169. #
  170. # See documentation for details.
  171. #
  172. # Arguments:
  173. # args Options parsed by the procedure.
  174. # Results:
  175. # TODO
  176. proc http::config {args} {
  177. variable http
  178. set options [lsort [array names http -*]]
  179. set usage [join $options ", "]
  180. if {[llength $args] == 0} {
  181. set result {}
  182. foreach name $options {
  183. lappend result $name $http($name)
  184. }
  185. return $result
  186. }
  187. set options [string map {- ""} $options]
  188. set pat ^-(?:[join $options |])$
  189. if {[llength $args] == 1} {
  190. set flag [lindex $args 0]
  191. if {![regexp -- $pat $flag]} {
  192. return -code error "Unknown option $flag, must be: $usage"
  193. }
  194. return $http($flag)
  195. } else {
  196. foreach {flag value} $args {
  197. if {![regexp -- $pat $flag]} {
  198. return -code error "Unknown option $flag, must be: $usage"
  199. }
  200. set http($flag) $value
  201. }
  202. }
  203. }
  204. # http::Finish --
  205. #
  206. # Clean up the socket and eval close time callbacks
  207. #
  208. # Arguments:
  209. # token Connection token.
  210. # errormsg (optional) If set, forces status to error.
  211. # skipCB (optional) If set, don't call the -command callback. This
  212. # is useful when geturl wants to throw an exception instead
  213. # of calling the callback. That way, the same error isn't
  214. # reported to two places.
  215. #
  216. # Side Effects:
  217. # May close the socket.
  218. proc http::Finish {token {errormsg ""} {skipCB 0}} {
  219. variable socketMapping
  220. variable socketRdState
  221. variable socketWrState
  222. variable socketRdQueue
  223. variable socketWrQueue
  224. variable socketClosing
  225. variable socketPlayCmd
  226. variable $token
  227. upvar 0 $token state
  228. global errorInfo errorCode
  229. set closeQueue 0
  230. if {$errormsg ne ""} {
  231. set state(error) [list $errormsg $errorInfo $errorCode]
  232. set state(status) "error"
  233. }
  234. if {[info commands ${token}EventCoroutine] ne {}} {
  235. rename ${token}EventCoroutine {}
  236. }
  237. if { ($state(status) eq "timeout")
  238. || ($state(status) eq "error")
  239. || ($state(status) eq "eof")
  240. || ([info exists state(-keepalive)] && !$state(-keepalive))
  241. || ([info exists state(connection)] && ($state(connection) eq "close"))
  242. } {
  243. set closeQueue 1
  244. set connId $state(socketinfo)
  245. set sock $state(sock)
  246. CloseSocket $state(sock) $token
  247. } elseif {
  248. ([info exists state(-keepalive)] && $state(-keepalive))
  249. && ([info exists state(connection)] && ($state(connection) ne "close"))
  250. } {
  251. KeepSocket $token
  252. }
  253. if {[info exists state(after)]} {
  254. after cancel $state(after)
  255. unset state(after)
  256. }
  257. if {[info exists state(-command)] && (!$skipCB)
  258. && (![info exists state(done-command-cb)])} {
  259. set state(done-command-cb) yes
  260. if {[catch {eval $state(-command) {$token}} err] && $errormsg eq ""} {
  261. set state(error) [list $err $errorInfo $errorCode]
  262. set state(status) error
  263. }
  264. }
  265. if { $closeQueue
  266. && [info exists socketMapping($connId)]
  267. && ($socketMapping($connId) eq $sock)
  268. } {
  269. http::CloseQueuedQueries $connId $token
  270. }
  271. }
  272. # http::KeepSocket -
  273. #
  274. # Keep a socket in the persistent sockets table and connect it to its next
  275. # queued task if possible. Otherwise leave it idle and ready for its next
  276. # use.
  277. #
  278. # If $socketClosing(*), then ($state(connection) eq "close") and therefore
  279. # this command will not be called by Finish.
  280. #
  281. # Arguments:
  282. # token Connection token.
  283. proc http::KeepSocket {token} {
  284. variable http
  285. variable socketMapping
  286. variable socketRdState
  287. variable socketWrState
  288. variable socketRdQueue
  289. variable socketWrQueue
  290. variable socketClosing
  291. variable socketPlayCmd
  292. variable $token
  293. upvar 0 $token state
  294. set tk [namespace tail $token]
  295. # Keep this socket open for another request ("Keep-Alive").
  296. # React if the server half-closes the socket.
  297. # Discussion is in http::geturl.
  298. catch {fileevent $state(sock) readable [list http::CheckEof $state(sock)]}
  299. # The line below should not be changed in production code.
  300. # It is edited by the test suite.
  301. set TEST_EOF 0
  302. if {$TEST_EOF} {
  303. # ONLY for testing reaction to server eof.
  304. # No server timeouts will be caught.
  305. catch {fileevent $state(sock) readable {}}
  306. }
  307. if { [info exists state(socketinfo)]
  308. && [info exists socketMapping($state(socketinfo))]
  309. } {
  310. set connId $state(socketinfo)
  311. # The value "Rready" is set only here.
  312. set socketRdState($connId) Rready
  313. if { $state(-pipeline)
  314. && [info exists socketRdQueue($connId)]
  315. && [llength $socketRdQueue($connId)]
  316. } {
  317. # The usual case for pipelined responses - if another response is
  318. # queued, arrange to read it.
  319. set token3 [lindex $socketRdQueue($connId) 0]
  320. set socketRdQueue($connId) [lrange $socketRdQueue($connId) 1 end]
  321. variable $token3
  322. upvar 0 $token3 state3
  323. set tk2 [namespace tail $token3]
  324. #Log pipelined, GRANT read access to $token3 in KeepSocket
  325. set socketRdState($connId) $token3
  326. ReceiveResponse $token3
  327. # Other pipelined cases.
  328. # - The test above ensures that, for the pipelined cases in the two
  329. # tests below, the read queue is empty.
  330. # - In those two tests, check whether the next write will be
  331. # nonpipeline.
  332. } elseif {
  333. $state(-pipeline)
  334. && [info exists socketWrState($connId)]
  335. && ($socketWrState($connId) eq "peNding")
  336. && [info exists socketWrQueue($connId)]
  337. && [llength $socketWrQueue($connId)]
  338. && (![set token3 [lindex $socketWrQueue($connId) 0]
  339. set ${token3}(-pipeline)
  340. ]
  341. )
  342. } {
  343. # This case:
  344. # - Now it the time to run the "pending" request.
  345. # - The next token in the write queue is nonpipeline, and
  346. # socketWrState has been marked "pending" (in
  347. # http::NextPipelinedWrite or http::geturl) so a new pipelined
  348. # request cannot jump the queue.
  349. #
  350. # Tests:
  351. # - In this case the read queue (tested above) is empty and this
  352. # "pending" write token is in front of the rest of the write
  353. # queue.
  354. # - The write state is not Wready and therefore appears to be busy,
  355. # but because it is "pending" we know that it is reserved for the
  356. # first item in the write queue, a non-pipelined request that is
  357. # waiting for the read queue to empty. That has now happened: so
  358. # give that request read and write access.
  359. variable $token3
  360. set conn [set ${token3}(tmpConnArgs)]
  361. #Log nonpipeline, GRANT r/w access to $token3 in KeepSocket
  362. set socketRdState($connId) $token3
  363. set socketWrState($connId) $token3
  364. set socketWrQueue($connId) [lrange $socketWrQueue($connId) 1 end]
  365. # Connect does its own fconfigure.
  366. fileevent $state(sock) writable [list http::Connect $token3 {*}$conn]
  367. #Log ---- $state(sock) << conn to $token3 for HTTP request (c)
  368. } elseif {
  369. $state(-pipeline)
  370. && [info exists socketWrState($connId)]
  371. && ($socketWrState($connId) eq "peNding")
  372. } {
  373. # Should not come here. The second block in the previous "elseif"
  374. # test should be tautologous (but was needed in an earlier
  375. # implementation) and will be removed after testing.
  376. # If we get here, the value "pending" was assigned in error.
  377. # This error would block the queue for ever.
  378. Log ^X$tk <<<<< Error in queueing of requests >>>>> - token $token
  379. } elseif {
  380. $state(-pipeline)
  381. && [info exists socketWrState($connId)]
  382. && ($socketWrState($connId) eq "Wready")
  383. && [info exists socketWrQueue($connId)]
  384. && [llength $socketWrQueue($connId)]
  385. && (![set token3 [lindex $socketWrQueue($connId) 0]
  386. set ${token3}(-pipeline)
  387. ]
  388. )
  389. } {
  390. # This case:
  391. # - The next token in the write queue is nonpipeline, and
  392. # socketWrState is Wready. Get the next event from socketWrQueue.
  393. # Tests:
  394. # - In this case the read state (tested above) is Rready and the
  395. # write state (tested here) is Wready - there is no "pending"
  396. # request.
  397. # Code:
  398. # - The code is the same as the code below for the nonpipelined
  399. # case with a queued request.
  400. variable $token3
  401. set conn [set ${token3}(tmpConnArgs)]
  402. #Log nonpipeline, GRANT r/w access to $token3 in KeepSocket
  403. set socketRdState($connId) $token3
  404. set socketWrState($connId) $token3
  405. set socketWrQueue($connId) [lrange $socketWrQueue($connId) 1 end]
  406. # Connect does its own fconfigure.
  407. fileevent $state(sock) writable [list http::Connect $token3 {*}$conn]
  408. #Log ---- $state(sock) << conn to $token3 for HTTP request (c)
  409. } elseif {
  410. (!$state(-pipeline))
  411. && [info exists socketWrQueue($connId)]
  412. && [llength $socketWrQueue($connId)]
  413. && ($state(connection) ne "close")
  414. } {
  415. # If not pipelined, (socketRdState eq Rready) tells us that we are
  416. # ready for the next write - there is no need to check
  417. # socketWrState. Write the next request, if one is waiting.
  418. # If the next request is pipelined, it receives premature read
  419. # access to the socket. This is not a problem.
  420. set token3 [lindex $socketWrQueue($connId) 0]
  421. variable $token3
  422. set conn [set ${token3}(tmpConnArgs)]
  423. #Log nonpipeline, GRANT r/w access to $token3 in KeepSocket
  424. set socketRdState($connId) $token3
  425. set socketWrState($connId) $token3
  426. set socketWrQueue($connId) [lrange $socketWrQueue($connId) 1 end]
  427. # Connect does its own fconfigure.
  428. fileevent $state(sock) writable [list http::Connect $token3 {*}$conn]
  429. #Log ---- $state(sock) << conn to $token3 for HTTP request (d)
  430. } elseif {(!$state(-pipeline))} {
  431. set socketWrState($connId) Wready
  432. # Rready and Wready and idle: nothing to do.
  433. }
  434. } else {
  435. CloseSocket $state(sock) $token
  436. # There is no socketMapping($state(socketinfo)), so it does not matter
  437. # that CloseQueuedQueries is not called.
  438. }
  439. }
  440. # http::CheckEof -
  441. #
  442. # Read from a socket and close it if eof.
  443. # The command is bound to "fileevent readable" on an idle socket, and
  444. # "eof" is the only event that should trigger the binding, occurring when
  445. # the server times out and half-closes the socket.
  446. #
  447. # A read is necessary so that [eof] gives a meaningful result.
  448. # Any bytes sent are junk (or a bug).
  449. proc http::CheckEof {sock} {
  450. set junk [read $sock]
  451. set n [string length $junk]
  452. if {$n} {
  453. Log "WARNING: $n bytes received but no HTTP request sent"
  454. }
  455. if {[catch {eof $sock} res] || $res} {
  456. # The server has half-closed the socket.
  457. # If a new write has started, its transaction will fail and
  458. # will then be error-handled.
  459. CloseSocket $sock
  460. }
  461. }
  462. # http::CloseSocket -
  463. #
  464. # Close a socket and remove it from the persistent sockets table. If
  465. # possible an http token is included here but when we are called from a
  466. # fileevent on remote closure we need to find the correct entry - hence
  467. # the "else" block of the first "if" command.
  468. proc http::CloseSocket {s {token {}}} {
  469. variable socketMapping
  470. variable socketRdState
  471. variable socketWrState
  472. variable socketRdQueue
  473. variable socketWrQueue
  474. variable socketClosing
  475. variable socketPlayCmd
  476. set tk [namespace tail $token]
  477. catch {fileevent $s readable {}}
  478. set connId {}
  479. if {$token ne ""} {
  480. variable $token
  481. upvar 0 $token state
  482. if {[info exists state(socketinfo)]} {
  483. set connId $state(socketinfo)
  484. }
  485. } else {
  486. set map [array get socketMapping]
  487. set ndx [lsearch -exact $map $s]
  488. if {$ndx != -1} {
  489. incr ndx -1
  490. set connId [lindex $map $ndx]
  491. }
  492. }
  493. if { ($connId ne {})
  494. && [info exists socketMapping($connId)]
  495. && ($socketMapping($connId) eq $s)
  496. } {
  497. Log "Closing connection $connId (sock $socketMapping($connId))"
  498. if {[catch {close $socketMapping($connId)} err]} {
  499. Log "Error closing connection: $err"
  500. }
  501. if {$token eq {}} {
  502. # Cases with a non-empty token are handled by Finish, so the tokens
  503. # are finished in connection order.
  504. http::CloseQueuedQueries $connId
  505. }
  506. } else {
  507. Log "Closing socket $s (no connection info)"
  508. if {[catch {close $s} err]} {
  509. Log "Error closing socket: $err"
  510. }
  511. }
  512. }
  513. # http::CloseQueuedQueries
  514. #
  515. # connId - identifier "domain:port" for the connection
  516. # token - (optional) used only for logging
  517. #
  518. # Called from http::CloseSocket and http::Finish, after a connection is closed,
  519. # to clear the read and write queues if this has not already been done.
  520. proc http::CloseQueuedQueries {connId {token {}}} {
  521. variable socketMapping
  522. variable socketRdState
  523. variable socketWrState
  524. variable socketRdQueue
  525. variable socketWrQueue
  526. variable socketClosing
  527. variable socketPlayCmd
  528. if {![info exists socketMapping($connId)]} {
  529. # Command has already been called.
  530. # Don't come here again - especially recursively.
  531. return
  532. }
  533. # Used only for logging.
  534. if {$token eq {}} {
  535. set tk {}
  536. } else {
  537. set tk [namespace tail $token]
  538. }
  539. if { [info exists socketPlayCmd($connId)]
  540. && ($socketPlayCmd($connId) ne {ReplayIfClose Wready {} {}})
  541. } {
  542. # Before unsetting, there is some unfinished business.
  543. # - If the server sent "Connection: close", we have stored the command
  544. # for retrying any queued requests in socketPlayCmd, so copy that
  545. # value for execution below. socketClosing(*) was also set.
  546. # - Also clear the queues to prevent calls to Finish that would set the
  547. # state for the requests that will be retried to "finished with error
  548. # status".
  549. set unfinished $socketPlayCmd($connId)
  550. set socketRdQueue($connId) {}
  551. set socketWrQueue($connId) {}
  552. } else {
  553. set unfinished {}
  554. }
  555. Unset $connId
  556. if {$unfinished ne {}} {
  557. Log ^R$tk Any unfinished transactions (excluding $token) failed \
  558. - token $token
  559. {*}$unfinished
  560. }
  561. }
  562. # http::Unset
  563. #
  564. # The trace on "unset socketRdState(*)" will call CancelReadPipeline
  565. # and cancel any queued responses.
  566. # The trace on "unset socketWrState(*)" will call CancelWritePipeline
  567. # and cancel any queued requests.
  568. proc http::Unset {connId} {
  569. variable socketMapping
  570. variable socketRdState
  571. variable socketWrState
  572. variable socketRdQueue
  573. variable socketWrQueue
  574. variable socketClosing
  575. variable socketPlayCmd
  576. unset socketMapping($connId)
  577. unset socketRdState($connId)
  578. unset socketWrState($connId)
  579. unset -nocomplain socketRdQueue($connId)
  580. unset -nocomplain socketWrQueue($connId)
  581. unset -nocomplain socketClosing($connId)
  582. unset -nocomplain socketPlayCmd($connId)
  583. }
  584. # http::reset --
  585. #
  586. # See documentation for details.
  587. #
  588. # Arguments:
  589. # token Connection token.
  590. # why Status info.
  591. #
  592. # Side Effects:
  593. # See Finish
  594. proc http::reset {token {why reset}} {
  595. variable $token
  596. upvar 0 $token state
  597. set state(status) $why
  598. catch {fileevent $state(sock) readable {}}
  599. catch {fileevent $state(sock) writable {}}
  600. Finish $token
  601. if {[info exists state(error)]} {
  602. set errorlist $state(error)
  603. unset state
  604. eval ::error $errorlist
  605. }
  606. }
  607. # http::geturl --
  608. #
  609. # Establishes a connection to a remote url via http.
  610. #
  611. # Arguments:
  612. # url The http URL to goget.
  613. # args Option value pairs. Valid options include:
  614. # -blocksize, -validate, -headers, -timeout
  615. # Results:
  616. # Returns a token for this connection. This token is the name of an
  617. # array that the caller should unset to garbage collect the state.
  618. proc http::geturl {url args} {
  619. variable http
  620. variable urlTypes
  621. variable defaultCharset
  622. variable defaultKeepalive
  623. variable strict
  624. # Initialize the state variable, an array. We'll return the name of this
  625. # array as the token for the transaction.
  626. if {![info exists http(uid)]} {
  627. set http(uid) 0
  628. }
  629. set token [namespace current]::[incr http(uid)]
  630. ##Log Starting http::geturl - token $token
  631. variable $token
  632. upvar 0 $token state
  633. set tk [namespace tail $token]
  634. reset $token
  635. Log ^A$tk URL $url - token $token
  636. # Process command options.
  637. array set state {
  638. -binary false
  639. -blocksize 8192
  640. -queryblocksize 8192
  641. -validate 0
  642. -headers {}
  643. -timeout 0
  644. -type application/x-www-form-urlencoded
  645. -queryprogress {}
  646. -protocol 1.1
  647. binary 0
  648. state created
  649. meta {}
  650. method {}
  651. coding {}
  652. currentsize 0
  653. totalsize 0
  654. querylength 0
  655. queryoffset 0
  656. type text/html
  657. body {}
  658. status ""
  659. http ""
  660. connection close
  661. }
  662. set state(-keepalive) $defaultKeepalive
  663. set state(-strict) $strict
  664. # These flags have their types verified [Bug 811170]
  665. array set type {
  666. -binary boolean
  667. -blocksize integer
  668. -queryblocksize integer
  669. -strict boolean
  670. -timeout integer
  671. -validate boolean
  672. }
  673. set state(charset) $defaultCharset
  674. set options {
  675. -binary -blocksize -channel -command -handler -headers -keepalive
  676. -method -myaddr -progress -protocol -query -queryblocksize
  677. -querychannel -queryprogress -strict -timeout -type -validate
  678. }
  679. set usage [join [lsort $options] ", "]
  680. set options [string map {- ""} $options]
  681. set pat ^-(?:[join $options |])$
  682. foreach {flag value} $args {
  683. if {[regexp -- $pat $flag]} {
  684. # Validate numbers
  685. if {
  686. [info exists type($flag)] &&
  687. ![string is $type($flag) -strict $value]
  688. } {
  689. unset $token
  690. return -code error \
  691. "Bad value for $flag ($value), must be $type($flag)"
  692. }
  693. set state($flag) $value
  694. } else {
  695. unset $token
  696. return -code error "Unknown option $flag, can be: $usage"
  697. }
  698. }
  699. # Make sure -query and -querychannel aren't both specified
  700. set isQueryChannel [info exists state(-querychannel)]
  701. set isQuery [info exists state(-query)]
  702. if {$isQuery && $isQueryChannel} {
  703. unset $token
  704. return -code error "Can't combine -query and -querychannel options!"
  705. }
  706. # Validate URL, determine the server host and port, and check proxy case
  707. # Recognize user:pass@host URLs also, although we do not do anything with
  708. # that info yet.
  709. # URLs have basically four parts.
  710. # First, before the colon, is the protocol scheme (e.g. http)
  711. # Second, for HTTP-like protocols, is the authority
  712. # The authority is preceded by // and lasts up to (but not including)
  713. # the following / or ? and it identifies up to four parts, of which
  714. # only one, the host, is required (if an authority is present at all).
  715. # All other parts of the authority (user name, password, port number)
  716. # are optional.
  717. # Third is the resource name, which is split into two parts at a ?
  718. # The first part (from the single "/" up to "?") is the path, and the
  719. # second part (from that "?" up to "#") is the query. *HOWEVER*, we do
  720. # not need to separate them; we send the whole lot to the server.
  721. # Both, path and query are allowed to be missing, including their
  722. # delimiting character.
  723. # Fourth is the fragment identifier, which is everything after the first
  724. # "#" in the URL. The fragment identifier MUST NOT be sent to the server
  725. # and indeed, we don't bother to validate it (it could be an error to
  726. # pass it in here, but it's cheap to strip).
  727. #
  728. # An example of a URL that has all the parts:
  729. #
  730. # http://jschmoe:xyzzy@www.bogus.net:8000/foo/bar.tml?q=foo#changes
  731. #
  732. # The "http" is the protocol, the user is "jschmoe", the password is
  733. # "xyzzy", the host is "www.bogus.net", the port is "8000", the path is
  734. # "/foo/bar.tml", the query is "q=foo", and the fragment is "changes".
  735. #
  736. # Note that the RE actually combines the user and password parts, as
  737. # recommended in RFC 3986. Indeed, that RFC states that putting passwords
  738. # in URLs is a Really Bad Idea, something with which I would agree utterly.
  739. #
  740. # From a validation perspective, we need to ensure that the parts of the
  741. # URL that are going to the server are correctly encoded. This is only
  742. # done if $state(-strict) is true (inherited from $::http::strict).
  743. set URLmatcher {(?x) # this is _expanded_ syntax
  744. ^
  745. (?: (\w+) : ) ? # <protocol scheme>
  746. (?: //
  747. (?:
  748. (
  749. [^@/\#?]+ # <userinfo part of authority>
  750. ) @
  751. )?
  752. ( # <host part of authority>
  753. [^/:\#?]+ | # host name or IPv4 address
  754. \[ [^/\#?]+ \] # IPv6 address in square brackets
  755. )
  756. (?: : (\d+) )? # <port part of authority>
  757. )?
  758. ( [/\?] [^\#]*)? # <path> (including query)
  759. (?: \# (.*) )? # <fragment>
  760. $
  761. }
  762. # Phase one: parse
  763. if {![regexp -- $URLmatcher $url -> proto user host port srvurl]} {
  764. unset $token
  765. return -code error "Unsupported URL: $url"
  766. }
  767. # Phase two: validate
  768. set host [string trim $host {[]}]; # strip square brackets from IPv6 address
  769. if {$host eq ""} {
  770. # Caller has to provide a host name; we do not have a "default host"
  771. # that would enable us to handle relative URLs.
  772. unset $token
  773. return -code error "Missing host part: $url"
  774. # Note that we don't check the hostname for validity here; if it's
  775. # invalid, we'll simply fail to resolve it later on.
  776. }
  777. if {$port ne "" && $port > 65535} {
  778. unset $token
  779. return -code error "Invalid port number: $port"
  780. }
  781. # The user identification and resource identification parts of the URL can
  782. # have encoded characters in them; take care!
  783. if {$user ne ""} {
  784. # Check for validity according to RFC 3986, Appendix A
  785. set validityRE {(?xi)
  786. ^
  787. (?: [-\w.~!$&'()*+,;=:] | %[0-9a-f][0-9a-f] )+
  788. $
  789. }
  790. if {$state(-strict) && ![regexp -- $validityRE $user]} {
  791. unset $token
  792. # Provide a better error message in this error case
  793. if {[regexp {(?i)%(?![0-9a-f][0-9a-f]).?.?} $user bad]} {
  794. return -code error \
  795. "Illegal encoding character usage \"$bad\" in URL user"
  796. }
  797. return -code error "Illegal characters in URL user"
  798. }
  799. }
  800. if {$srvurl ne ""} {
  801. # RFC 3986 allows empty paths (not even a /), but servers
  802. # return 400 if the path in the HTTP request doesn't start
  803. # with / , so add it here if needed.
  804. if {[string index $srvurl 0] ne "/"} {
  805. set srvurl /$srvurl
  806. }
  807. # Check for validity according to RFC 3986, Appendix A
  808. set validityRE {(?xi)
  809. ^
  810. # Path part (already must start with / character)
  811. (?: [-\w.~!$&'()*+,;=:@/] | %[0-9a-f][0-9a-f] )*
  812. # Query part (optional, permits ? characters)
  813. (?: \? (?: [-\w.~!$&'()*+,;=:@/?] | %[0-9a-f][0-9a-f] )* )?
  814. $
  815. }
  816. if {$state(-strict) && ![regexp -- $validityRE $srvurl]} {
  817. unset $token
  818. # Provide a better error message in this error case
  819. if {[regexp {(?i)%(?![0-9a-f][0-9a-f])..} $srvurl bad]} {
  820. return -code error \
  821. "Illegal encoding character usage \"$bad\" in URL path"
  822. }
  823. return -code error "Illegal characters in URL path"
  824. }
  825. } else {
  826. set srvurl /
  827. }
  828. if {$proto eq ""} {
  829. set proto http
  830. }
  831. set lower [string tolower $proto]
  832. if {![info exists urlTypes($lower)]} {
  833. unset $token
  834. return -code error "Unsupported URL type \"$proto\""
  835. }
  836. set defport [lindex $urlTypes($lower) 0]
  837. set defcmd [lindex $urlTypes($lower) 1]
  838. if {$port eq ""} {
  839. set port $defport
  840. }
  841. if {![catch {$http(-proxyfilter) $host} proxy]} {
  842. set phost [lindex $proxy 0]
  843. set pport [lindex $proxy 1]
  844. }
  845. # OK, now reassemble into a full URL
  846. set url ${proto}://
  847. if {$user ne ""} {
  848. append url $user
  849. append url @
  850. }
  851. append url $host
  852. if {$port != $defport} {
  853. append url : $port
  854. }
  855. append url $srvurl
  856. # Don't append the fragment!
  857. set state(url) $url
  858. set sockopts [list -async]
  859. # If we are using the proxy, we must pass in the full URL that includes
  860. # the server name.
  861. if {[info exists phost] && ($phost ne "")} {
  862. set srvurl $url
  863. set targetAddr [list $phost $pport]
  864. } else {
  865. set targetAddr [list $host $port]
  866. }
  867. # Proxy connections aren't shared among different hosts.
  868. set state(socketinfo) $host:$port
  869. # Save the accept types at this point to prevent a race condition. [Bug
  870. # c11a51c482]
  871. set state(accept-types) $http(-accept)
  872. if {$isQuery || $isQueryChannel} {
  873. # It's a POST.
  874. # A client wishing to send a non-idempotent request SHOULD wait to send
  875. # that request until it has received the response status for the
  876. # previous request.
  877. if {$http(-postfresh)} {
  878. # Override -keepalive for a POST. Use a new connection, and thus
  879. # avoid the small risk of a race against server timeout.
  880. set state(-keepalive) 0
  881. } else {
  882. # Allow -keepalive but do not -pipeline - wait for the previous
  883. # transaction to finish.
  884. # There is a small risk of a race against server timeout.
  885. set state(-pipeline) 0
  886. }
  887. } else {
  888. # It's a GET or HEAD.
  889. set state(-pipeline) $http(-pipeline)
  890. }
  891. # See if we are supposed to use a previously opened channel.
  892. # - In principle, ANY call to http::geturl could use a previously opened
  893. # channel if it is available - the "Connection: keep-alive" header is a
  894. # request to leave the channel open AFTER completion of this call.
  895. # - In fact, we try to use an existing channel only if -keepalive 1 -- this
  896. # means that at most one channel is left open for each value of
  897. # $state(socketinfo). This property simplifies the mapping of open
  898. # channels.
  899. set reusing 0
  900. set alreadyQueued 0
  901. if {$state(-keepalive)} {
  902. variable socketMapping
  903. variable socketRdState
  904. variable socketWrState
  905. variable socketRdQueue
  906. variable socketWrQueue
  907. variable socketClosing
  908. variable socketPlayCmd
  909. if {[info exists socketMapping($state(socketinfo))]} {
  910. # - If the connection is idle, it has a "fileevent readable" binding
  911. # to http::CheckEof, in case the server times out and half-closes
  912. # the socket (http::CheckEof closes the other half).
  913. # - We leave this binding in place until just before the last
  914. # puts+flush in http::Connected (GET/HEAD) or http::Write (POST),
  915. # after which the HTTP response might be generated.
  916. if { [info exists socketClosing($state(socketinfo))]
  917. && $socketClosing($state(socketinfo))
  918. } {
  919. # socketClosing(*) is set because the server has sent a
  920. # "Connection: close" header.
  921. # Do not use the persistent socket again.
  922. # Since we have only one persistent socket per server, and the
  923. # old socket is not yet dead, add the request to the write queue
  924. # of the dying socket, which will be replayed by ReplayIfClose.
  925. # Also add it to socketWrQueue(*) which is used only if an error
  926. # causes a call to Finish.
  927. set reusing 1
  928. set sock $socketMapping($state(socketinfo))
  929. Log "reusing socket $sock for $state(socketinfo) - token $token"
  930. set alreadyQueued 1
  931. lassign $socketPlayCmd($state(socketinfo)) com0 com1 com2 com3
  932. lappend com3 $token
  933. set socketPlayCmd($state(socketinfo)) [list $com0 $com1 $com2 $com3]
  934. lappend socketWrQueue($state(socketinfo)) $token
  935. } elseif {[catch {fconfigure $socketMapping($state(socketinfo))}]} {
  936. # FIXME Is it still possible for this code to be executed? If
  937. # so, this could be another place to call TestForReplay,
  938. # rather than discarding the queued transactions.
  939. Log "WARNING: socket for $state(socketinfo) was closed\
  940. - token $token"
  941. Log "WARNING - if testing, pay special attention to this\
  942. case (GH) which is seldom executed - token $token"
  943. # This will call CancelReadPipeline, CancelWritePipeline, and
  944. # cancel any queued requests, responses.
  945. Unset $state(socketinfo)
  946. } else {
  947. # Use the persistent socket.
  948. # The socket may not be ready to write: an earlier request might
  949. # still be still writing (in the pipelined case) or
  950. # writing/reading (in the nonpipeline case). This possibility
  951. # is handled by socketWrQueue later in this command.
  952. set reusing 1
  953. set sock $socketMapping($state(socketinfo))
  954. Log "reusing socket $sock for $state(socketinfo) - token $token"
  955. }
  956. # Do not automatically close the connection socket.
  957. set state(connection) {}
  958. }
  959. }
  960. if {$reusing} {
  961. # Define state(tmpState) and state(tmpOpenCmd) for use
  962. # by http::ReplayIfDead if the persistent connection has died.
  963. set state(tmpState) [array get state]
  964. # Pass -myaddr directly to the socket command
  965. if {[info exists state(-myaddr)]} {
  966. lappend sockopts -myaddr $state(-myaddr)
  967. }
  968. set state(tmpOpenCmd) [list {*}$defcmd {*}$sockopts {*}$targetAddr]
  969. }
  970. set state(reusing) $reusing
  971. # Excluding ReplayIfDead and the decision whether to call it, there are four
  972. # places outside http::geturl where state(reusing) is used:
  973. # - Connected - if reusing and not pipelined, start the state(-timeout)
  974. # timeout (when writing).
  975. # - DoneRequest - if reusing and pipelined, send the next pipelined write
  976. # - Event - if reusing and pipelined, start the state(-timeout)
  977. # timeout (when reading).
  978. # - Event - if (not reusing) and pipelined, send the next pipelined
  979. # write
  980. # See comments above re the start of this timeout in other cases.
  981. if {(!$state(reusing)) && ($state(-timeout) > 0)} {
  982. set state(after) [after $state(-timeout) \
  983. [list http::reset $token timeout]]
  984. }
  985. if {![info exists sock]} {
  986. # Pass -myaddr directly to the socket command
  987. if {[info exists state(-myaddr)]} {
  988. lappend sockopts -myaddr $state(-myaddr)
  989. }
  990. set pre [clock milliseconds]
  991. ##Log pre socket opened, - token $token
  992. ##Log [concat $defcmd $sockopts $targetAddr] - token $token
  993. if {[catch {eval $defcmd $sockopts $targetAddr} sock errdict]} {
  994. # Something went wrong while trying to establish the connection.
  995. # Clean up after events and such, but DON'T call the command
  996. # callback (if available) because we're going to throw an
  997. # exception from here instead.
  998. set state(sock) NONE
  999. Finish $token $sock 1
  1000. cleanup $token
  1001. dict unset errdict -level
  1002. return -options $errdict $sock
  1003. } else {
  1004. # Initialisation of a new socket.
  1005. ##Log post socket opened, - token $token
  1006. ##Log socket opened, now fconfigure - token $token
  1007. set delay [expr {[clock milliseconds] - $pre}]
  1008. if {$delay > 3000} {
  1009. Log socket delay $delay - token $token
  1010. }
  1011. fconfigure $sock -translation {auto crlf} \
  1012. -buffersize $state(-blocksize)
  1013. ##Log socket opened, DONE fconfigure - token $token
  1014. }
  1015. }
  1016. # Command [socket] is called with -async, but takes 5s to 5.1s to return,
  1017. # with probability of order 1 in 10,000. This may be a bizarre scheduling
  1018. # issue with my (KJN's) system (Fedora Linux).
  1019. # This does not cause a problem (unless the request times out when this
  1020. # command returns).
  1021. set state(sock) $sock
  1022. Log "Using $sock for $state(socketinfo) - token $token" \
  1023. [expr {$state(-keepalive)?"keepalive":""}]
  1024. if { $state(-keepalive)
  1025. && (![info exists socketMapping($state(socketinfo))])
  1026. } {
  1027. # Freshly-opened socket that we would like to become persistent.
  1028. set socketMapping($state(socketinfo)) $sock
  1029. if {![info exists socketRdState($state(socketinfo))]} {
  1030. set socketRdState($state(socketinfo)) {}
  1031. set varName ::http::socketRdState($state(socketinfo))
  1032. trace add variable $varName unset ::http::CancelReadPipeline
  1033. }
  1034. if {![info exists socketWrState($state(socketinfo))]} {
  1035. set socketWrState($state(socketinfo)) {}
  1036. set varName ::http::socketWrState($state(socketinfo))
  1037. trace add variable $varName unset ::http::CancelWritePipeline
  1038. }
  1039. if {$state(-pipeline)} {
  1040. #Log new, init for pipelined, GRANT write access to $token in geturl
  1041. # Also grant premature read access to the socket. This is OK.
  1042. set socketRdState($state(socketinfo)) $token
  1043. set socketWrState($state(socketinfo)) $token
  1044. } else {
  1045. # socketWrState is not used by this non-pipelined transaction.
  1046. # We cannot leave it as "Wready" because the next call to
  1047. # http::geturl with a pipelined transaction would conclude that the
  1048. # socket is available for writing.
  1049. #Log new, init for nonpipeline, GRANT r/w access to $token in geturl
  1050. set socketRdState($state(socketinfo)) $token
  1051. set socketWrState($state(socketinfo)) $token
  1052. }
  1053. set socketRdQueue($state(socketinfo)) {}
  1054. set socketWrQueue($state(socketinfo)) {}
  1055. set socketClosing($state(socketinfo)) 0
  1056. set socketPlayCmd($state(socketinfo)) {ReplayIfClose Wready {} {}}
  1057. }
  1058. if {![info exists phost]} {
  1059. set phost ""
  1060. }
  1061. if {$reusing} {
  1062. # For use by http::ReplayIfDead if the persistent connection has died.
  1063. # Also used by NextPipelinedWrite.
  1064. set state(tmpConnArgs) [list $proto $phost $srvurl]
  1065. }
  1066. # The element socketWrState($connId) has a value which is either the name of
  1067. # the token that is permitted to write to the socket, or "Wready" if no
  1068. # token is permitted to write.
  1069. #
  1070. # The code that sets the value to Wready immediately calls
  1071. # http::NextPipelinedWrite, which examines socketWrQueue($connId) and
  1072. # processes the next request in the queue, if there is one. The value
  1073. # Wready is not found when the interpreter is in the event loop unless the
  1074. # socket is idle.
  1075. #
  1076. # The element socketRdState($connId) has a value which is either the name of
  1077. # the token that is permitted to read from the socket, or "Rready" if no
  1078. # token is permitted to read.
  1079. #
  1080. # The code that sets the value to Rready then examines
  1081. # socketRdQueue($connId) and processes the next request in the queue, if
  1082. # there is one. The value Rready is not found when the interpreter is in
  1083. # the event loop unless the socket is idle.
  1084. if {$alreadyQueued} {
  1085. # A write may or may not be in progress. There is no need to set
  1086. # socketWrState to prevent another call stealing write access - all
  1087. # subsequent calls on this socket will come here because the socket
  1088. # will close after the current read, and its
  1089. # socketClosing($connId) is 1.
  1090. ##Log "HTTP request for token $token is queued"
  1091. } elseif { $reusing
  1092. && $state(-pipeline)
  1093. && ($socketWrState($state(socketinfo)) ne "Wready")
  1094. } {
  1095. ##Log "HTTP request for token $token is queued for pipelined use"
  1096. lappend socketWrQueue($state(socketinfo)) $token
  1097. } elseif { $reusing
  1098. && (!$state(-pipeline))
  1099. && ($socketWrState($state(socketinfo)) ne "Wready")
  1100. } {
  1101. # A write is queued or in progress. Lappend to the write queue.
  1102. ##Log "HTTP request for token $token is queued for nonpipeline use"
  1103. lappend socketWrQueue($state(socketinfo)) $token
  1104. } elseif { $reusing
  1105. && (!$state(-pipeline))
  1106. && ($socketWrState($state(socketinfo)) eq "Wready")
  1107. && ($socketRdState($state(socketinfo)) ne "Rready")
  1108. } {
  1109. # A read is queued or in progress, but not a write. Cannot start the
  1110. # nonpipeline transaction, but must set socketWrState to prevent a
  1111. # pipelined request jumping the queue.
  1112. ##Log "HTTP request for token $token is queued for nonpipeline use"
  1113. #Log re-use nonpipeline, GRANT delayed write access to $token in geturl
  1114. set socketWrState($state(socketinfo)) peNding
  1115. lappend socketWrQueue($state(socketinfo)) $token
  1116. } else {
  1117. if {$reusing && $state(-pipeline)} {
  1118. #Log re-use pipelined, GRANT write access to $token in geturl
  1119. set socketWrState($state(socketinfo)) $token
  1120. } elseif {$reusing} {
  1121. # Cf tests above - both are ready.
  1122. #Log re-use nonpipeline, GRANT r/w access to $token in geturl
  1123. set socketRdState($state(socketinfo)) $token
  1124. set socketWrState($state(socketinfo)) $token
  1125. }
  1126. # All (!$reusing) cases come here, and also some $reusing cases if the
  1127. # connection is ready.
  1128. #Log ---- $state(socketinfo) << conn to $token for HTTP request (a)
  1129. # Connect does its own fconfigure.
  1130. fileevent $sock writable \
  1131. [list http::Connect $token $proto $phost $srvurl]
  1132. }
  1133. # Wait for the connection to complete.
  1134. if {![info exists state(-command)]} {
  1135. # geturl does EVERYTHING asynchronously, so if the user
  1136. # calls it synchronously, we just do a wait here.
  1137. http::wait $token
  1138. if {![info exists state]} {
  1139. # If we timed out then Finish has been called and the users
  1140. # command callback may have cleaned up the token. If so we end up
  1141. # here with nothing left to do.
  1142. return $token
  1143. } elseif {$state(status) eq "error"} {
  1144. # Something went wrong while trying to establish the connection.
  1145. # Clean up after events and such, but DON'T call the command
  1146. # callback (if available) because we're going to throw an
  1147. # exception from here instead.
  1148. set err [lindex $state(error) 0]
  1149. cleanup $token
  1150. return -code error $err
  1151. }
  1152. }
  1153. ##Log Leaving http::geturl - token $token
  1154. return $token
  1155. }
  1156. # http::Connected --
  1157. #
  1158. # Callback used when the connection to the HTTP server is actually
  1159. # established.
  1160. #
  1161. # Arguments:
  1162. # token State token.
  1163. # proto What protocol (http, https, etc.) was used to connect.
  1164. # phost Are we using keep-alive? Non-empty if yes.
  1165. # srvurl Service-local URL that we're requesting
  1166. # Results:
  1167. # None.
  1168. proc http::Connected {token proto phost srvurl} {
  1169. variable http
  1170. variable urlTypes
  1171. variable socketMapping
  1172. variable socketRdState
  1173. variable socketWrState
  1174. variable socketRdQueue
  1175. variable socketWrQueue
  1176. variable socketClosing
  1177. variable socketPlayCmd
  1178. variable $token
  1179. upvar 0 $token state
  1180. set tk [namespace tail $token]
  1181. if {$state(reusing) && (!$state(-pipeline)) && ($state(-timeout) > 0)} {
  1182. set state(after) [after $state(-timeout) \
  1183. [list http::reset $token timeout]]
  1184. }
  1185. # Set back the variables needed here.
  1186. set sock $state(sock)
  1187. set isQueryChannel [info exists state(-querychannel)]
  1188. set isQuery [info exists state(-query)]
  1189. set host [lindex [split $state(socketinfo) :] 0]
  1190. set port [lindex [split $state(socketinfo) :] 1]
  1191. set lower [string tolower $proto]
  1192. set defport [lindex $urlTypes($lower) 0]
  1193. # Send data in cr-lf format, but accept any line terminators.
  1194. # Initialisation to {auto *} now done in geturl, KeepSocket and DoneRequest.
  1195. # We are concerned here with the request (write) not the response (read).
  1196. lassign [fconfigure $sock -translation] trRead trWrite
  1197. fconfigure $sock -translation [list $trRead crlf] \
  1198. -buffersize $state(-blocksize)
  1199. # The following is disallowed in safe interpreters, but the socket is
  1200. # already in non-blocking mode in that case.
  1201. catch {fconfigure $sock -blocking off}
  1202. set how GET
  1203. if {$isQuery} {
  1204. set state(querylength) [string length $state(-query)]
  1205. if {$state(querylength) > 0} {
  1206. set how POST
  1207. set contDone 0
  1208. } else {
  1209. # There's no query data.
  1210. unset state(-query)
  1211. set isQuery 0
  1212. }
  1213. } elseif {$state(-validate)} {
  1214. set how HEAD
  1215. } elseif {$isQueryChannel} {
  1216. set how POST
  1217. # The query channel must be blocking for the async Write to
  1218. # work properly.
  1219. lassign [fconfigure $sock -translation] trRead trWrite
  1220. fconfigure $state(-querychannel) -blocking 1 \
  1221. -translation [list $trRead binary]
  1222. set contDone 0
  1223. }
  1224. if {[info exists state(-method)] && ($state(-method) ne "")} {
  1225. set how $state(-method)
  1226. }
  1227. # We cannot handle chunked encodings with -handler, so force HTTP/1.0
  1228. # until we can manage this.
  1229. if {[info exists state(-handler)]} {
  1230. set state(-protocol) 1.0
  1231. }
  1232. set accept_types_seen 0
  1233. Log ^B$tk begin sending request - token $token
  1234. if {[catch {
  1235. set state(method) $how
  1236. puts $sock "$how $srvurl HTTP/$state(-protocol)"
  1237. if {[dict exists $state(-headers) Host]} {
  1238. # Allow Host spoofing. [Bug 928154]
  1239. puts $sock "Host: [dict get $state(-headers) Host]"
  1240. } elseif {$port == $defport} {
  1241. # Don't add port in this case, to handle broken servers. [Bug
  1242. # #504508]
  1243. puts $sock "Host: $host"
  1244. } else {
  1245. puts $sock "Host: $host:$port"
  1246. }
  1247. puts $sock "User-Agent: $http(-useragent)"
  1248. if {($state(-protocol) >= 1.0) && $state(-keepalive)} {
  1249. # Send this header, because a 1.1 server is not compelled to treat
  1250. # this as the default.
  1251. puts $sock "Connection: keep-alive"
  1252. }
  1253. if {($state(-protocol) > 1.0) && !$state(-keepalive)} {
  1254. puts $sock "Connection: close" ;# RFC2616 sec 8.1.2.1
  1255. }
  1256. if {[info exists phost] && ($phost ne "") && $state(-keepalive)} {
  1257. puts $sock "Proxy-Connection: Keep-Alive"
  1258. }
  1259. set accept_encoding_seen 0
  1260. set content_type_seen 0
  1261. dict for {key value} $state(-headers) {
  1262. set value [string map [list \n "" \r ""] $value]
  1263. set key [string map {" " -} [string trim $key]]
  1264. if {[string equal -nocase $key "host"]} {
  1265. continue
  1266. }
  1267. if {[string equal -nocase $key "accept-encoding"]} {
  1268. set accept_encoding_seen 1
  1269. }
  1270. if {[string equal -nocase $key "accept"]} {
  1271. set accept_types_seen 1
  1272. }
  1273. if {[string equal -nocase $key "content-type"]} {
  1274. set content_type_seen 1
  1275. }
  1276. if {[string equal -nocase $key "content-length"]} {
  1277. set contDone 1
  1278. set state(querylength) $value
  1279. }
  1280. if {[string length $key]} {
  1281. puts $sock "$key: $value"
  1282. }
  1283. }
  1284. # Allow overriding the Accept header on a per-connection basis. Useful
  1285. # for working with REST services. [Bug c11a51c482]
  1286. if {!$accept_types_seen} {
  1287. puts $sock "Accept: $state(accept-types)"
  1288. }
  1289. if { (!$accept_encoding_seen)
  1290. && (![info exists state(-handler)])
  1291. && $http(-zip)
  1292. } {
  1293. puts $sock "Accept-Encoding: gzip,deflate,compress"
  1294. }
  1295. if {$isQueryChannel && ($state(querylength) == 0)} {
  1296. # Try to determine size of data in channel. If we cannot seek, the
  1297. # surrounding catch will trap us
  1298. set start [tell $state(-querychannel)]
  1299. seek $state(-querychannel) 0 end
  1300. set state(querylength) \
  1301. [expr {[tell $state(-querychannel)] - $start}]
  1302. seek $state(-querychannel) $start
  1303. }
  1304. # Flush the request header and set up the fileevent that will either
  1305. # push the POST data or read the response.
  1306. #
  1307. # fileevent note:
  1308. #
  1309. # It is possible to have both the read and write fileevents active at
  1310. # this point. The only scenario it seems to affect is a server that
  1311. # closes the connection without reading the POST data. (e.g., early
  1312. # versions TclHttpd in various error cases). Depending on the
  1313. # platform, the client may or may not be able to get the response from
  1314. # the server because of the error it will get trying to write the post
  1315. # data. Having both fileevents active changes the timing and the
  1316. # behavior, but no two platforms (among Solaris, Linux, and NT) behave
  1317. # the same, and none behave all that well in any case. Servers should
  1318. # always read their POST data if they expect the client to read their
  1319. # response.
  1320. if {$isQuery || $isQueryChannel} {
  1321. # POST method.
  1322. if {!$content_type_seen} {
  1323. puts $sock "Content-Type: $state(-type)"
  1324. }
  1325. if {!$contDone} {
  1326. puts $sock "Content-Length: $state(querylength)"
  1327. }
  1328. puts $sock ""
  1329. flush $sock
  1330. # Flush flushes the error in the https case with a bad handshake:
  1331. # else the socket never becomes writable again, and hangs until
  1332. # timeout (if any).
  1333. lassign [fconfigure $sock -translation] trRead trWrite
  1334. fconfigure $sock -translation [list $trRead binary]
  1335. fileevent $sock writable [list http::Write $token]
  1336. # The http::Write command decides when to make the socket readable,
  1337. # using the same test as the GET/HEAD case below.
  1338. } else {
  1339. # GET or HEAD method.
  1340. if { (![catch {fileevent $sock readable} binding])
  1341. && ($binding eq [list http::CheckEof $sock])
  1342. } {
  1343. # Remove the "fileevent readable" binding of an idle persistent
  1344. # socket to http::CheckEof. We can no longer treat bytes
  1345. # received as junk. The server might still time out and
  1346. # half-close the socket if it has not yet received the first
  1347. # "puts".
  1348. fileevent $sock readable {}
  1349. }
  1350. puts $sock ""
  1351. flush $sock
  1352. Log ^C$tk end sending request - token $token
  1353. # End of writing (GET/HEAD methods). The request has been sent.
  1354. DoneRequest $token
  1355. }
  1356. } err]} {
  1357. # The socket probably was never connected, OR the connection dropped
  1358. # later, OR https handshake error, which may be discovered as late as
  1359. # the "flush" command above...
  1360. Log "WARNING - if testing, pay special attention to this\
  1361. case (GI) which is seldom executed - token $token"
  1362. if {[info exists state(reusing)] && $state(reusing)} {
  1363. # The socket was closed at the server end, and closed at
  1364. # this end by http::CheckEof.
  1365. if {[TestForReplay $token write $err a]} {
  1366. return
  1367. } else {
  1368. Finish $token {failed to re-use socket}
  1369. }
  1370. # else:
  1371. # This is NOT a persistent socket that has been closed since its
  1372. # last use.
  1373. # If any other requests are in flight or pipelined/queued, they will
  1374. # be discarded.
  1375. } elseif {$state(status) eq ""} {
  1376. # ...https handshake errors come here.
  1377. set msg [registerError $sock]
  1378. registerError $sock {}
  1379. if {$msg eq {}} {
  1380. set msg {failed to use socket}
  1381. }
  1382. Finish $token $msg
  1383. } elseif {$state(status) ne "error"} {
  1384. Finish $token $err
  1385. }
  1386. }
  1387. }
  1388. # http::registerError
  1389. #
  1390. # Called (for example when processing TclTLS activity) to register
  1391. # an error for a connection on a specific socket. This helps
  1392. # http::Connected to deliver meaningful error messages, e.g. when a TLS
  1393. # certificate fails verification.
  1394. #
  1395. # Usage: http::registerError socket ?newValue?
  1396. #
  1397. # "set" semantics, except that a "get" (a call without a new value) for a
  1398. # non-existent socket returns {}, not an error.
  1399. proc http::registerError {sock args} {
  1400. variable registeredErrors
  1401. if { ([llength $args] == 0)
  1402. && (![info exists registeredErrors($sock)])
  1403. } {
  1404. return
  1405. } elseif { ([llength $args] == 1)
  1406. && ([lindex $args 0] eq {})
  1407. } {
  1408. unset -nocomplain registeredErrors($sock)
  1409. return
  1410. }
  1411. set registeredErrors($sock) {*}$args
  1412. }
  1413. # http::DoneRequest --
  1414. #
  1415. # Command called when a request has been sent. It will arrange the
  1416. # next request and/or response as appropriate.
  1417. #
  1418. # If this command is called when $socketClosing(*), the request $token
  1419. # that calls it must be pipelined and destined to fail.
  1420. proc http::DoneRequest {token} {
  1421. variable http
  1422. variable socketMapping
  1423. variable socketRdState
  1424. variable socketWrState
  1425. variable socketRdQueue
  1426. variable socketWrQueue
  1427. variable socketClosing
  1428. variable socketPlayCmd
  1429. variable $token
  1430. upvar 0 $token state
  1431. set tk [namespace tail $token]
  1432. set sock $state(sock)
  1433. # If pipelined, connect the next HTTP request to the socket.
  1434. if {$state(reusing) && $state(-pipeline)} {
  1435. # Enable next token (if any) to write.
  1436. # The value "Wready" is set only here, and
  1437. # in http::Event after reading the response-headers of a
  1438. # non-reusing transaction.
  1439. # Previous value is $token. It cannot be pending.
  1440. set socketWrState($state(socketinfo)) Wready
  1441. # Now ready to write the next pipelined request (if any).
  1442. http::NextPipelinedWrite $token
  1443. } else {
  1444. # If pipelined, this is the first transaction on this socket. We wait
  1445. # for the response headers to discover whether the connection is
  1446. # persistent. (If this is not done and the connection is not
  1447. # persistent, we SHOULD retry and then MUST NOT pipeline before knowing
  1448. # that we have a persistent connection
  1449. # (rfc2616 8.1.2.2)).
  1450. }
  1451. # Connect to receive the response, unless the socket is pipelined
  1452. # and another response is being sent.
  1453. # This code block is separate from the code below because there are
  1454. # cases where socketRdState already has the value $token.
  1455. if { $state(-keepalive)
  1456. && $state(-pipeline)
  1457. && [info exists socketRdState($state(socketinfo))]
  1458. && ($socketRdState($state(socketinfo)) eq "Rready")
  1459. } {
  1460. #Log pipelined, GRANT read access to $token in Connected
  1461. set socketRdState($state(socketinfo)) $token
  1462. }
  1463. if { $state(-keepalive)
  1464. && $state(-pipeline)
  1465. && [info exists socketRdState($state(socketinfo))]
  1466. && ($socketRdState($state(socketinfo)) ne $token)
  1467. } {
  1468. # Do not read from the socket until it is ready.
  1469. ##Log "HTTP response for token $token is queued for pipelined use"
  1470. # If $socketClosing(*), then the caller will be a pipelined write and
  1471. # execution will come here.
  1472. # This token has already been recorded as "in flight" for writing.
  1473. # When the socket is closed, the read queue will be cleared in
  1474. # CloseQueuedQueries and so the "lappend" here has no effect.
  1475. lappend socketRdQueue($state(socketinfo)) $token
  1476. } else {
  1477. # In the pipelined case, connection for reading depends on the
  1478. # value of socketRdState.
  1479. # In the nonpipeline case, connection for reading always occurs.
  1480. ReceiveResponse $token
  1481. }
  1482. }
  1483. # http::ReceiveResponse
  1484. #
  1485. # Connects token to its socket for reading.
  1486. proc http::ReceiveResponse {token} {
  1487. variable $token
  1488. upvar 0 $token state
  1489. set tk [namespace tail $token]
  1490. set sock $state(sock)
  1491. #Log ---- $state(socketinfo) >> conn to $token for HTTP response
  1492. lassign [fconfigure $sock -translation] trRead trWrite
  1493. fconfigure $sock -translation [list auto $trWrite] \
  1494. -buffersize $state(-blocksize)
  1495. Log ^D$tk begin receiving response - token $token
  1496. coroutine ${token}EventCoroutine http::Event $sock $token
  1497. fileevent $sock readable ${token}EventCoroutine
  1498. }
  1499. # http::NextPipelinedWrite
  1500. #
  1501. # - Connecting a socket to a token for writing is done by this command and by
  1502. # command KeepSocket.
  1503. # - If another request has a pipelined write scheduled for $token's socket,
  1504. # and if the socket is ready to accept it, connect the write and update
  1505. # the queue accordingly.
  1506. # - This command is called from http::DoneRequest and http::Event,
  1507. # IF $state(-pipeline) AND (the current transfer has reached the point at
  1508. # which the socket is ready for the next request to be written).
  1509. # - This command is called when a token has write access and is pipelined and
  1510. # keep-alive, and sets socketWrState to Wready.
  1511. # - The command need not consider the case where socketWrState is set to a token
  1512. # that does not yet have write access. Such a token is waiting for Rready,
  1513. # and the assignment of the connection to the token will be done elsewhere (in
  1514. # http::KeepSocket).
  1515. # - This command cannot be called after socketWrState has been set to a
  1516. # "pending" token value (that is then overwritten by the caller), because that
  1517. # value is set by this command when it is called by an earlier token when it
  1518. # relinquishes its write access, and the pending token is always the next in
  1519. # line to write.
  1520. proc http::NextPipelinedWrite {token} {
  1521. variable http
  1522. variable socketRdState
  1523. variable socketWrState
  1524. variable socketWrQueue
  1525. variable socketClosing
  1526. variable $token
  1527. upvar 0 $token state
  1528. set connId $state(socketinfo)
  1529. if { [info exists socketClosing($connId)]
  1530. && $socketClosing($connId)
  1531. } {
  1532. # socketClosing(*) is set because the server has sent a
  1533. # "Connection: close" header.
  1534. # Behave as if the queues are empty - so do nothing.
  1535. } elseif { $state(-pipeline)
  1536. && [info exists socketWrState($connId)]
  1537. && ($socketWrState($connId) eq "Wready")
  1538. && [info exists socketWrQueue($connId)]
  1539. && [llength $socketWrQueue($connId)]
  1540. && ([set token2 [lindex $socketWrQueue($connId) 0]
  1541. set ${token2}(-pipeline)
  1542. ]
  1543. )
  1544. } {
  1545. # - The usual case for a pipelined connection, ready for a new request.
  1546. #Log pipelined, GRANT write access to $token2 in NextPipelinedWrite
  1547. set conn [set ${token2}(tmpConnArgs)]
  1548. set socketWrState($connId) $token2
  1549. set socketWrQueue($connId) [lrange $socketWrQueue($connId) 1 end]
  1550. # Connect does its own fconfigure.
  1551. fileevent $state(sock) writable [list http::Connect $token2 {*}$conn]
  1552. #Log ---- $connId << conn to $token2 for HTTP request (b)
  1553. # In the tests below, the next request will be nonpipeline.
  1554. } elseif { $state(-pipeline)
  1555. && [info exists socketWrState($connId)]
  1556. && ($socketWrState($connId) eq "Wready")
  1557. && [info exists socketWrQueue($connId)]
  1558. && [llength $socketWrQueue($connId)]
  1559. && (![ set token3 [lindex $socketWrQueue($connId) 0]
  1560. set ${token3}(-pipeline)
  1561. ]
  1562. )
  1563. && [info exists socketRdState($connId)]
  1564. && ($socketRdState($connId) eq "Rready")
  1565. } {
  1566. # The case in which the next request will be non-pipelined, and the read
  1567. # and write queues is ready: which is the condition for a non-pipelined
  1568. # write.
  1569. variable $token3
  1570. upvar 0 $token3 state3
  1571. set conn [set ${token3}(tmpConnArgs)]
  1572. #Log nonpipeline, GRANT r/w access to $token3 in NextPipelinedWrite
  1573. set socketRdState($connId) $token3
  1574. set socketWrState($connId) $token3
  1575. set socketWrQueue($connId) [lrange $socketWrQueue($connId) 1 end]
  1576. # Connect does its own fconfigure.
  1577. fileevent $state(sock) writable [list http::Connect $token3 {*}$conn]
  1578. #Log ---- $state(sock) << conn to $token3 for HTTP request (c)
  1579. } elseif { $state(-pipeline)
  1580. && [info exists socketWrState($connId)]
  1581. && ($socketWrState($connId) eq "Wready")
  1582. && [info exists socketWrQueue($connId)]
  1583. && [llength $socketWrQueue($connId)]
  1584. && (![set token2 [lindex $socketWrQueue($connId) 0]
  1585. set ${token2}(-pipeline)
  1586. ]
  1587. )
  1588. } {
  1589. # - The case in which the next request will be non-pipelined, but the
  1590. # read queue is NOT ready.
  1591. # - A read is queued or in progress, but not a write. Cannot start the
  1592. # nonpipeline transaction, but must set socketWrState to prevent a new
  1593. # pipelined request (in http::geturl) jumping the queue.
  1594. # - Because socketWrState($connId) is not set to Wready, the assignment
  1595. # of the connection to $token2 will be done elsewhere - by command
  1596. # http::KeepSocket when $socketRdState($connId) is set to "Rready".
  1597. #Log re-use nonpipeline, GRANT delayed write access to $token in NextP..
  1598. set socketWrState($connId) peNding
  1599. }
  1600. }
  1601. # http::CancelReadPipeline
  1602. #
  1603. # Cancel pipelined responses on a closing "Keep-Alive" socket.
  1604. #
  1605. # - Called by a variable trace on "unset socketRdState($connId)".
  1606. # - The variable relates to a Keep-Alive socket, which has been closed.
  1607. # - Cancels all pipelined responses. The requests have been sent,
  1608. # the responses have not yet been received.
  1609. # - This is a hard cancel that ends each transaction with error status,
  1610. # and closes the connection. Do not use it if you want to replay failed
  1611. # transactions.
  1612. # - N.B. Always delete ::http::socketRdState($connId) before deleting
  1613. # ::http::socketRdQueue($connId), or this command will do nothing.
  1614. #
  1615. # Arguments
  1616. # As for a trace command on a variable.
  1617. proc http::CancelReadPipeline {name1 connId op} {
  1618. variable socketRdQueue
  1619. ##Log CancelReadPipeline $name1 $connId $op
  1620. if {[info exists socketRdQueue($connId)]} {
  1621. set msg {the connection was closed by CancelReadPipeline}
  1622. foreach token $socketRdQueue($connId) {
  1623. set tk [namespace tail $token]
  1624. Log ^X$tk end of response "($msg)" - token $token
  1625. set ${token}(status) eof
  1626. Finish $token ;#$msg
  1627. }
  1628. set socketRdQueue($connId) {}
  1629. }
  1630. }
  1631. # http::CancelWritePipeline
  1632. #
  1633. # Cancel queued events on a closing "Keep-Alive" socket.
  1634. #
  1635. # - Called by a variable trace on "unset socketWrState($connId)".
  1636. # - The variable relates to a Keep-Alive socket, which has been closed.
  1637. # - In pipelined or nonpipeline case: cancels all queued requests. The
  1638. # requests have not yet been sent, the responses are not due.
  1639. # - This is a hard cancel that ends each transaction with error status,
  1640. # and closes the connection. Do not use it if you want to replay failed
  1641. # transactions.
  1642. # - N.B. Always delete ::http::socketWrState($connId) before deleting
  1643. # ::http::socketWrQueue($connId), or this command will do nothing.
  1644. #
  1645. # Arguments
  1646. # As for a trace command on a variable.
  1647. proc http::CancelWritePipeline {name1 connId op} {
  1648. variable socketWrQueue
  1649. ##Log CancelWritePipeline $name1 $connId $op
  1650. if {[info exists socketWrQueue($connId)]} {
  1651. set msg {the connection was closed by CancelWritePipeline}
  1652. foreach token $socketWrQueue($connId) {
  1653. set tk [namespace tail $token]
  1654. Log ^X$tk end of response "($msg)" - token $token
  1655. set ${token}(status) eof
  1656. Finish $token ;#$msg
  1657. }
  1658. set socketWrQueue($connId) {}
  1659. }
  1660. }
  1661. # http::ReplayIfDead --
  1662. #
  1663. # - A query on a re-used persistent socket failed at the earliest opportunity,
  1664. # because the socket had been closed by the server. Keep the token, tidy up,
  1665. # and try to connect on a fresh socket.
  1666. # - The connection is monitored for eof by the command http::CheckEof. Thus
  1667. # http::ReplayIfDead is needed only when a server event (half-closing an
  1668. # apparently idle connection), and a client event (sending a request) occur at
  1669. # almost the same time, and neither client nor server detects the other's
  1670. # action before performing its own (an "asynchronous close event").
  1671. # - To simplify testing of http::ReplayIfDead, set TEST_EOF 1 in
  1672. # http::KeepSocket, and then http::ReplayIfDead will be called if http::geturl
  1673. # is called at any time after the server timeout.
  1674. #
  1675. # Arguments:
  1676. # token Connection token.
  1677. #
  1678. # Side Effects:
  1679. # Use the same token, but try to open a new socket.
  1680. proc http::ReplayIfDead {tokenArg doing} {
  1681. variable socketMapping
  1682. variable socketRdState
  1683. variable socketWrState
  1684. variable socketRdQueue
  1685. variable socketWrQueue
  1686. variable socketClosing
  1687. variable socketPlayCmd
  1688. variable $tokenArg
  1689. upvar 0 $tokenArg stateArg
  1690. Log running http::ReplayIfDead for $tokenArg $doing
  1691. # 1. Merge the tokens for transactions in flight, the read (response) queue,
  1692. # and the write (request) queue.
  1693. set InFlightR {}
  1694. set InFlightW {}
  1695. # Obtain the tokens for transactions in flight.
  1696. if {$stateArg(-pipeline)} {
  1697. # Two transactions may be in flight. The "read" transaction was first.
  1698. # It is unlikely that the server would close the socket if a response
  1699. # was pending; however, an earlier request (as well as the present
  1700. # request) may have been sent and ignored if the socket was half-closed
  1701. # by the server.
  1702. if { [info exists socketRdState($stateArg(socketinfo))]
  1703. && ($socketRdState($stateArg(socketinfo)) ne "Rready")
  1704. } {
  1705. lappend InFlightR $socketRdState($stateArg(socketinfo))
  1706. } elseif {($doing eq "read")} {
  1707. lappend InFlightR $tokenArg
  1708. }
  1709. if { [info exists socketWrState($stateArg(socketinfo))]
  1710. && $socketWrState($stateArg(socketinfo)) ni {Wready peNding}
  1711. } {
  1712. lappend InFlightW $socketWrState($stateArg(socketinfo))
  1713. } elseif {($doing eq "write")} {
  1714. lappend InFlightW $tokenArg
  1715. }
  1716. # Report any inconsistency of $tokenArg with socket*state.
  1717. if { ($doing eq "read")
  1718. && [info exists socketRdState($stateArg(socketinfo))]
  1719. && ($tokenArg ne $socketRdState($stateArg(socketinfo)))
  1720. } {
  1721. Log WARNING - ReplayIfDead pipelined tokenArg $tokenArg $doing \
  1722. ne socketRdState($stateArg(socketinfo)) \
  1723. $socketRdState($stateArg(socketinfo))
  1724. } elseif {
  1725. ($doing eq "write")
  1726. && [info exists socketWrState($stateArg(socketinfo))]
  1727. && ($tokenArg ne $socketWrState($stateArg(socketinfo)))
  1728. } {
  1729. Log WARNING - ReplayIfDead pipelined tokenArg $tokenArg $doing \
  1730. ne socketWrState($stateArg(socketinfo)) \
  1731. $socketWrState($stateArg(socketinfo))
  1732. }
  1733. } else {
  1734. # One transaction should be in flight.
  1735. # socketRdState, socketWrQueue are used.
  1736. # socketRdQueue should be empty.
  1737. # Report any inconsistency of $tokenArg with socket*state.
  1738. if {$tokenArg ne $socketRdState($stateArg(socketinfo))} {
  1739. Log WARNING - ReplayIfDead nonpipeline tokenArg $tokenArg $doing \
  1740. ne socketRdState($stateArg(socketinfo)) \
  1741. $socketRdState($stateArg(socketinfo))
  1742. }
  1743. # Report the inconsistency that socketRdQueue is non-empty.
  1744. if { [info exists socketRdQueue($stateArg(socketinfo))]
  1745. && ($socketRdQueue($stateArg(socketinfo)) ne {})
  1746. } {
  1747. Log WARNING - ReplayIfDead nonpipeline tokenArg $tokenArg $doing \
  1748. has read queue socketRdQueue($stateArg(socketinfo)) \
  1749. $socketRdQueue($stateArg(socketinfo)) ne {}
  1750. }
  1751. lappend InFlightW $socketRdState($stateArg(socketinfo))
  1752. set socketRdQueue($stateArg(socketinfo)) {}
  1753. }
  1754. set newQueue {}
  1755. lappend newQueue {*}$InFlightR
  1756. lappend newQueue {*}$socketRdQueue($stateArg(socketinfo))
  1757. lappend newQueue {*}$InFlightW
  1758. lappend newQueue {*}$socketWrQueue($stateArg(socketinfo))
  1759. # 2. Tidy up tokenArg. This is a cut-down form of Finish/CloseSocket.
  1760. # Do not change state(status).
  1761. # No need to after cancel stateArg(after) - either this is done in
  1762. # ReplayCore/ReInit, or Finish is called.
  1763. catch {close $stateArg(sock)}
  1764. # 2a. Tidy the tokens in the queues - this is done in ReplayCore/ReInit.
  1765. # - Transactions, if any, that are awaiting responses cannot be completed.
  1766. # They are listed for re-sending in newQueue.
  1767. # - All tokens are preserved for re-use by ReplayCore, and their variables
  1768. # will be re-initialised by calls to ReInit.
  1769. # - The relevant element of socketMapping, socketRdState, socketWrState,
  1770. # socketRdQueue, socketWrQueue, socketClosing, socketPlayCmd will be set
  1771. # to new values in ReplayCore.
  1772. ReplayCore $newQueue
  1773. }
  1774. # http::ReplayIfClose --
  1775. #
  1776. # A request on a socket that was previously "Connection: keep-alive" has
  1777. # received a "Connection: close" response header. The server supplies
  1778. # that response correctly, but any later requests already queued on this
  1779. # connection will be lost when the socket closes.
  1780. #
  1781. # This command takes arguments that represent the socketWrState,
  1782. # socketRdQueue and socketWrQueue for this connection. The socketRdState
  1783. # is not needed because the server responds in full to the request that
  1784. # received the "Connection: close" response header.
  1785. #
  1786. # Existing request tokens $token (::http::$n) are preserved. The caller
  1787. # will be unaware that the request was processed this way.
  1788. proc http::ReplayIfClose {Wstate Rqueue Wqueue} {
  1789. Log running http::ReplayIfClose for $Wstate $Rqueue $Wqueue
  1790. if {$Wstate in $Rqueue || $Wstate in $Wqueue} {
  1791. Log WARNING duplicate token in http::ReplayIfClose - token $Wstate
  1792. set Wstate Wready
  1793. }
  1794. # 1. Create newQueue
  1795. set InFlightW {}
  1796. if {$Wstate ni {Wready peNding}} {
  1797. lappend InFlightW $Wstate
  1798. }
  1799. set newQueue {}
  1800. lappend newQueue {*}$Rqueue
  1801. lappend newQueue {*}$InFlightW
  1802. lappend newQueue {*}$Wqueue
  1803. # 2. Cleanup - none needed, done by the caller.
  1804. ReplayCore $newQueue
  1805. }
  1806. # http::ReInit --
  1807. #
  1808. # Command to restore a token's state to a condition that
  1809. # makes it ready to replay a request.
  1810. #
  1811. # Command http::geturl stores extra state in state(tmp*) so
  1812. # we don't need to do the argument processing again.
  1813. #
  1814. # The caller must:
  1815. # - Set state(reusing) and state(sock) to their new values after calling
  1816. # this command.
  1817. # - Unset state(tmpState), state(tmpOpenCmd) if future calls to ReplayCore
  1818. # or ReInit are inappropriate for this token. Typically only one retry
  1819. # is allowed.
  1820. # The caller may also unset state(tmpConnArgs) if this value (and the
  1821. # token) will be used immediately. The value is needed by tokens that
  1822. # will be stored in a queue.
  1823. #
  1824. # Arguments:
  1825. # token Connection token.
  1826. #
  1827. # Return Value: (boolean) true iff the re-initialisation was successful.
  1828. proc http::ReInit {token} {
  1829. variable $token
  1830. upvar 0 $token state
  1831. if {!(
  1832. [info exists state(tmpState)]
  1833. && [info exists state(tmpOpenCmd)]
  1834. && [info exists state(tmpConnArgs)]
  1835. )
  1836. } {
  1837. Log FAILED in http::ReInit via ReplayCore - NO tmp vars for $token
  1838. return 0
  1839. }
  1840. if {[info exists state(after)]} {
  1841. after cancel $state(after)
  1842. unset state(after)
  1843. }
  1844. # Don't alter state(status) - this would trigger http::wait if it is in use.
  1845. set tmpState $state(tmpState)
  1846. set tmpOpenCmd $state(tmpOpenCmd)
  1847. set tmpConnArgs $state(tmpConnArgs)
  1848. foreach name [array names state] {
  1849. if {$name ne "status"} {
  1850. unset state($name)
  1851. }
  1852. }
  1853. # Don't alter state(status).
  1854. # Restore state(tmp*) - the caller may decide to unset them.
  1855. # Restore state(tmpConnArgs) which is needed for connection.
  1856. # state(tmpState), state(tmpOpenCmd) are needed only for retries.
  1857. dict unset tmpState status
  1858. array set state $tmpState
  1859. set state(tmpState) $tmpState
  1860. set state(tmpOpenCmd) $tmpOpenCmd
  1861. set state(tmpConnArgs) $tmpConnArgs
  1862. return 1
  1863. }
  1864. # http::ReplayCore --
  1865. #
  1866. # Command to replay a list of requests, using existing connection tokens.
  1867. #
  1868. # Abstracted from http::geturl which stores extra state in state(tmp*) so
  1869. # we don't need to do the argument processing again.
  1870. #
  1871. # Arguments:
  1872. # newQueue List of connection tokens.
  1873. #
  1874. # Side Effects:
  1875. # Use existing tokens, but try to open a new socket.
  1876. proc http::ReplayCore {newQueue} {
  1877. variable socketMapping
  1878. variable socketRdState
  1879. variable socketWrState
  1880. variable socketRdQueue
  1881. variable socketWrQueue
  1882. variable socketClosing
  1883. variable socketPlayCmd
  1884. if {[llength $newQueue] == 0} {
  1885. # Nothing to do.
  1886. return
  1887. }
  1888. ##Log running ReplayCore for {*}$newQueue
  1889. set newToken [lindex $newQueue 0]
  1890. set newQueue [lrange $newQueue 1 end]
  1891. # 3. Use newToken, and restore its values of state(*). Do not restore
  1892. # elements tmp* - we try again only once.
  1893. set token $newToken
  1894. variable $token
  1895. upvar 0 $token state
  1896. if {![ReInit $token]} {
  1897. Log FAILED in http::ReplayCore - NO tmp vars
  1898. Finish $token {cannot send this request again}
  1899. return
  1900. }
  1901. set tmpState $state(tmpState)
  1902. set tmpOpenCmd $state(tmpOpenCmd)
  1903. set tmpConnArgs $state(tmpConnArgs)
  1904. unset state(tmpState)
  1905. unset state(tmpOpenCmd)
  1906. unset state(tmpConnArgs)
  1907. set state(reusing) 0
  1908. if {$state(-timeout) > 0} {
  1909. set resetCmd [list http::reset $token timeout]
  1910. set state(after) [after $state(-timeout) $resetCmd]
  1911. }
  1912. set pre [clock milliseconds]
  1913. ##Log pre socket opened, - token $token
  1914. ##Log $tmpOpenCmd - token $token
  1915. # 4. Open a socket.
  1916. if {[catch {eval $tmpOpenCmd} sock]} {
  1917. # Something went wrong while trying to establish the connection.
  1918. Log FAILED - $sock
  1919. set state(sock) NONE
  1920. Finish $token $sock
  1921. return
  1922. }
  1923. ##Log post socket opened, - token $token
  1924. set delay [expr {[clock milliseconds] - $pre}]
  1925. if {$delay > 3000} {
  1926. Log socket delay $delay - token $token
  1927. }
  1928. # Command [socket] is called with -async, but takes 5s to 5.1s to return,
  1929. # with probability of order 1 in 10,000. This may be a bizarre scheduling
  1930. # issue with my (KJN's) system (Fedora Linux).
  1931. # This does not cause a problem (unless the request times out when this
  1932. # command returns).
  1933. # 5. Configure the persistent socket data.
  1934. if {$state(-keepalive)} {
  1935. set socketMapping($state(socketinfo)) $sock
  1936. if {![info exists socketRdState($state(socketinfo))]} {
  1937. set socketRdState($state(socketinfo)) {}
  1938. set varName ::http::socketRdState($state(socketinfo))
  1939. trace add variable $varName unset ::http::CancelReadPipeline
  1940. }
  1941. if {![info exists socketWrState($state(socketinfo))]} {
  1942. set socketWrState($state(socketinfo)) {}
  1943. set varName ::http::socketWrState($state(socketinfo))
  1944. trace add variable $varName unset ::http::CancelWritePipeline
  1945. }
  1946. if {$state(-pipeline)} {
  1947. #Log new, init for pipelined, GRANT write acc to $token ReplayCore
  1948. set socketRdState($state(socketinfo)) $token
  1949. set socketWrState($state(socketinfo)) $token
  1950. } else {
  1951. #Log new, init for nonpipeline, GRANT r/w acc to $token ReplayCore
  1952. set socketRdState($state(socketinfo)) $token
  1953. set socketWrState($state(socketinfo)) $token
  1954. }
  1955. set socketRdQueue($state(socketinfo)) {}
  1956. set socketWrQueue($state(socketinfo)) $newQueue
  1957. set socketClosing($state(socketinfo)) 0
  1958. set socketPlayCmd($state(socketinfo)) {ReplayIfClose Wready {} {}}
  1959. }
  1960. ##Log pre newQueue ReInit, - token $token
  1961. # 6. Configure sockets in the queue.
  1962. foreach tok $newQueue {
  1963. if {[ReInit $tok]} {
  1964. set ${tok}(reusing) 1
  1965. set ${tok}(sock) $sock
  1966. } else {
  1967. set ${tok}(reusing) 1
  1968. set ${tok}(sock) NONE
  1969. Finish $token {cannot send this request again}
  1970. }
  1971. }
  1972. # 7. Configure the socket for newToken to send a request.
  1973. set state(sock) $sock
  1974. Log "Using $sock for $state(socketinfo) - token $token" \
  1975. [expr {$state(-keepalive)?"keepalive":""}]
  1976. # Initialisation of a new socket.
  1977. ##Log socket opened, now fconfigure - token $token
  1978. fconfigure $sock -translation {auto crlf} -buffersize $state(-blocksize)
  1979. ##Log socket opened, DONE fconfigure - token $token
  1980. # Connect does its own fconfigure.
  1981. fileevent $sock writable [list http::Connect $token {*}$tmpConnArgs]
  1982. #Log ---- $sock << conn to $token for HTTP request (e)
  1983. }
  1984. # Data access functions:
  1985. # Data - the URL data
  1986. # Status - the transaction status: ok, reset, eof, timeout, error
  1987. # Code - the HTTP transaction code, e.g., 200
  1988. # Size - the size of the URL data
  1989. proc http::data {token} {
  1990. variable $token
  1991. upvar 0 $token state
  1992. return $state(body)
  1993. }
  1994. proc http::status {token} {
  1995. if {![info exists $token]} {
  1996. return "error"
  1997. }
  1998. variable $token
  1999. upvar 0 $token state
  2000. return $state(status)
  2001. }
  2002. proc http::code {token} {
  2003. variable $token
  2004. upvar 0 $token state
  2005. return $state(http)
  2006. }
  2007. proc http::ncode {token} {
  2008. variable $token
  2009. upvar 0 $token state
  2010. if {[regexp {[0-9]{3}} $state(http) numeric_code]} {
  2011. return $numeric_code
  2012. } else {
  2013. return $state(http)
  2014. }
  2015. }
  2016. proc http::size {token} {
  2017. variable $token
  2018. upvar 0 $token state
  2019. return $state(currentsize)
  2020. }
  2021. proc http::meta {token} {
  2022. variable $token
  2023. upvar 0 $token state
  2024. return $state(meta)
  2025. }
  2026. proc http::error {token} {
  2027. variable $token
  2028. upvar 0 $token state
  2029. if {[info exists state(error)]} {
  2030. return $state(error)
  2031. }
  2032. return ""
  2033. }
  2034. # http::cleanup
  2035. #
  2036. # Garbage collect the state associated with a transaction
  2037. #
  2038. # Arguments
  2039. # token The token returned from http::geturl
  2040. #
  2041. # Side Effects
  2042. # unsets the state array
  2043. proc http::cleanup {token} {
  2044. variable $token
  2045. upvar 0 $token state
  2046. if {[info commands ${token}EventCoroutine] ne {}} {
  2047. rename ${token}EventCoroutine {}
  2048. }
  2049. if {[info exists state(after)]} {
  2050. after cancel $state(after)
  2051. unset state(after)
  2052. }
  2053. if {[info exists state]} {
  2054. unset state
  2055. }
  2056. }
  2057. # http::Connect
  2058. #
  2059. # This callback is made when an asyncronous connection completes.
  2060. #
  2061. # Arguments
  2062. # token The token returned from http::geturl
  2063. #
  2064. # Side Effects
  2065. # Sets the status of the connection, which unblocks
  2066. # the waiting geturl call
  2067. proc http::Connect {token proto phost srvurl} {
  2068. variable $token
  2069. upvar 0 $token state
  2070. set tk [namespace tail $token]
  2071. set err "due to unexpected EOF"
  2072. if {
  2073. [eof $state(sock)] ||
  2074. [set err [fconfigure $state(sock) -error]] ne ""
  2075. } {
  2076. Log "WARNING - if testing, pay special attention to this\
  2077. case (GJ) which is seldom executed - token $token"
  2078. if {[info exists state(reusing)] && $state(reusing)} {
  2079. # The socket was closed at the server end, and closed at
  2080. # this end by http::CheckEof.
  2081. if {[TestForReplay $token write $err b]} {
  2082. return
  2083. }
  2084. # else:
  2085. # This is NOT a persistent socket that has been closed since its
  2086. # last use.
  2087. # If any other requests are in flight or pipelined/queued, they will
  2088. # be discarded.
  2089. }
  2090. Finish $token "connect failed $err"
  2091. } else {
  2092. set state(state) connecting
  2093. fileevent $state(sock) writable {}
  2094. ::http::Connected $token $proto $phost $srvurl
  2095. }
  2096. }
  2097. # http::Write
  2098. #
  2099. # Write POST query data to the socket
  2100. #
  2101. # Arguments
  2102. # token The token for the connection
  2103. #
  2104. # Side Effects
  2105. # Write the socket and handle callbacks.
  2106. proc http::Write {token} {
  2107. variable http
  2108. variable socketMapping
  2109. variable socketRdState
  2110. variable socketWrState
  2111. variable socketRdQueue
  2112. variable socketWrQueue
  2113. variable socketClosing
  2114. variable socketPlayCmd
  2115. variable $token
  2116. upvar 0 $token state
  2117. set tk [namespace tail $token]
  2118. set sock $state(sock)
  2119. # Output a block. Tcl will buffer this if the socket blocks
  2120. set done 0
  2121. if {[catch {
  2122. # Catch I/O errors on dead sockets
  2123. if {[info exists state(-query)]} {
  2124. # Chop up large query strings so queryprogress callback can give
  2125. # smooth feedback.
  2126. if { $state(queryoffset) + $state(-queryblocksize)
  2127. >= $state(querylength)
  2128. } {
  2129. # This will be the last puts for the request-body.
  2130. if { (![catch {fileevent $sock readable} binding])
  2131. && ($binding eq [list http::CheckEof $sock])
  2132. } {
  2133. # Remove the "fileevent readable" binding of an idle
  2134. # persistent socket to http::CheckEof. We can no longer
  2135. # treat bytes received as junk. The server might still time
  2136. # out and half-close the socket if it has not yet received
  2137. # the first "puts".
  2138. fileevent $sock readable {}
  2139. }
  2140. }
  2141. puts -nonewline $sock \
  2142. [string range $state(-query) $state(queryoffset) \
  2143. [expr {$state(queryoffset) + $state(-queryblocksize) - 1}]]
  2144. incr state(queryoffset) $state(-queryblocksize)
  2145. if {$state(queryoffset) >= $state(querylength)} {
  2146. set state(queryoffset) $state(querylength)
  2147. set done 1
  2148. }
  2149. } else {
  2150. # Copy blocks from the query channel
  2151. set outStr [read $state(-querychannel) $state(-queryblocksize)]
  2152. if {[eof $state(-querychannel)]} {
  2153. # This will be the last puts for the request-body.
  2154. if { (![catch {fileevent $sock readable} binding])
  2155. && ($binding eq [list http::CheckEof $sock])
  2156. } {
  2157. # Remove the "fileevent readable" binding of an idle
  2158. # persistent socket to http::CheckEof. We can no longer
  2159. # treat bytes received as junk. The server might still time
  2160. # out and half-close the socket if it has not yet received
  2161. # the first "puts".
  2162. fileevent $sock readable {}
  2163. }
  2164. }
  2165. puts -nonewline $sock $outStr
  2166. incr state(queryoffset) [string length $outStr]
  2167. if {[eof $state(-querychannel)]} {
  2168. set done 1
  2169. }
  2170. }
  2171. } err]} {
  2172. # Do not call Finish here, but instead let the read half of the socket
  2173. # process whatever server reply there is to get.
  2174. set state(posterror) $err
  2175. set done 1
  2176. }
  2177. if {$done} {
  2178. catch {flush $sock}
  2179. fileevent $sock writable {}
  2180. Log ^C$tk end sending request - token $token
  2181. # End of writing (POST method). The request has been sent.
  2182. DoneRequest $token
  2183. }
  2184. # Callback to the client after we've completely handled everything.
  2185. if {[string length $state(-queryprogress)]} {
  2186. eval $state(-queryprogress) \
  2187. [list $token $state(querylength) $state(queryoffset)]
  2188. }
  2189. }
  2190. # http::Event
  2191. #
  2192. # Handle input on the socket. This command is the core of
  2193. # the coroutine commands ${token}EventCoroutine that are
  2194. # bound to "fileevent $sock readable" and process input.
  2195. #
  2196. # Arguments
  2197. # sock The socket receiving input.
  2198. # token The token returned from http::geturl
  2199. #
  2200. # Side Effects
  2201. # Read the socket and handle callbacks.
  2202. proc http::Event {sock token} {
  2203. variable http
  2204. variable socketMapping
  2205. variable socketRdState
  2206. variable socketWrState
  2207. variable socketRdQueue
  2208. variable socketWrQueue
  2209. variable socketClosing
  2210. variable socketPlayCmd
  2211. variable $token
  2212. upvar 0 $token state
  2213. set tk [namespace tail $token]
  2214. while 1 {
  2215. yield
  2216. ##Log Event call - token $token
  2217. if {![info exists state]} {
  2218. Log "Event $sock with invalid token '$token' - remote close?"
  2219. if {![eof $sock]} {
  2220. if {[set d [read $sock]] ne ""} {
  2221. Log "WARNING: additional data left on closed socket\
  2222. - token $token"
  2223. }
  2224. }
  2225. Log ^X$tk end of response (token error) - token $token
  2226. CloseSocket $sock
  2227. return
  2228. }
  2229. if {$state(state) eq "connecting"} {
  2230. ##Log - connecting - token $token
  2231. if { $state(reusing)
  2232. && $state(-pipeline)
  2233. && ($state(-timeout) > 0)
  2234. && (![info exists state(after)])
  2235. } {
  2236. set state(after) [after $state(-timeout) \
  2237. [list http::reset $token timeout]]
  2238. }
  2239. if {[catch {gets $sock state(http)} nsl]} {
  2240. Log "WARNING - if testing, pay special attention to this\
  2241. case (GK) which is seldom executed - token $token"
  2242. if {[info exists state(reusing)] && $state(reusing)} {
  2243. # The socket was closed at the server end, and closed at
  2244. # this end by http::CheckEof.
  2245. if {[TestForReplay $token read $nsl c]} {
  2246. return
  2247. }
  2248. # else:
  2249. # This is NOT a persistent socket that has been closed since
  2250. # its last use.
  2251. # If any other requests are in flight or pipelined/queued,
  2252. # they will be discarded.
  2253. } else {
  2254. Log ^X$tk end of response (error) - token $token
  2255. Finish $token $nsl
  2256. return
  2257. }
  2258. } elseif {$nsl >= 0} {
  2259. ##Log - connecting 1 - token $token
  2260. set state(state) "header"
  2261. } elseif { [eof $sock]
  2262. && [info exists state(reusing)]
  2263. && $state(reusing)
  2264. } {
  2265. # The socket was closed at the server end, and we didn't notice.
  2266. # This is the first read - where the closure is usually first
  2267. # detected.
  2268. if {[TestForReplay $token read {} d]} {
  2269. return
  2270. }
  2271. # else:
  2272. # This is NOT a persistent socket that has been closed since its
  2273. # last use.
  2274. # If any other requests are in flight or pipelined/queued, they
  2275. # will be discarded.
  2276. }
  2277. } elseif {$state(state) eq "header"} {
  2278. if {[catch {gets $sock line} nhl]} {
  2279. ##Log header failed - token $token
  2280. Log ^X$tk end of response (error) - token $token
  2281. Finish $token $nhl
  2282. return
  2283. } elseif {$nhl == 0} {
  2284. ##Log header done - token $token
  2285. Log ^E$tk end of response headers - token $token
  2286. # We have now read all headers
  2287. # We ignore HTTP/1.1 100 Continue returns. RFC2616 sec 8.2.3
  2288. if { ($state(http) == "")
  2289. || ([regexp {^\S+\s(\d+)} $state(http) {} x] && $x == 100)
  2290. } {
  2291. set state(state) "connecting"
  2292. continue
  2293. # This was a "return" in the pre-coroutine code.
  2294. }
  2295. if { ([info exists state(connection)])
  2296. && ([info exists socketMapping($state(socketinfo))])
  2297. && ($state(connection) eq "keep-alive")
  2298. && ($state(-keepalive))
  2299. && (!$state(reusing))
  2300. && ($state(-pipeline))
  2301. } {
  2302. # Response headers received for first request on a
  2303. # persistent socket. Now ready for pipelined writes (if
  2304. # any).
  2305. # Previous value is $token. It cannot be "pending".
  2306. set socketWrState($state(socketinfo)) Wready
  2307. http::NextPipelinedWrite $token
  2308. }
  2309. # Once a "close" has been signaled, the client MUST NOT send any
  2310. # more requests on that connection.
  2311. #
  2312. # If either the client or the server sends the "close" token in
  2313. # the Connection header, that request becomes the last one for
  2314. # the connection.
  2315. if { ([info exists state(connection)])
  2316. && ([info exists socketMapping($state(socketinfo))])
  2317. && ($state(connection) eq "close")
  2318. && ($state(-keepalive))
  2319. } {
  2320. # The server warns that it will close the socket after this
  2321. # response.
  2322. ##Log WARNING - socket will close after response for $token
  2323. # Prepare data for a call to ReplayIfClose.
  2324. if { ($socketRdQueue($state(socketinfo)) ne {})
  2325. || ($socketWrQueue($state(socketinfo)) ne {})
  2326. || ($socketWrState($state(socketinfo)) ni
  2327. [list Wready peNding $token])
  2328. } {
  2329. set InFlightW $socketWrState($state(socketinfo))
  2330. if {$InFlightW in [list Wready peNding $token]} {
  2331. set InFlightW Wready
  2332. } else {
  2333. set msg "token ${InFlightW} is InFlightW"
  2334. ##Log $msg - token $token
  2335. }
  2336. set socketPlayCmd($state(socketinfo)) \
  2337. [list ReplayIfClose $InFlightW \
  2338. $socketRdQueue($state(socketinfo)) \
  2339. $socketWrQueue($state(socketinfo))]
  2340. # - All tokens are preserved for re-use by ReplayCore.
  2341. # - Queues are preserved in case of Finish with error,
  2342. # but are not used for anything else because
  2343. # socketClosing(*) is set below.
  2344. # - Cancel the state(after) timeout events.
  2345. foreach tokenVal $socketRdQueue($state(socketinfo)) {
  2346. if {[info exists ${tokenVal}(after)]} {
  2347. after cancel [set ${tokenVal}(after)]
  2348. unset ${tokenVal}(after)
  2349. }
  2350. }
  2351. } else {
  2352. set socketPlayCmd($state(socketinfo)) \
  2353. {ReplayIfClose Wready {} {}}
  2354. }
  2355. # Do not allow further connections on this socket.
  2356. set socketClosing($state(socketinfo)) 1
  2357. }
  2358. set state(state) body
  2359. # If doing a HEAD, then we won't get any body
  2360. if {$state(-validate)} {
  2361. Log ^F$tk end of response for HEAD request - token $token
  2362. set state(state) complete
  2363. Eot $token
  2364. return
  2365. }
  2366. # - For non-chunked transfer we may have no body - in this case
  2367. # we may get no further file event if the connection doesn't
  2368. # close and no more data is sent. We can tell and must finish
  2369. # up now - not later - the alternative would be to wait until
  2370. # the server times out.
  2371. # - In this case, the server has NOT told the client it will
  2372. # close the connection, AND it has NOT indicated the resource
  2373. # length EITHER by setting the Content-Length (totalsize) OR
  2374. # by using chunked Transfer-Encoding.
  2375. # - Do not worry here about the case (Connection: close) because
  2376. # the server should close the connection.
  2377. # - IF (NOT Connection: close) AND (NOT chunked encoding) AND
  2378. # (totalsize == 0).
  2379. if { (!( [info exists state(connection)]
  2380. && ($state(connection) eq "close")
  2381. )
  2382. )
  2383. && (![info exists state(transfer)])
  2384. && ($state(totalsize) == 0)
  2385. } {
  2386. set msg {body size is 0 and no events likely - complete}
  2387. Log "$msg - token $token"
  2388. set msg {(length unknown, set to 0)}
  2389. Log ^F$tk end of response body {*}$msg - token $token
  2390. set state(state) complete
  2391. Eot $token
  2392. return
  2393. }
  2394. # We have to use binary translation to count bytes properly.
  2395. lassign [fconfigure $sock -translation] trRead trWrite
  2396. fconfigure $sock -translation [list binary $trWrite]
  2397. if {
  2398. $state(-binary) || [IsBinaryContentType $state(type)]
  2399. } {
  2400. # Turn off conversions for non-text data.
  2401. set state(binary) 1
  2402. }
  2403. if {[info exists state(-channel)]} {
  2404. if {$state(binary) || [llength [ContentEncoding $token]]} {
  2405. fconfigure $state(-channel) -translation binary
  2406. }
  2407. if {![info exists state(-handler)]} {
  2408. # Initiate a sequence of background fcopies.
  2409. fileevent $sock readable {}
  2410. rename ${token}EventCoroutine {}
  2411. CopyStart $sock $token
  2412. return
  2413. }
  2414. }
  2415. } elseif {$nhl > 0} {
  2416. # Process header lines.
  2417. ##Log header - token $token - $line
  2418. if {[regexp -nocase {^([^:]+):(.+)$} $line x key value]} {
  2419. switch -- [string tolower $key] {
  2420. content-type {
  2421. set state(type) [string trim [string tolower $value]]
  2422. # Grab the optional charset information.
  2423. if {[regexp -nocase \
  2424. {charset\s*=\s*\"((?:[^""]|\\\")*)\"} \
  2425. $state(type) -> cs]} {
  2426. set state(charset) [string map {{\"} \"} $cs]
  2427. } else {
  2428. regexp -nocase {charset\s*=\s*(\S+?);?} \
  2429. $state(type) -> state(charset)
  2430. }
  2431. }
  2432. content-length {
  2433. set state(totalsize) [string trim $value]
  2434. }
  2435. content-encoding {
  2436. set state(coding) [string trim $value]
  2437. }
  2438. transfer-encoding {
  2439. set state(transfer) \
  2440. [string trim [string tolower $value]]
  2441. }
  2442. proxy-connection -
  2443. connection {
  2444. set state(connection) \
  2445. [string trim [string tolower $value]]
  2446. }
  2447. }
  2448. lappend state(meta) $key [string trim $value]
  2449. }
  2450. }
  2451. } else {
  2452. # Now reading body
  2453. ##Log body - token $token
  2454. if {[catch {
  2455. if {[info exists state(-handler)]} {
  2456. set n [eval $state(-handler) [list $sock $token]]
  2457. ##Log handler $n - token $token
  2458. # N.B. the protocol has been set to 1.0 because the -handler
  2459. # logic is not expected to handle chunked encoding.
  2460. # FIXME Allow -handler with 1.1 on dechunked stacked chan.
  2461. if {$state(totalsize) == 0} {
  2462. # We know the transfer is complete only when the server
  2463. # closes the connection - i.e. eof is not an error.
  2464. set state(state) complete
  2465. }
  2466. if {![string is integer -strict $n]} {
  2467. if 1 {
  2468. # Do not tolerate bad -handler - fail with error
  2469. # status.
  2470. set msg {the -handler command for http::geturl must\
  2471. return an integer (the number of bytes\
  2472. read)}
  2473. Log ^X$tk end of response (handler error) -\
  2474. token $token
  2475. Eot $token $msg
  2476. } else {
  2477. # Tolerate the bad -handler, and continue. The
  2478. # penalty:
  2479. # (a) Because the handler returns nonsense, we know
  2480. # the transfer is complete only when the server
  2481. # closes the connection - i.e. eof is not an
  2482. # error.
  2483. # (b) http::size will not be accurate.
  2484. # (c) The transaction is already downgraded to 1.0
  2485. # to avoid chunked transfer encoding. It MUST
  2486. # also be forced to "Connection: close" or the
  2487. # HTTP/1.0 equivalent; or it MUST fail (as
  2488. # above) if the server sends
  2489. # "Connection: keep-alive" or the HTTP/1.0
  2490. # equivalent.
  2491. set n 0
  2492. set state(state) complete
  2493. }
  2494. }
  2495. } elseif {[info exists state(transfer_final)]} {
  2496. # This code forgives EOF in place of the final CRLF.
  2497. set line [getTextLine $sock]
  2498. set n [string length $line]
  2499. set state(state) complete
  2500. if {$n > 0} {
  2501. # - HTTP trailers (late response headers) are permitted
  2502. # by Chunked Transfer-Encoding, and can be safely
  2503. # ignored.
  2504. # - Do not count these bytes in the total received for
  2505. # the response body.
  2506. Log "trailer of $n bytes after final chunk -\
  2507. token $token"
  2508. append state(transfer_final) $line
  2509. set n 0
  2510. } else {
  2511. Log ^F$tk end of response body (chunked) - token $token
  2512. Log "final chunk part - token $token"
  2513. Eot $token
  2514. }
  2515. } elseif { [info exists state(transfer)]
  2516. && ($state(transfer) eq "chunked")
  2517. } {
  2518. ##Log chunked - token $token
  2519. set size 0
  2520. set hexLenChunk [getTextLine $sock]
  2521. #set ntl [string length $hexLenChunk]
  2522. if {[string trim $hexLenChunk] ne ""} {
  2523. scan $hexLenChunk %x size
  2524. if {$size != 0} {
  2525. ##Log chunk-measure $size - token $token
  2526. set chunk [BlockingRead $sock $size]
  2527. set n [string length $chunk]
  2528. if {$n >= 0} {
  2529. append state(body) $chunk
  2530. incr state(log_size) [string length $chunk]
  2531. ##Log chunk $n cumul $state(log_size) -\
  2532. token $token
  2533. }
  2534. if {$size != [string length $chunk]} {
  2535. Log "WARNING: mis-sized chunk:\
  2536. was [string length $chunk], should be\
  2537. $size - token $token"
  2538. set n 0
  2539. set state(connection) close
  2540. Log ^X$tk end of response (chunk error) \
  2541. - token $token
  2542. set msg {error in chunked encoding - fetch\
  2543. terminated}
  2544. Eot $token $msg
  2545. }
  2546. # CRLF that follows chunk.
  2547. # If eof, this is handled at the end of this proc.
  2548. getTextLine $sock
  2549. } else {
  2550. set n 0
  2551. set state(transfer_final) {}
  2552. }
  2553. } else {
  2554. # Line expected to hold chunk length is empty, or eof.
  2555. ##Log bad-chunk-measure - token $token
  2556. set n 0
  2557. set state(connection) close
  2558. Log ^X$tk end of response (chunk error) - token $token
  2559. Eot $token {error in chunked encoding -\
  2560. fetch terminated}
  2561. }
  2562. } else {
  2563. ##Log unchunked - token $token
  2564. if {$state(totalsize) == 0} {
  2565. # We know the transfer is complete only when the server
  2566. # closes the connection.
  2567. set state(state) complete
  2568. set reqSize $state(-blocksize)
  2569. } else {
  2570. # Ask for the whole of the unserved response-body.
  2571. # This works around a problem with a tls::socket - for
  2572. # https in keep-alive mode, and a request for
  2573. # $state(-blocksize) bytes, the last part of the
  2574. # resource does not get read until the server times out.
  2575. set reqSize [expr { $state(totalsize)
  2576. - $state(currentsize)}]
  2577. # The workaround fails if reqSize is
  2578. # capped at $state(-blocksize).
  2579. # set reqSize [expr {min($reqSize, $state(-blocksize))}]
  2580. }
  2581. set c $state(currentsize)
  2582. set t $state(totalsize)
  2583. ##Log non-chunk currentsize $c of totalsize $t -\
  2584. token $token
  2585. set block [read $sock $reqSize]
  2586. set n [string length $block]
  2587. if {$n >= 0} {
  2588. append state(body) $block
  2589. ##Log non-chunk [string length $state(body)] -\
  2590. token $token
  2591. }
  2592. }
  2593. # This calculation uses n from the -handler, chunked, or
  2594. # unchunked case as appropriate.
  2595. if {[info exists state]} {
  2596. if {$n >= 0} {
  2597. incr state(currentsize) $n
  2598. set c $state(currentsize)
  2599. set t $state(totalsize)
  2600. ##Log another $n currentsize $c totalsize $t -\
  2601. token $token
  2602. }
  2603. # If Content-Length - check for end of data.
  2604. if {
  2605. ($state(totalsize) > 0)
  2606. && ($state(currentsize) >= $state(totalsize))
  2607. } {
  2608. Log ^F$tk end of response body (unchunked) -\
  2609. token $token
  2610. set state(state) complete
  2611. Eot $token
  2612. }
  2613. }
  2614. } err]} {
  2615. Log ^X$tk end of response (error ${err}) - token $token
  2616. Finish $token $err
  2617. return
  2618. } else {
  2619. if {[info exists state(-progress)]} {
  2620. eval $state(-progress) \
  2621. [list $token $state(totalsize) $state(currentsize)]
  2622. }
  2623. }
  2624. }
  2625. # catch as an Eot above may have closed the socket already
  2626. # $state(state) may be connecting, header, body, or complete
  2627. if {![set cc [catch {eof $sock} eof]] && $eof} {
  2628. ##Log eof - token $token
  2629. if {[info exists $token]} {
  2630. set state(connection) close
  2631. if {$state(state) eq "complete"} {
  2632. # This includes all cases in which the transaction
  2633. # can be completed by eof.
  2634. # The value "complete" is set only in http::Event, and it is
  2635. # used only in the test above.
  2636. Log ^F$tk end of response body (unchunked, eof) -\
  2637. token $token
  2638. Eot $token
  2639. } else {
  2640. # Premature eof.
  2641. Log ^X$tk end of response (unexpected eof) - token $token
  2642. Eot $token eof
  2643. }
  2644. } else {
  2645. # open connection closed on a token that has been cleaned up.
  2646. Log ^X$tk end of response (token error) - token $token
  2647. CloseSocket $sock
  2648. }
  2649. } elseif {$cc} {
  2650. return
  2651. }
  2652. }
  2653. }
  2654. # http::TestForReplay
  2655. #
  2656. # Command called if eof is discovered when a socket is first used for a
  2657. # new transaction. Typically this occurs if a persistent socket is used
  2658. # after a period of idleness and the server has half-closed the socket.
  2659. #
  2660. # token - the connection token returned by http::geturl
  2661. # doing - "read" or "write"
  2662. # err - error message, if any
  2663. # caller - code to identify the caller - used only in logging
  2664. #
  2665. # Return Value: boolean, true iff the command calls http::ReplayIfDead.
  2666. proc http::TestForReplay {token doing err caller} {
  2667. variable http
  2668. variable $token
  2669. upvar 0 $token state
  2670. set tk [namespace tail $token]
  2671. if {$doing eq "read"} {
  2672. set code Q
  2673. set action response
  2674. set ing reading
  2675. } else {
  2676. set code P
  2677. set action request
  2678. set ing writing
  2679. }
  2680. if {$err eq {}} {
  2681. set err "detect eof when $ing (server timed out?)"
  2682. }
  2683. if {$state(method) eq "POST" && !$http(-repost)} {
  2684. # No Replay.
  2685. # The present transaction will end when Finish is called.
  2686. # That call to Finish will abort any other transactions
  2687. # currently in the write queue.
  2688. # For calls from http::Event this occurs when execution
  2689. # reaches the code block at the end of that proc.
  2690. set msg {no retry for POST with http::config -repost 0}
  2691. Log reusing socket failed "($caller)" - $msg - token $token
  2692. Log error - $err - token $token
  2693. Log ^X$tk end of $action (error) - token $token
  2694. return 0
  2695. } else {
  2696. # Replay.
  2697. set msg {try a new socket}
  2698. Log reusing socket failed "($caller)" - $msg - token $token
  2699. Log error - $err - token $token
  2700. Log ^$code$tk Any unfinished (incl this one) failed - token $token
  2701. ReplayIfDead $token $doing
  2702. return 1
  2703. }
  2704. }
  2705. # http::IsBinaryContentType --
  2706. #
  2707. # Determine if the content-type means that we should definitely transfer
  2708. # the data as binary. [Bug 838e99a76d]
  2709. #
  2710. # Arguments
  2711. # type The content-type of the data.
  2712. #
  2713. # Results:
  2714. # Boolean, true if we definitely should be binary.
  2715. proc http::IsBinaryContentType {type} {
  2716. lassign [split [string tolower $type] "/;"] major minor
  2717. if {$major eq "text"} {
  2718. return false
  2719. }
  2720. # There's a bunch of XML-as-application-format things about. See RFC 3023
  2721. # and so on.
  2722. if {$major eq "application"} {
  2723. set minor [string trimright $minor]
  2724. if {$minor in {"xml" "xml-external-parsed-entity" "xml-dtd"}} {
  2725. return false
  2726. }
  2727. }
  2728. # Not just application/foobar+xml but also image/svg+xml, so let us not
  2729. # restrict things for now...
  2730. if {[string match "*+xml" $minor]} {
  2731. return false
  2732. }
  2733. return true
  2734. }
  2735. # http::getTextLine --
  2736. #
  2737. # Get one line with the stream in crlf mode.
  2738. # Used if Transfer-Encoding is chunked.
  2739. # Empty line is not distinguished from eof. The caller must
  2740. # be able to handle this.
  2741. #
  2742. # Arguments
  2743. # sock The socket receiving input.
  2744. #
  2745. # Results:
  2746. # The line of text, without trailing newline
  2747. proc http::getTextLine {sock} {
  2748. set tr [fconfigure $sock -translation]
  2749. lassign $tr trRead trWrite
  2750. fconfigure $sock -translation [list crlf $trWrite]
  2751. set r [BlockingGets $sock]
  2752. fconfigure $sock -translation $tr
  2753. return $r
  2754. }
  2755. # http::BlockingRead
  2756. #
  2757. # Replacement for a blocking read.
  2758. # The caller must be a coroutine.
  2759. proc http::BlockingRead {sock size} {
  2760. if {$size < 1} {
  2761. return
  2762. }
  2763. set result {}
  2764. while 1 {
  2765. set need [expr {$size - [string length $result]}]
  2766. set block [read $sock $need]
  2767. set eof [eof $sock]
  2768. append result $block
  2769. if {[string length $result] >= $size || $eof} {
  2770. return $result
  2771. } else {
  2772. yield
  2773. }
  2774. }
  2775. }
  2776. # http::BlockingGets
  2777. #
  2778. # Replacement for a blocking gets.
  2779. # The caller must be a coroutine.
  2780. # Empty line is not distinguished from eof. The caller must
  2781. # be able to handle this.
  2782. proc http::BlockingGets {sock} {
  2783. while 1 {
  2784. set count [gets $sock line]
  2785. set eof [eof $sock]
  2786. if {$count > -1 || $eof} {
  2787. return $line
  2788. } else {
  2789. yield
  2790. }
  2791. }
  2792. }
  2793. # http::CopyStart
  2794. #
  2795. # Error handling wrapper around fcopy
  2796. #
  2797. # Arguments
  2798. # sock The socket to copy from
  2799. # token The token returned from http::geturl
  2800. #
  2801. # Side Effects
  2802. # This closes the connection upon error
  2803. proc http::CopyStart {sock token {initial 1}} {
  2804. upvar #0 $token state
  2805. if {[info exists state(transfer)] && $state(transfer) eq "chunked"} {
  2806. foreach coding [ContentEncoding $token] {
  2807. lappend state(zlib) [zlib stream $coding]
  2808. }
  2809. make-transformation-chunked $sock [namespace code [list CopyChunk $token]]
  2810. } else {
  2811. if {$initial} {
  2812. foreach coding [ContentEncoding $token] {
  2813. zlib push $coding $sock
  2814. }
  2815. }
  2816. if {[catch {
  2817. # FIXME Keep-Alive on https tls::socket with unchunked transfer
  2818. # hangs until the server times out. A workaround is possible, as for
  2819. # the case without -channel, but it does not use the neat "fcopy"
  2820. # solution.
  2821. fcopy $sock $state(-channel) -size $state(-blocksize) -command \
  2822. [list http::CopyDone $token]
  2823. } err]} {
  2824. Finish $token $err
  2825. }
  2826. }
  2827. }
  2828. proc http::CopyChunk {token chunk} {
  2829. upvar 0 $token state
  2830. if {[set count [string length $chunk]]} {
  2831. incr state(currentsize) $count
  2832. if {[info exists state(zlib)]} {
  2833. foreach stream $state(zlib) {
  2834. set chunk [$stream add $chunk]
  2835. }
  2836. }
  2837. puts -nonewline $state(-channel) $chunk
  2838. if {[info exists state(-progress)]} {
  2839. eval [linsert $state(-progress) end \
  2840. $token $state(totalsize) $state(currentsize)]
  2841. }
  2842. } else {
  2843. Log "CopyChunk Finish - token $token"
  2844. if {[info exists state(zlib)]} {
  2845. set excess ""
  2846. foreach stream $state(zlib) {
  2847. catch {set excess [$stream add -finalize $excess]}
  2848. }
  2849. puts -nonewline $state(-channel) $excess
  2850. foreach stream $state(zlib) { $stream close }
  2851. unset state(zlib)
  2852. }
  2853. Eot $token ;# FIX ME: pipelining.
  2854. }
  2855. }
  2856. # http::CopyDone
  2857. #
  2858. # fcopy completion callback
  2859. #
  2860. # Arguments
  2861. # token The token returned from http::geturl
  2862. # count The amount transfered
  2863. #
  2864. # Side Effects
  2865. # Invokes callbacks
  2866. proc http::CopyDone {token count {error {}}} {
  2867. variable $token
  2868. upvar 0 $token state
  2869. set sock $state(sock)
  2870. incr state(currentsize) $count
  2871. if {[info exists state(-progress)]} {
  2872. eval $state(-progress) \
  2873. [list $token $state(totalsize) $state(currentsize)]
  2874. }
  2875. # At this point the token may have been reset.
  2876. if {[string length $error]} {
  2877. Finish $token $error
  2878. } elseif {[catch {eof $sock} iseof] || $iseof} {
  2879. Eot $token
  2880. } else {
  2881. CopyStart $sock $token 0
  2882. }
  2883. }
  2884. # http::Eot
  2885. #
  2886. # Called when either:
  2887. # a. An eof condition is detected on the socket.
  2888. # b. The client decides that the response is complete.
  2889. # c. The client detects an inconsistency and aborts the transaction.
  2890. #
  2891. # Does:
  2892. # 1. Set state(status)
  2893. # 2. Reverse any Content-Encoding
  2894. # 3. Convert charset encoding and line ends if necessary
  2895. # 4. Call http::Finish
  2896. #
  2897. # Arguments
  2898. # token The token returned from http::geturl
  2899. # force (previously) optional, has no effect
  2900. # reason - "eof" means premature EOF (not EOF as the natural end of
  2901. # the response)
  2902. # - "" means completion of response, with or without EOF
  2903. # - anything else describes an error confition other than
  2904. # premature EOF.
  2905. #
  2906. # Side Effects
  2907. # Clean up the socket
  2908. proc http::Eot {token {reason {}}} {
  2909. variable $token
  2910. upvar 0 $token state
  2911. if {$reason eq "eof"} {
  2912. # Premature eof.
  2913. set state(status) eof
  2914. set reason {}
  2915. } elseif {$reason ne ""} {
  2916. # Abort the transaction.
  2917. set state(status) $reason
  2918. } else {
  2919. # The response is complete.
  2920. set state(status) ok
  2921. }
  2922. if {[string length $state(body)] > 0} {
  2923. if {[catch {
  2924. foreach coding [ContentEncoding $token] {
  2925. set state(body) [zlib $coding $state(body)]
  2926. }
  2927. } err]} {
  2928. Log "error doing decompression for token $token: $err"
  2929. Finish $token $err
  2930. return
  2931. }
  2932. if {!$state(binary)} {
  2933. # If we are getting text, set the incoming channel's encoding
  2934. # correctly. iso8859-1 is the RFC default, but this could be any
  2935. # IANA charset. However, we only know how to convert what we have
  2936. # encodings for.
  2937. set enc [CharsetToEncoding $state(charset)]
  2938. if {$enc ne "binary"} {
  2939. set state(body) [encoding convertfrom $enc $state(body)]
  2940. }
  2941. # Translate text line endings.
  2942. set state(body) [string map {\r\n \n \r \n} $state(body)]
  2943. }
  2944. }
  2945. Finish $token $reason
  2946. }
  2947. # http::wait --
  2948. #
  2949. # See documentation for details.
  2950. #
  2951. # Arguments:
  2952. # token Connection token.
  2953. #
  2954. # Results:
  2955. # The status after the wait.
  2956. proc http::wait {token} {
  2957. variable $token
  2958. upvar 0 $token state
  2959. if {![info exists state(status)] || $state(status) eq ""} {
  2960. # We must wait on the original variable name, not the upvar alias
  2961. vwait ${token}(status)
  2962. }
  2963. return [status $token]
  2964. }
  2965. # http::formatQuery --
  2966. #
  2967. # See documentation for details. Call http::formatQuery with an even
  2968. # number of arguments, where the first is a name, the second is a value,
  2969. # the third is another name, and so on.
  2970. #
  2971. # Arguments:
  2972. # args A list of name-value pairs.
  2973. #
  2974. # Results:
  2975. # TODO
  2976. proc http::formatQuery {args} {
  2977. if {[llength $args] % 2} {
  2978. return \
  2979. -code error \
  2980. -errorcode [list HTTP BADARGCNT $args] \
  2981. {Incorrect number of arguments, must be an even number.}
  2982. }
  2983. set result ""
  2984. set sep ""
  2985. foreach i $args {
  2986. append result $sep [mapReply $i]
  2987. if {$sep eq "="} {
  2988. set sep &
  2989. } else {
  2990. set sep =
  2991. }
  2992. }
  2993. return $result
  2994. }
  2995. # http::mapReply --
  2996. #
  2997. # Do x-www-urlencoded character mapping
  2998. #
  2999. # Arguments:
  3000. # string The string the needs to be encoded
  3001. #
  3002. # Results:
  3003. # The encoded string
  3004. proc http::mapReply {string} {
  3005. variable http
  3006. variable formMap
  3007. # The spec says: "non-alphanumeric characters are replaced by '%HH'". Use
  3008. # a pre-computed map and [string map] to do the conversion (much faster
  3009. # than [regsub]/[subst]). [Bug 1020491]
  3010. if {$http(-urlencoding) ne ""} {
  3011. set string [encoding convertto $http(-urlencoding) $string]
  3012. return [string map $formMap $string]
  3013. }
  3014. set converted [string map $formMap $string]
  3015. if {[string match "*\[\u0100-\uffff\]*" $converted]} {
  3016. regexp "\[\u0100-\uffff\]" $converted badChar
  3017. # Return this error message for maximum compatibility... :^/
  3018. return -code error \
  3019. "can't read \"formMap($badChar)\": no such element in array"
  3020. }
  3021. return $converted
  3022. }
  3023. interp alias {} http::quoteString {} http::mapReply
  3024. # http::ProxyRequired --
  3025. # Default proxy filter.
  3026. #
  3027. # Arguments:
  3028. # host The destination host
  3029. #
  3030. # Results:
  3031. # The current proxy settings
  3032. proc http::ProxyRequired {host} {
  3033. variable http
  3034. if {[info exists http(-proxyhost)] && [string length $http(-proxyhost)]} {
  3035. if {
  3036. ![info exists http(-proxyport)] ||
  3037. ![string length $http(-proxyport)]
  3038. } {
  3039. set http(-proxyport) 8080
  3040. }
  3041. return [list $http(-proxyhost) $http(-proxyport)]
  3042. }
  3043. }
  3044. # http::CharsetToEncoding --
  3045. #
  3046. # Tries to map a given IANA charset to a tcl encoding. If no encoding
  3047. # can be found, returns binary.
  3048. #
  3049. proc http::CharsetToEncoding {charset} {
  3050. variable encodings
  3051. set charset [string tolower $charset]
  3052. if {[regexp {iso-?8859-([0-9]+)} $charset -> num]} {
  3053. set encoding "iso8859-$num"
  3054. } elseif {[regexp {iso-?2022-(jp|kr)} $charset -> ext]} {
  3055. set encoding "iso2022-$ext"
  3056. } elseif {[regexp {shift[-_]?js} $charset]} {
  3057. set encoding "shiftjis"
  3058. } elseif {[regexp {(?:windows|cp)-?([0-9]+)} $charset -> num]} {
  3059. set encoding "cp$num"
  3060. } elseif {$charset eq "us-ascii"} {
  3061. set encoding "ascii"
  3062. } elseif {[regexp {(?:iso-?)?lat(?:in)?-?([0-9]+)} $charset -> num]} {
  3063. switch -- $num {
  3064. 5 {set encoding "iso8859-9"}
  3065. 1 - 2 - 3 {
  3066. set encoding "iso8859-$num"
  3067. }
  3068. }
  3069. } else {
  3070. # other charset, like euc-xx, utf-8,... may directly map to encoding
  3071. set encoding $charset
  3072. }
  3073. set idx [lsearch -exact $encodings $encoding]
  3074. if {$idx >= 0} {
  3075. return $encoding
  3076. } else {
  3077. return "binary"
  3078. }
  3079. }
  3080. # Return the list of content-encoding transformations we need to do in order.
  3081. proc http::ContentEncoding {token} {
  3082. upvar 0 $token state
  3083. set r {}
  3084. if {[info exists state(coding)]} {
  3085. foreach coding [split $state(coding) ,] {
  3086. switch -exact -- $coding {
  3087. deflate { lappend r inflate }
  3088. gzip - x-gzip { lappend r gunzip }
  3089. compress - x-compress { lappend r decompress }
  3090. identity {}
  3091. default {
  3092. return -code error "unsupported content-encoding \"$coding\""
  3093. }
  3094. }
  3095. }
  3096. }
  3097. return $r
  3098. }
  3099. proc http::ReceiveChunked {chan command} {
  3100. set data ""
  3101. set size -1
  3102. yield
  3103. while {1} {
  3104. chan configure $chan -translation {crlf binary}
  3105. while {[gets $chan line] < 1} { yield }
  3106. chan configure $chan -translation {binary binary}
  3107. if {[scan $line %x size] != 1} {
  3108. return -code error "invalid size: \"$line\""
  3109. }
  3110. set chunk ""
  3111. while {$size && ![chan eof $chan]} {
  3112. set part [chan read $chan $size]
  3113. incr size -[string length $part]
  3114. append chunk $part
  3115. }
  3116. if {[catch {
  3117. uplevel #0 [linsert $command end $chunk]
  3118. }]} {
  3119. http::Log "Error in callback: $::errorInfo"
  3120. }
  3121. if {[string length $chunk] == 0} {
  3122. # channel might have been closed in the callback
  3123. catch {chan event $chan readable {}}
  3124. return
  3125. }
  3126. }
  3127. }
  3128. proc http::make-transformation-chunked {chan command} {
  3129. coroutine [namespace current]::dechunk$chan ::http::ReceiveChunked $chan $command
  3130. chan event $chan readable [namespace current]::dechunk$chan
  3131. }
  3132. # Local variables:
  3133. # indent-tabs-mode: t
  3134. # End: