http-2.9.5.tm 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506
  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.5
  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 >= 0} {
  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 keep-alive
  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. -headers dict
  673. }
  674. set state(charset) $defaultCharset
  675. set options {
  676. -binary -blocksize -channel -command -handler -headers -keepalive
  677. -method -myaddr -progress -protocol -query -queryblocksize
  678. -querychannel -queryprogress -strict -timeout -type -validate
  679. }
  680. set usage [join [lsort $options] ", "]
  681. set options [string map {- ""} $options]
  682. set pat ^-(?:[join $options |])$
  683. foreach {flag value} $args {
  684. if {[regexp -- $pat $flag]} {
  685. # Validate numbers
  686. if {($flag eq "-headers") ? [catch {dict size $value}] :
  687. ([info exists type($flag)] && ![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. # We cannot handle chunked encodings with -handler, so force HTTP/1.0
  892. # until we can manage this.
  893. if {[info exists state(-handler)]} {
  894. set state(-protocol) 1.0
  895. }
  896. # RFC 7320 A.1 - HTTP/1.0 Keep-Alive is problematic. We do not support it.
  897. if {$state(-protocol) eq "1.0"} {
  898. set state(connection) close
  899. set state(-keepalive) 0
  900. }
  901. # See if we are supposed to use a previously opened channel.
  902. # - In principle, ANY call to http::geturl could use a previously opened
  903. # channel if it is available - the "Connection: keep-alive" header is a
  904. # request to leave the channel open AFTER completion of this call.
  905. # - In fact, we try to use an existing channel only if -keepalive 1 -- this
  906. # means that at most one channel is left open for each value of
  907. # $state(socketinfo). This property simplifies the mapping of open
  908. # channels.
  909. set reusing 0
  910. set alreadyQueued 0
  911. if {$state(-keepalive)} {
  912. variable socketMapping
  913. variable socketRdState
  914. variable socketWrState
  915. variable socketRdQueue
  916. variable socketWrQueue
  917. variable socketClosing
  918. variable socketPlayCmd
  919. if {[info exists socketMapping($state(socketinfo))]} {
  920. # - If the connection is idle, it has a "fileevent readable" binding
  921. # to http::CheckEof, in case the server times out and half-closes
  922. # the socket (http::CheckEof closes the other half).
  923. # - We leave this binding in place until just before the last
  924. # puts+flush in http::Connected (GET/HEAD) or http::Write (POST),
  925. # after which the HTTP response might be generated.
  926. if { [info exists socketClosing($state(socketinfo))]
  927. && $socketClosing($state(socketinfo))
  928. } {
  929. # socketClosing(*) is set because the server has sent a
  930. # "Connection: close" header.
  931. # Do not use the persistent socket again.
  932. # Since we have only one persistent socket per server, and the
  933. # old socket is not yet dead, add the request to the write queue
  934. # of the dying socket, which will be replayed by ReplayIfClose.
  935. # Also add it to socketWrQueue(*) which is used only if an error
  936. # causes a call to Finish.
  937. set reusing 1
  938. set sock $socketMapping($state(socketinfo))
  939. Log "reusing socket $sock for $state(socketinfo) - token $token"
  940. set alreadyQueued 1
  941. lassign $socketPlayCmd($state(socketinfo)) com0 com1 com2 com3
  942. lappend com3 $token
  943. set socketPlayCmd($state(socketinfo)) [list $com0 $com1 $com2 $com3]
  944. lappend socketWrQueue($state(socketinfo)) $token
  945. } elseif {[catch {fconfigure $socketMapping($state(socketinfo))}]} {
  946. # FIXME Is it still possible for this code to be executed? If
  947. # so, this could be another place to call TestForReplay,
  948. # rather than discarding the queued transactions.
  949. Log "WARNING: socket for $state(socketinfo) was closed\
  950. - token $token"
  951. Log "WARNING - if testing, pay special attention to this\
  952. case (GH) which is seldom executed - token $token"
  953. # This will call CancelReadPipeline, CancelWritePipeline, and
  954. # cancel any queued requests, responses.
  955. Unset $state(socketinfo)
  956. } else {
  957. # Use the persistent socket.
  958. # The socket may not be ready to write: an earlier request might
  959. # still be still writing (in the pipelined case) or
  960. # writing/reading (in the nonpipeline case). This possibility
  961. # is handled by socketWrQueue later in this command.
  962. set reusing 1
  963. set sock $socketMapping($state(socketinfo))
  964. Log "reusing socket $sock for $state(socketinfo) - token $token"
  965. }
  966. # Do not automatically close the connection socket.
  967. set state(connection) keep-alive
  968. }
  969. }
  970. if {$reusing} {
  971. # Define state(tmpState) and state(tmpOpenCmd) for use
  972. # by http::ReplayIfDead if the persistent connection has died.
  973. set state(tmpState) [array get state]
  974. # Pass -myaddr directly to the socket command
  975. if {[info exists state(-myaddr)]} {
  976. lappend sockopts -myaddr $state(-myaddr)
  977. }
  978. set state(tmpOpenCmd) [list {*}$defcmd {*}$sockopts {*}$targetAddr]
  979. }
  980. set state(reusing) $reusing
  981. # Excluding ReplayIfDead and the decision whether to call it, there are four
  982. # places outside http::geturl where state(reusing) is used:
  983. # - Connected - if reusing and not pipelined, start the state(-timeout)
  984. # timeout (when writing).
  985. # - DoneRequest - if reusing and pipelined, send the next pipelined write
  986. # - Event - if reusing and pipelined, start the state(-timeout)
  987. # timeout (when reading).
  988. # - Event - if (not reusing) and pipelined, send the next pipelined
  989. # write
  990. # See comments above re the start of this timeout in other cases.
  991. if {(!$state(reusing)) && ($state(-timeout) > 0)} {
  992. set state(after) [after $state(-timeout) \
  993. [list http::reset $token timeout]]
  994. }
  995. if {![info exists sock]} {
  996. # Pass -myaddr directly to the socket command
  997. if {[info exists state(-myaddr)]} {
  998. lappend sockopts -myaddr $state(-myaddr)
  999. }
  1000. set pre [clock milliseconds]
  1001. ##Log pre socket opened, - token $token
  1002. ##Log [concat $defcmd $sockopts $targetAddr] - token $token
  1003. if {[catch {eval $defcmd $sockopts $targetAddr} sock errdict]} {
  1004. # Something went wrong while trying to establish the connection.
  1005. # Clean up after events and such, but DON'T call the command
  1006. # callback (if available) because we're going to throw an
  1007. # exception from here instead.
  1008. set state(sock) NONE
  1009. Finish $token $sock 1
  1010. cleanup $token
  1011. dict unset errdict -level
  1012. return -options $errdict $sock
  1013. } else {
  1014. # Initialisation of a new socket.
  1015. ##Log post socket opened, - token $token
  1016. ##Log socket opened, now fconfigure - token $token
  1017. set delay [expr {[clock milliseconds] - $pre}]
  1018. if {$delay > 3000} {
  1019. Log socket delay $delay - token $token
  1020. }
  1021. fconfigure $sock -translation {auto crlf} \
  1022. -buffersize $state(-blocksize)
  1023. ##Log socket opened, DONE fconfigure - token $token
  1024. }
  1025. }
  1026. # Command [socket] is called with -async, but takes 5s to 5.1s to return,
  1027. # with probability of order 1 in 10,000. This may be a bizarre scheduling
  1028. # issue with my (KJN's) system (Fedora Linux).
  1029. # This does not cause a problem (unless the request times out when this
  1030. # command returns).
  1031. set state(sock) $sock
  1032. Log "Using $sock for $state(socketinfo) - token $token" \
  1033. [expr {$state(-keepalive)?"keepalive":""}]
  1034. if { $state(-keepalive)
  1035. && (![info exists socketMapping($state(socketinfo))])
  1036. } {
  1037. # Freshly-opened socket that we would like to become persistent.
  1038. set socketMapping($state(socketinfo)) $sock
  1039. if {![info exists socketRdState($state(socketinfo))]} {
  1040. set socketRdState($state(socketinfo)) {}
  1041. set varName ::http::socketRdState($state(socketinfo))
  1042. trace add variable $varName unset ::http::CancelReadPipeline
  1043. }
  1044. if {![info exists socketWrState($state(socketinfo))]} {
  1045. set socketWrState($state(socketinfo)) {}
  1046. set varName ::http::socketWrState($state(socketinfo))
  1047. trace add variable $varName unset ::http::CancelWritePipeline
  1048. }
  1049. if {$state(-pipeline)} {
  1050. #Log new, init for pipelined, GRANT write access to $token in geturl
  1051. # Also grant premature read access to the socket. This is OK.
  1052. set socketRdState($state(socketinfo)) $token
  1053. set socketWrState($state(socketinfo)) $token
  1054. } else {
  1055. # socketWrState is not used by this non-pipelined transaction.
  1056. # We cannot leave it as "Wready" because the next call to
  1057. # http::geturl with a pipelined transaction would conclude that the
  1058. # socket is available for writing.
  1059. #Log new, init for nonpipeline, GRANT r/w access to $token in geturl
  1060. set socketRdState($state(socketinfo)) $token
  1061. set socketWrState($state(socketinfo)) $token
  1062. }
  1063. set socketRdQueue($state(socketinfo)) {}
  1064. set socketWrQueue($state(socketinfo)) {}
  1065. set socketClosing($state(socketinfo)) 0
  1066. set socketPlayCmd($state(socketinfo)) {ReplayIfClose Wready {} {}}
  1067. }
  1068. if {![info exists phost]} {
  1069. set phost ""
  1070. }
  1071. if {$reusing} {
  1072. # For use by http::ReplayIfDead if the persistent connection has died.
  1073. # Also used by NextPipelinedWrite.
  1074. set state(tmpConnArgs) [list $proto $phost $srvurl]
  1075. }
  1076. # The element socketWrState($connId) has a value which is either the name of
  1077. # the token that is permitted to write to the socket, or "Wready" if no
  1078. # token is permitted to write.
  1079. #
  1080. # The code that sets the value to Wready immediately calls
  1081. # http::NextPipelinedWrite, which examines socketWrQueue($connId) and
  1082. # processes the next request in the queue, if there is one. The value
  1083. # Wready is not found when the interpreter is in the event loop unless the
  1084. # socket is idle.
  1085. #
  1086. # The element socketRdState($connId) has a value which is either the name of
  1087. # the token that is permitted to read from the socket, or "Rready" if no
  1088. # token is permitted to read.
  1089. #
  1090. # The code that sets the value to Rready then examines
  1091. # socketRdQueue($connId) and processes the next request in the queue, if
  1092. # there is one. The value Rready is not found when the interpreter is in
  1093. # the event loop unless the socket is idle.
  1094. if {$alreadyQueued} {
  1095. # A write may or may not be in progress. There is no need to set
  1096. # socketWrState to prevent another call stealing write access - all
  1097. # subsequent calls on this socket will come here because the socket
  1098. # will close after the current read, and its
  1099. # socketClosing($connId) is 1.
  1100. ##Log "HTTP request for token $token is queued"
  1101. } elseif { $reusing
  1102. && $state(-pipeline)
  1103. && ($socketWrState($state(socketinfo)) ne "Wready")
  1104. } {
  1105. ##Log "HTTP request for token $token is queued for pipelined use"
  1106. lappend socketWrQueue($state(socketinfo)) $token
  1107. } elseif { $reusing
  1108. && (!$state(-pipeline))
  1109. && ($socketWrState($state(socketinfo)) ne "Wready")
  1110. } {
  1111. # A write is queued or in progress. Lappend to the write queue.
  1112. ##Log "HTTP request for token $token is queued for nonpipeline use"
  1113. lappend socketWrQueue($state(socketinfo)) $token
  1114. } elseif { $reusing
  1115. && (!$state(-pipeline))
  1116. && ($socketWrState($state(socketinfo)) eq "Wready")
  1117. && ($socketRdState($state(socketinfo)) ne "Rready")
  1118. } {
  1119. # A read is queued or in progress, but not a write. Cannot start the
  1120. # nonpipeline transaction, but must set socketWrState to prevent a
  1121. # pipelined request jumping the queue.
  1122. ##Log "HTTP request for token $token is queued for nonpipeline use"
  1123. #Log re-use nonpipeline, GRANT delayed write access to $token in geturl
  1124. set socketWrState($state(socketinfo)) peNding
  1125. lappend socketWrQueue($state(socketinfo)) $token
  1126. } else {
  1127. if {$reusing && $state(-pipeline)} {
  1128. #Log re-use pipelined, GRANT write access to $token in geturl
  1129. set socketWrState($state(socketinfo)) $token
  1130. } elseif {$reusing} {
  1131. # Cf tests above - both are ready.
  1132. #Log re-use nonpipeline, GRANT r/w access to $token in geturl
  1133. set socketRdState($state(socketinfo)) $token
  1134. set socketWrState($state(socketinfo)) $token
  1135. }
  1136. # All (!$reusing) cases come here, and also some $reusing cases if the
  1137. # connection is ready.
  1138. #Log ---- $state(socketinfo) << conn to $token for HTTP request (a)
  1139. # Connect does its own fconfigure.
  1140. fileevent $sock writable \
  1141. [list http::Connect $token $proto $phost $srvurl]
  1142. }
  1143. # Wait for the connection to complete.
  1144. if {![info exists state(-command)]} {
  1145. # geturl does EVERYTHING asynchronously, so if the user
  1146. # calls it synchronously, we just do a wait here.
  1147. http::wait $token
  1148. if {![info exists state]} {
  1149. # If we timed out then Finish has been called and the users
  1150. # command callback may have cleaned up the token. If so we end up
  1151. # here with nothing left to do.
  1152. return $token
  1153. } elseif {$state(status) eq "error"} {
  1154. # Something went wrong while trying to establish the connection.
  1155. # Clean up after events and such, but DON'T call the command
  1156. # callback (if available) because we're going to throw an
  1157. # exception from here instead.
  1158. set err [lindex $state(error) 0]
  1159. cleanup $token
  1160. return -code error $err
  1161. }
  1162. }
  1163. ##Log Leaving http::geturl - token $token
  1164. return $token
  1165. }
  1166. # http::Connected --
  1167. #
  1168. # Callback used when the connection to the HTTP server is actually
  1169. # established.
  1170. #
  1171. # Arguments:
  1172. # token State token.
  1173. # proto What protocol (http, https, etc.) was used to connect.
  1174. # phost Are we using keep-alive? Non-empty if yes.
  1175. # srvurl Service-local URL that we're requesting
  1176. # Results:
  1177. # None.
  1178. proc http::Connected {token proto phost srvurl} {
  1179. variable http
  1180. variable urlTypes
  1181. variable socketMapping
  1182. variable socketRdState
  1183. variable socketWrState
  1184. variable socketRdQueue
  1185. variable socketWrQueue
  1186. variable socketClosing
  1187. variable socketPlayCmd
  1188. variable $token
  1189. upvar 0 $token state
  1190. set tk [namespace tail $token]
  1191. if {$state(reusing) && (!$state(-pipeline)) && ($state(-timeout) > 0)} {
  1192. set state(after) [after $state(-timeout) \
  1193. [list http::reset $token timeout]]
  1194. }
  1195. # Set back the variables needed here.
  1196. set sock $state(sock)
  1197. set isQueryChannel [info exists state(-querychannel)]
  1198. set isQuery [info exists state(-query)]
  1199. set host [lindex [split $state(socketinfo) :] 0]
  1200. set port [lindex [split $state(socketinfo) :] 1]
  1201. set lower [string tolower $proto]
  1202. set defport [lindex $urlTypes($lower) 0]
  1203. # Send data in cr-lf format, but accept any line terminators.
  1204. # Initialisation to {auto *} now done in geturl, KeepSocket and DoneRequest.
  1205. # We are concerned here with the request (write) not the response (read).
  1206. lassign [fconfigure $sock -translation] trRead trWrite
  1207. fconfigure $sock -translation [list $trRead crlf] \
  1208. -buffersize $state(-blocksize)
  1209. # The following is disallowed in safe interpreters, but the socket is
  1210. # already in non-blocking mode in that case.
  1211. catch {fconfigure $sock -blocking off}
  1212. set how GET
  1213. if {$isQuery} {
  1214. set state(querylength) [string length $state(-query)]
  1215. if {$state(querylength) > 0} {
  1216. set how POST
  1217. set contDone 0
  1218. } else {
  1219. # There's no query data.
  1220. unset state(-query)
  1221. set isQuery 0
  1222. }
  1223. } elseif {$state(-validate)} {
  1224. set how HEAD
  1225. } elseif {$isQueryChannel} {
  1226. set how POST
  1227. # The query channel must be blocking for the async Write to
  1228. # work properly.
  1229. fconfigure $state(-querychannel) -blocking 1 -translation binary
  1230. set contDone 0
  1231. }
  1232. if {[info exists state(-method)] && ($state(-method) ne "")} {
  1233. set how $state(-method)
  1234. }
  1235. set accept_types_seen 0
  1236. Log ^B$tk begin sending request - token $token
  1237. if {[catch {
  1238. set state(method) $how
  1239. puts $sock "$how $srvurl HTTP/$state(-protocol)"
  1240. if {[dict exists $state(-headers) Host]} {
  1241. # Allow Host spoofing. [Bug 928154]
  1242. puts $sock "Host: [dict get $state(-headers) Host]"
  1243. } elseif {$port == $defport} {
  1244. # Don't add port in this case, to handle broken servers. [Bug
  1245. # #504508]
  1246. puts $sock "Host: $host"
  1247. } else {
  1248. puts $sock "Host: $host:$port"
  1249. }
  1250. puts $sock "User-Agent: $http(-useragent)"
  1251. if {($state(-protocol) > 1.0) && $state(-keepalive)} {
  1252. # Send this header, because a 1.1 server is not compelled to treat
  1253. # this as the default.
  1254. puts $sock "Connection: keep-alive"
  1255. }
  1256. if {($state(-protocol) > 1.0) && !$state(-keepalive)} {
  1257. puts $sock "Connection: close" ;# RFC2616 sec 8.1.2.1
  1258. }
  1259. if {($state(-protocol) < 1.1)} {
  1260. # RFC7230 A.1
  1261. # Some server implementations of HTTP/1.0 have a faulty
  1262. # implementation of RFC 2068 Keep-Alive.
  1263. # Don't leave this to chance.
  1264. # For HTTP/1.0 we have already "set state(connection) close"
  1265. # and "state(-keepalive) 0".
  1266. puts $sock "Connection: close"
  1267. }
  1268. # RFC7230 A.1 - "clients are encouraged not to send the
  1269. # Proxy-Connection header field in any requests"
  1270. set accept_encoding_seen 0
  1271. set content_type_seen 0
  1272. dict for {key value} $state(-headers) {
  1273. set value [string map [list \n "" \r ""] $value]
  1274. set key [string map {" " -} [string trim $key]]
  1275. if {[string equal -nocase $key "host"]} {
  1276. continue
  1277. }
  1278. if {[string equal -nocase $key "accept-encoding"]} {
  1279. set accept_encoding_seen 1
  1280. }
  1281. if {[string equal -nocase $key "accept"]} {
  1282. set accept_types_seen 1
  1283. }
  1284. if {[string equal -nocase $key "content-type"]} {
  1285. set content_type_seen 1
  1286. }
  1287. if {[string equal -nocase $key "content-length"]} {
  1288. set contDone 1
  1289. set state(querylength) $value
  1290. }
  1291. if {[string length $key]} {
  1292. puts $sock "$key: $value"
  1293. }
  1294. }
  1295. # Allow overriding the Accept header on a per-connection basis. Useful
  1296. # for working with REST services. [Bug c11a51c482]
  1297. if {!$accept_types_seen} {
  1298. puts $sock "Accept: $state(accept-types)"
  1299. }
  1300. if { (!$accept_encoding_seen)
  1301. && (![info exists state(-handler)])
  1302. && $http(-zip)
  1303. } {
  1304. puts $sock "Accept-Encoding: gzip,deflate,compress"
  1305. }
  1306. if {$isQueryChannel && ($state(querylength) == 0)} {
  1307. # Try to determine size of data in channel. If we cannot seek, the
  1308. # surrounding catch will trap us
  1309. set start [tell $state(-querychannel)]
  1310. seek $state(-querychannel) 0 end
  1311. set state(querylength) \
  1312. [expr {[tell $state(-querychannel)] - $start}]
  1313. seek $state(-querychannel) $start
  1314. }
  1315. # Flush the request header and set up the fileevent that will either
  1316. # push the POST data or read the response.
  1317. #
  1318. # fileevent note:
  1319. #
  1320. # It is possible to have both the read and write fileevents active at
  1321. # this point. The only scenario it seems to affect is a server that
  1322. # closes the connection without reading the POST data. (e.g., early
  1323. # versions TclHttpd in various error cases). Depending on the
  1324. # platform, the client may or may not be able to get the response from
  1325. # the server because of the error it will get trying to write the post
  1326. # data. Having both fileevents active changes the timing and the
  1327. # behavior, but no two platforms (among Solaris, Linux, and NT) behave
  1328. # the same, and none behave all that well in any case. Servers should
  1329. # always read their POST data if they expect the client to read their
  1330. # response.
  1331. if {$isQuery || $isQueryChannel} {
  1332. # POST method.
  1333. if {!$content_type_seen} {
  1334. puts $sock "Content-Type: $state(-type)"
  1335. }
  1336. if {!$contDone} {
  1337. puts $sock "Content-Length: $state(querylength)"
  1338. }
  1339. puts $sock ""
  1340. flush $sock
  1341. # Flush flushes the error in the https case with a bad handshake:
  1342. # else the socket never becomes writable again, and hangs until
  1343. # timeout (if any).
  1344. lassign [fconfigure $sock -translation] trRead trWrite
  1345. fconfigure $sock -translation [list $trRead binary]
  1346. fileevent $sock writable [list http::Write $token]
  1347. # The http::Write command decides when to make the socket readable,
  1348. # using the same test as the GET/HEAD case below.
  1349. } else {
  1350. # GET or HEAD method.
  1351. if { (![catch {fileevent $sock readable} binding])
  1352. && ($binding eq [list http::CheckEof $sock])
  1353. } {
  1354. # Remove the "fileevent readable" binding of an idle persistent
  1355. # socket to http::CheckEof. We can no longer treat bytes
  1356. # received as junk. The server might still time out and
  1357. # half-close the socket if it has not yet received the first
  1358. # "puts".
  1359. fileevent $sock readable {}
  1360. }
  1361. puts $sock ""
  1362. flush $sock
  1363. Log ^C$tk end sending request - token $token
  1364. # End of writing (GET/HEAD methods). The request has been sent.
  1365. DoneRequest $token
  1366. }
  1367. } err]} {
  1368. # The socket probably was never connected, OR the connection dropped
  1369. # later, OR https handshake error, which may be discovered as late as
  1370. # the "flush" command above...
  1371. Log "WARNING - if testing, pay special attention to this\
  1372. case (GI) which is seldom executed - token $token"
  1373. if {[info exists state(reusing)] && $state(reusing)} {
  1374. # The socket was closed at the server end, and closed at
  1375. # this end by http::CheckEof.
  1376. if {[TestForReplay $token write $err a]} {
  1377. return
  1378. } else {
  1379. Finish $token {failed to re-use socket}
  1380. }
  1381. # else:
  1382. # This is NOT a persistent socket that has been closed since its
  1383. # last use.
  1384. # If any other requests are in flight or pipelined/queued, they will
  1385. # be discarded.
  1386. } elseif {$state(status) eq ""} {
  1387. # ...https handshake errors come here.
  1388. set msg [registerError $sock]
  1389. registerError $sock {}
  1390. if {$msg eq {}} {
  1391. set msg {failed to use socket}
  1392. }
  1393. Finish $token $msg
  1394. } elseif {$state(status) ne "error"} {
  1395. Finish $token $err
  1396. }
  1397. }
  1398. }
  1399. # http::registerError
  1400. #
  1401. # Called (for example when processing TclTLS activity) to register
  1402. # an error for a connection on a specific socket. This helps
  1403. # http::Connected to deliver meaningful error messages, e.g. when a TLS
  1404. # certificate fails verification.
  1405. #
  1406. # Usage: http::registerError socket ?newValue?
  1407. #
  1408. # "set" semantics, except that a "get" (a call without a new value) for a
  1409. # non-existent socket returns {}, not an error.
  1410. proc http::registerError {sock args} {
  1411. variable registeredErrors
  1412. if { ([llength $args] == 0)
  1413. && (![info exists registeredErrors($sock)])
  1414. } {
  1415. return
  1416. } elseif { ([llength $args] == 1)
  1417. && ([lindex $args 0] eq {})
  1418. } {
  1419. unset -nocomplain registeredErrors($sock)
  1420. return
  1421. }
  1422. set registeredErrors($sock) {*}$args
  1423. }
  1424. # http::DoneRequest --
  1425. #
  1426. # Command called when a request has been sent. It will arrange the
  1427. # next request and/or response as appropriate.
  1428. #
  1429. # If this command is called when $socketClosing(*), the request $token
  1430. # that calls it must be pipelined and destined to fail.
  1431. proc http::DoneRequest {token} {
  1432. variable http
  1433. variable socketMapping
  1434. variable socketRdState
  1435. variable socketWrState
  1436. variable socketRdQueue
  1437. variable socketWrQueue
  1438. variable socketClosing
  1439. variable socketPlayCmd
  1440. variable $token
  1441. upvar 0 $token state
  1442. set tk [namespace tail $token]
  1443. set sock $state(sock)
  1444. # If pipelined, connect the next HTTP request to the socket.
  1445. if {$state(reusing) && $state(-pipeline)} {
  1446. # Enable next token (if any) to write.
  1447. # The value "Wready" is set only here, and
  1448. # in http::Event after reading the response-headers of a
  1449. # non-reusing transaction.
  1450. # Previous value is $token. It cannot be pending.
  1451. set socketWrState($state(socketinfo)) Wready
  1452. # Now ready to write the next pipelined request (if any).
  1453. http::NextPipelinedWrite $token
  1454. } else {
  1455. # If pipelined, this is the first transaction on this socket. We wait
  1456. # for the response headers to discover whether the connection is
  1457. # persistent. (If this is not done and the connection is not
  1458. # persistent, we SHOULD retry and then MUST NOT pipeline before knowing
  1459. # that we have a persistent connection
  1460. # (rfc2616 8.1.2.2)).
  1461. }
  1462. # Connect to receive the response, unless the socket is pipelined
  1463. # and another response is being sent.
  1464. # This code block is separate from the code below because there are
  1465. # cases where socketRdState already has the value $token.
  1466. if { $state(-keepalive)
  1467. && $state(-pipeline)
  1468. && [info exists socketRdState($state(socketinfo))]
  1469. && ($socketRdState($state(socketinfo)) eq "Rready")
  1470. } {
  1471. #Log pipelined, GRANT read access to $token in Connected
  1472. set socketRdState($state(socketinfo)) $token
  1473. }
  1474. if { $state(-keepalive)
  1475. && $state(-pipeline)
  1476. && [info exists socketRdState($state(socketinfo))]
  1477. && ($socketRdState($state(socketinfo)) ne $token)
  1478. } {
  1479. # Do not read from the socket until it is ready.
  1480. ##Log "HTTP response for token $token is queued for pipelined use"
  1481. # If $socketClosing(*), then the caller will be a pipelined write and
  1482. # execution will come here.
  1483. # This token has already been recorded as "in flight" for writing.
  1484. # When the socket is closed, the read queue will be cleared in
  1485. # CloseQueuedQueries and so the "lappend" here has no effect.
  1486. lappend socketRdQueue($state(socketinfo)) $token
  1487. } else {
  1488. # In the pipelined case, connection for reading depends on the
  1489. # value of socketRdState.
  1490. # In the nonpipeline case, connection for reading always occurs.
  1491. ReceiveResponse $token
  1492. }
  1493. }
  1494. # http::ReceiveResponse
  1495. #
  1496. # Connects token to its socket for reading.
  1497. proc http::ReceiveResponse {token} {
  1498. variable $token
  1499. upvar 0 $token state
  1500. set tk [namespace tail $token]
  1501. set sock $state(sock)
  1502. #Log ---- $state(socketinfo) >> conn to $token for HTTP response
  1503. lassign [fconfigure $sock -translation] trRead trWrite
  1504. fconfigure $sock -translation [list auto $trWrite] \
  1505. -buffersize $state(-blocksize)
  1506. Log ^D$tk begin receiving response - token $token
  1507. coroutine ${token}EventCoroutine http::Event $sock $token
  1508. if {[info exists state(-handler)] || [info exists state(-progress)]} {
  1509. fileevent $sock readable [list http::EventGateway $sock $token]
  1510. } else {
  1511. fileevent $sock readable ${token}EventCoroutine
  1512. }
  1513. return
  1514. }
  1515. # http::EventGateway
  1516. #
  1517. # Bug [c2dc1da315].
  1518. # - Recursive launch of the coroutine can occur if a -handler or -progress
  1519. # callback is used, and the callback command enters the event loop.
  1520. # - To prevent this, the fileevent "binding" is disabled while the
  1521. # coroutine is in flight.
  1522. # - If a recursive call occurs despite these precautions, it is not
  1523. # trapped and discarded here, because it is better to report it as a
  1524. # bug.
  1525. # - Although this solution is believed to be sufficiently general, it is
  1526. # used only if -handler or -progress is specified. In other cases,
  1527. # the coroutine is called directly.
  1528. proc http::EventGateway {sock token} {
  1529. variable $token
  1530. upvar 0 $token state
  1531. fileevent $sock readable {}
  1532. catch {${token}EventCoroutine} res opts
  1533. if {[info commands ${token}EventCoroutine] ne {}} {
  1534. # The coroutine can be deleted by completion (a non-yield return), by
  1535. # http::Finish (when there is a premature end to the transaction), by
  1536. # http::reset or http::cleanup, or if the caller set option -channel
  1537. # but not option -handler: in the last case reading from the socket is
  1538. # now managed by commands ::http::Copy*, http::ReceiveChunked, and
  1539. # http::make-transformation-chunked.
  1540. #
  1541. # Catch in case the coroutine has closed the socket.
  1542. catch {fileevent $sock readable [list http::EventGateway $sock $token]}
  1543. }
  1544. # If there was an error, re-throw it.
  1545. return -options $opts $res
  1546. }
  1547. # http::NextPipelinedWrite
  1548. #
  1549. # - Connecting a socket to a token for writing is done by this command and by
  1550. # command KeepSocket.
  1551. # - If another request has a pipelined write scheduled for $token's socket,
  1552. # and if the socket is ready to accept it, connect the write and update
  1553. # the queue accordingly.
  1554. # - This command is called from http::DoneRequest and http::Event,
  1555. # IF $state(-pipeline) AND (the current transfer has reached the point at
  1556. # which the socket is ready for the next request to be written).
  1557. # - This command is called when a token has write access and is pipelined and
  1558. # keep-alive, and sets socketWrState to Wready.
  1559. # - The command need not consider the case where socketWrState is set to a token
  1560. # that does not yet have write access. Such a token is waiting for Rready,
  1561. # and the assignment of the connection to the token will be done elsewhere (in
  1562. # http::KeepSocket).
  1563. # - This command cannot be called after socketWrState has been set to a
  1564. # "pending" token value (that is then overwritten by the caller), because that
  1565. # value is set by this command when it is called by an earlier token when it
  1566. # relinquishes its write access, and the pending token is always the next in
  1567. # line to write.
  1568. proc http::NextPipelinedWrite {token} {
  1569. variable http
  1570. variable socketRdState
  1571. variable socketWrState
  1572. variable socketWrQueue
  1573. variable socketClosing
  1574. variable $token
  1575. upvar 0 $token state
  1576. set connId $state(socketinfo)
  1577. if { [info exists socketClosing($connId)]
  1578. && $socketClosing($connId)
  1579. } {
  1580. # socketClosing(*) is set because the server has sent a
  1581. # "Connection: close" header.
  1582. # Behave as if the queues are empty - so do nothing.
  1583. } elseif { $state(-pipeline)
  1584. && [info exists socketWrState($connId)]
  1585. && ($socketWrState($connId) eq "Wready")
  1586. && [info exists socketWrQueue($connId)]
  1587. && [llength $socketWrQueue($connId)]
  1588. && ([set token2 [lindex $socketWrQueue($connId) 0]
  1589. set ${token2}(-pipeline)
  1590. ]
  1591. )
  1592. } {
  1593. # - The usual case for a pipelined connection, ready for a new request.
  1594. #Log pipelined, GRANT write access to $token2 in NextPipelinedWrite
  1595. set conn [set ${token2}(tmpConnArgs)]
  1596. set socketWrState($connId) $token2
  1597. set socketWrQueue($connId) [lrange $socketWrQueue($connId) 1 end]
  1598. # Connect does its own fconfigure.
  1599. fileevent $state(sock) writable [list http::Connect $token2 {*}$conn]
  1600. #Log ---- $connId << conn to $token2 for HTTP request (b)
  1601. # In the tests below, the next request will be nonpipeline.
  1602. } elseif { $state(-pipeline)
  1603. && [info exists socketWrState($connId)]
  1604. && ($socketWrState($connId) eq "Wready")
  1605. && [info exists socketWrQueue($connId)]
  1606. && [llength $socketWrQueue($connId)]
  1607. && (![ set token3 [lindex $socketWrQueue($connId) 0]
  1608. set ${token3}(-pipeline)
  1609. ]
  1610. )
  1611. && [info exists socketRdState($connId)]
  1612. && ($socketRdState($connId) eq "Rready")
  1613. } {
  1614. # The case in which the next request will be non-pipelined, and the read
  1615. # and write queues is ready: which is the condition for a non-pipelined
  1616. # write.
  1617. variable $token3
  1618. upvar 0 $token3 state3
  1619. set conn [set ${token3}(tmpConnArgs)]
  1620. #Log nonpipeline, GRANT r/w access to $token3 in NextPipelinedWrite
  1621. set socketRdState($connId) $token3
  1622. set socketWrState($connId) $token3
  1623. set socketWrQueue($connId) [lrange $socketWrQueue($connId) 1 end]
  1624. # Connect does its own fconfigure.
  1625. fileevent $state(sock) writable [list http::Connect $token3 {*}$conn]
  1626. #Log ---- $state(sock) << conn to $token3 for HTTP request (c)
  1627. } elseif { $state(-pipeline)
  1628. && [info exists socketWrState($connId)]
  1629. && ($socketWrState($connId) eq "Wready")
  1630. && [info exists socketWrQueue($connId)]
  1631. && [llength $socketWrQueue($connId)]
  1632. && (![set token2 [lindex $socketWrQueue($connId) 0]
  1633. set ${token2}(-pipeline)
  1634. ]
  1635. )
  1636. } {
  1637. # - The case in which the next request will be non-pipelined, but the
  1638. # read queue is NOT ready.
  1639. # - A read is queued or in progress, but not a write. Cannot start the
  1640. # nonpipeline transaction, but must set socketWrState to prevent a new
  1641. # pipelined request (in http::geturl) jumping the queue.
  1642. # - Because socketWrState($connId) is not set to Wready, the assignment
  1643. # of the connection to $token2 will be done elsewhere - by command
  1644. # http::KeepSocket when $socketRdState($connId) is set to "Rready".
  1645. #Log re-use nonpipeline, GRANT delayed write access to $token in NextP..
  1646. set socketWrState($connId) peNding
  1647. }
  1648. }
  1649. # http::CancelReadPipeline
  1650. #
  1651. # Cancel pipelined responses on a closing "Keep-Alive" socket.
  1652. #
  1653. # - Called by a variable trace on "unset socketRdState($connId)".
  1654. # - The variable relates to a Keep-Alive socket, which has been closed.
  1655. # - Cancels all pipelined responses. The requests have been sent,
  1656. # the responses have not yet been received.
  1657. # - This is a hard cancel that ends each transaction with error status,
  1658. # and closes the connection. Do not use it if you want to replay failed
  1659. # transactions.
  1660. # - N.B. Always delete ::http::socketRdState($connId) before deleting
  1661. # ::http::socketRdQueue($connId), or this command will do nothing.
  1662. #
  1663. # Arguments
  1664. # As for a trace command on a variable.
  1665. proc http::CancelReadPipeline {name1 connId op} {
  1666. variable socketRdQueue
  1667. ##Log CancelReadPipeline $name1 $connId $op
  1668. if {[info exists socketRdQueue($connId)]} {
  1669. set msg {the connection was closed by CancelReadPipeline}
  1670. foreach token $socketRdQueue($connId) {
  1671. set tk [namespace tail $token]
  1672. Log ^X$tk end of response "($msg)" - token $token
  1673. set ${token}(status) eof
  1674. Finish $token ;#$msg
  1675. }
  1676. set socketRdQueue($connId) {}
  1677. }
  1678. }
  1679. # http::CancelWritePipeline
  1680. #
  1681. # Cancel queued events on a closing "Keep-Alive" socket.
  1682. #
  1683. # - Called by a variable trace on "unset socketWrState($connId)".
  1684. # - The variable relates to a Keep-Alive socket, which has been closed.
  1685. # - In pipelined or nonpipeline case: cancels all queued requests. The
  1686. # requests have not yet been sent, the responses are not due.
  1687. # - This is a hard cancel that ends each transaction with error status,
  1688. # and closes the connection. Do not use it if you want to replay failed
  1689. # transactions.
  1690. # - N.B. Always delete ::http::socketWrState($connId) before deleting
  1691. # ::http::socketWrQueue($connId), or this command will do nothing.
  1692. #
  1693. # Arguments
  1694. # As for a trace command on a variable.
  1695. proc http::CancelWritePipeline {name1 connId op} {
  1696. variable socketWrQueue
  1697. ##Log CancelWritePipeline $name1 $connId $op
  1698. if {[info exists socketWrQueue($connId)]} {
  1699. set msg {the connection was closed by CancelWritePipeline}
  1700. foreach token $socketWrQueue($connId) {
  1701. set tk [namespace tail $token]
  1702. Log ^X$tk end of response "($msg)" - token $token
  1703. set ${token}(status) eof
  1704. Finish $token ;#$msg
  1705. }
  1706. set socketWrQueue($connId) {}
  1707. }
  1708. }
  1709. # http::ReplayIfDead --
  1710. #
  1711. # - A query on a re-used persistent socket failed at the earliest opportunity,
  1712. # because the socket had been closed by the server. Keep the token, tidy up,
  1713. # and try to connect on a fresh socket.
  1714. # - The connection is monitored for eof by the command http::CheckEof. Thus
  1715. # http::ReplayIfDead is needed only when a server event (half-closing an
  1716. # apparently idle connection), and a client event (sending a request) occur at
  1717. # almost the same time, and neither client nor server detects the other's
  1718. # action before performing its own (an "asynchronous close event").
  1719. # - To simplify testing of http::ReplayIfDead, set TEST_EOF 1 in
  1720. # http::KeepSocket, and then http::ReplayIfDead will be called if http::geturl
  1721. # is called at any time after the server timeout.
  1722. #
  1723. # Arguments:
  1724. # token Connection token.
  1725. #
  1726. # Side Effects:
  1727. # Use the same token, but try to open a new socket.
  1728. proc http::ReplayIfDead {tokenArg doing} {
  1729. variable socketMapping
  1730. variable socketRdState
  1731. variable socketWrState
  1732. variable socketRdQueue
  1733. variable socketWrQueue
  1734. variable socketClosing
  1735. variable socketPlayCmd
  1736. variable $tokenArg
  1737. upvar 0 $tokenArg stateArg
  1738. Log running http::ReplayIfDead for $tokenArg $doing
  1739. # 1. Merge the tokens for transactions in flight, the read (response) queue,
  1740. # and the write (request) queue.
  1741. set InFlightR {}
  1742. set InFlightW {}
  1743. # Obtain the tokens for transactions in flight.
  1744. if {$stateArg(-pipeline)} {
  1745. # Two transactions may be in flight. The "read" transaction was first.
  1746. # It is unlikely that the server would close the socket if a response
  1747. # was pending; however, an earlier request (as well as the present
  1748. # request) may have been sent and ignored if the socket was half-closed
  1749. # by the server.
  1750. if { [info exists socketRdState($stateArg(socketinfo))]
  1751. && ($socketRdState($stateArg(socketinfo)) ne "Rready")
  1752. } {
  1753. lappend InFlightR $socketRdState($stateArg(socketinfo))
  1754. } elseif {($doing eq "read")} {
  1755. lappend InFlightR $tokenArg
  1756. }
  1757. if { [info exists socketWrState($stateArg(socketinfo))]
  1758. && $socketWrState($stateArg(socketinfo)) ni {Wready peNding}
  1759. } {
  1760. lappend InFlightW $socketWrState($stateArg(socketinfo))
  1761. } elseif {($doing eq "write")} {
  1762. lappend InFlightW $tokenArg
  1763. }
  1764. # Report any inconsistency of $tokenArg with socket*state.
  1765. if { ($doing eq "read")
  1766. && [info exists socketRdState($stateArg(socketinfo))]
  1767. && ($tokenArg ne $socketRdState($stateArg(socketinfo)))
  1768. } {
  1769. Log WARNING - ReplayIfDead pipelined tokenArg $tokenArg $doing \
  1770. ne socketRdState($stateArg(socketinfo)) \
  1771. $socketRdState($stateArg(socketinfo))
  1772. } elseif {
  1773. ($doing eq "write")
  1774. && [info exists socketWrState($stateArg(socketinfo))]
  1775. && ($tokenArg ne $socketWrState($stateArg(socketinfo)))
  1776. } {
  1777. Log WARNING - ReplayIfDead pipelined tokenArg $tokenArg $doing \
  1778. ne socketWrState($stateArg(socketinfo)) \
  1779. $socketWrState($stateArg(socketinfo))
  1780. }
  1781. } else {
  1782. # One transaction should be in flight.
  1783. # socketRdState, socketWrQueue are used.
  1784. # socketRdQueue should be empty.
  1785. # Report any inconsistency of $tokenArg with socket*state.
  1786. if {$tokenArg ne $socketRdState($stateArg(socketinfo))} {
  1787. Log WARNING - ReplayIfDead nonpipeline tokenArg $tokenArg $doing \
  1788. ne socketRdState($stateArg(socketinfo)) \
  1789. $socketRdState($stateArg(socketinfo))
  1790. }
  1791. # Report the inconsistency that socketRdQueue is non-empty.
  1792. if { [info exists socketRdQueue($stateArg(socketinfo))]
  1793. && ($socketRdQueue($stateArg(socketinfo)) ne {})
  1794. } {
  1795. Log WARNING - ReplayIfDead nonpipeline tokenArg $tokenArg $doing \
  1796. has read queue socketRdQueue($stateArg(socketinfo)) \
  1797. $socketRdQueue($stateArg(socketinfo)) ne {}
  1798. }
  1799. lappend InFlightW $socketRdState($stateArg(socketinfo))
  1800. set socketRdQueue($stateArg(socketinfo)) {}
  1801. }
  1802. set newQueue {}
  1803. lappend newQueue {*}$InFlightR
  1804. lappend newQueue {*}$socketRdQueue($stateArg(socketinfo))
  1805. lappend newQueue {*}$InFlightW
  1806. lappend newQueue {*}$socketWrQueue($stateArg(socketinfo))
  1807. # 2. Tidy up tokenArg. This is a cut-down form of Finish/CloseSocket.
  1808. # Do not change state(status).
  1809. # No need to after cancel stateArg(after) - either this is done in
  1810. # ReplayCore/ReInit, or Finish is called.
  1811. catch {close $stateArg(sock)}
  1812. # 2a. Tidy the tokens in the queues - this is done in ReplayCore/ReInit.
  1813. # - Transactions, if any, that are awaiting responses cannot be completed.
  1814. # They are listed for re-sending in newQueue.
  1815. # - All tokens are preserved for re-use by ReplayCore, and their variables
  1816. # will be re-initialised by calls to ReInit.
  1817. # - The relevant element of socketMapping, socketRdState, socketWrState,
  1818. # socketRdQueue, socketWrQueue, socketClosing, socketPlayCmd will be set
  1819. # to new values in ReplayCore.
  1820. ReplayCore $newQueue
  1821. }
  1822. # http::ReplayIfClose --
  1823. #
  1824. # A request on a socket that was previously "Connection: keep-alive" has
  1825. # received a "Connection: close" response header. The server supplies
  1826. # that response correctly, but any later requests already queued on this
  1827. # connection will be lost when the socket closes.
  1828. #
  1829. # This command takes arguments that represent the socketWrState,
  1830. # socketRdQueue and socketWrQueue for this connection. The socketRdState
  1831. # is not needed because the server responds in full to the request that
  1832. # received the "Connection: close" response header.
  1833. #
  1834. # Existing request tokens $token (::http::$n) are preserved. The caller
  1835. # will be unaware that the request was processed this way.
  1836. proc http::ReplayIfClose {Wstate Rqueue Wqueue} {
  1837. Log running http::ReplayIfClose for $Wstate $Rqueue $Wqueue
  1838. if {$Wstate in $Rqueue || $Wstate in $Wqueue} {
  1839. Log WARNING duplicate token in http::ReplayIfClose - token $Wstate
  1840. set Wstate Wready
  1841. }
  1842. # 1. Create newQueue
  1843. set InFlightW {}
  1844. if {$Wstate ni {Wready peNding}} {
  1845. lappend InFlightW $Wstate
  1846. }
  1847. set newQueue {}
  1848. lappend newQueue {*}$Rqueue
  1849. lappend newQueue {*}$InFlightW
  1850. lappend newQueue {*}$Wqueue
  1851. # 2. Cleanup - none needed, done by the caller.
  1852. ReplayCore $newQueue
  1853. }
  1854. # http::ReInit --
  1855. #
  1856. # Command to restore a token's state to a condition that
  1857. # makes it ready to replay a request.
  1858. #
  1859. # Command http::geturl stores extra state in state(tmp*) so
  1860. # we don't need to do the argument processing again.
  1861. #
  1862. # The caller must:
  1863. # - Set state(reusing) and state(sock) to their new values after calling
  1864. # this command.
  1865. # - Unset state(tmpState), state(tmpOpenCmd) if future calls to ReplayCore
  1866. # or ReInit are inappropriate for this token. Typically only one retry
  1867. # is allowed.
  1868. # The caller may also unset state(tmpConnArgs) if this value (and the
  1869. # token) will be used immediately. The value is needed by tokens that
  1870. # will be stored in a queue.
  1871. #
  1872. # Arguments:
  1873. # token Connection token.
  1874. #
  1875. # Return Value: (boolean) true iff the re-initialisation was successful.
  1876. proc http::ReInit {token} {
  1877. variable $token
  1878. upvar 0 $token state
  1879. if {!(
  1880. [info exists state(tmpState)]
  1881. && [info exists state(tmpOpenCmd)]
  1882. && [info exists state(tmpConnArgs)]
  1883. )
  1884. } {
  1885. Log FAILED in http::ReInit via ReplayCore - NO tmp vars for $token
  1886. return 0
  1887. }
  1888. if {[info exists state(after)]} {
  1889. after cancel $state(after)
  1890. unset state(after)
  1891. }
  1892. # Don't alter state(status) - this would trigger http::wait if it is in use.
  1893. set tmpState $state(tmpState)
  1894. set tmpOpenCmd $state(tmpOpenCmd)
  1895. set tmpConnArgs $state(tmpConnArgs)
  1896. foreach name [array names state] {
  1897. if {$name ne "status"} {
  1898. unset state($name)
  1899. }
  1900. }
  1901. # Don't alter state(status).
  1902. # Restore state(tmp*) - the caller may decide to unset them.
  1903. # Restore state(tmpConnArgs) which is needed for connection.
  1904. # state(tmpState), state(tmpOpenCmd) are needed only for retries.
  1905. dict unset tmpState status
  1906. array set state $tmpState
  1907. set state(tmpState) $tmpState
  1908. set state(tmpOpenCmd) $tmpOpenCmd
  1909. set state(tmpConnArgs) $tmpConnArgs
  1910. return 1
  1911. }
  1912. # http::ReplayCore --
  1913. #
  1914. # Command to replay a list of requests, using existing connection tokens.
  1915. #
  1916. # Abstracted from http::geturl which stores extra state in state(tmp*) so
  1917. # we don't need to do the argument processing again.
  1918. #
  1919. # Arguments:
  1920. # newQueue List of connection tokens.
  1921. #
  1922. # Side Effects:
  1923. # Use existing tokens, but try to open a new socket.
  1924. proc http::ReplayCore {newQueue} {
  1925. variable socketMapping
  1926. variable socketRdState
  1927. variable socketWrState
  1928. variable socketRdQueue
  1929. variable socketWrQueue
  1930. variable socketClosing
  1931. variable socketPlayCmd
  1932. if {[llength $newQueue] == 0} {
  1933. # Nothing to do.
  1934. return
  1935. }
  1936. ##Log running ReplayCore for {*}$newQueue
  1937. set newToken [lindex $newQueue 0]
  1938. set newQueue [lrange $newQueue 1 end]
  1939. # 3. Use newToken, and restore its values of state(*). Do not restore
  1940. # elements tmp* - we try again only once.
  1941. set token $newToken
  1942. variable $token
  1943. upvar 0 $token state
  1944. if {![ReInit $token]} {
  1945. Log FAILED in http::ReplayCore - NO tmp vars
  1946. Finish $token {cannot send this request again}
  1947. return
  1948. }
  1949. set tmpState $state(tmpState)
  1950. set tmpOpenCmd $state(tmpOpenCmd)
  1951. set tmpConnArgs $state(tmpConnArgs)
  1952. unset state(tmpState)
  1953. unset state(tmpOpenCmd)
  1954. unset state(tmpConnArgs)
  1955. set state(reusing) 0
  1956. if {$state(-timeout) > 0} {
  1957. set resetCmd [list http::reset $token timeout]
  1958. set state(after) [after $state(-timeout) $resetCmd]
  1959. }
  1960. set pre [clock milliseconds]
  1961. ##Log pre socket opened, - token $token
  1962. ##Log $tmpOpenCmd - token $token
  1963. # 4. Open a socket.
  1964. if {[catch {eval $tmpOpenCmd} sock]} {
  1965. # Something went wrong while trying to establish the connection.
  1966. Log FAILED - $sock
  1967. set state(sock) NONE
  1968. Finish $token $sock
  1969. return
  1970. }
  1971. ##Log post socket opened, - token $token
  1972. set delay [expr {[clock milliseconds] - $pre}]
  1973. if {$delay > 3000} {
  1974. Log socket delay $delay - token $token
  1975. }
  1976. # Command [socket] is called with -async, but takes 5s to 5.1s to return,
  1977. # with probability of order 1 in 10,000. This may be a bizarre scheduling
  1978. # issue with my (KJN's) system (Fedora Linux).
  1979. # This does not cause a problem (unless the request times out when this
  1980. # command returns).
  1981. # 5. Configure the persistent socket data.
  1982. if {$state(-keepalive)} {
  1983. set socketMapping($state(socketinfo)) $sock
  1984. if {![info exists socketRdState($state(socketinfo))]} {
  1985. set socketRdState($state(socketinfo)) {}
  1986. set varName ::http::socketRdState($state(socketinfo))
  1987. trace add variable $varName unset ::http::CancelReadPipeline
  1988. }
  1989. if {![info exists socketWrState($state(socketinfo))]} {
  1990. set socketWrState($state(socketinfo)) {}
  1991. set varName ::http::socketWrState($state(socketinfo))
  1992. trace add variable $varName unset ::http::CancelWritePipeline
  1993. }
  1994. if {$state(-pipeline)} {
  1995. #Log new, init for pipelined, GRANT write acc to $token ReplayCore
  1996. set socketRdState($state(socketinfo)) $token
  1997. set socketWrState($state(socketinfo)) $token
  1998. } else {
  1999. #Log new, init for nonpipeline, GRANT r/w acc to $token ReplayCore
  2000. set socketRdState($state(socketinfo)) $token
  2001. set socketWrState($state(socketinfo)) $token
  2002. }
  2003. set socketRdQueue($state(socketinfo)) {}
  2004. set socketWrQueue($state(socketinfo)) $newQueue
  2005. set socketClosing($state(socketinfo)) 0
  2006. set socketPlayCmd($state(socketinfo)) {ReplayIfClose Wready {} {}}
  2007. }
  2008. ##Log pre newQueue ReInit, - token $token
  2009. # 6. Configure sockets in the queue.
  2010. foreach tok $newQueue {
  2011. if {[ReInit $tok]} {
  2012. set ${tok}(reusing) 1
  2013. set ${tok}(sock) $sock
  2014. } else {
  2015. set ${tok}(reusing) 1
  2016. set ${tok}(sock) NONE
  2017. Finish $token {cannot send this request again}
  2018. }
  2019. }
  2020. # 7. Configure the socket for newToken to send a request.
  2021. set state(sock) $sock
  2022. Log "Using $sock for $state(socketinfo) - token $token" \
  2023. [expr {$state(-keepalive)?"keepalive":""}]
  2024. # Initialisation of a new socket.
  2025. ##Log socket opened, now fconfigure - token $token
  2026. fconfigure $sock -translation {auto crlf} -buffersize $state(-blocksize)
  2027. ##Log socket opened, DONE fconfigure - token $token
  2028. # Connect does its own fconfigure.
  2029. fileevent $sock writable [list http::Connect $token {*}$tmpConnArgs]
  2030. #Log ---- $sock << conn to $token for HTTP request (e)
  2031. }
  2032. # Data access functions:
  2033. # Data - the URL data
  2034. # Status - the transaction status: ok, reset, eof, timeout, error
  2035. # Code - the HTTP transaction code, e.g., 200
  2036. # Size - the size of the URL data
  2037. proc http::data {token} {
  2038. variable $token
  2039. upvar 0 $token state
  2040. return $state(body)
  2041. }
  2042. proc http::status {token} {
  2043. if {![info exists $token]} {
  2044. return "error"
  2045. }
  2046. variable $token
  2047. upvar 0 $token state
  2048. return $state(status)
  2049. }
  2050. proc http::code {token} {
  2051. variable $token
  2052. upvar 0 $token state
  2053. return $state(http)
  2054. }
  2055. proc http::ncode {token} {
  2056. variable $token
  2057. upvar 0 $token state
  2058. if {[regexp {[0-9]{3}} $state(http) numeric_code]} {
  2059. return $numeric_code
  2060. } else {
  2061. return $state(http)
  2062. }
  2063. }
  2064. proc http::size {token} {
  2065. variable $token
  2066. upvar 0 $token state
  2067. return $state(currentsize)
  2068. }
  2069. proc http::meta {token} {
  2070. variable $token
  2071. upvar 0 $token state
  2072. return $state(meta)
  2073. }
  2074. proc http::error {token} {
  2075. variable $token
  2076. upvar 0 $token state
  2077. if {[info exists state(error)]} {
  2078. return $state(error)
  2079. }
  2080. return ""
  2081. }
  2082. # http::cleanup
  2083. #
  2084. # Garbage collect the state associated with a transaction
  2085. #
  2086. # Arguments
  2087. # token The token returned from http::geturl
  2088. #
  2089. # Side Effects
  2090. # unsets the state array
  2091. proc http::cleanup {token} {
  2092. variable $token
  2093. upvar 0 $token state
  2094. if {[info commands ${token}EventCoroutine] ne {}} {
  2095. rename ${token}EventCoroutine {}
  2096. }
  2097. if {[info exists state(after)]} {
  2098. after cancel $state(after)
  2099. unset state(after)
  2100. }
  2101. if {[info exists state]} {
  2102. unset state
  2103. }
  2104. }
  2105. # http::Connect
  2106. #
  2107. # This callback is made when an asyncronous connection completes.
  2108. #
  2109. # Arguments
  2110. # token The token returned from http::geturl
  2111. #
  2112. # Side Effects
  2113. # Sets the status of the connection, which unblocks
  2114. # the waiting geturl call
  2115. proc http::Connect {token proto phost srvurl} {
  2116. variable $token
  2117. upvar 0 $token state
  2118. set tk [namespace tail $token]
  2119. set err "due to unexpected EOF"
  2120. if {
  2121. [eof $state(sock)] ||
  2122. [set err [fconfigure $state(sock) -error]] ne ""
  2123. } {
  2124. Log "WARNING - if testing, pay special attention to this\
  2125. case (GJ) which is seldom executed - token $token"
  2126. if {[info exists state(reusing)] && $state(reusing)} {
  2127. # The socket was closed at the server end, and closed at
  2128. # this end by http::CheckEof.
  2129. if {[TestForReplay $token write $err b]} {
  2130. return
  2131. }
  2132. # else:
  2133. # This is NOT a persistent socket that has been closed since its
  2134. # last use.
  2135. # If any other requests are in flight or pipelined/queued, they will
  2136. # be discarded.
  2137. }
  2138. Finish $token "connect failed $err"
  2139. } else {
  2140. set state(state) connecting
  2141. fileevent $state(sock) writable {}
  2142. ::http::Connected $token $proto $phost $srvurl
  2143. }
  2144. }
  2145. # http::Write
  2146. #
  2147. # Write POST query data to the socket
  2148. #
  2149. # Arguments
  2150. # token The token for the connection
  2151. #
  2152. # Side Effects
  2153. # Write the socket and handle callbacks.
  2154. proc http::Write {token} {
  2155. variable http
  2156. variable socketMapping
  2157. variable socketRdState
  2158. variable socketWrState
  2159. variable socketRdQueue
  2160. variable socketWrQueue
  2161. variable socketClosing
  2162. variable socketPlayCmd
  2163. variable $token
  2164. upvar 0 $token state
  2165. set tk [namespace tail $token]
  2166. set sock $state(sock)
  2167. # Output a block. Tcl will buffer this if the socket blocks
  2168. set done 0
  2169. if {[catch {
  2170. # Catch I/O errors on dead sockets
  2171. if {[info exists state(-query)]} {
  2172. # Chop up large query strings so queryprogress callback can give
  2173. # smooth feedback.
  2174. if { $state(queryoffset) + $state(-queryblocksize)
  2175. >= $state(querylength)
  2176. } {
  2177. # This will be the last puts for the request-body.
  2178. if { (![catch {fileevent $sock readable} binding])
  2179. && ($binding eq [list http::CheckEof $sock])
  2180. } {
  2181. # Remove the "fileevent readable" binding of an idle
  2182. # persistent socket to http::CheckEof. We can no longer
  2183. # treat bytes received as junk. The server might still time
  2184. # out and half-close the socket if it has not yet received
  2185. # the first "puts".
  2186. fileevent $sock readable {}
  2187. }
  2188. }
  2189. puts -nonewline $sock \
  2190. [string range $state(-query) $state(queryoffset) \
  2191. [expr {$state(queryoffset) + $state(-queryblocksize) - 1}]]
  2192. incr state(queryoffset) $state(-queryblocksize)
  2193. if {$state(queryoffset) >= $state(querylength)} {
  2194. set state(queryoffset) $state(querylength)
  2195. set done 1
  2196. }
  2197. } else {
  2198. # Copy blocks from the query channel
  2199. set outStr [read $state(-querychannel) $state(-queryblocksize)]
  2200. if {[eof $state(-querychannel)]} {
  2201. # This will be the last puts for the request-body.
  2202. if { (![catch {fileevent $sock readable} binding])
  2203. && ($binding eq [list http::CheckEof $sock])
  2204. } {
  2205. # Remove the "fileevent readable" binding of an idle
  2206. # persistent socket to http::CheckEof. We can no longer
  2207. # treat bytes received as junk. The server might still time
  2208. # out and half-close the socket if it has not yet received
  2209. # the first "puts".
  2210. fileevent $sock readable {}
  2211. }
  2212. }
  2213. puts -nonewline $sock $outStr
  2214. incr state(queryoffset) [string length $outStr]
  2215. if {[eof $state(-querychannel)]} {
  2216. set done 1
  2217. }
  2218. }
  2219. } err]} {
  2220. # Do not call Finish here, but instead let the read half of the socket
  2221. # process whatever server reply there is to get.
  2222. set state(posterror) $err
  2223. set done 1
  2224. }
  2225. if {$done} {
  2226. catch {flush $sock}
  2227. fileevent $sock writable {}
  2228. Log ^C$tk end sending request - token $token
  2229. # End of writing (POST method). The request has been sent.
  2230. DoneRequest $token
  2231. }
  2232. # Callback to the client after we've completely handled everything.
  2233. if {[string length $state(-queryprogress)]} {
  2234. eval $state(-queryprogress) \
  2235. [list $token $state(querylength) $state(queryoffset)]
  2236. }
  2237. }
  2238. # http::Event
  2239. #
  2240. # Handle input on the socket. This command is the core of
  2241. # the coroutine commands ${token}EventCoroutine that are
  2242. # bound to "fileevent $sock readable" and process input.
  2243. #
  2244. # Arguments
  2245. # sock The socket receiving input.
  2246. # token The token returned from http::geturl
  2247. #
  2248. # Side Effects
  2249. # Read the socket and handle callbacks.
  2250. proc http::Event {sock token} {
  2251. variable http
  2252. variable socketMapping
  2253. variable socketRdState
  2254. variable socketWrState
  2255. variable socketRdQueue
  2256. variable socketWrQueue
  2257. variable socketClosing
  2258. variable socketPlayCmd
  2259. variable $token
  2260. upvar 0 $token state
  2261. set tk [namespace tail $token]
  2262. while 1 {
  2263. yield
  2264. ##Log Event call - token $token
  2265. if {![info exists state]} {
  2266. Log "Event $sock with invalid token '$token' - remote close?"
  2267. if {![eof $sock]} {
  2268. if {[set d [read $sock]] ne ""} {
  2269. Log "WARNING: additional data left on closed socket\
  2270. - token $token"
  2271. }
  2272. }
  2273. Log ^X$tk end of response (token error) - token $token
  2274. CloseSocket $sock
  2275. return
  2276. }
  2277. if {$state(state) eq "connecting"} {
  2278. ##Log - connecting - token $token
  2279. if { $state(reusing)
  2280. && $state(-pipeline)
  2281. && ($state(-timeout) > 0)
  2282. && (![info exists state(after)])
  2283. } {
  2284. set state(after) [after $state(-timeout) \
  2285. [list http::reset $token timeout]]
  2286. }
  2287. if {[catch {gets $sock state(http)} nsl]} {
  2288. Log "WARNING - if testing, pay special attention to this\
  2289. case (GK) which is seldom executed - token $token"
  2290. if {[info exists state(reusing)] && $state(reusing)} {
  2291. # The socket was closed at the server end, and closed at
  2292. # this end by http::CheckEof.
  2293. if {[TestForReplay $token read $nsl c]} {
  2294. return
  2295. }
  2296. # else:
  2297. # This is NOT a persistent socket that has been closed since
  2298. # its last use.
  2299. # If any other requests are in flight or pipelined/queued,
  2300. # they will be discarded.
  2301. } else {
  2302. Log ^X$tk end of response (error) - token $token
  2303. Finish $token $nsl
  2304. return
  2305. }
  2306. } elseif {$nsl >= 0} {
  2307. ##Log - connecting 1 - token $token
  2308. set state(state) "header"
  2309. } elseif { [eof $sock]
  2310. && [info exists state(reusing)]
  2311. && $state(reusing)
  2312. } {
  2313. # The socket was closed at the server end, and we didn't notice.
  2314. # This is the first read - where the closure is usually first
  2315. # detected.
  2316. if {[TestForReplay $token read {} d]} {
  2317. return
  2318. }
  2319. # else:
  2320. # This is NOT a persistent socket that has been closed since its
  2321. # last use.
  2322. # If any other requests are in flight or pipelined/queued, they
  2323. # will be discarded.
  2324. }
  2325. } elseif {$state(state) eq "header"} {
  2326. if {[catch {gets $sock line} nhl]} {
  2327. ##Log header failed - token $token
  2328. Log ^X$tk end of response (error) - token $token
  2329. Finish $token $nhl
  2330. return
  2331. } elseif {$nhl == 0} {
  2332. ##Log header done - token $token
  2333. Log ^E$tk end of response headers - token $token
  2334. # We have now read all headers
  2335. # We ignore HTTP/1.1 100 Continue returns. RFC2616 sec 8.2.3
  2336. if { ($state(http) == "")
  2337. || ([regexp {^\S+\s(\d+)} $state(http) {} x] && $x == 100)
  2338. } {
  2339. set state(state) "connecting"
  2340. continue
  2341. # This was a "return" in the pre-coroutine code.
  2342. }
  2343. if { ([info exists state(connection)])
  2344. && ([info exists socketMapping($state(socketinfo))])
  2345. && ($state(connection) eq "keep-alive")
  2346. && ($state(-keepalive))
  2347. && (!$state(reusing))
  2348. && ($state(-pipeline))
  2349. } {
  2350. # Response headers received for first request on a
  2351. # persistent socket. Now ready for pipelined writes (if
  2352. # any).
  2353. # Previous value is $token. It cannot be "pending".
  2354. set socketWrState($state(socketinfo)) Wready
  2355. http::NextPipelinedWrite $token
  2356. }
  2357. # Once a "close" has been signaled, the client MUST NOT send any
  2358. # more requests on that connection.
  2359. #
  2360. # If either the client or the server sends the "close" token in
  2361. # the Connection header, that request becomes the last one for
  2362. # the connection.
  2363. if { ([info exists state(connection)])
  2364. && ([info exists socketMapping($state(socketinfo))])
  2365. && ($state(connection) eq "close")
  2366. && ($state(-keepalive))
  2367. } {
  2368. # The server warns that it will close the socket after this
  2369. # response.
  2370. ##Log WARNING - socket will close after response for $token
  2371. # Prepare data for a call to ReplayIfClose.
  2372. if { ($socketRdQueue($state(socketinfo)) ne {})
  2373. || ($socketWrQueue($state(socketinfo)) ne {})
  2374. || ($socketWrState($state(socketinfo)) ni
  2375. [list Wready peNding $token])
  2376. } {
  2377. set InFlightW $socketWrState($state(socketinfo))
  2378. if {$InFlightW in [list Wready peNding $token]} {
  2379. set InFlightW Wready
  2380. } else {
  2381. set msg "token ${InFlightW} is InFlightW"
  2382. ##Log $msg - token $token
  2383. }
  2384. set socketPlayCmd($state(socketinfo)) \
  2385. [list ReplayIfClose $InFlightW \
  2386. $socketRdQueue($state(socketinfo)) \
  2387. $socketWrQueue($state(socketinfo))]
  2388. # - All tokens are preserved for re-use by ReplayCore.
  2389. # - Queues are preserved in case of Finish with error,
  2390. # but are not used for anything else because
  2391. # socketClosing(*) is set below.
  2392. # - Cancel the state(after) timeout events.
  2393. foreach tokenVal $socketRdQueue($state(socketinfo)) {
  2394. if {[info exists ${tokenVal}(after)]} {
  2395. after cancel [set ${tokenVal}(after)]
  2396. unset ${tokenVal}(after)
  2397. }
  2398. }
  2399. } else {
  2400. set socketPlayCmd($state(socketinfo)) \
  2401. {ReplayIfClose Wready {} {}}
  2402. }
  2403. # Do not allow further connections on this socket.
  2404. set socketClosing($state(socketinfo)) 1
  2405. }
  2406. set state(state) body
  2407. # If doing a HEAD, then we won't get any body
  2408. if {$state(-validate)} {
  2409. Log ^F$tk end of response for HEAD request - token $token
  2410. set state(state) complete
  2411. Eot $token
  2412. return
  2413. }
  2414. # - For non-chunked transfer we may have no body - in this case
  2415. # we may get no further file event if the connection doesn't
  2416. # close and no more data is sent. We can tell and must finish
  2417. # up now - not later - the alternative would be to wait until
  2418. # the server times out.
  2419. # - In this case, the server has NOT told the client it will
  2420. # close the connection, AND it has NOT indicated the resource
  2421. # length EITHER by setting the Content-Length (totalsize) OR
  2422. # by using chunked Transfer-Encoding.
  2423. # - Do not worry here about the case (Connection: close) because
  2424. # the server should close the connection.
  2425. # - IF (NOT Connection: close) AND (NOT chunked encoding) AND
  2426. # (totalsize == 0).
  2427. if { (!( [info exists state(connection)]
  2428. && ($state(connection) eq "close")
  2429. )
  2430. )
  2431. && (![info exists state(transfer)])
  2432. && ($state(totalsize) == 0)
  2433. } {
  2434. set msg {body size is 0 and no events likely - complete}
  2435. Log "$msg - token $token"
  2436. set msg {(length unknown, set to 0)}
  2437. Log ^F$tk end of response body {*}$msg - token $token
  2438. set state(state) complete
  2439. Eot $token
  2440. return
  2441. }
  2442. # We have to use binary translation to count bytes properly.
  2443. lassign [fconfigure $sock -translation] trRead trWrite
  2444. fconfigure $sock -translation [list binary $trWrite]
  2445. if {
  2446. $state(-binary) || [IsBinaryContentType $state(type)]
  2447. } {
  2448. # Turn off conversions for non-text data.
  2449. set state(binary) 1
  2450. }
  2451. if {[info exists state(-channel)]} {
  2452. if {$state(binary) || [llength [ContentEncoding $token]]} {
  2453. fconfigure $state(-channel) -translation binary
  2454. }
  2455. if {![info exists state(-handler)]} {
  2456. # Initiate a sequence of background fcopies.
  2457. fileevent $sock readable {}
  2458. rename ${token}EventCoroutine {}
  2459. CopyStart $sock $token
  2460. return
  2461. }
  2462. }
  2463. } elseif {$nhl > 0} {
  2464. # Process header lines.
  2465. ##Log header - token $token - $line
  2466. if {[regexp -nocase {^([^:]+):(.+)$} $line x key value]} {
  2467. switch -- [string tolower $key] {
  2468. content-type {
  2469. set state(type) [string trim [string tolower $value]]
  2470. # Grab the optional charset information.
  2471. if {[regexp -nocase \
  2472. {charset\s*=\s*\"((?:[^""]|\\\")*)\"} \
  2473. $state(type) -> cs]} {
  2474. set state(charset) [string map {{\"} \"} $cs]
  2475. } else {
  2476. regexp -nocase {charset\s*=\s*(\S+?);?} \
  2477. $state(type) -> state(charset)
  2478. }
  2479. }
  2480. content-length {
  2481. set state(totalsize) [string trim $value]
  2482. }
  2483. content-encoding {
  2484. set state(coding) [string trim $value]
  2485. }
  2486. transfer-encoding {
  2487. set state(transfer) \
  2488. [string trim [string tolower $value]]
  2489. }
  2490. proxy-connection -
  2491. connection {
  2492. set tmpHeader [string trim [string tolower $value]]
  2493. # RFC 7230 Section 6.1 states that a comma-separated
  2494. # list is an acceptable value. According to
  2495. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection
  2496. # any comma-separated list implies keep-alive, but I
  2497. # don't see this in the RFC so we'll play safe and
  2498. # scan any list for "close".
  2499. if {$tmpHeader in {close keep-alive}} {
  2500. # The common cases, continue.
  2501. } elseif {[string first , $tmpHeader] < 0} {
  2502. # Not a comma-separated list, not "close",
  2503. # therefore "keep-alive".
  2504. set tmpHeader keep-alive
  2505. } else {
  2506. set tmpResult keep-alive
  2507. set tmpCsl [split $tmpHeader ,]
  2508. # Optional whitespace either side of separator.
  2509. foreach el $tmpCsl {
  2510. if {[string trim $el] eq {close}} {
  2511. set tmpResult close
  2512. break
  2513. }
  2514. }
  2515. set tmpHeader $tmpResult
  2516. }
  2517. set state(connection) $tmpHeader
  2518. }
  2519. }
  2520. lappend state(meta) $key [string trim $value]
  2521. }
  2522. }
  2523. } else {
  2524. # Now reading body
  2525. ##Log body - token $token
  2526. if {[catch {
  2527. if {[info exists state(-handler)]} {
  2528. set n [eval $state(-handler) [list $sock $token]]
  2529. ##Log handler $n - token $token
  2530. # N.B. the protocol has been set to 1.0 because the -handler
  2531. # logic is not expected to handle chunked encoding.
  2532. # FIXME Allow -handler with 1.1 on dechunked stacked chan.
  2533. if {$state(totalsize) == 0} {
  2534. # We know the transfer is complete only when the server
  2535. # closes the connection - i.e. eof is not an error.
  2536. set state(state) complete
  2537. }
  2538. if {![string is integer -strict $n]} {
  2539. if 1 {
  2540. # Do not tolerate bad -handler - fail with error
  2541. # status.
  2542. set msg {the -handler command for http::geturl must\
  2543. return an integer (the number of bytes\
  2544. read)}
  2545. Log ^X$tk end of response (handler error) -\
  2546. token $token
  2547. Eot $token $msg
  2548. } else {
  2549. # Tolerate the bad -handler, and continue. The
  2550. # penalty:
  2551. # (a) Because the handler returns nonsense, we know
  2552. # the transfer is complete only when the server
  2553. # closes the connection - i.e. eof is not an
  2554. # error.
  2555. # (b) http::size will not be accurate.
  2556. # (c) The transaction is already downgraded to 1.0
  2557. # to avoid chunked transfer encoding. It MUST
  2558. # also be forced to "Connection: close" or the
  2559. # HTTP/1.0 equivalent; or it MUST fail (as
  2560. # above) if the server sends
  2561. # "Connection: keep-alive" or the HTTP/1.0
  2562. # equivalent.
  2563. set n 0
  2564. set state(state) complete
  2565. }
  2566. }
  2567. } elseif {[info exists state(transfer_final)]} {
  2568. # This code forgives EOF in place of the final CRLF.
  2569. set line [getTextLine $sock]
  2570. set n [string length $line]
  2571. set state(state) complete
  2572. if {$n > 0} {
  2573. # - HTTP trailers (late response headers) are permitted
  2574. # by Chunked Transfer-Encoding, and can be safely
  2575. # ignored.
  2576. # - Do not count these bytes in the total received for
  2577. # the response body.
  2578. Log "trailer of $n bytes after final chunk -\
  2579. token $token"
  2580. append state(transfer_final) $line
  2581. set n 0
  2582. } else {
  2583. Log ^F$tk end of response body (chunked) - token $token
  2584. Log "final chunk part - token $token"
  2585. Eot $token
  2586. }
  2587. } elseif { [info exists state(transfer)]
  2588. && ($state(transfer) eq "chunked")
  2589. } {
  2590. ##Log chunked - token $token
  2591. set size 0
  2592. set hexLenChunk [getTextLine $sock]
  2593. #set ntl [string length $hexLenChunk]
  2594. if {[string trim $hexLenChunk] ne ""} {
  2595. scan $hexLenChunk %x size
  2596. if {$size != 0} {
  2597. ##Log chunk-measure $size - token $token
  2598. set chunk [BlockingRead $sock $size]
  2599. set n [string length $chunk]
  2600. if {$n >= 0} {
  2601. append state(body) $chunk
  2602. incr state(log_size) [string length $chunk]
  2603. ##Log chunk $n cumul $state(log_size) -\
  2604. token $token
  2605. }
  2606. if {$size != [string length $chunk]} {
  2607. Log "WARNING: mis-sized chunk:\
  2608. was [string length $chunk], should be\
  2609. $size - token $token"
  2610. set n 0
  2611. set state(connection) close
  2612. Log ^X$tk end of response (chunk error) \
  2613. - token $token
  2614. set msg {error in chunked encoding - fetch\
  2615. terminated}
  2616. Eot $token $msg
  2617. }
  2618. # CRLF that follows chunk.
  2619. # If eof, this is handled at the end of this proc.
  2620. getTextLine $sock
  2621. } else {
  2622. set n 0
  2623. set state(transfer_final) {}
  2624. }
  2625. } else {
  2626. # Line expected to hold chunk length is empty, or eof.
  2627. ##Log bad-chunk-measure - token $token
  2628. set n 0
  2629. set state(connection) close
  2630. Log ^X$tk end of response (chunk error) - token $token
  2631. Eot $token {error in chunked encoding -\
  2632. fetch terminated}
  2633. }
  2634. } else {
  2635. ##Log unchunked - token $token
  2636. if {$state(totalsize) == 0} {
  2637. # We know the transfer is complete only when the server
  2638. # closes the connection.
  2639. set state(state) complete
  2640. set reqSize $state(-blocksize)
  2641. } else {
  2642. # Ask for the whole of the unserved response-body.
  2643. # This works around a problem with a tls::socket - for
  2644. # https in keep-alive mode, and a request for
  2645. # $state(-blocksize) bytes, the last part of the
  2646. # resource does not get read until the server times out.
  2647. set reqSize [expr { $state(totalsize)
  2648. - $state(currentsize)}]
  2649. # The workaround fails if reqSize is
  2650. # capped at $state(-blocksize).
  2651. # set reqSize [expr {min($reqSize, $state(-blocksize))}]
  2652. }
  2653. set c $state(currentsize)
  2654. set t $state(totalsize)
  2655. ##Log non-chunk currentsize $c of totalsize $t -\
  2656. token $token
  2657. set block [read $sock $reqSize]
  2658. set n [string length $block]
  2659. if {$n >= 0} {
  2660. append state(body) $block
  2661. ##Log non-chunk [string length $state(body)] -\
  2662. token $token
  2663. }
  2664. }
  2665. # This calculation uses n from the -handler, chunked, or
  2666. # unchunked case as appropriate.
  2667. if {[info exists state]} {
  2668. if {$n >= 0} {
  2669. incr state(currentsize) $n
  2670. set c $state(currentsize)
  2671. set t $state(totalsize)
  2672. ##Log another $n currentsize $c totalsize $t -\
  2673. token $token
  2674. }
  2675. # If Content-Length - check for end of data.
  2676. if {
  2677. ($state(totalsize) > 0)
  2678. && ($state(currentsize) >= $state(totalsize))
  2679. } {
  2680. Log ^F$tk end of response body (unchunked) -\
  2681. token $token
  2682. set state(state) complete
  2683. Eot $token
  2684. }
  2685. }
  2686. } err]} {
  2687. Log ^X$tk end of response (error ${err}) - token $token
  2688. Finish $token $err
  2689. return
  2690. } else {
  2691. if {[info exists state(-progress)]} {
  2692. eval $state(-progress) \
  2693. [list $token $state(totalsize) $state(currentsize)]
  2694. }
  2695. }
  2696. }
  2697. # catch as an Eot above may have closed the socket already
  2698. # $state(state) may be connecting, header, body, or complete
  2699. if {![set cc [catch {eof $sock} eof]] && $eof} {
  2700. ##Log eof - token $token
  2701. if {[info exists $token]} {
  2702. set state(connection) close
  2703. if {$state(state) eq "complete"} {
  2704. # This includes all cases in which the transaction
  2705. # can be completed by eof.
  2706. # The value "complete" is set only in http::Event, and it is
  2707. # used only in the test above.
  2708. Log ^F$tk end of response body (unchunked, eof) -\
  2709. token $token
  2710. Eot $token
  2711. } else {
  2712. # Premature eof.
  2713. Log ^X$tk end of response (unexpected eof) - token $token
  2714. Eot $token eof
  2715. }
  2716. } else {
  2717. # open connection closed on a token that has been cleaned up.
  2718. Log ^X$tk end of response (token error) - token $token
  2719. CloseSocket $sock
  2720. }
  2721. } elseif {$cc} {
  2722. return
  2723. }
  2724. }
  2725. }
  2726. # http::TestForReplay
  2727. #
  2728. # Command called if eof is discovered when a socket is first used for a
  2729. # new transaction. Typically this occurs if a persistent socket is used
  2730. # after a period of idleness and the server has half-closed the socket.
  2731. #
  2732. # token - the connection token returned by http::geturl
  2733. # doing - "read" or "write"
  2734. # err - error message, if any
  2735. # caller - code to identify the caller - used only in logging
  2736. #
  2737. # Return Value: boolean, true iff the command calls http::ReplayIfDead.
  2738. proc http::TestForReplay {token doing err caller} {
  2739. variable http
  2740. variable $token
  2741. upvar 0 $token state
  2742. set tk [namespace tail $token]
  2743. if {$doing eq "read"} {
  2744. set code Q
  2745. set action response
  2746. set ing reading
  2747. } else {
  2748. set code P
  2749. set action request
  2750. set ing writing
  2751. }
  2752. if {$err eq {}} {
  2753. set err "detect eof when $ing (server timed out?)"
  2754. }
  2755. if {$state(method) eq "POST" && !$http(-repost)} {
  2756. # No Replay.
  2757. # The present transaction will end when Finish is called.
  2758. # That call to Finish will abort any other transactions
  2759. # currently in the write queue.
  2760. # For calls from http::Event this occurs when execution
  2761. # reaches the code block at the end of that proc.
  2762. set msg {no retry for POST with http::config -repost 0}
  2763. Log reusing socket failed "($caller)" - $msg - token $token
  2764. Log error - $err - token $token
  2765. Log ^X$tk end of $action (error) - token $token
  2766. return 0
  2767. } else {
  2768. # Replay.
  2769. set msg {try a new socket}
  2770. Log reusing socket failed "($caller)" - $msg - token $token
  2771. Log error - $err - token $token
  2772. Log ^$code$tk Any unfinished (incl this one) failed - token $token
  2773. ReplayIfDead $token $doing
  2774. return 1
  2775. }
  2776. }
  2777. # http::IsBinaryContentType --
  2778. #
  2779. # Determine if the content-type means that we should definitely transfer
  2780. # the data as binary. [Bug 838e99a76d]
  2781. #
  2782. # Arguments
  2783. # type The content-type of the data.
  2784. #
  2785. # Results:
  2786. # Boolean, true if we definitely should be binary.
  2787. proc http::IsBinaryContentType {type} {
  2788. lassign [split [string tolower $type] "/;"] major minor
  2789. if {$major eq "text"} {
  2790. return false
  2791. }
  2792. # There's a bunch of XML-as-application-format things about. See RFC 3023
  2793. # and so on.
  2794. if {$major eq "application"} {
  2795. set minor [string trimright $minor]
  2796. if {$minor in {"json" "xml" "xml-external-parsed-entity" "xml-dtd"}} {
  2797. return false
  2798. }
  2799. }
  2800. # Not just application/foobar+xml but also image/svg+xml, so let us not
  2801. # restrict things for now...
  2802. if {[string match "*+xml" $minor]} {
  2803. return false
  2804. }
  2805. return true
  2806. }
  2807. # http::getTextLine --
  2808. #
  2809. # Get one line with the stream in crlf mode.
  2810. # Used if Transfer-Encoding is chunked.
  2811. # Empty line is not distinguished from eof. The caller must
  2812. # be able to handle this.
  2813. #
  2814. # Arguments
  2815. # sock The socket receiving input.
  2816. #
  2817. # Results:
  2818. # The line of text, without trailing newline
  2819. proc http::getTextLine {sock} {
  2820. set tr [fconfigure $sock -translation]
  2821. lassign $tr trRead trWrite
  2822. fconfigure $sock -translation [list crlf $trWrite]
  2823. set r [BlockingGets $sock]
  2824. fconfigure $sock -translation $tr
  2825. return $r
  2826. }
  2827. # http::BlockingRead
  2828. #
  2829. # Replacement for a blocking read.
  2830. # The caller must be a coroutine.
  2831. proc http::BlockingRead {sock size} {
  2832. if {$size < 1} {
  2833. return
  2834. }
  2835. set result {}
  2836. while 1 {
  2837. set need [expr {$size - [string length $result]}]
  2838. set block [read $sock $need]
  2839. set eof [eof $sock]
  2840. append result $block
  2841. if {[string length $result] >= $size || $eof} {
  2842. return $result
  2843. } else {
  2844. yield
  2845. }
  2846. }
  2847. }
  2848. # http::BlockingGets
  2849. #
  2850. # Replacement for a blocking gets.
  2851. # The caller must be a coroutine.
  2852. # Empty line is not distinguished from eof. The caller must
  2853. # be able to handle this.
  2854. proc http::BlockingGets {sock} {
  2855. while 1 {
  2856. set count [gets $sock line]
  2857. set eof [eof $sock]
  2858. if {$count > -1 || $eof} {
  2859. return $line
  2860. } else {
  2861. yield
  2862. }
  2863. }
  2864. }
  2865. # http::CopyStart
  2866. #
  2867. # Error handling wrapper around fcopy
  2868. #
  2869. # Arguments
  2870. # sock The socket to copy from
  2871. # token The token returned from http::geturl
  2872. #
  2873. # Side Effects
  2874. # This closes the connection upon error
  2875. proc http::CopyStart {sock token {initial 1}} {
  2876. upvar #0 $token state
  2877. if {[info exists state(transfer)] && $state(transfer) eq "chunked"} {
  2878. foreach coding [ContentEncoding $token] {
  2879. lappend state(zlib) [zlib stream $coding]
  2880. }
  2881. make-transformation-chunked $sock [namespace code [list CopyChunk $token]]
  2882. } else {
  2883. if {$initial} {
  2884. foreach coding [ContentEncoding $token] {
  2885. zlib push $coding $sock
  2886. }
  2887. }
  2888. if {[catch {
  2889. # FIXME Keep-Alive on https tls::socket with unchunked transfer
  2890. # hangs until the server times out. A workaround is possible, as for
  2891. # the case without -channel, but it does not use the neat "fcopy"
  2892. # solution.
  2893. fcopy $sock $state(-channel) -size $state(-blocksize) -command \
  2894. [list http::CopyDone $token]
  2895. } err]} {
  2896. Finish $token $err
  2897. }
  2898. }
  2899. }
  2900. proc http::CopyChunk {token chunk} {
  2901. upvar 0 $token state
  2902. if {[set count [string length $chunk]]} {
  2903. incr state(currentsize) $count
  2904. if {[info exists state(zlib)]} {
  2905. foreach stream $state(zlib) {
  2906. set chunk [$stream add $chunk]
  2907. }
  2908. }
  2909. puts -nonewline $state(-channel) $chunk
  2910. if {[info exists state(-progress)]} {
  2911. eval [linsert $state(-progress) end \
  2912. $token $state(totalsize) $state(currentsize)]
  2913. }
  2914. } else {
  2915. Log "CopyChunk Finish - token $token"
  2916. if {[info exists state(zlib)]} {
  2917. set excess ""
  2918. foreach stream $state(zlib) {
  2919. catch {set excess [$stream add -finalize $excess]}
  2920. }
  2921. puts -nonewline $state(-channel) $excess
  2922. foreach stream $state(zlib) { $stream close }
  2923. unset state(zlib)
  2924. }
  2925. Eot $token ;# FIX ME: pipelining.
  2926. }
  2927. }
  2928. # http::CopyDone
  2929. #
  2930. # fcopy completion callback
  2931. #
  2932. # Arguments
  2933. # token The token returned from http::geturl
  2934. # count The amount transfered
  2935. #
  2936. # Side Effects
  2937. # Invokes callbacks
  2938. proc http::CopyDone {token count {error {}}} {
  2939. variable $token
  2940. upvar 0 $token state
  2941. set sock $state(sock)
  2942. incr state(currentsize) $count
  2943. if {[info exists state(-progress)]} {
  2944. eval $state(-progress) \
  2945. [list $token $state(totalsize) $state(currentsize)]
  2946. }
  2947. # At this point the token may have been reset.
  2948. if {[string length $error]} {
  2949. Finish $token $error
  2950. } elseif {[catch {eof $sock} iseof] || $iseof} {
  2951. Eot $token
  2952. } else {
  2953. CopyStart $sock $token 0
  2954. }
  2955. }
  2956. # http::Eot
  2957. #
  2958. # Called when either:
  2959. # a. An eof condition is detected on the socket.
  2960. # b. The client decides that the response is complete.
  2961. # c. The client detects an inconsistency and aborts the transaction.
  2962. #
  2963. # Does:
  2964. # 1. Set state(status)
  2965. # 2. Reverse any Content-Encoding
  2966. # 3. Convert charset encoding and line ends if necessary
  2967. # 4. Call http::Finish
  2968. #
  2969. # Arguments
  2970. # token The token returned from http::geturl
  2971. # force (previously) optional, has no effect
  2972. # reason - "eof" means premature EOF (not EOF as the natural end of
  2973. # the response)
  2974. # - "" means completion of response, with or without EOF
  2975. # - anything else describes an error confition other than
  2976. # premature EOF.
  2977. #
  2978. # Side Effects
  2979. # Clean up the socket
  2980. proc http::Eot {token {reason {}}} {
  2981. variable $token
  2982. upvar 0 $token state
  2983. if {$reason eq "eof"} {
  2984. # Premature eof.
  2985. set state(status) eof
  2986. set reason {}
  2987. } elseif {$reason ne ""} {
  2988. # Abort the transaction.
  2989. set state(status) $reason
  2990. } else {
  2991. # The response is complete.
  2992. set state(status) ok
  2993. }
  2994. if {[string length $state(body)] > 0} {
  2995. if {[catch {
  2996. foreach coding [ContentEncoding $token] {
  2997. set state(body) [zlib $coding $state(body)]
  2998. }
  2999. } err]} {
  3000. Log "error doing decompression for token $token: $err"
  3001. Finish $token $err
  3002. return
  3003. }
  3004. if {!$state(binary)} {
  3005. # If we are getting text, set the incoming channel's encoding
  3006. # correctly. iso8859-1 is the RFC default, but this could be any
  3007. # IANA charset. However, we only know how to convert what we have
  3008. # encodings for.
  3009. set enc [CharsetToEncoding $state(charset)]
  3010. if {$enc ne "binary"} {
  3011. set state(body) [encoding convertfrom $enc $state(body)]
  3012. }
  3013. # Translate text line endings.
  3014. set state(body) [string map {\r\n \n \r \n} $state(body)]
  3015. }
  3016. }
  3017. Finish $token $reason
  3018. }
  3019. # http::wait --
  3020. #
  3021. # See documentation for details.
  3022. #
  3023. # Arguments:
  3024. # token Connection token.
  3025. #
  3026. # Results:
  3027. # The status after the wait.
  3028. proc http::wait {token} {
  3029. variable $token
  3030. upvar 0 $token state
  3031. if {![info exists state(status)] || $state(status) eq ""} {
  3032. # We must wait on the original variable name, not the upvar alias
  3033. vwait ${token}(status)
  3034. }
  3035. return [status $token]
  3036. }
  3037. # http::formatQuery --
  3038. #
  3039. # See documentation for details. Call http::formatQuery with an even
  3040. # number of arguments, where the first is a name, the second is a value,
  3041. # the third is another name, and so on.
  3042. #
  3043. # Arguments:
  3044. # args A list of name-value pairs.
  3045. #
  3046. # Results:
  3047. # TODO
  3048. proc http::formatQuery {args} {
  3049. if {[llength $args] % 2} {
  3050. return \
  3051. -code error \
  3052. -errorcode [list HTTP BADARGCNT $args] \
  3053. {Incorrect number of arguments, must be an even number.}
  3054. }
  3055. set result ""
  3056. set sep ""
  3057. foreach i $args {
  3058. append result $sep [mapReply $i]
  3059. if {$sep eq "="} {
  3060. set sep &
  3061. } else {
  3062. set sep =
  3063. }
  3064. }
  3065. return $result
  3066. }
  3067. # http::mapReply --
  3068. #
  3069. # Do x-www-urlencoded character mapping
  3070. #
  3071. # Arguments:
  3072. # string The string the needs to be encoded
  3073. #
  3074. # Results:
  3075. # The encoded string
  3076. proc http::mapReply {string} {
  3077. variable http
  3078. variable formMap
  3079. # The spec says: "non-alphanumeric characters are replaced by '%HH'". Use
  3080. # a pre-computed map and [string map] to do the conversion (much faster
  3081. # than [regsub]/[subst]). [Bug 1020491]
  3082. if {$http(-urlencoding) ne ""} {
  3083. set string [encoding convertto $http(-urlencoding) $string]
  3084. return [string map $formMap $string]
  3085. }
  3086. set converted [string map $formMap $string]
  3087. if {[string match "*\[\u0100-\uffff\]*" $converted]} {
  3088. regexp "\[\u0100-\uffff\]" $converted badChar
  3089. # Return this error message for maximum compatibility... :^/
  3090. return -code error \
  3091. "can't read \"formMap($badChar)\": no such element in array"
  3092. }
  3093. return $converted
  3094. }
  3095. interp alias {} http::quoteString {} http::mapReply
  3096. # http::ProxyRequired --
  3097. # Default proxy filter.
  3098. #
  3099. # Arguments:
  3100. # host The destination host
  3101. #
  3102. # Results:
  3103. # The current proxy settings
  3104. proc http::ProxyRequired {host} {
  3105. variable http
  3106. if {[info exists http(-proxyhost)] && [string length $http(-proxyhost)]} {
  3107. if {
  3108. ![info exists http(-proxyport)] ||
  3109. ![string length $http(-proxyport)]
  3110. } {
  3111. set http(-proxyport) 8080
  3112. }
  3113. return [list $http(-proxyhost) $http(-proxyport)]
  3114. }
  3115. }
  3116. # http::CharsetToEncoding --
  3117. #
  3118. # Tries to map a given IANA charset to a tcl encoding. If no encoding
  3119. # can be found, returns binary.
  3120. #
  3121. proc http::CharsetToEncoding {charset} {
  3122. variable encodings
  3123. set charset [string tolower $charset]
  3124. if {[regexp {iso-?8859-([0-9]+)} $charset -> num]} {
  3125. set encoding "iso8859-$num"
  3126. } elseif {[regexp {iso-?2022-(jp|kr)} $charset -> ext]} {
  3127. set encoding "iso2022-$ext"
  3128. } elseif {[regexp {shift[-_]?js} $charset]} {
  3129. set encoding "shiftjis"
  3130. } elseif {[regexp {(?:windows|cp)-?([0-9]+)} $charset -> num]} {
  3131. set encoding "cp$num"
  3132. } elseif {$charset eq "us-ascii"} {
  3133. set encoding "ascii"
  3134. } elseif {[regexp {(?:iso-?)?lat(?:in)?-?([0-9]+)} $charset -> num]} {
  3135. switch -- $num {
  3136. 5 {set encoding "iso8859-9"}
  3137. 1 - 2 - 3 {
  3138. set encoding "iso8859-$num"
  3139. }
  3140. }
  3141. } else {
  3142. # other charset, like euc-xx, utf-8,... may directly map to encoding
  3143. set encoding $charset
  3144. }
  3145. set idx [lsearch -exact $encodings $encoding]
  3146. if {$idx >= 0} {
  3147. return $encoding
  3148. } else {
  3149. return "binary"
  3150. }
  3151. }
  3152. # Return the list of content-encoding transformations we need to do in order.
  3153. proc http::ContentEncoding {token} {
  3154. upvar 0 $token state
  3155. set r {}
  3156. if {[info exists state(coding)]} {
  3157. foreach coding [split $state(coding) ,] {
  3158. switch -exact -- $coding {
  3159. deflate { lappend r inflate }
  3160. gzip - x-gzip { lappend r gunzip }
  3161. compress - x-compress { lappend r decompress }
  3162. identity {}
  3163. default {
  3164. return -code error "unsupported content-encoding \"$coding\""
  3165. }
  3166. }
  3167. }
  3168. }
  3169. return $r
  3170. }
  3171. proc http::ReceiveChunked {chan command} {
  3172. set data ""
  3173. set size -1
  3174. yield
  3175. while {1} {
  3176. chan configure $chan -translation {crlf binary}
  3177. while {[gets $chan line] < 1} { yield }
  3178. chan configure $chan -translation {binary binary}
  3179. if {[scan $line %x size] != 1} {
  3180. return -code error "invalid size: \"$line\""
  3181. }
  3182. set chunk ""
  3183. while {$size && ![chan eof $chan]} {
  3184. set part [chan read $chan $size]
  3185. incr size -[string length $part]
  3186. append chunk $part
  3187. }
  3188. if {[catch {
  3189. uplevel #0 [linsert $command end $chunk]
  3190. }]} {
  3191. http::Log "Error in callback: $::errorInfo"
  3192. }
  3193. if {[string length $chunk] == 0} {
  3194. # channel might have been closed in the callback
  3195. catch {chan event $chan readable {}}
  3196. return
  3197. }
  3198. }
  3199. }
  3200. proc http::make-transformation-chunked {chan command} {
  3201. coroutine [namespace current]::dechunk$chan ::http::ReceiveChunked $chan $command
  3202. chan event $chan readable [namespace current]::dechunk$chan
  3203. }
  3204. # Local variables:
  3205. # indent-tabs-mode: t
  3206. # End: