2
0

http.c 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_HTTP
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_NET_IF_H
  36. #include <net/if.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #ifdef HAVE_SYS_PARAM_H
  42. #include <sys/param.h>
  43. #endif
  44. #ifdef USE_HYPER
  45. #include <hyper.h>
  46. #endif
  47. #include "urldata.h"
  48. #include <curl/curl.h>
  49. #include "transfer.h"
  50. #include "sendf.h"
  51. #include "formdata.h"
  52. #include "mime.h"
  53. #include "progress.h"
  54. #include "curl_base64.h"
  55. #include "cookie.h"
  56. #include "vauth/vauth.h"
  57. #include "vtls/vtls.h"
  58. #include "vquic/vquic.h"
  59. #include "http_digest.h"
  60. #include "http_ntlm.h"
  61. #include "curl_ntlm_wb.h"
  62. #include "http_negotiate.h"
  63. #include "http_aws_sigv4.h"
  64. #include "url.h"
  65. #include "share.h"
  66. #include "hostip.h"
  67. #include "dynhds.h"
  68. #include "http.h"
  69. #include "select.h"
  70. #include "parsedate.h" /* for the week day and month names */
  71. #include "strtoofft.h"
  72. #include "multiif.h"
  73. #include "strcase.h"
  74. #include "content_encoding.h"
  75. #include "http_proxy.h"
  76. #include "warnless.h"
  77. #include "http2.h"
  78. #include "cfilters.h"
  79. #include "connect.h"
  80. #include "strdup.h"
  81. #include "altsvc.h"
  82. #include "hsts.h"
  83. #include "ws.h"
  84. #include "c-hyper.h"
  85. #include "curl_ctype.h"
  86. /* The last 3 #include files should be in this order */
  87. #include "curl_printf.h"
  88. #include "curl_memory.h"
  89. #include "memdebug.h"
  90. /*
  91. * Forward declarations.
  92. */
  93. static int http_getsock_do(struct Curl_easy *data,
  94. struct connectdata *conn,
  95. curl_socket_t *socks);
  96. static bool http_should_fail(struct Curl_easy *data);
  97. static CURLcode http_setup_conn(struct Curl_easy *data,
  98. struct connectdata *conn);
  99. #ifdef USE_WEBSOCKETS
  100. static CURLcode ws_setup_conn(struct Curl_easy *data,
  101. struct connectdata *conn);
  102. #endif
  103. /*
  104. * HTTP handler interface.
  105. */
  106. const struct Curl_handler Curl_handler_http = {
  107. "HTTP", /* scheme */
  108. http_setup_conn, /* setup_connection */
  109. Curl_http, /* do_it */
  110. Curl_http_done, /* done */
  111. ZERO_NULL, /* do_more */
  112. Curl_http_connect, /* connect_it */
  113. ZERO_NULL, /* connecting */
  114. ZERO_NULL, /* doing */
  115. ZERO_NULL, /* proto_getsock */
  116. http_getsock_do, /* doing_getsock */
  117. ZERO_NULL, /* domore_getsock */
  118. ZERO_NULL, /* perform_getsock */
  119. ZERO_NULL, /* disconnect */
  120. ZERO_NULL, /* readwrite */
  121. ZERO_NULL, /* connection_check */
  122. ZERO_NULL, /* attach connection */
  123. PORT_HTTP, /* defport */
  124. CURLPROTO_HTTP, /* protocol */
  125. CURLPROTO_HTTP, /* family */
  126. PROTOPT_CREDSPERREQUEST | /* flags */
  127. PROTOPT_USERPWDCTRL
  128. };
  129. #ifdef USE_WEBSOCKETS
  130. const struct Curl_handler Curl_handler_ws = {
  131. "WS", /* scheme */
  132. ws_setup_conn, /* setup_connection */
  133. Curl_http, /* do_it */
  134. Curl_http_done, /* done */
  135. ZERO_NULL, /* do_more */
  136. Curl_http_connect, /* connect_it */
  137. ZERO_NULL, /* connecting */
  138. ZERO_NULL, /* doing */
  139. ZERO_NULL, /* proto_getsock */
  140. http_getsock_do, /* doing_getsock */
  141. ZERO_NULL, /* domore_getsock */
  142. ZERO_NULL, /* perform_getsock */
  143. Curl_ws_disconnect, /* disconnect */
  144. ZERO_NULL, /* readwrite */
  145. ZERO_NULL, /* connection_check */
  146. ZERO_NULL, /* attach connection */
  147. PORT_HTTP, /* defport */
  148. CURLPROTO_WS, /* protocol */
  149. CURLPROTO_HTTP, /* family */
  150. PROTOPT_CREDSPERREQUEST | /* flags */
  151. PROTOPT_USERPWDCTRL
  152. };
  153. #endif
  154. #ifdef USE_SSL
  155. /*
  156. * HTTPS handler interface.
  157. */
  158. const struct Curl_handler Curl_handler_https = {
  159. "HTTPS", /* scheme */
  160. http_setup_conn, /* setup_connection */
  161. Curl_http, /* do_it */
  162. Curl_http_done, /* done */
  163. ZERO_NULL, /* do_more */
  164. Curl_http_connect, /* connect_it */
  165. NULL, /* connecting */
  166. ZERO_NULL, /* doing */
  167. NULL, /* proto_getsock */
  168. http_getsock_do, /* doing_getsock */
  169. ZERO_NULL, /* domore_getsock */
  170. ZERO_NULL, /* perform_getsock */
  171. ZERO_NULL, /* disconnect */
  172. ZERO_NULL, /* readwrite */
  173. ZERO_NULL, /* connection_check */
  174. ZERO_NULL, /* attach connection */
  175. PORT_HTTPS, /* defport */
  176. CURLPROTO_HTTPS, /* protocol */
  177. CURLPROTO_HTTP, /* family */
  178. PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | PROTOPT_ALPN | /* flags */
  179. PROTOPT_USERPWDCTRL
  180. };
  181. #ifdef USE_WEBSOCKETS
  182. const struct Curl_handler Curl_handler_wss = {
  183. "WSS", /* scheme */
  184. ws_setup_conn, /* setup_connection */
  185. Curl_http, /* do_it */
  186. Curl_http_done, /* done */
  187. ZERO_NULL, /* do_more */
  188. Curl_http_connect, /* connect_it */
  189. NULL, /* connecting */
  190. ZERO_NULL, /* doing */
  191. NULL, /* proto_getsock */
  192. http_getsock_do, /* doing_getsock */
  193. ZERO_NULL, /* domore_getsock */
  194. ZERO_NULL, /* perform_getsock */
  195. Curl_ws_disconnect, /* disconnect */
  196. ZERO_NULL, /* readwrite */
  197. ZERO_NULL, /* connection_check */
  198. ZERO_NULL, /* attach connection */
  199. PORT_HTTPS, /* defport */
  200. CURLPROTO_WSS, /* protocol */
  201. CURLPROTO_HTTP, /* family */
  202. PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | /* flags */
  203. PROTOPT_USERPWDCTRL
  204. };
  205. #endif
  206. #endif
  207. static CURLcode http_setup_conn(struct Curl_easy *data,
  208. struct connectdata *conn)
  209. {
  210. /* allocate the HTTP-specific struct for the Curl_easy, only to survive
  211. during this request */
  212. struct HTTP *http;
  213. DEBUGASSERT(data->req.p.http == NULL);
  214. http = calloc(1, sizeof(struct HTTP));
  215. if(!http)
  216. return CURLE_OUT_OF_MEMORY;
  217. Curl_mime_initpart(&http->form);
  218. data->req.p.http = http;
  219. connkeep(conn, "HTTP default");
  220. if(data->state.httpwant == CURL_HTTP_VERSION_3ONLY) {
  221. CURLcode result = Curl_conn_may_http3(data, conn);
  222. if(result)
  223. return result;
  224. }
  225. return CURLE_OK;
  226. }
  227. #ifdef USE_WEBSOCKETS
  228. static CURLcode ws_setup_conn(struct Curl_easy *data,
  229. struct connectdata *conn)
  230. {
  231. /* websockets is 1.1 only (for now) */
  232. data->state.httpwant = CURL_HTTP_VERSION_1_1;
  233. return http_setup_conn(data, conn);
  234. }
  235. #endif
  236. #ifndef CURL_DISABLE_PROXY
  237. /*
  238. * checkProxyHeaders() checks the linked list of custom proxy headers
  239. * if proxy headers are not available, then it will lookup into http header
  240. * link list
  241. *
  242. * It takes a connectdata struct as input to see if this is a proxy request or
  243. * not, as it then might check a different header list. Provide the header
  244. * prefix without colon!
  245. */
  246. char *Curl_checkProxyheaders(struct Curl_easy *data,
  247. const struct connectdata *conn,
  248. const char *thisheader,
  249. const size_t thislen)
  250. {
  251. struct curl_slist *head;
  252. for(head = (conn->bits.proxy && data->set.sep_headers) ?
  253. data->set.proxyheaders : data->set.headers;
  254. head; head = head->next) {
  255. if(strncasecompare(head->data, thisheader, thislen) &&
  256. Curl_headersep(head->data[thislen]))
  257. return head->data;
  258. }
  259. return NULL;
  260. }
  261. #else
  262. /* disabled */
  263. #define Curl_checkProxyheaders(x,y,z,a) NULL
  264. #endif
  265. /*
  266. * Strip off leading and trailing whitespace from the value in the
  267. * given HTTP header line and return a strdupped copy. Returns NULL in
  268. * case of allocation failure. Returns an empty string if the header value
  269. * consists entirely of whitespace.
  270. */
  271. char *Curl_copy_header_value(const char *header)
  272. {
  273. const char *start;
  274. const char *end;
  275. char *value;
  276. size_t len;
  277. /* Find the end of the header name */
  278. while(*header && (*header != ':'))
  279. ++header;
  280. if(*header)
  281. /* Skip over colon */
  282. ++header;
  283. /* Find the first non-space letter */
  284. start = header;
  285. while(*start && ISSPACE(*start))
  286. start++;
  287. /* data is in the host encoding so
  288. use '\r' and '\n' instead of 0x0d and 0x0a */
  289. end = strchr(start, '\r');
  290. if(!end)
  291. end = strchr(start, '\n');
  292. if(!end)
  293. end = strchr(start, '\0');
  294. if(!end)
  295. return NULL;
  296. /* skip all trailing space letters */
  297. while((end > start) && ISSPACE(*end))
  298. end--;
  299. /* get length of the type */
  300. len = end - start + 1;
  301. value = malloc(len + 1);
  302. if(!value)
  303. return NULL;
  304. memcpy(value, start, len);
  305. value[len] = 0; /* null-terminate */
  306. return value;
  307. }
  308. #ifndef CURL_DISABLE_HTTP_AUTH
  309. /*
  310. * http_output_basic() sets up an Authorization: header (or the proxy version)
  311. * for HTTP Basic authentication.
  312. *
  313. * Returns CURLcode.
  314. */
  315. static CURLcode http_output_basic(struct Curl_easy *data, bool proxy)
  316. {
  317. size_t size = 0;
  318. char *authorization = NULL;
  319. char **userp;
  320. const char *user;
  321. const char *pwd;
  322. CURLcode result;
  323. char *out;
  324. /* credentials are unique per transfer for HTTP, do not use the ones for the
  325. connection */
  326. if(proxy) {
  327. #ifndef CURL_DISABLE_PROXY
  328. userp = &data->state.aptr.proxyuserpwd;
  329. user = data->state.aptr.proxyuser;
  330. pwd = data->state.aptr.proxypasswd;
  331. #else
  332. return CURLE_NOT_BUILT_IN;
  333. #endif
  334. }
  335. else {
  336. userp = &data->state.aptr.userpwd;
  337. user = data->state.aptr.user;
  338. pwd = data->state.aptr.passwd;
  339. }
  340. out = aprintf("%s:%s", user ? user : "", pwd ? pwd : "");
  341. if(!out)
  342. return CURLE_OUT_OF_MEMORY;
  343. result = Curl_base64_encode(out, strlen(out), &authorization, &size);
  344. if(result)
  345. goto fail;
  346. if(!authorization) {
  347. result = CURLE_REMOTE_ACCESS_DENIED;
  348. goto fail;
  349. }
  350. free(*userp);
  351. *userp = aprintf("%sAuthorization: Basic %s\r\n",
  352. proxy ? "Proxy-" : "",
  353. authorization);
  354. free(authorization);
  355. if(!*userp) {
  356. result = CURLE_OUT_OF_MEMORY;
  357. goto fail;
  358. }
  359. fail:
  360. free(out);
  361. return result;
  362. }
  363. /*
  364. * http_output_bearer() sets up an Authorization: header
  365. * for HTTP Bearer authentication.
  366. *
  367. * Returns CURLcode.
  368. */
  369. static CURLcode http_output_bearer(struct Curl_easy *data)
  370. {
  371. char **userp;
  372. CURLcode result = CURLE_OK;
  373. userp = &data->state.aptr.userpwd;
  374. free(*userp);
  375. *userp = aprintf("Authorization: Bearer %s\r\n",
  376. data->set.str[STRING_BEARER]);
  377. if(!*userp) {
  378. result = CURLE_OUT_OF_MEMORY;
  379. goto fail;
  380. }
  381. fail:
  382. return result;
  383. }
  384. #endif
  385. /* pickoneauth() selects the most favourable authentication method from the
  386. * ones available and the ones we want.
  387. *
  388. * return TRUE if one was picked
  389. */
  390. static bool pickoneauth(struct auth *pick, unsigned long mask)
  391. {
  392. bool picked;
  393. /* only deal with authentication we want */
  394. unsigned long avail = pick->avail & pick->want & mask;
  395. picked = TRUE;
  396. /* The order of these checks is highly relevant, as this will be the order
  397. of preference in case of the existence of multiple accepted types. */
  398. if(avail & CURLAUTH_NEGOTIATE)
  399. pick->picked = CURLAUTH_NEGOTIATE;
  400. else if(avail & CURLAUTH_BEARER)
  401. pick->picked = CURLAUTH_BEARER;
  402. else if(avail & CURLAUTH_DIGEST)
  403. pick->picked = CURLAUTH_DIGEST;
  404. else if(avail & CURLAUTH_NTLM)
  405. pick->picked = CURLAUTH_NTLM;
  406. else if(avail & CURLAUTH_NTLM_WB)
  407. pick->picked = CURLAUTH_NTLM_WB;
  408. else if(avail & CURLAUTH_BASIC)
  409. pick->picked = CURLAUTH_BASIC;
  410. else if(avail & CURLAUTH_AWS_SIGV4)
  411. pick->picked = CURLAUTH_AWS_SIGV4;
  412. else {
  413. pick->picked = CURLAUTH_PICKNONE; /* we select to use nothing */
  414. picked = FALSE;
  415. }
  416. pick->avail = CURLAUTH_NONE; /* clear it here */
  417. return picked;
  418. }
  419. /*
  420. * http_perhapsrewind()
  421. *
  422. * If we are doing POST or PUT {
  423. * If we have more data to send {
  424. * If we are doing NTLM {
  425. * Keep sending since we must not disconnect
  426. * }
  427. * else {
  428. * If there is more than just a little data left to send, close
  429. * the current connection by force.
  430. * }
  431. * }
  432. * If we have sent any data {
  433. * If we don't have track of all the data {
  434. * call app to tell it to rewind
  435. * }
  436. * else {
  437. * rewind internally so that the operation can restart fine
  438. * }
  439. * }
  440. * }
  441. */
  442. static CURLcode http_perhapsrewind(struct Curl_easy *data,
  443. struct connectdata *conn)
  444. {
  445. struct HTTP *http = data->req.p.http;
  446. curl_off_t bytessent;
  447. curl_off_t expectsend = -1; /* default is unknown */
  448. if(!http)
  449. /* If this is still NULL, we have not reach very far and we can safely
  450. skip this rewinding stuff */
  451. return CURLE_OK;
  452. switch(data->state.httpreq) {
  453. case HTTPREQ_GET:
  454. case HTTPREQ_HEAD:
  455. return CURLE_OK;
  456. default:
  457. break;
  458. }
  459. bytessent = data->req.writebytecount;
  460. if(conn->bits.authneg) {
  461. /* This is a state where we are known to be negotiating and we don't send
  462. any data then. */
  463. expectsend = 0;
  464. }
  465. else if(!conn->bits.protoconnstart) {
  466. /* HTTP CONNECT in progress: there is no body */
  467. expectsend = 0;
  468. }
  469. else {
  470. /* figure out how much data we are expected to send */
  471. switch(data->state.httpreq) {
  472. case HTTPREQ_POST:
  473. case HTTPREQ_PUT:
  474. if(data->state.infilesize != -1)
  475. expectsend = data->state.infilesize;
  476. break;
  477. case HTTPREQ_POST_FORM:
  478. case HTTPREQ_POST_MIME:
  479. expectsend = http->postsize;
  480. break;
  481. default:
  482. break;
  483. }
  484. }
  485. data->state.rewindbeforesend = FALSE; /* default */
  486. if((expectsend == -1) || (expectsend > bytessent)) {
  487. #if defined(USE_NTLM)
  488. /* There is still data left to send */
  489. if((data->state.authproxy.picked == CURLAUTH_NTLM) ||
  490. (data->state.authhost.picked == CURLAUTH_NTLM) ||
  491. (data->state.authproxy.picked == CURLAUTH_NTLM_WB) ||
  492. (data->state.authhost.picked == CURLAUTH_NTLM_WB)) {
  493. if(((expectsend - bytessent) < 2000) ||
  494. (conn->http_ntlm_state != NTLMSTATE_NONE) ||
  495. (conn->proxy_ntlm_state != NTLMSTATE_NONE)) {
  496. /* The NTLM-negotiation has started *OR* there is just a little (<2K)
  497. data left to send, keep on sending. */
  498. /* rewind data when completely done sending! */
  499. if(!conn->bits.authneg && (conn->writesockfd != CURL_SOCKET_BAD)) {
  500. data->state.rewindbeforesend = TRUE;
  501. infof(data, "Rewind stream before next send");
  502. }
  503. return CURLE_OK;
  504. }
  505. if(conn->bits.close)
  506. /* this is already marked to get closed */
  507. return CURLE_OK;
  508. infof(data, "NTLM send, close instead of sending %"
  509. CURL_FORMAT_CURL_OFF_T " bytes",
  510. (curl_off_t)(expectsend - bytessent));
  511. }
  512. #endif
  513. #if defined(USE_SPNEGO)
  514. /* There is still data left to send */
  515. if((data->state.authproxy.picked == CURLAUTH_NEGOTIATE) ||
  516. (data->state.authhost.picked == CURLAUTH_NEGOTIATE)) {
  517. if(((expectsend - bytessent) < 2000) ||
  518. (conn->http_negotiate_state != GSS_AUTHNONE) ||
  519. (conn->proxy_negotiate_state != GSS_AUTHNONE)) {
  520. /* The NEGOTIATE-negotiation has started *OR*
  521. there is just a little (<2K) data left to send, keep on sending. */
  522. /* rewind data when completely done sending! */
  523. if(!conn->bits.authneg && (conn->writesockfd != CURL_SOCKET_BAD)) {
  524. data->state.rewindbeforesend = TRUE;
  525. infof(data, "Rewind stream before next send");
  526. }
  527. return CURLE_OK;
  528. }
  529. if(conn->bits.close)
  530. /* this is already marked to get closed */
  531. return CURLE_OK;
  532. infof(data, "NEGOTIATE send, close instead of sending %"
  533. CURL_FORMAT_CURL_OFF_T " bytes",
  534. (curl_off_t)(expectsend - bytessent));
  535. }
  536. #endif
  537. /* This is not NEGOTIATE/NTLM or many bytes left to send: close */
  538. streamclose(conn, "Mid-auth HTTP and much data left to send");
  539. data->req.size = 0; /* don't download any more than 0 bytes */
  540. /* There still is data left to send, but this connection is marked for
  541. closure so we can safely do the rewind right now */
  542. }
  543. if(bytessent) {
  544. /* mark for rewind since if we already sent something */
  545. data->state.rewindbeforesend = TRUE;
  546. infof(data, "Please rewind output before next send");
  547. }
  548. return CURLE_OK;
  549. }
  550. /*
  551. * Curl_http_auth_act() gets called when all HTTP headers have been received
  552. * and it checks what authentication methods that are available and decides
  553. * which one (if any) to use. It will set 'newurl' if an auth method was
  554. * picked.
  555. */
  556. CURLcode Curl_http_auth_act(struct Curl_easy *data)
  557. {
  558. struct connectdata *conn = data->conn;
  559. bool pickhost = FALSE;
  560. bool pickproxy = FALSE;
  561. CURLcode result = CURLE_OK;
  562. unsigned long authmask = ~0ul;
  563. if(!data->set.str[STRING_BEARER])
  564. authmask &= (unsigned long)~CURLAUTH_BEARER;
  565. if(100 <= data->req.httpcode && data->req.httpcode <= 199)
  566. /* this is a transient response code, ignore */
  567. return CURLE_OK;
  568. if(data->state.authproblem)
  569. return data->set.http_fail_on_error?CURLE_HTTP_RETURNED_ERROR:CURLE_OK;
  570. if((data->state.aptr.user || data->set.str[STRING_BEARER]) &&
  571. ((data->req.httpcode == 401) ||
  572. (conn->bits.authneg && data->req.httpcode < 300))) {
  573. pickhost = pickoneauth(&data->state.authhost, authmask);
  574. if(!pickhost)
  575. data->state.authproblem = TRUE;
  576. if(data->state.authhost.picked == CURLAUTH_NTLM &&
  577. conn->httpversion > 11) {
  578. infof(data, "Forcing HTTP/1.1 for NTLM");
  579. connclose(conn, "Force HTTP/1.1 connection");
  580. data->state.httpwant = CURL_HTTP_VERSION_1_1;
  581. }
  582. }
  583. #ifndef CURL_DISABLE_PROXY
  584. if(conn->bits.proxy_user_passwd &&
  585. ((data->req.httpcode == 407) ||
  586. (conn->bits.authneg && data->req.httpcode < 300))) {
  587. pickproxy = pickoneauth(&data->state.authproxy,
  588. authmask & ~CURLAUTH_BEARER);
  589. if(!pickproxy)
  590. data->state.authproblem = TRUE;
  591. }
  592. #endif
  593. if(pickhost || pickproxy) {
  594. if((data->state.httpreq != HTTPREQ_GET) &&
  595. (data->state.httpreq != HTTPREQ_HEAD) &&
  596. !data->state.rewindbeforesend) {
  597. result = http_perhapsrewind(data, conn);
  598. if(result)
  599. return result;
  600. }
  601. /* In case this is GSS auth, the newurl field is already allocated so
  602. we must make sure to free it before allocating a new one. As figured
  603. out in bug #2284386 */
  604. Curl_safefree(data->req.newurl);
  605. data->req.newurl = strdup(data->state.url); /* clone URL */
  606. if(!data->req.newurl)
  607. return CURLE_OUT_OF_MEMORY;
  608. }
  609. else if((data->req.httpcode < 300) &&
  610. (!data->state.authhost.done) &&
  611. conn->bits.authneg) {
  612. /* no (known) authentication available,
  613. authentication is not "done" yet and
  614. no authentication seems to be required and
  615. we didn't try HEAD or GET */
  616. if((data->state.httpreq != HTTPREQ_GET) &&
  617. (data->state.httpreq != HTTPREQ_HEAD)) {
  618. data->req.newurl = strdup(data->state.url); /* clone URL */
  619. if(!data->req.newurl)
  620. return CURLE_OUT_OF_MEMORY;
  621. data->state.authhost.done = TRUE;
  622. }
  623. }
  624. if(http_should_fail(data)) {
  625. failf(data, "The requested URL returned error: %d",
  626. data->req.httpcode);
  627. result = CURLE_HTTP_RETURNED_ERROR;
  628. }
  629. return result;
  630. }
  631. #ifndef CURL_DISABLE_HTTP_AUTH
  632. /*
  633. * Output the correct authentication header depending on the auth type
  634. * and whether or not it is to a proxy.
  635. */
  636. static CURLcode
  637. output_auth_headers(struct Curl_easy *data,
  638. struct connectdata *conn,
  639. struct auth *authstatus,
  640. const char *request,
  641. const char *path,
  642. bool proxy)
  643. {
  644. const char *auth = NULL;
  645. CURLcode result = CURLE_OK;
  646. (void)conn;
  647. #ifdef CURL_DISABLE_CRYPTO_AUTH
  648. (void)request;
  649. (void)path;
  650. #endif
  651. #ifndef CURL_DISABLE_CRYPTO_AUTH
  652. if(authstatus->picked == CURLAUTH_AWS_SIGV4) {
  653. auth = "AWS_SIGV4";
  654. result = Curl_output_aws_sigv4(data, proxy);
  655. if(result)
  656. return result;
  657. }
  658. else
  659. #endif
  660. #ifdef USE_SPNEGO
  661. if(authstatus->picked == CURLAUTH_NEGOTIATE) {
  662. auth = "Negotiate";
  663. result = Curl_output_negotiate(data, conn, proxy);
  664. if(result)
  665. return result;
  666. }
  667. else
  668. #endif
  669. #ifdef USE_NTLM
  670. if(authstatus->picked == CURLAUTH_NTLM) {
  671. auth = "NTLM";
  672. result = Curl_output_ntlm(data, proxy);
  673. if(result)
  674. return result;
  675. }
  676. else
  677. #endif
  678. #if defined(USE_NTLM) && defined(NTLM_WB_ENABLED)
  679. if(authstatus->picked == CURLAUTH_NTLM_WB) {
  680. auth = "NTLM_WB";
  681. result = Curl_output_ntlm_wb(data, conn, proxy);
  682. if(result)
  683. return result;
  684. }
  685. else
  686. #endif
  687. #ifndef CURL_DISABLE_CRYPTO_AUTH
  688. if(authstatus->picked == CURLAUTH_DIGEST) {
  689. auth = "Digest";
  690. result = Curl_output_digest(data,
  691. proxy,
  692. (const unsigned char *)request,
  693. (const unsigned char *)path);
  694. if(result)
  695. return result;
  696. }
  697. else
  698. #endif
  699. if(authstatus->picked == CURLAUTH_BASIC) {
  700. /* Basic */
  701. if(
  702. #ifndef CURL_DISABLE_PROXY
  703. (proxy && conn->bits.proxy_user_passwd &&
  704. !Curl_checkProxyheaders(data, conn, STRCONST("Proxy-authorization"))) ||
  705. #endif
  706. (!proxy && data->state.aptr.user &&
  707. !Curl_checkheaders(data, STRCONST("Authorization")))) {
  708. auth = "Basic";
  709. result = http_output_basic(data, proxy);
  710. if(result)
  711. return result;
  712. }
  713. /* NOTE: this function should set 'done' TRUE, as the other auth
  714. functions work that way */
  715. authstatus->done = TRUE;
  716. }
  717. if(authstatus->picked == CURLAUTH_BEARER) {
  718. /* Bearer */
  719. if((!proxy && data->set.str[STRING_BEARER] &&
  720. !Curl_checkheaders(data, STRCONST("Authorization")))) {
  721. auth = "Bearer";
  722. result = http_output_bearer(data);
  723. if(result)
  724. return result;
  725. }
  726. /* NOTE: this function should set 'done' TRUE, as the other auth
  727. functions work that way */
  728. authstatus->done = TRUE;
  729. }
  730. if(auth) {
  731. #ifndef CURL_DISABLE_PROXY
  732. infof(data, "%s auth using %s with user '%s'",
  733. proxy ? "Proxy" : "Server", auth,
  734. proxy ? (data->state.aptr.proxyuser ?
  735. data->state.aptr.proxyuser : "") :
  736. (data->state.aptr.user ?
  737. data->state.aptr.user : ""));
  738. #else
  739. infof(data, "Server auth using %s with user '%s'",
  740. auth, data->state.aptr.user ?
  741. data->state.aptr.user : "");
  742. #endif
  743. authstatus->multipass = (!authstatus->done) ? TRUE : FALSE;
  744. }
  745. else
  746. authstatus->multipass = FALSE;
  747. return CURLE_OK;
  748. }
  749. /**
  750. * Curl_http_output_auth() setups the authentication headers for the
  751. * host/proxy and the correct authentication
  752. * method. data->state.authdone is set to TRUE when authentication is
  753. * done.
  754. *
  755. * @param conn all information about the current connection
  756. * @param request pointer to the request keyword
  757. * @param path pointer to the requested path; should include query part
  758. * @param proxytunnel boolean if this is the request setting up a "proxy
  759. * tunnel"
  760. *
  761. * @returns CURLcode
  762. */
  763. CURLcode
  764. Curl_http_output_auth(struct Curl_easy *data,
  765. struct connectdata *conn,
  766. const char *request,
  767. Curl_HttpReq httpreq,
  768. const char *path,
  769. bool proxytunnel) /* TRUE if this is the request setting
  770. up the proxy tunnel */
  771. {
  772. CURLcode result = CURLE_OK;
  773. struct auth *authhost;
  774. struct auth *authproxy;
  775. DEBUGASSERT(data);
  776. authhost = &data->state.authhost;
  777. authproxy = &data->state.authproxy;
  778. if(
  779. #ifndef CURL_DISABLE_PROXY
  780. (conn->bits.httpproxy && conn->bits.proxy_user_passwd) ||
  781. #endif
  782. data->state.aptr.user || data->set.str[STRING_BEARER])
  783. /* continue please */;
  784. else {
  785. authhost->done = TRUE;
  786. authproxy->done = TRUE;
  787. return CURLE_OK; /* no authentication with no user or password */
  788. }
  789. if(authhost->want && !authhost->picked)
  790. /* The app has selected one or more methods, but none has been picked
  791. so far by a server round-trip. Then we set the picked one to the
  792. want one, and if this is one single bit it'll be used instantly. */
  793. authhost->picked = authhost->want;
  794. if(authproxy->want && !authproxy->picked)
  795. /* The app has selected one or more methods, but none has been picked so
  796. far by a proxy round-trip. Then we set the picked one to the want one,
  797. and if this is one single bit it'll be used instantly. */
  798. authproxy->picked = authproxy->want;
  799. #ifndef CURL_DISABLE_PROXY
  800. /* Send proxy authentication header if needed */
  801. if(conn->bits.httpproxy &&
  802. (conn->bits.tunnel_proxy == (bit)proxytunnel)) {
  803. result = output_auth_headers(data, conn, authproxy, request, path, TRUE);
  804. if(result)
  805. return result;
  806. }
  807. else
  808. #else
  809. (void)proxytunnel;
  810. #endif /* CURL_DISABLE_PROXY */
  811. /* we have no proxy so let's pretend we're done authenticating
  812. with it */
  813. authproxy->done = TRUE;
  814. /* To prevent the user+password to get sent to other than the original host
  815. due to a location-follow */
  816. if(Curl_auth_allowed_to_host(data)
  817. #ifndef CURL_DISABLE_NETRC
  818. || conn->bits.netrc
  819. #endif
  820. )
  821. result = output_auth_headers(data, conn, authhost, request, path, FALSE);
  822. else
  823. authhost->done = TRUE;
  824. if(((authhost->multipass && !authhost->done) ||
  825. (authproxy->multipass && !authproxy->done)) &&
  826. (httpreq != HTTPREQ_GET) &&
  827. (httpreq != HTTPREQ_HEAD)) {
  828. /* Auth is required and we are not authenticated yet. Make a PUT or POST
  829. with content-length zero as a "probe". */
  830. conn->bits.authneg = TRUE;
  831. }
  832. else
  833. conn->bits.authneg = FALSE;
  834. return result;
  835. }
  836. #else
  837. /* when disabled */
  838. CURLcode
  839. Curl_http_output_auth(struct Curl_easy *data,
  840. struct connectdata *conn,
  841. const char *request,
  842. Curl_HttpReq httpreq,
  843. const char *path,
  844. bool proxytunnel)
  845. {
  846. (void)data;
  847. (void)conn;
  848. (void)request;
  849. (void)httpreq;
  850. (void)path;
  851. (void)proxytunnel;
  852. return CURLE_OK;
  853. }
  854. #endif
  855. /*
  856. * Curl_http_input_auth() deals with Proxy-Authenticate: and WWW-Authenticate:
  857. * headers. They are dealt with both in the transfer.c main loop and in the
  858. * proxy CONNECT loop.
  859. */
  860. static int is_valid_auth_separator(char ch)
  861. {
  862. return ch == '\0' || ch == ',' || ISSPACE(ch);
  863. }
  864. CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
  865. const char *auth) /* the first non-space */
  866. {
  867. /*
  868. * This resource requires authentication
  869. */
  870. struct connectdata *conn = data->conn;
  871. #ifdef USE_SPNEGO
  872. curlnegotiate *negstate = proxy ? &conn->proxy_negotiate_state :
  873. &conn->http_negotiate_state;
  874. #endif
  875. unsigned long *availp;
  876. struct auth *authp;
  877. (void) conn; /* In case conditionals make it unused. */
  878. if(proxy) {
  879. availp = &data->info.proxyauthavail;
  880. authp = &data->state.authproxy;
  881. }
  882. else {
  883. availp = &data->info.httpauthavail;
  884. authp = &data->state.authhost;
  885. }
  886. /*
  887. * Here we check if we want the specific single authentication (using ==) and
  888. * if we do, we initiate usage of it.
  889. *
  890. * If the provided authentication is wanted as one out of several accepted
  891. * types (using &), we OR this authentication type to the authavail
  892. * variable.
  893. *
  894. * Note:
  895. *
  896. * ->picked is first set to the 'want' value (one or more bits) before the
  897. * request is sent, and then it is again set _after_ all response 401/407
  898. * headers have been received but then only to a single preferred method
  899. * (bit).
  900. */
  901. while(*auth) {
  902. #ifdef USE_SPNEGO
  903. if(checkprefix("Negotiate", auth) && is_valid_auth_separator(auth[9])) {
  904. if((authp->avail & CURLAUTH_NEGOTIATE) ||
  905. Curl_auth_is_spnego_supported()) {
  906. *availp |= CURLAUTH_NEGOTIATE;
  907. authp->avail |= CURLAUTH_NEGOTIATE;
  908. if(authp->picked == CURLAUTH_NEGOTIATE) {
  909. CURLcode result = Curl_input_negotiate(data, conn, proxy, auth);
  910. if(!result) {
  911. free(data->req.newurl);
  912. data->req.newurl = strdup(data->state.url);
  913. if(!data->req.newurl)
  914. return CURLE_OUT_OF_MEMORY;
  915. data->state.authproblem = FALSE;
  916. /* we received a GSS auth token and we dealt with it fine */
  917. *negstate = GSS_AUTHRECV;
  918. }
  919. else
  920. data->state.authproblem = TRUE;
  921. }
  922. }
  923. }
  924. else
  925. #endif
  926. #ifdef USE_NTLM
  927. /* NTLM support requires the SSL crypto libs */
  928. if(checkprefix("NTLM", auth) && is_valid_auth_separator(auth[4])) {
  929. if((authp->avail & CURLAUTH_NTLM) ||
  930. (authp->avail & CURLAUTH_NTLM_WB) ||
  931. Curl_auth_is_ntlm_supported()) {
  932. *availp |= CURLAUTH_NTLM;
  933. authp->avail |= CURLAUTH_NTLM;
  934. if(authp->picked == CURLAUTH_NTLM ||
  935. authp->picked == CURLAUTH_NTLM_WB) {
  936. /* NTLM authentication is picked and activated */
  937. CURLcode result = Curl_input_ntlm(data, proxy, auth);
  938. if(!result) {
  939. data->state.authproblem = FALSE;
  940. #ifdef NTLM_WB_ENABLED
  941. if(authp->picked == CURLAUTH_NTLM_WB) {
  942. *availp &= ~CURLAUTH_NTLM;
  943. authp->avail &= ~CURLAUTH_NTLM;
  944. *availp |= CURLAUTH_NTLM_WB;
  945. authp->avail |= CURLAUTH_NTLM_WB;
  946. result = Curl_input_ntlm_wb(data, conn, proxy, auth);
  947. if(result) {
  948. infof(data, "Authentication problem. Ignoring this.");
  949. data->state.authproblem = TRUE;
  950. }
  951. }
  952. #endif
  953. }
  954. else {
  955. infof(data, "Authentication problem. Ignoring this.");
  956. data->state.authproblem = TRUE;
  957. }
  958. }
  959. }
  960. }
  961. else
  962. #endif
  963. #ifndef CURL_DISABLE_CRYPTO_AUTH
  964. if(checkprefix("Digest", auth) && is_valid_auth_separator(auth[6])) {
  965. if((authp->avail & CURLAUTH_DIGEST) != 0)
  966. infof(data, "Ignoring duplicate digest auth header.");
  967. else if(Curl_auth_is_digest_supported()) {
  968. CURLcode result;
  969. *availp |= CURLAUTH_DIGEST;
  970. authp->avail |= CURLAUTH_DIGEST;
  971. /* We call this function on input Digest headers even if Digest
  972. * authentication isn't activated yet, as we need to store the
  973. * incoming data from this header in case we are going to use
  974. * Digest */
  975. result = Curl_input_digest(data, proxy, auth);
  976. if(result) {
  977. infof(data, "Authentication problem. Ignoring this.");
  978. data->state.authproblem = TRUE;
  979. }
  980. }
  981. }
  982. else
  983. #endif
  984. if(checkprefix("Basic", auth) &&
  985. is_valid_auth_separator(auth[5])) {
  986. *availp |= CURLAUTH_BASIC;
  987. authp->avail |= CURLAUTH_BASIC;
  988. if(authp->picked == CURLAUTH_BASIC) {
  989. /* We asked for Basic authentication but got a 40X back
  990. anyway, which basically means our name+password isn't
  991. valid. */
  992. authp->avail = CURLAUTH_NONE;
  993. infof(data, "Authentication problem. Ignoring this.");
  994. data->state.authproblem = TRUE;
  995. }
  996. }
  997. else
  998. if(checkprefix("Bearer", auth) &&
  999. is_valid_auth_separator(auth[6])) {
  1000. *availp |= CURLAUTH_BEARER;
  1001. authp->avail |= CURLAUTH_BEARER;
  1002. if(authp->picked == CURLAUTH_BEARER) {
  1003. /* We asked for Bearer authentication but got a 40X back
  1004. anyway, which basically means our token isn't valid. */
  1005. authp->avail = CURLAUTH_NONE;
  1006. infof(data, "Authentication problem. Ignoring this.");
  1007. data->state.authproblem = TRUE;
  1008. }
  1009. }
  1010. /* there may be multiple methods on one line, so keep reading */
  1011. while(*auth && *auth != ',') /* read up to the next comma */
  1012. auth++;
  1013. if(*auth == ',') /* if we're on a comma, skip it */
  1014. auth++;
  1015. while(*auth && ISSPACE(*auth))
  1016. auth++;
  1017. }
  1018. return CURLE_OK;
  1019. }
  1020. /**
  1021. * http_should_fail() determines whether an HTTP response has gotten us
  1022. * into an error state or not.
  1023. *
  1024. * @param conn all information about the current connection
  1025. *
  1026. * @retval FALSE communications should continue
  1027. *
  1028. * @retval TRUE communications should not continue
  1029. */
  1030. static bool http_should_fail(struct Curl_easy *data)
  1031. {
  1032. int httpcode;
  1033. DEBUGASSERT(data);
  1034. DEBUGASSERT(data->conn);
  1035. httpcode = data->req.httpcode;
  1036. /*
  1037. ** If we haven't been asked to fail on error,
  1038. ** don't fail.
  1039. */
  1040. if(!data->set.http_fail_on_error)
  1041. return FALSE;
  1042. /*
  1043. ** Any code < 400 is never terminal.
  1044. */
  1045. if(httpcode < 400)
  1046. return FALSE;
  1047. /*
  1048. ** A 416 response to a resume request is presumably because the file is
  1049. ** already completely downloaded and thus not actually a fail.
  1050. */
  1051. if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
  1052. httpcode == 416)
  1053. return FALSE;
  1054. /*
  1055. ** Any code >= 400 that's not 401 or 407 is always
  1056. ** a terminal error
  1057. */
  1058. if((httpcode != 401) && (httpcode != 407))
  1059. return TRUE;
  1060. /*
  1061. ** All we have left to deal with is 401 and 407
  1062. */
  1063. DEBUGASSERT((httpcode == 401) || (httpcode == 407));
  1064. /*
  1065. ** Examine the current authentication state to see if this
  1066. ** is an error. The idea is for this function to get
  1067. ** called after processing all the headers in a response
  1068. ** message. So, if we've been to asked to authenticate a
  1069. ** particular stage, and we've done it, we're OK. But, if
  1070. ** we're already completely authenticated, it's not OK to
  1071. ** get another 401 or 407.
  1072. **
  1073. ** It is possible for authentication to go stale such that
  1074. ** the client needs to reauthenticate. Once that info is
  1075. ** available, use it here.
  1076. */
  1077. /*
  1078. ** Either we're not authenticating, or we're supposed to
  1079. ** be authenticating something else. This is an error.
  1080. */
  1081. if((httpcode == 401) && !data->state.aptr.user)
  1082. return TRUE;
  1083. #ifndef CURL_DISABLE_PROXY
  1084. if((httpcode == 407) && !data->conn->bits.proxy_user_passwd)
  1085. return TRUE;
  1086. #endif
  1087. return data->state.authproblem;
  1088. }
  1089. /*
  1090. * readmoredata() is a "fread() emulation" to provide POST and/or request
  1091. * data. It is used when a huge POST is to be made and the entire chunk wasn't
  1092. * sent in the first send(). This function will then be called from the
  1093. * transfer.c loop when more data is to be sent to the peer.
  1094. *
  1095. * Returns the amount of bytes it filled the buffer with.
  1096. */
  1097. static size_t readmoredata(char *buffer,
  1098. size_t size,
  1099. size_t nitems,
  1100. void *userp)
  1101. {
  1102. struct HTTP *http = (struct HTTP *)userp;
  1103. struct Curl_easy *data = http->backup.data;
  1104. size_t fullsize = size * nitems;
  1105. if(!http->postsize)
  1106. /* nothing to return */
  1107. return 0;
  1108. /* make sure that an HTTP request is never sent away chunked! */
  1109. data->req.forbidchunk = (http->sending == HTTPSEND_REQUEST)?TRUE:FALSE;
  1110. if(data->set.max_send_speed &&
  1111. (data->set.max_send_speed < (curl_off_t)fullsize) &&
  1112. (data->set.max_send_speed < http->postsize))
  1113. /* speed limit */
  1114. fullsize = (size_t)data->set.max_send_speed;
  1115. else if(http->postsize <= (curl_off_t)fullsize) {
  1116. memcpy(buffer, http->postdata, (size_t)http->postsize);
  1117. fullsize = (size_t)http->postsize;
  1118. if(http->backup.postsize) {
  1119. /* move backup data into focus and continue on that */
  1120. http->postdata = http->backup.postdata;
  1121. http->postsize = http->backup.postsize;
  1122. data->state.fread_func = http->backup.fread_func;
  1123. data->state.in = http->backup.fread_in;
  1124. http->sending++; /* move one step up */
  1125. http->backup.postsize = 0;
  1126. }
  1127. else
  1128. http->postsize = 0;
  1129. return fullsize;
  1130. }
  1131. memcpy(buffer, http->postdata, fullsize);
  1132. http->postdata += fullsize;
  1133. http->postsize -= fullsize;
  1134. return fullsize;
  1135. }
  1136. /*
  1137. * Curl_buffer_send() sends a header buffer and frees all associated
  1138. * memory. Body data may be appended to the header data if desired.
  1139. *
  1140. * Returns CURLcode
  1141. */
  1142. CURLcode Curl_buffer_send(struct dynbuf *in,
  1143. struct Curl_easy *data,
  1144. struct HTTP *http,
  1145. /* add the number of sent bytes to this
  1146. counter */
  1147. curl_off_t *bytes_written,
  1148. /* how much of the buffer contains body data */
  1149. curl_off_t included_body_bytes,
  1150. int socketindex)
  1151. {
  1152. ssize_t amount;
  1153. CURLcode result;
  1154. char *ptr;
  1155. size_t size;
  1156. struct connectdata *conn = data->conn;
  1157. size_t sendsize;
  1158. curl_socket_t sockfd;
  1159. size_t headersize;
  1160. DEBUGASSERT(socketindex <= SECONDARYSOCKET);
  1161. sockfd = Curl_conn_get_socket(data, socketindex);
  1162. /* The looping below is required since we use non-blocking sockets, but due
  1163. to the circumstances we will just loop and try again and again etc */
  1164. ptr = Curl_dyn_ptr(in);
  1165. size = Curl_dyn_len(in);
  1166. headersize = size - (size_t)included_body_bytes; /* the initial part that
  1167. isn't body is header */
  1168. DEBUGASSERT(size > (size_t)included_body_bytes);
  1169. if((conn->handler->flags & PROTOPT_SSL
  1170. #ifndef CURL_DISABLE_PROXY
  1171. || IS_HTTPS_PROXY(conn->http_proxy.proxytype)
  1172. #endif
  1173. )
  1174. && conn->httpversion < 20) {
  1175. /* Make sure this doesn't send more body bytes than what the max send
  1176. speed says. The request bytes do not count to the max speed.
  1177. */
  1178. if(data->set.max_send_speed &&
  1179. (included_body_bytes > data->set.max_send_speed)) {
  1180. curl_off_t overflow = included_body_bytes - data->set.max_send_speed;
  1181. DEBUGASSERT((size_t)overflow < size);
  1182. sendsize = size - (size_t)overflow;
  1183. }
  1184. else
  1185. sendsize = size;
  1186. /* OpenSSL is very picky and we must send the SAME buffer pointer to the
  1187. library when we attempt to re-send this buffer. Sending the same data
  1188. is not enough, we must use the exact same address. For this reason, we
  1189. must copy the data to the uploadbuffer first, since that is the buffer
  1190. we will be using if this send is retried later.
  1191. */
  1192. result = Curl_get_upload_buffer(data);
  1193. if(result) {
  1194. /* malloc failed, free memory and return to the caller */
  1195. Curl_dyn_free(in);
  1196. return result;
  1197. }
  1198. /* We never send more than upload_buffer_size bytes in one single chunk
  1199. when we speak HTTPS, as if only a fraction of it is sent now, this data
  1200. needs to fit into the normal read-callback buffer later on and that
  1201. buffer is using this size.
  1202. */
  1203. if(sendsize > (size_t)data->set.upload_buffer_size)
  1204. sendsize = (size_t)data->set.upload_buffer_size;
  1205. memcpy(data->state.ulbuf, ptr, sendsize);
  1206. ptr = data->state.ulbuf;
  1207. }
  1208. else {
  1209. #ifdef CURLDEBUG
  1210. /* Allow debug builds to override this logic to force short initial
  1211. sends
  1212. */
  1213. char *p = getenv("CURL_SMALLREQSEND");
  1214. if(p) {
  1215. size_t altsize = (size_t)strtoul(p, NULL, 10);
  1216. if(altsize)
  1217. sendsize = CURLMIN(size, altsize);
  1218. else
  1219. sendsize = size;
  1220. }
  1221. else
  1222. #endif
  1223. {
  1224. /* Make sure this doesn't send more body bytes than what the max send
  1225. speed says. The request bytes do not count to the max speed.
  1226. */
  1227. if(data->set.max_send_speed &&
  1228. (included_body_bytes > data->set.max_send_speed)) {
  1229. curl_off_t overflow = included_body_bytes - data->set.max_send_speed;
  1230. DEBUGASSERT((size_t)overflow < size);
  1231. sendsize = size - (size_t)overflow;
  1232. }
  1233. else
  1234. sendsize = size;
  1235. }
  1236. }
  1237. result = Curl_write(data, sockfd, ptr, sendsize, &amount);
  1238. if(!result) {
  1239. /*
  1240. * Note that we may not send the entire chunk at once, and we have a set
  1241. * number of data bytes at the end of the big buffer (out of which we may
  1242. * only send away a part).
  1243. */
  1244. /* how much of the header that was sent */
  1245. size_t headlen = (size_t)amount>headersize ? headersize : (size_t)amount;
  1246. size_t bodylen = amount - headlen;
  1247. /* this data _may_ contain binary stuff */
  1248. Curl_debug(data, CURLINFO_HEADER_OUT, ptr, headlen);
  1249. if(bodylen)
  1250. /* there was body data sent beyond the initial header part, pass that on
  1251. to the debug callback too */
  1252. Curl_debug(data, CURLINFO_DATA_OUT, ptr + headlen, bodylen);
  1253. /* 'amount' can never be a very large value here so typecasting it so a
  1254. signed 31 bit value should not cause problems even if ssize_t is
  1255. 64bit */
  1256. *bytes_written += (long)amount;
  1257. if(http) {
  1258. /* if we sent a piece of the body here, up the byte counter for it
  1259. accordingly */
  1260. data->req.writebytecount += bodylen;
  1261. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  1262. if((size_t)amount != size) {
  1263. /* The whole request could not be sent in one system call. We must
  1264. queue it up and send it later when we get the chance. We must not
  1265. loop here and wait until it might work again. */
  1266. size -= amount;
  1267. ptr = Curl_dyn_ptr(in) + amount;
  1268. /* backup the currently set pointers */
  1269. http->backup.fread_func = data->state.fread_func;
  1270. http->backup.fread_in = data->state.in;
  1271. http->backup.postdata = http->postdata;
  1272. http->backup.postsize = http->postsize;
  1273. http->backup.data = data;
  1274. /* set the new pointers for the request-sending */
  1275. data->state.fread_func = (curl_read_callback)readmoredata;
  1276. data->state.in = (void *)http;
  1277. http->postdata = ptr;
  1278. http->postsize = (curl_off_t)size;
  1279. /* this much data is remaining header: */
  1280. data->req.pendingheader = headersize - headlen;
  1281. http->send_buffer = *in; /* copy the whole struct */
  1282. http->sending = HTTPSEND_REQUEST;
  1283. return CURLE_OK;
  1284. }
  1285. http->sending = HTTPSEND_BODY;
  1286. /* the full buffer was sent, clean up and return */
  1287. }
  1288. else {
  1289. if((size_t)amount != size)
  1290. /* We have no continue-send mechanism now, fail. This can only happen
  1291. when this function is used from the CONNECT sending function. We
  1292. currently (stupidly) assume that the whole request is always sent
  1293. away in the first single chunk.
  1294. This needs FIXing.
  1295. */
  1296. return CURLE_SEND_ERROR;
  1297. }
  1298. }
  1299. Curl_dyn_free(in);
  1300. /* no remaining header data */
  1301. data->req.pendingheader = 0;
  1302. return result;
  1303. }
  1304. /* end of the add_buffer functions */
  1305. /* ------------------------------------------------------------------------- */
  1306. /*
  1307. * Curl_compareheader()
  1308. *
  1309. * Returns TRUE if 'headerline' contains the 'header' with given 'content'.
  1310. * Pass headers WITH the colon.
  1311. */
  1312. bool
  1313. Curl_compareheader(const char *headerline, /* line to check */
  1314. const char *header, /* header keyword _with_ colon */
  1315. const size_t hlen, /* len of the keyword in bytes */
  1316. const char *content, /* content string to find */
  1317. const size_t clen) /* len of the content in bytes */
  1318. {
  1319. /* RFC2616, section 4.2 says: "Each header field consists of a name followed
  1320. * by a colon (":") and the field value. Field names are case-insensitive.
  1321. * The field value MAY be preceded by any amount of LWS, though a single SP
  1322. * is preferred." */
  1323. size_t len;
  1324. const char *start;
  1325. const char *end;
  1326. DEBUGASSERT(hlen);
  1327. DEBUGASSERT(clen);
  1328. DEBUGASSERT(header);
  1329. DEBUGASSERT(content);
  1330. if(!strncasecompare(headerline, header, hlen))
  1331. return FALSE; /* doesn't start with header */
  1332. /* pass the header */
  1333. start = &headerline[hlen];
  1334. /* pass all whitespace */
  1335. while(*start && ISSPACE(*start))
  1336. start++;
  1337. /* find the end of the header line */
  1338. end = strchr(start, '\r'); /* lines end with CRLF */
  1339. if(!end) {
  1340. /* in case there's a non-standard compliant line here */
  1341. end = strchr(start, '\n');
  1342. if(!end)
  1343. /* hm, there's no line ending here, use the zero byte! */
  1344. end = strchr(start, '\0');
  1345. }
  1346. len = end-start; /* length of the content part of the input line */
  1347. /* find the content string in the rest of the line */
  1348. for(; len >= clen; len--, start++) {
  1349. if(strncasecompare(start, content, clen))
  1350. return TRUE; /* match! */
  1351. }
  1352. return FALSE; /* no match */
  1353. }
  1354. /*
  1355. * Curl_http_connect() performs HTTP stuff to do at connect-time, called from
  1356. * the generic Curl_connect().
  1357. */
  1358. CURLcode Curl_http_connect(struct Curl_easy *data, bool *done)
  1359. {
  1360. struct connectdata *conn = data->conn;
  1361. /* We default to persistent connections. We set this already in this connect
  1362. function to make the re-use checks properly be able to check this bit. */
  1363. connkeep(conn, "HTTP default");
  1364. return Curl_conn_connect(data, FIRSTSOCKET, FALSE, done);
  1365. }
  1366. /* this returns the socket to wait for in the DO and DOING state for the multi
  1367. interface and then we're always _sending_ a request and thus we wait for
  1368. the single socket to become writable only */
  1369. static int http_getsock_do(struct Curl_easy *data,
  1370. struct connectdata *conn,
  1371. curl_socket_t *socks)
  1372. {
  1373. /* write mode */
  1374. (void)conn;
  1375. socks[0] = Curl_conn_get_socket(data, FIRSTSOCKET);
  1376. return GETSOCK_WRITESOCK(0);
  1377. }
  1378. /*
  1379. * Curl_http_done() gets called after a single HTTP request has been
  1380. * performed.
  1381. */
  1382. CURLcode Curl_http_done(struct Curl_easy *data,
  1383. CURLcode status, bool premature)
  1384. {
  1385. struct connectdata *conn = data->conn;
  1386. struct HTTP *http = data->req.p.http;
  1387. /* Clear multipass flag. If authentication isn't done yet, then it will get
  1388. * a chance to be set back to true when we output the next auth header */
  1389. data->state.authhost.multipass = FALSE;
  1390. data->state.authproxy.multipass = FALSE;
  1391. Curl_unencode_cleanup(data);
  1392. /* set the proper values (possibly modified on POST) */
  1393. conn->seek_func = data->set.seek_func; /* restore */
  1394. conn->seek_client = data->set.seek_client; /* restore */
  1395. if(!http)
  1396. return CURLE_OK;
  1397. Curl_dyn_free(&http->send_buffer);
  1398. Curl_mime_cleanpart(&http->form);
  1399. Curl_dyn_reset(&data->state.headerb);
  1400. Curl_hyper_done(data);
  1401. Curl_ws_done(data);
  1402. if(status)
  1403. return status;
  1404. if(!premature && /* this check is pointless when DONE is called before the
  1405. entire operation is complete */
  1406. !conn->bits.retry &&
  1407. !data->set.connect_only &&
  1408. (data->req.bytecount +
  1409. data->req.headerbytecount -
  1410. data->req.deductheadercount) <= 0) {
  1411. /* If this connection isn't simply closed to be retried, AND nothing was
  1412. read from the HTTP server (that counts), this can't be right so we
  1413. return an error here */
  1414. failf(data, "Empty reply from server");
  1415. /* Mark it as closed to avoid the "left intact" message */
  1416. streamclose(conn, "Empty reply from server");
  1417. return CURLE_GOT_NOTHING;
  1418. }
  1419. return CURLE_OK;
  1420. }
  1421. /*
  1422. * Determine if we should use HTTP 1.1 (OR BETTER) for this request. Reasons
  1423. * to avoid it include:
  1424. *
  1425. * - if the user specifically requested HTTP 1.0
  1426. * - if the server we are connected to only supports 1.0
  1427. * - if any server previously contacted to handle this request only supports
  1428. * 1.0.
  1429. */
  1430. bool Curl_use_http_1_1plus(const struct Curl_easy *data,
  1431. const struct connectdata *conn)
  1432. {
  1433. if((data->state.httpversion == 10) || (conn->httpversion == 10))
  1434. return FALSE;
  1435. if((data->state.httpwant == CURL_HTTP_VERSION_1_0) &&
  1436. (conn->httpversion <= 10))
  1437. return FALSE;
  1438. return ((data->state.httpwant == CURL_HTTP_VERSION_NONE) ||
  1439. (data->state.httpwant >= CURL_HTTP_VERSION_1_1));
  1440. }
  1441. #ifndef USE_HYPER
  1442. static const char *get_http_string(const struct Curl_easy *data,
  1443. const struct connectdata *conn)
  1444. {
  1445. if(Curl_conn_is_http3(data, conn, FIRSTSOCKET))
  1446. return "3";
  1447. if(Curl_conn_is_http2(data, conn, FIRSTSOCKET))
  1448. return "2";
  1449. if(Curl_use_http_1_1plus(data, conn))
  1450. return "1.1";
  1451. return "1.0";
  1452. }
  1453. #endif
  1454. /* check and possibly add an Expect: header */
  1455. static CURLcode expect100(struct Curl_easy *data,
  1456. struct connectdata *conn,
  1457. struct dynbuf *req)
  1458. {
  1459. CURLcode result = CURLE_OK;
  1460. data->state.expect100header = FALSE; /* default to false unless it is set
  1461. to TRUE below */
  1462. if(!data->state.disableexpect && Curl_use_http_1_1plus(data, conn) &&
  1463. (conn->httpversion < 20)) {
  1464. /* if not doing HTTP 1.0 or version 2, or disabled explicitly, we add an
  1465. Expect: 100-continue to the headers which actually speeds up post
  1466. operations (as there is one packet coming back from the web server) */
  1467. const char *ptr = Curl_checkheaders(data, STRCONST("Expect"));
  1468. if(ptr) {
  1469. data->state.expect100header =
  1470. Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
  1471. }
  1472. else {
  1473. result = Curl_dyn_addn(req, STRCONST("Expect: 100-continue\r\n"));
  1474. if(!result)
  1475. data->state.expect100header = TRUE;
  1476. }
  1477. }
  1478. return result;
  1479. }
  1480. enum proxy_use {
  1481. HEADER_SERVER, /* direct to server */
  1482. HEADER_PROXY, /* regular request to proxy */
  1483. HEADER_CONNECT /* sending CONNECT to a proxy */
  1484. };
  1485. /* used to compile the provided trailers into one buffer
  1486. will return an error code if one of the headers is
  1487. not formatted correctly */
  1488. CURLcode Curl_http_compile_trailers(struct curl_slist *trailers,
  1489. struct dynbuf *b,
  1490. struct Curl_easy *handle)
  1491. {
  1492. char *ptr = NULL;
  1493. CURLcode result = CURLE_OK;
  1494. const char *endofline_native = NULL;
  1495. const char *endofline_network = NULL;
  1496. if(
  1497. #ifdef CURL_DO_LINEEND_CONV
  1498. (handle->state.prefer_ascii) ||
  1499. #endif
  1500. (handle->set.crlf)) {
  1501. /* \n will become \r\n later on */
  1502. endofline_native = "\n";
  1503. endofline_network = "\x0a";
  1504. }
  1505. else {
  1506. endofline_native = "\r\n";
  1507. endofline_network = "\x0d\x0a";
  1508. }
  1509. while(trailers) {
  1510. /* only add correctly formatted trailers */
  1511. ptr = strchr(trailers->data, ':');
  1512. if(ptr && *(ptr + 1) == ' ') {
  1513. result = Curl_dyn_add(b, trailers->data);
  1514. if(result)
  1515. return result;
  1516. result = Curl_dyn_add(b, endofline_native);
  1517. if(result)
  1518. return result;
  1519. }
  1520. else
  1521. infof(handle, "Malformatted trailing header, skipping trailer");
  1522. trailers = trailers->next;
  1523. }
  1524. result = Curl_dyn_add(b, endofline_network);
  1525. return result;
  1526. }
  1527. static bool hd_name_eq(const char *n1, size_t n1len,
  1528. const char *n2, size_t n2len)
  1529. {
  1530. if(n1len == n2len) {
  1531. return strncasecompare(n1, n2, n1len);
  1532. }
  1533. return FALSE;
  1534. }
  1535. CURLcode Curl_dynhds_add_custom(struct Curl_easy *data,
  1536. bool is_connect,
  1537. struct dynhds *hds)
  1538. {
  1539. struct connectdata *conn = data->conn;
  1540. char *ptr;
  1541. struct curl_slist *h[2];
  1542. struct curl_slist *headers;
  1543. int numlists = 1; /* by default */
  1544. int i;
  1545. #ifndef CURL_DISABLE_PROXY
  1546. enum proxy_use proxy;
  1547. if(is_connect)
  1548. proxy = HEADER_CONNECT;
  1549. else
  1550. proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy?
  1551. HEADER_PROXY:HEADER_SERVER;
  1552. switch(proxy) {
  1553. case HEADER_SERVER:
  1554. h[0] = data->set.headers;
  1555. break;
  1556. case HEADER_PROXY:
  1557. h[0] = data->set.headers;
  1558. if(data->set.sep_headers) {
  1559. h[1] = data->set.proxyheaders;
  1560. numlists++;
  1561. }
  1562. break;
  1563. case HEADER_CONNECT:
  1564. if(data->set.sep_headers)
  1565. h[0] = data->set.proxyheaders;
  1566. else
  1567. h[0] = data->set.headers;
  1568. break;
  1569. }
  1570. #else
  1571. (void)is_connect;
  1572. h[0] = data->set.headers;
  1573. #endif
  1574. /* loop through one or two lists */
  1575. for(i = 0; i < numlists; i++) {
  1576. for(headers = h[i]; headers; headers = headers->next) {
  1577. const char *name, *value;
  1578. size_t namelen, valuelen;
  1579. /* There are 2 quirks in place for custom headers:
  1580. * 1. setting only 'name:' to suppress a header from being sent
  1581. * 2. setting only 'name;' to send an empty (illegal) header
  1582. */
  1583. ptr = strchr(headers->data, ':');
  1584. if(ptr) {
  1585. name = headers->data;
  1586. namelen = ptr - headers->data;
  1587. ptr++; /* pass the colon */
  1588. while(*ptr && ISSPACE(*ptr))
  1589. ptr++;
  1590. if(*ptr) {
  1591. value = ptr;
  1592. valuelen = strlen(value);
  1593. }
  1594. else {
  1595. /* quirk #1, suppress this header */
  1596. continue;
  1597. }
  1598. }
  1599. else {
  1600. ptr = strchr(headers->data, ';');
  1601. if(!ptr) {
  1602. /* neither : nor ; in provided header value. We seem
  1603. * to ignore this silently */
  1604. continue;
  1605. }
  1606. name = headers->data;
  1607. namelen = ptr - headers->data;
  1608. ptr++; /* pass the semicolon */
  1609. while(*ptr && ISSPACE(*ptr))
  1610. ptr++;
  1611. if(!*ptr) {
  1612. /* quirk #2, send an empty header */
  1613. value = "";
  1614. valuelen = 0;
  1615. }
  1616. else {
  1617. /* this may be used for something else in the future,
  1618. * ignore this for now */
  1619. continue;
  1620. }
  1621. }
  1622. DEBUGASSERT(name && value);
  1623. if(data->state.aptr.host &&
  1624. /* a Host: header was sent already, don't pass on any custom Host:
  1625. header as that will produce *two* in the same request! */
  1626. hd_name_eq(name, namelen, STRCONST("Host:")))
  1627. ;
  1628. else if(data->state.httpreq == HTTPREQ_POST_FORM &&
  1629. /* this header (extended by formdata.c) is sent later */
  1630. hd_name_eq(name, namelen, STRCONST("Content-Type:")))
  1631. ;
  1632. else if(data->state.httpreq == HTTPREQ_POST_MIME &&
  1633. /* this header is sent later */
  1634. hd_name_eq(name, namelen, STRCONST("Content-Type:")))
  1635. ;
  1636. else if(conn->bits.authneg &&
  1637. /* while doing auth neg, don't allow the custom length since
  1638. we will force length zero then */
  1639. hd_name_eq(name, namelen, STRCONST("Content-Length:")))
  1640. ;
  1641. else if(data->state.aptr.te &&
  1642. /* when asking for Transfer-Encoding, don't pass on a custom
  1643. Connection: */
  1644. hd_name_eq(name, namelen, STRCONST("Connection:")))
  1645. ;
  1646. else if((conn->httpversion >= 20) &&
  1647. hd_name_eq(name, namelen, STRCONST("Transfer-Encoding:")))
  1648. /* HTTP/2 doesn't support chunked requests */
  1649. ;
  1650. else if((hd_name_eq(name, namelen, STRCONST("Authorization:")) ||
  1651. hd_name_eq(name, namelen, STRCONST("Cookie:"))) &&
  1652. /* be careful of sending this potentially sensitive header to
  1653. other hosts */
  1654. !Curl_auth_allowed_to_host(data))
  1655. ;
  1656. else {
  1657. CURLcode result;
  1658. result = Curl_dynhds_add(hds, name, namelen, value, valuelen);
  1659. if(result)
  1660. return result;
  1661. }
  1662. }
  1663. }
  1664. return CURLE_OK;
  1665. }
  1666. CURLcode Curl_add_custom_headers(struct Curl_easy *data,
  1667. bool is_connect,
  1668. #ifndef USE_HYPER
  1669. struct dynbuf *req
  1670. #else
  1671. void *req
  1672. #endif
  1673. )
  1674. {
  1675. struct connectdata *conn = data->conn;
  1676. char *ptr;
  1677. struct curl_slist *h[2];
  1678. struct curl_slist *headers;
  1679. int numlists = 1; /* by default */
  1680. int i;
  1681. #ifndef CURL_DISABLE_PROXY
  1682. enum proxy_use proxy;
  1683. if(is_connect)
  1684. proxy = HEADER_CONNECT;
  1685. else
  1686. proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy?
  1687. HEADER_PROXY:HEADER_SERVER;
  1688. switch(proxy) {
  1689. case HEADER_SERVER:
  1690. h[0] = data->set.headers;
  1691. break;
  1692. case HEADER_PROXY:
  1693. h[0] = data->set.headers;
  1694. if(data->set.sep_headers) {
  1695. h[1] = data->set.proxyheaders;
  1696. numlists++;
  1697. }
  1698. break;
  1699. case HEADER_CONNECT:
  1700. if(data->set.sep_headers)
  1701. h[0] = data->set.proxyheaders;
  1702. else
  1703. h[0] = data->set.headers;
  1704. break;
  1705. }
  1706. #else
  1707. (void)is_connect;
  1708. h[0] = data->set.headers;
  1709. #endif
  1710. /* loop through one or two lists */
  1711. for(i = 0; i < numlists; i++) {
  1712. headers = h[i];
  1713. while(headers) {
  1714. char *semicolonp = NULL;
  1715. ptr = strchr(headers->data, ':');
  1716. if(!ptr) {
  1717. char *optr;
  1718. /* no colon, semicolon? */
  1719. ptr = strchr(headers->data, ';');
  1720. if(ptr) {
  1721. optr = ptr;
  1722. ptr++; /* pass the semicolon */
  1723. while(*ptr && ISSPACE(*ptr))
  1724. ptr++;
  1725. if(*ptr) {
  1726. /* this may be used for something else in the future */
  1727. optr = NULL;
  1728. }
  1729. else {
  1730. if(*(--ptr) == ';') {
  1731. /* copy the source */
  1732. semicolonp = strdup(headers->data);
  1733. if(!semicolonp) {
  1734. #ifndef USE_HYPER
  1735. Curl_dyn_free(req);
  1736. #endif
  1737. return CURLE_OUT_OF_MEMORY;
  1738. }
  1739. /* put a colon where the semicolon is */
  1740. semicolonp[ptr - headers->data] = ':';
  1741. /* point at the colon */
  1742. optr = &semicolonp [ptr - headers->data];
  1743. }
  1744. }
  1745. ptr = optr;
  1746. }
  1747. }
  1748. if(ptr && (ptr != headers->data)) {
  1749. /* we require a colon for this to be a true header */
  1750. ptr++; /* pass the colon */
  1751. while(*ptr && ISSPACE(*ptr))
  1752. ptr++;
  1753. if(*ptr || semicolonp) {
  1754. /* only send this if the contents was non-blank or done special */
  1755. CURLcode result = CURLE_OK;
  1756. char *compare = semicolonp ? semicolonp : headers->data;
  1757. if(data->state.aptr.host &&
  1758. /* a Host: header was sent already, don't pass on any custom Host:
  1759. header as that will produce *two* in the same request! */
  1760. checkprefix("Host:", compare))
  1761. ;
  1762. else if(data->state.httpreq == HTTPREQ_POST_FORM &&
  1763. /* this header (extended by formdata.c) is sent later */
  1764. checkprefix("Content-Type:", compare))
  1765. ;
  1766. else if(data->state.httpreq == HTTPREQ_POST_MIME &&
  1767. /* this header is sent later */
  1768. checkprefix("Content-Type:", compare))
  1769. ;
  1770. else if(conn->bits.authneg &&
  1771. /* while doing auth neg, don't allow the custom length since
  1772. we will force length zero then */
  1773. checkprefix("Content-Length:", compare))
  1774. ;
  1775. else if(data->state.aptr.te &&
  1776. /* when asking for Transfer-Encoding, don't pass on a custom
  1777. Connection: */
  1778. checkprefix("Connection:", compare))
  1779. ;
  1780. else if((conn->httpversion >= 20) &&
  1781. checkprefix("Transfer-Encoding:", compare))
  1782. /* HTTP/2 doesn't support chunked requests */
  1783. ;
  1784. else if((checkprefix("Authorization:", compare) ||
  1785. checkprefix("Cookie:", compare)) &&
  1786. /* be careful of sending this potentially sensitive header to
  1787. other hosts */
  1788. !Curl_auth_allowed_to_host(data))
  1789. ;
  1790. else {
  1791. #ifdef USE_HYPER
  1792. result = Curl_hyper_header(data, req, compare);
  1793. #else
  1794. result = Curl_dyn_addf(req, "%s\r\n", compare);
  1795. #endif
  1796. }
  1797. if(semicolonp)
  1798. free(semicolonp);
  1799. if(result)
  1800. return result;
  1801. }
  1802. }
  1803. headers = headers->next;
  1804. }
  1805. }
  1806. return CURLE_OK;
  1807. }
  1808. #ifndef CURL_DISABLE_PARSEDATE
  1809. CURLcode Curl_add_timecondition(struct Curl_easy *data,
  1810. #ifndef USE_HYPER
  1811. struct dynbuf *req
  1812. #else
  1813. void *req
  1814. #endif
  1815. )
  1816. {
  1817. const struct tm *tm;
  1818. struct tm keeptime;
  1819. CURLcode result;
  1820. char datestr[80];
  1821. const char *condp;
  1822. size_t len;
  1823. if(data->set.timecondition == CURL_TIMECOND_NONE)
  1824. /* no condition was asked for */
  1825. return CURLE_OK;
  1826. result = Curl_gmtime(data->set.timevalue, &keeptime);
  1827. if(result) {
  1828. failf(data, "Invalid TIMEVALUE");
  1829. return result;
  1830. }
  1831. tm = &keeptime;
  1832. switch(data->set.timecondition) {
  1833. default:
  1834. return CURLE_BAD_FUNCTION_ARGUMENT;
  1835. case CURL_TIMECOND_IFMODSINCE:
  1836. condp = "If-Modified-Since";
  1837. len = 17;
  1838. break;
  1839. case CURL_TIMECOND_IFUNMODSINCE:
  1840. condp = "If-Unmodified-Since";
  1841. len = 19;
  1842. break;
  1843. case CURL_TIMECOND_LASTMOD:
  1844. condp = "Last-Modified";
  1845. len = 13;
  1846. break;
  1847. }
  1848. if(Curl_checkheaders(data, condp, len)) {
  1849. /* A custom header was specified; it will be sent instead. */
  1850. return CURLE_OK;
  1851. }
  1852. /* The If-Modified-Since header family should have their times set in
  1853. * GMT as RFC2616 defines: "All HTTP date/time stamps MUST be
  1854. * represented in Greenwich Mean Time (GMT), without exception. For the
  1855. * purposes of HTTP, GMT is exactly equal to UTC (Coordinated Universal
  1856. * Time)." (see page 20 of RFC2616).
  1857. */
  1858. /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
  1859. msnprintf(datestr, sizeof(datestr),
  1860. "%s: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
  1861. condp,
  1862. Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
  1863. tm->tm_mday,
  1864. Curl_month[tm->tm_mon],
  1865. tm->tm_year + 1900,
  1866. tm->tm_hour,
  1867. tm->tm_min,
  1868. tm->tm_sec);
  1869. #ifndef USE_HYPER
  1870. result = Curl_dyn_add(req, datestr);
  1871. #else
  1872. result = Curl_hyper_header(data, req, datestr);
  1873. #endif
  1874. return result;
  1875. }
  1876. #else
  1877. /* disabled */
  1878. CURLcode Curl_add_timecondition(struct Curl_easy *data,
  1879. struct dynbuf *req)
  1880. {
  1881. (void)data;
  1882. (void)req;
  1883. return CURLE_OK;
  1884. }
  1885. #endif
  1886. void Curl_http_method(struct Curl_easy *data, struct connectdata *conn,
  1887. const char **method, Curl_HttpReq *reqp)
  1888. {
  1889. Curl_HttpReq httpreq = (Curl_HttpReq)data->state.httpreq;
  1890. const char *request;
  1891. if((conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_FTP)) &&
  1892. data->state.upload)
  1893. httpreq = HTTPREQ_PUT;
  1894. /* Now set the 'request' pointer to the proper request string */
  1895. if(data->set.str[STRING_CUSTOMREQUEST])
  1896. request = data->set.str[STRING_CUSTOMREQUEST];
  1897. else {
  1898. if(data->req.no_body)
  1899. request = "HEAD";
  1900. else {
  1901. DEBUGASSERT((httpreq >= HTTPREQ_GET) && (httpreq <= HTTPREQ_HEAD));
  1902. switch(httpreq) {
  1903. case HTTPREQ_POST:
  1904. case HTTPREQ_POST_FORM:
  1905. case HTTPREQ_POST_MIME:
  1906. request = "POST";
  1907. break;
  1908. case HTTPREQ_PUT:
  1909. request = "PUT";
  1910. break;
  1911. default: /* this should never happen */
  1912. case HTTPREQ_GET:
  1913. request = "GET";
  1914. break;
  1915. case HTTPREQ_HEAD:
  1916. request = "HEAD";
  1917. break;
  1918. }
  1919. }
  1920. }
  1921. *method = request;
  1922. *reqp = httpreq;
  1923. }
  1924. CURLcode Curl_http_useragent(struct Curl_easy *data)
  1925. {
  1926. /* The User-Agent string might have been allocated in url.c already, because
  1927. it might have been used in the proxy connect, but if we have got a header
  1928. with the user-agent string specified, we erase the previously made string
  1929. here. */
  1930. if(Curl_checkheaders(data, STRCONST("User-Agent"))) {
  1931. free(data->state.aptr.uagent);
  1932. data->state.aptr.uagent = NULL;
  1933. }
  1934. return CURLE_OK;
  1935. }
  1936. CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn)
  1937. {
  1938. const char *ptr;
  1939. struct dynamically_allocated_data *aptr = &data->state.aptr;
  1940. if(!data->state.this_is_a_follow) {
  1941. /* Free to avoid leaking memory on multiple requests */
  1942. free(data->state.first_host);
  1943. data->state.first_host = strdup(conn->host.name);
  1944. if(!data->state.first_host)
  1945. return CURLE_OUT_OF_MEMORY;
  1946. data->state.first_remote_port = conn->remote_port;
  1947. data->state.first_remote_protocol = conn->handler->protocol;
  1948. }
  1949. Curl_safefree(aptr->host);
  1950. ptr = Curl_checkheaders(data, STRCONST("Host"));
  1951. if(ptr && (!data->state.this_is_a_follow ||
  1952. strcasecompare(data->state.first_host, conn->host.name))) {
  1953. #if !defined(CURL_DISABLE_COOKIES)
  1954. /* If we have a given custom Host: header, we extract the host name in
  1955. order to possibly use it for cookie reasons later on. We only allow the
  1956. custom Host: header if this is NOT a redirect, as setting Host: in the
  1957. redirected request is being out on thin ice. Except if the host name
  1958. is the same as the first one! */
  1959. char *cookiehost = Curl_copy_header_value(ptr);
  1960. if(!cookiehost)
  1961. return CURLE_OUT_OF_MEMORY;
  1962. if(!*cookiehost)
  1963. /* ignore empty data */
  1964. free(cookiehost);
  1965. else {
  1966. /* If the host begins with '[', we start searching for the port after
  1967. the bracket has been closed */
  1968. if(*cookiehost == '[') {
  1969. char *closingbracket;
  1970. /* since the 'cookiehost' is an allocated memory area that will be
  1971. freed later we cannot simply increment the pointer */
  1972. memmove(cookiehost, cookiehost + 1, strlen(cookiehost) - 1);
  1973. closingbracket = strchr(cookiehost, ']');
  1974. if(closingbracket)
  1975. *closingbracket = 0;
  1976. }
  1977. else {
  1978. int startsearch = 0;
  1979. char *colon = strchr(cookiehost + startsearch, ':');
  1980. if(colon)
  1981. *colon = 0; /* The host must not include an embedded port number */
  1982. }
  1983. Curl_safefree(aptr->cookiehost);
  1984. aptr->cookiehost = cookiehost;
  1985. }
  1986. #endif
  1987. if(strcmp("Host:", ptr)) {
  1988. aptr->host = aprintf("Host:%s\r\n", &ptr[5]);
  1989. if(!aptr->host)
  1990. return CURLE_OUT_OF_MEMORY;
  1991. }
  1992. }
  1993. else {
  1994. /* When building Host: headers, we must put the host name within
  1995. [brackets] if the host name is a plain IPv6-address. RFC2732-style. */
  1996. const char *host = conn->host.name;
  1997. if(((conn->given->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS)) &&
  1998. (conn->remote_port == PORT_HTTPS)) ||
  1999. ((conn->given->protocol&(CURLPROTO_HTTP|CURLPROTO_WS)) &&
  2000. (conn->remote_port == PORT_HTTP)) )
  2001. /* if(HTTPS on port 443) OR (HTTP on port 80) then don't include
  2002. the port number in the host string */
  2003. aptr->host = aprintf("Host: %s%s%s\r\n", conn->bits.ipv6_ip?"[":"",
  2004. host, conn->bits.ipv6_ip?"]":"");
  2005. else
  2006. aptr->host = aprintf("Host: %s%s%s:%d\r\n", conn->bits.ipv6_ip?"[":"",
  2007. host, conn->bits.ipv6_ip?"]":"",
  2008. conn->remote_port);
  2009. if(!aptr->host)
  2010. /* without Host: we can't make a nice request */
  2011. return CURLE_OUT_OF_MEMORY;
  2012. }
  2013. return CURLE_OK;
  2014. }
  2015. /*
  2016. * Append the request-target to the HTTP request
  2017. */
  2018. CURLcode Curl_http_target(struct Curl_easy *data,
  2019. struct connectdata *conn,
  2020. struct dynbuf *r)
  2021. {
  2022. CURLcode result = CURLE_OK;
  2023. const char *path = data->state.up.path;
  2024. const char *query = data->state.up.query;
  2025. if(data->set.str[STRING_TARGET]) {
  2026. path = data->set.str[STRING_TARGET];
  2027. query = NULL;
  2028. }
  2029. #ifndef CURL_DISABLE_PROXY
  2030. if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
  2031. /* Using a proxy but does not tunnel through it */
  2032. /* The path sent to the proxy is in fact the entire URL. But if the remote
  2033. host is a IDN-name, we must make sure that the request we produce only
  2034. uses the encoded host name! */
  2035. /* and no fragment part */
  2036. CURLUcode uc;
  2037. char *url;
  2038. CURLU *h = curl_url_dup(data->state.uh);
  2039. if(!h)
  2040. return CURLE_OUT_OF_MEMORY;
  2041. if(conn->host.dispname != conn->host.name) {
  2042. uc = curl_url_set(h, CURLUPART_HOST, conn->host.name, 0);
  2043. if(uc) {
  2044. curl_url_cleanup(h);
  2045. return CURLE_OUT_OF_MEMORY;
  2046. }
  2047. }
  2048. uc = curl_url_set(h, CURLUPART_FRAGMENT, NULL, 0);
  2049. if(uc) {
  2050. curl_url_cleanup(h);
  2051. return CURLE_OUT_OF_MEMORY;
  2052. }
  2053. if(strcasecompare("http", data->state.up.scheme)) {
  2054. /* when getting HTTP, we don't want the userinfo the URL */
  2055. uc = curl_url_set(h, CURLUPART_USER, NULL, 0);
  2056. if(uc) {
  2057. curl_url_cleanup(h);
  2058. return CURLE_OUT_OF_MEMORY;
  2059. }
  2060. uc = curl_url_set(h, CURLUPART_PASSWORD, NULL, 0);
  2061. if(uc) {
  2062. curl_url_cleanup(h);
  2063. return CURLE_OUT_OF_MEMORY;
  2064. }
  2065. }
  2066. /* Extract the URL to use in the request. Store in STRING_TEMP_URL for
  2067. clean-up reasons if the function returns before the free() further
  2068. down. */
  2069. uc = curl_url_get(h, CURLUPART_URL, &url, CURLU_NO_DEFAULT_PORT);
  2070. if(uc) {
  2071. curl_url_cleanup(h);
  2072. return CURLE_OUT_OF_MEMORY;
  2073. }
  2074. curl_url_cleanup(h);
  2075. /* target or url */
  2076. result = Curl_dyn_add(r, data->set.str[STRING_TARGET]?
  2077. data->set.str[STRING_TARGET]:url);
  2078. free(url);
  2079. if(result)
  2080. return (result);
  2081. if(strcasecompare("ftp", data->state.up.scheme)) {
  2082. if(data->set.proxy_transfer_mode) {
  2083. /* when doing ftp, append ;type=<a|i> if not present */
  2084. char *type = strstr(path, ";type=");
  2085. if(type && type[6] && type[7] == 0) {
  2086. switch(Curl_raw_toupper(type[6])) {
  2087. case 'A':
  2088. case 'D':
  2089. case 'I':
  2090. break;
  2091. default:
  2092. type = NULL;
  2093. }
  2094. }
  2095. if(!type) {
  2096. result = Curl_dyn_addf(r, ";type=%c",
  2097. data->state.prefer_ascii ? 'a' : 'i');
  2098. if(result)
  2099. return result;
  2100. }
  2101. }
  2102. }
  2103. }
  2104. else
  2105. #else
  2106. (void)conn; /* not used in disabled-proxy builds */
  2107. #endif
  2108. {
  2109. result = Curl_dyn_add(r, path);
  2110. if(result)
  2111. return result;
  2112. if(query)
  2113. result = Curl_dyn_addf(r, "?%s", query);
  2114. }
  2115. return result;
  2116. }
  2117. CURLcode Curl_http_body(struct Curl_easy *data, struct connectdata *conn,
  2118. Curl_HttpReq httpreq, const char **tep)
  2119. {
  2120. CURLcode result = CURLE_OK;
  2121. const char *ptr;
  2122. struct HTTP *http = data->req.p.http;
  2123. http->postsize = 0;
  2124. switch(httpreq) {
  2125. case HTTPREQ_POST_MIME:
  2126. http->sendit = &data->set.mimepost;
  2127. break;
  2128. case HTTPREQ_POST_FORM:
  2129. /* Convert the form structure into a mime structure. */
  2130. Curl_mime_cleanpart(&http->form);
  2131. result = Curl_getformdata(data, &http->form, data->set.httppost,
  2132. data->state.fread_func);
  2133. if(result)
  2134. return result;
  2135. http->sendit = &http->form;
  2136. break;
  2137. default:
  2138. http->sendit = NULL;
  2139. }
  2140. #ifndef CURL_DISABLE_MIME
  2141. if(http->sendit) {
  2142. const char *cthdr = Curl_checkheaders(data, STRCONST("Content-Type"));
  2143. /* Read and seek body only. */
  2144. http->sendit->flags |= MIME_BODY_ONLY;
  2145. /* Prepare the mime structure headers & set content type. */
  2146. if(cthdr)
  2147. for(cthdr += 13; *cthdr == ' '; cthdr++)
  2148. ;
  2149. else if(http->sendit->kind == MIMEKIND_MULTIPART)
  2150. cthdr = "multipart/form-data";
  2151. curl_mime_headers(http->sendit, data->set.headers, 0);
  2152. result = Curl_mime_prepare_headers(data, http->sendit, cthdr,
  2153. NULL, MIMESTRATEGY_FORM);
  2154. curl_mime_headers(http->sendit, NULL, 0);
  2155. if(!result)
  2156. result = Curl_mime_rewind(http->sendit);
  2157. if(result)
  2158. return result;
  2159. http->postsize = Curl_mime_size(http->sendit);
  2160. }
  2161. #endif
  2162. ptr = Curl_checkheaders(data, STRCONST("Transfer-Encoding"));
  2163. if(ptr) {
  2164. /* Some kind of TE is requested, check if 'chunked' is chosen */
  2165. data->req.upload_chunky =
  2166. Curl_compareheader(ptr,
  2167. STRCONST("Transfer-Encoding:"), STRCONST("chunked"));
  2168. }
  2169. else {
  2170. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  2171. (((httpreq == HTTPREQ_POST_MIME || httpreq == HTTPREQ_POST_FORM) &&
  2172. http->postsize < 0) ||
  2173. ((data->state.upload || httpreq == HTTPREQ_POST) &&
  2174. data->state.infilesize == -1))) {
  2175. if(conn->bits.authneg)
  2176. /* don't enable chunked during auth neg */
  2177. ;
  2178. else if(Curl_use_http_1_1plus(data, conn)) {
  2179. if(conn->httpversion < 20)
  2180. /* HTTP, upload, unknown file size and not HTTP 1.0 */
  2181. data->req.upload_chunky = TRUE;
  2182. }
  2183. else {
  2184. failf(data, "Chunky upload is not supported by HTTP 1.0");
  2185. return CURLE_UPLOAD_FAILED;
  2186. }
  2187. }
  2188. else {
  2189. /* else, no chunky upload */
  2190. data->req.upload_chunky = FALSE;
  2191. }
  2192. if(data->req.upload_chunky)
  2193. *tep = "Transfer-Encoding: chunked\r\n";
  2194. }
  2195. return result;
  2196. }
  2197. CURLcode Curl_http_bodysend(struct Curl_easy *data, struct connectdata *conn,
  2198. struct dynbuf *r, Curl_HttpReq httpreq)
  2199. {
  2200. #ifndef USE_HYPER
  2201. /* Hyper always handles the body separately */
  2202. curl_off_t included_body = 0;
  2203. #else
  2204. /* from this point down, this function should not be used */
  2205. #define Curl_buffer_send(a,b,c,d,e,f) CURLE_OK
  2206. #endif
  2207. CURLcode result = CURLE_OK;
  2208. struct HTTP *http = data->req.p.http;
  2209. const char *ptr;
  2210. /* If 'authdone' is FALSE, we must not set the write socket index to the
  2211. Curl_transfer() call below, as we're not ready to actually upload any
  2212. data yet. */
  2213. switch(httpreq) {
  2214. case HTTPREQ_PUT: /* Let's PUT the data to the server! */
  2215. if(conn->bits.authneg)
  2216. http->postsize = 0;
  2217. else
  2218. http->postsize = data->state.infilesize;
  2219. if((http->postsize != -1) && !data->req.upload_chunky &&
  2220. (conn->bits.authneg ||
  2221. !Curl_checkheaders(data, STRCONST("Content-Length")))) {
  2222. /* only add Content-Length if not uploading chunked */
  2223. result = Curl_dyn_addf(r, "Content-Length: %" CURL_FORMAT_CURL_OFF_T
  2224. "\r\n", http->postsize);
  2225. if(result)
  2226. return result;
  2227. }
  2228. /* For really small puts we don't use Expect: headers at all, and for
  2229. the somewhat bigger ones we allow the app to disable it. Just make
  2230. sure that the expect100header is always set to the preferred value
  2231. here. */
  2232. ptr = Curl_checkheaders(data, STRCONST("Expect"));
  2233. if(ptr) {
  2234. data->state.expect100header =
  2235. Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
  2236. }
  2237. else if(http->postsize > EXPECT_100_THRESHOLD || http->postsize < 0) {
  2238. result = expect100(data, conn, r);
  2239. if(result)
  2240. return result;
  2241. }
  2242. /* end of headers */
  2243. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2244. if(result)
  2245. return result;
  2246. /* set the upload size to the progress meter */
  2247. Curl_pgrsSetUploadSize(data, http->postsize);
  2248. /* this sends the buffer and frees all the buffer resources */
  2249. result = Curl_buffer_send(r, data, data->req.p.http,
  2250. &data->info.request_size, 0,
  2251. FIRSTSOCKET);
  2252. if(result)
  2253. failf(data, "Failed sending PUT request");
  2254. else
  2255. /* prepare for transfer */
  2256. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
  2257. http->postsize?FIRSTSOCKET:-1);
  2258. if(result)
  2259. return result;
  2260. break;
  2261. case HTTPREQ_POST_FORM:
  2262. case HTTPREQ_POST_MIME:
  2263. /* This is form posting using mime data. */
  2264. if(conn->bits.authneg) {
  2265. /* nothing to post! */
  2266. result = Curl_dyn_addn(r, STRCONST("Content-Length: 0\r\n\r\n"));
  2267. if(result)
  2268. return result;
  2269. result = Curl_buffer_send(r, data, data->req.p.http,
  2270. &data->info.request_size, 0,
  2271. FIRSTSOCKET);
  2272. if(result)
  2273. failf(data, "Failed sending POST request");
  2274. else
  2275. /* setup variables for the upcoming transfer */
  2276. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
  2277. break;
  2278. }
  2279. data->state.infilesize = http->postsize;
  2280. /* We only set Content-Length and allow a custom Content-Length if
  2281. we don't upload data chunked, as RFC2616 forbids us to set both
  2282. kinds of headers (Transfer-Encoding: chunked and Content-Length) */
  2283. if(http->postsize != -1 && !data->req.upload_chunky &&
  2284. (!Curl_checkheaders(data, STRCONST("Content-Length")))) {
  2285. /* we allow replacing this header if not during auth negotiation,
  2286. although it isn't very wise to actually set your own */
  2287. result = Curl_dyn_addf(r,
  2288. "Content-Length: %" CURL_FORMAT_CURL_OFF_T
  2289. "\r\n", http->postsize);
  2290. if(result)
  2291. return result;
  2292. }
  2293. #ifndef CURL_DISABLE_MIME
  2294. /* Output mime-generated headers. */
  2295. {
  2296. struct curl_slist *hdr;
  2297. for(hdr = http->sendit->curlheaders; hdr; hdr = hdr->next) {
  2298. result = Curl_dyn_addf(r, "%s\r\n", hdr->data);
  2299. if(result)
  2300. return result;
  2301. }
  2302. }
  2303. #endif
  2304. /* For really small posts we don't use Expect: headers at all, and for
  2305. the somewhat bigger ones we allow the app to disable it. Just make
  2306. sure that the expect100header is always set to the preferred value
  2307. here. */
  2308. ptr = Curl_checkheaders(data, STRCONST("Expect"));
  2309. if(ptr) {
  2310. data->state.expect100header =
  2311. Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
  2312. }
  2313. else if(http->postsize > EXPECT_100_THRESHOLD || http->postsize < 0) {
  2314. result = expect100(data, conn, r);
  2315. if(result)
  2316. return result;
  2317. }
  2318. else
  2319. data->state.expect100header = FALSE;
  2320. /* make the request end in a true CRLF */
  2321. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2322. if(result)
  2323. return result;
  2324. /* set the upload size to the progress meter */
  2325. Curl_pgrsSetUploadSize(data, http->postsize);
  2326. /* Read from mime structure. */
  2327. data->state.fread_func = (curl_read_callback) Curl_mime_read;
  2328. data->state.in = (void *) http->sendit;
  2329. http->sending = HTTPSEND_BODY;
  2330. /* this sends the buffer and frees all the buffer resources */
  2331. result = Curl_buffer_send(r, data, data->req.p.http,
  2332. &data->info.request_size, 0,
  2333. FIRSTSOCKET);
  2334. if(result)
  2335. failf(data, "Failed sending POST request");
  2336. else
  2337. /* prepare for transfer */
  2338. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
  2339. http->postsize?FIRSTSOCKET:-1);
  2340. if(result)
  2341. return result;
  2342. break;
  2343. case HTTPREQ_POST:
  2344. /* this is the simple POST, using x-www-form-urlencoded style */
  2345. if(conn->bits.authneg)
  2346. http->postsize = 0;
  2347. else
  2348. /* the size of the post body */
  2349. http->postsize = data->state.infilesize;
  2350. /* We only set Content-Length and allow a custom Content-Length if
  2351. we don't upload data chunked, as RFC2616 forbids us to set both
  2352. kinds of headers (Transfer-Encoding: chunked and Content-Length) */
  2353. if((http->postsize != -1) && !data->req.upload_chunky &&
  2354. (conn->bits.authneg ||
  2355. !Curl_checkheaders(data, STRCONST("Content-Length")))) {
  2356. /* we allow replacing this header if not during auth negotiation,
  2357. although it isn't very wise to actually set your own */
  2358. result = Curl_dyn_addf(r, "Content-Length: %" CURL_FORMAT_CURL_OFF_T
  2359. "\r\n", http->postsize);
  2360. if(result)
  2361. return result;
  2362. }
  2363. if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
  2364. result = Curl_dyn_addn(r, STRCONST("Content-Type: application/"
  2365. "x-www-form-urlencoded\r\n"));
  2366. if(result)
  2367. return result;
  2368. }
  2369. /* For really small posts we don't use Expect: headers at all, and for
  2370. the somewhat bigger ones we allow the app to disable it. Just make
  2371. sure that the expect100header is always set to the preferred value
  2372. here. */
  2373. ptr = Curl_checkheaders(data, STRCONST("Expect"));
  2374. if(ptr) {
  2375. data->state.expect100header =
  2376. Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
  2377. }
  2378. else if(http->postsize > EXPECT_100_THRESHOLD || http->postsize < 0) {
  2379. result = expect100(data, conn, r);
  2380. if(result)
  2381. return result;
  2382. }
  2383. else
  2384. data->state.expect100header = FALSE;
  2385. #ifndef USE_HYPER
  2386. /* With Hyper the body is always passed on separately */
  2387. if(data->set.postfields) {
  2388. if(!data->state.expect100header &&
  2389. (http->postsize < MAX_INITIAL_POST_SIZE)) {
  2390. /* if we don't use expect: 100 AND
  2391. postsize is less than MAX_INITIAL_POST_SIZE
  2392. then append the post data to the HTTP request header. This limit
  2393. is no magic limit but only set to prevent really huge POSTs to
  2394. get the data duplicated with malloc() and family. */
  2395. /* end of headers! */
  2396. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2397. if(result)
  2398. return result;
  2399. if(!data->req.upload_chunky) {
  2400. /* We're not sending it 'chunked', append it to the request
  2401. already now to reduce the number if send() calls */
  2402. result = Curl_dyn_addn(r, data->set.postfields,
  2403. (size_t)http->postsize);
  2404. included_body = http->postsize;
  2405. }
  2406. else {
  2407. if(http->postsize) {
  2408. char chunk[16];
  2409. /* Append the POST data chunky-style */
  2410. msnprintf(chunk, sizeof(chunk), "%x\r\n", (int)http->postsize);
  2411. result = Curl_dyn_add(r, chunk);
  2412. if(!result) {
  2413. included_body = http->postsize + strlen(chunk);
  2414. result = Curl_dyn_addn(r, data->set.postfields,
  2415. (size_t)http->postsize);
  2416. if(!result)
  2417. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2418. included_body += 2;
  2419. }
  2420. }
  2421. if(!result) {
  2422. result = Curl_dyn_addn(r, STRCONST("\x30\x0d\x0a\x0d\x0a"));
  2423. /* 0 CR LF CR LF */
  2424. included_body += 5;
  2425. }
  2426. }
  2427. if(result)
  2428. return result;
  2429. /* Make sure the progress information is accurate */
  2430. Curl_pgrsSetUploadSize(data, http->postsize);
  2431. }
  2432. else {
  2433. /* A huge POST coming up, do data separate from the request */
  2434. http->postdata = data->set.postfields;
  2435. http->sending = HTTPSEND_BODY;
  2436. http->backup.data = data;
  2437. data->state.fread_func = (curl_read_callback)readmoredata;
  2438. data->state.in = (void *)http;
  2439. /* set the upload size to the progress meter */
  2440. Curl_pgrsSetUploadSize(data, http->postsize);
  2441. /* end of headers! */
  2442. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2443. if(result)
  2444. return result;
  2445. }
  2446. }
  2447. else
  2448. #endif
  2449. {
  2450. /* end of headers! */
  2451. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2452. if(result)
  2453. return result;
  2454. if(data->req.upload_chunky && conn->bits.authneg) {
  2455. /* Chunky upload is selected and we're negotiating auth still, send
  2456. end-of-data only */
  2457. result = Curl_dyn_addn(r, (char *)STRCONST("\x30\x0d\x0a\x0d\x0a"));
  2458. /* 0 CR LF CR LF */
  2459. if(result)
  2460. return result;
  2461. }
  2462. else if(data->state.infilesize) {
  2463. /* set the upload size to the progress meter */
  2464. Curl_pgrsSetUploadSize(data, http->postsize?http->postsize:-1);
  2465. /* set the pointer to mark that we will send the post body using the
  2466. read callback, but only if we're not in authenticate negotiation */
  2467. if(!conn->bits.authneg)
  2468. http->postdata = (char *)&http->postdata;
  2469. }
  2470. }
  2471. /* issue the request */
  2472. result = Curl_buffer_send(r, data, data->req.p.http,
  2473. &data->info.request_size, included_body,
  2474. FIRSTSOCKET);
  2475. if(result)
  2476. failf(data, "Failed sending HTTP POST request");
  2477. else
  2478. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
  2479. http->postdata?FIRSTSOCKET:-1);
  2480. break;
  2481. default:
  2482. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2483. if(result)
  2484. return result;
  2485. /* issue the request */
  2486. result = Curl_buffer_send(r, data, data->req.p.http,
  2487. &data->info.request_size, 0,
  2488. FIRSTSOCKET);
  2489. if(result)
  2490. failf(data, "Failed sending HTTP request");
  2491. #ifdef USE_WEBSOCKETS
  2492. else if((conn->handler->protocol & (CURLPROTO_WS|CURLPROTO_WSS)) &&
  2493. !(data->set.connect_only))
  2494. /* Set up the transfer for two-way since without CONNECT_ONLY set, this
  2495. request probably wants to send data too post upgrade */
  2496. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, FIRSTSOCKET);
  2497. #endif
  2498. else
  2499. /* HTTP GET/HEAD download: */
  2500. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
  2501. }
  2502. return result;
  2503. }
  2504. #if !defined(CURL_DISABLE_COOKIES)
  2505. CURLcode Curl_http_cookies(struct Curl_easy *data,
  2506. struct connectdata *conn,
  2507. struct dynbuf *r)
  2508. {
  2509. CURLcode result = CURLE_OK;
  2510. char *addcookies = NULL;
  2511. bool linecap = FALSE;
  2512. if(data->set.str[STRING_COOKIE] &&
  2513. !Curl_checkheaders(data, STRCONST("Cookie")))
  2514. addcookies = data->set.str[STRING_COOKIE];
  2515. if(data->cookies || addcookies) {
  2516. struct Cookie *co = NULL; /* no cookies from start */
  2517. int count = 0;
  2518. if(data->cookies && data->state.cookie_engine) {
  2519. const char *host = data->state.aptr.cookiehost ?
  2520. data->state.aptr.cookiehost : conn->host.name;
  2521. const bool secure_context =
  2522. conn->handler->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS) ||
  2523. strcasecompare("localhost", host) ||
  2524. !strcmp(host, "127.0.0.1") ||
  2525. !strcmp(host, "::1") ? TRUE : FALSE;
  2526. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  2527. co = Curl_cookie_getlist(data, data->cookies, host, data->state.up.path,
  2528. secure_context);
  2529. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  2530. }
  2531. if(co) {
  2532. struct Cookie *store = co;
  2533. size_t clen = 8; /* hold the size of the generated Cookie: header */
  2534. /* now loop through all cookies that matched */
  2535. while(co) {
  2536. if(co->value) {
  2537. size_t add;
  2538. if(!count) {
  2539. result = Curl_dyn_addn(r, STRCONST("Cookie: "));
  2540. if(result)
  2541. break;
  2542. }
  2543. add = strlen(co->name) + strlen(co->value) + 1;
  2544. if(clen + add >= MAX_COOKIE_HEADER_LEN) {
  2545. infof(data, "Restricted outgoing cookies due to header size, "
  2546. "'%s' not sent", co->name);
  2547. linecap = TRUE;
  2548. break;
  2549. }
  2550. result = Curl_dyn_addf(r, "%s%s=%s", count?"; ":"",
  2551. co->name, co->value);
  2552. if(result)
  2553. break;
  2554. clen += add + (count ? 2 : 0);
  2555. count++;
  2556. }
  2557. co = co->next; /* next cookie please */
  2558. }
  2559. Curl_cookie_freelist(store);
  2560. }
  2561. if(addcookies && !result && !linecap) {
  2562. if(!count)
  2563. result = Curl_dyn_addn(r, STRCONST("Cookie: "));
  2564. if(!result) {
  2565. result = Curl_dyn_addf(r, "%s%s", count?"; ":"", addcookies);
  2566. count++;
  2567. }
  2568. }
  2569. if(count && !result)
  2570. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2571. if(result)
  2572. return result;
  2573. }
  2574. return result;
  2575. }
  2576. #endif
  2577. CURLcode Curl_http_range(struct Curl_easy *data,
  2578. Curl_HttpReq httpreq)
  2579. {
  2580. if(data->state.use_range) {
  2581. /*
  2582. * A range is selected. We use different headers whether we're downloading
  2583. * or uploading and we always let customized headers override our internal
  2584. * ones if any such are specified.
  2585. */
  2586. if(((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD)) &&
  2587. !Curl_checkheaders(data, STRCONST("Range"))) {
  2588. /* if a line like this was already allocated, free the previous one */
  2589. free(data->state.aptr.rangeline);
  2590. data->state.aptr.rangeline = aprintf("Range: bytes=%s\r\n",
  2591. data->state.range);
  2592. }
  2593. else if((httpreq == HTTPREQ_POST || httpreq == HTTPREQ_PUT) &&
  2594. !Curl_checkheaders(data, STRCONST("Content-Range"))) {
  2595. /* if a line like this was already allocated, free the previous one */
  2596. free(data->state.aptr.rangeline);
  2597. if(data->set.set_resume_from < 0) {
  2598. /* Upload resume was asked for, but we don't know the size of the
  2599. remote part so we tell the server (and act accordingly) that we
  2600. upload the whole file (again) */
  2601. data->state.aptr.rangeline =
  2602. aprintf("Content-Range: bytes 0-%" CURL_FORMAT_CURL_OFF_T
  2603. "/%" CURL_FORMAT_CURL_OFF_T "\r\n",
  2604. data->state.infilesize - 1, data->state.infilesize);
  2605. }
  2606. else if(data->state.resume_from) {
  2607. /* This is because "resume" was selected */
  2608. curl_off_t total_expected_size =
  2609. data->state.resume_from + data->state.infilesize;
  2610. data->state.aptr.rangeline =
  2611. aprintf("Content-Range: bytes %s%" CURL_FORMAT_CURL_OFF_T
  2612. "/%" CURL_FORMAT_CURL_OFF_T "\r\n",
  2613. data->state.range, total_expected_size-1,
  2614. total_expected_size);
  2615. }
  2616. else {
  2617. /* Range was selected and then we just pass the incoming range and
  2618. append total size */
  2619. data->state.aptr.rangeline =
  2620. aprintf("Content-Range: bytes %s/%" CURL_FORMAT_CURL_OFF_T "\r\n",
  2621. data->state.range, data->state.infilesize);
  2622. }
  2623. if(!data->state.aptr.rangeline)
  2624. return CURLE_OUT_OF_MEMORY;
  2625. }
  2626. }
  2627. return CURLE_OK;
  2628. }
  2629. CURLcode Curl_http_resume(struct Curl_easy *data,
  2630. struct connectdata *conn,
  2631. Curl_HttpReq httpreq)
  2632. {
  2633. if((HTTPREQ_POST == httpreq || HTTPREQ_PUT == httpreq) &&
  2634. data->state.resume_from) {
  2635. /**********************************************************************
  2636. * Resuming upload in HTTP means that we PUT or POST and that we have
  2637. * got a resume_from value set. The resume value has already created
  2638. * a Range: header that will be passed along. We need to "fast forward"
  2639. * the file the given number of bytes and decrease the assume upload
  2640. * file size before we continue this venture in the dark lands of HTTP.
  2641. * Resuming mime/form posting at an offset > 0 has no sense and is ignored.
  2642. *********************************************************************/
  2643. if(data->state.resume_from < 0) {
  2644. /*
  2645. * This is meant to get the size of the present remote-file by itself.
  2646. * We don't support this now. Bail out!
  2647. */
  2648. data->state.resume_from = 0;
  2649. }
  2650. if(data->state.resume_from && !data->state.followlocation) {
  2651. /* only act on the first request */
  2652. /* Now, let's read off the proper amount of bytes from the
  2653. input. */
  2654. int seekerr = CURL_SEEKFUNC_CANTSEEK;
  2655. if(conn->seek_func) {
  2656. Curl_set_in_callback(data, true);
  2657. seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
  2658. SEEK_SET);
  2659. Curl_set_in_callback(data, false);
  2660. }
  2661. if(seekerr != CURL_SEEKFUNC_OK) {
  2662. curl_off_t passed = 0;
  2663. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  2664. failf(data, "Could not seek stream");
  2665. return CURLE_READ_ERROR;
  2666. }
  2667. /* when seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  2668. do {
  2669. size_t readthisamountnow =
  2670. (data->state.resume_from - passed > data->set.buffer_size) ?
  2671. (size_t)data->set.buffer_size :
  2672. curlx_sotouz(data->state.resume_from - passed);
  2673. size_t actuallyread =
  2674. data->state.fread_func(data->state.buffer, 1, readthisamountnow,
  2675. data->state.in);
  2676. passed += actuallyread;
  2677. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  2678. /* this checks for greater-than only to make sure that the
  2679. CURL_READFUNC_ABORT return code still aborts */
  2680. failf(data, "Could only read %" CURL_FORMAT_CURL_OFF_T
  2681. " bytes from the input", passed);
  2682. return CURLE_READ_ERROR;
  2683. }
  2684. } while(passed < data->state.resume_from);
  2685. }
  2686. /* now, decrease the size of the read */
  2687. if(data->state.infilesize>0) {
  2688. data->state.infilesize -= data->state.resume_from;
  2689. if(data->state.infilesize <= 0) {
  2690. failf(data, "File already completely uploaded");
  2691. return CURLE_PARTIAL_FILE;
  2692. }
  2693. }
  2694. /* we've passed, proceed as normal */
  2695. }
  2696. }
  2697. return CURLE_OK;
  2698. }
  2699. CURLcode Curl_http_firstwrite(struct Curl_easy *data,
  2700. struct connectdata *conn,
  2701. bool *done)
  2702. {
  2703. struct SingleRequest *k = &data->req;
  2704. if(data->req.newurl) {
  2705. if(conn->bits.close) {
  2706. /* Abort after the headers if "follow Location" is set
  2707. and we're set to close anyway. */
  2708. k->keepon &= ~KEEP_RECV;
  2709. *done = TRUE;
  2710. return CURLE_OK;
  2711. }
  2712. /* We have a new url to load, but since we want to be able to re-use this
  2713. connection properly, we read the full response in "ignore more" */
  2714. k->ignorebody = TRUE;
  2715. infof(data, "Ignoring the response-body");
  2716. }
  2717. if(data->state.resume_from && !k->content_range &&
  2718. (data->state.httpreq == HTTPREQ_GET) &&
  2719. !k->ignorebody) {
  2720. if(k->size == data->state.resume_from) {
  2721. /* The resume point is at the end of file, consider this fine even if it
  2722. doesn't allow resume from here. */
  2723. infof(data, "The entire document is already downloaded");
  2724. streamclose(conn, "already downloaded");
  2725. /* Abort download */
  2726. k->keepon &= ~KEEP_RECV;
  2727. *done = TRUE;
  2728. return CURLE_OK;
  2729. }
  2730. /* we wanted to resume a download, although the server doesn't seem to
  2731. * support this and we did this with a GET (if it wasn't a GET we did a
  2732. * POST or PUT resume) */
  2733. failf(data, "HTTP server doesn't seem to support "
  2734. "byte ranges. Cannot resume.");
  2735. return CURLE_RANGE_ERROR;
  2736. }
  2737. if(data->set.timecondition && !data->state.range) {
  2738. /* A time condition has been set AND no ranges have been requested. This
  2739. seems to be what chapter 13.3.4 of RFC 2616 defines to be the correct
  2740. action for an HTTP/1.1 client */
  2741. if(!Curl_meets_timecondition(data, k->timeofdoc)) {
  2742. *done = TRUE;
  2743. /* We're simulating an HTTP 304 from server so we return
  2744. what should have been returned from the server */
  2745. data->info.httpcode = 304;
  2746. infof(data, "Simulate an HTTP 304 response");
  2747. /* we abort the transfer before it is completed == we ruin the
  2748. re-use ability. Close the connection */
  2749. streamclose(conn, "Simulated 304 handling");
  2750. return CURLE_OK;
  2751. }
  2752. } /* we have a time condition */
  2753. return CURLE_OK;
  2754. }
  2755. #ifdef HAVE_LIBZ
  2756. CURLcode Curl_transferencode(struct Curl_easy *data)
  2757. {
  2758. if(!Curl_checkheaders(data, STRCONST("TE")) &&
  2759. data->set.http_transfer_encoding) {
  2760. /* When we are to insert a TE: header in the request, we must also insert
  2761. TE in a Connection: header, so we need to merge the custom provided
  2762. Connection: header and prevent the original to get sent. Note that if
  2763. the user has inserted his/her own TE: header we don't do this magic
  2764. but then assume that the user will handle it all! */
  2765. char *cptr = Curl_checkheaders(data, STRCONST("Connection"));
  2766. #define TE_HEADER "TE: gzip\r\n"
  2767. Curl_safefree(data->state.aptr.te);
  2768. if(cptr) {
  2769. cptr = Curl_copy_header_value(cptr);
  2770. if(!cptr)
  2771. return CURLE_OUT_OF_MEMORY;
  2772. }
  2773. /* Create the (updated) Connection: header */
  2774. data->state.aptr.te = aprintf("Connection: %s%sTE\r\n" TE_HEADER,
  2775. cptr ? cptr : "", (cptr && *cptr) ? ", ":"");
  2776. free(cptr);
  2777. if(!data->state.aptr.te)
  2778. return CURLE_OUT_OF_MEMORY;
  2779. }
  2780. return CURLE_OK;
  2781. }
  2782. #endif
  2783. #ifndef USE_HYPER
  2784. /*
  2785. * Curl_http() gets called from the generic multi_do() function when an HTTP
  2786. * request is to be performed. This creates and sends a properly constructed
  2787. * HTTP request.
  2788. */
  2789. CURLcode Curl_http(struct Curl_easy *data, bool *done)
  2790. {
  2791. struct connectdata *conn = data->conn;
  2792. CURLcode result = CURLE_OK;
  2793. struct HTTP *http;
  2794. Curl_HttpReq httpreq;
  2795. const char *te = ""; /* transfer-encoding */
  2796. const char *request;
  2797. const char *httpstring;
  2798. struct dynbuf req;
  2799. char *altused = NULL;
  2800. const char *p_accept; /* Accept: string */
  2801. /* Always consider the DO phase done after this function call, even if there
  2802. may be parts of the request that are not yet sent, since we can deal with
  2803. the rest of the request in the PERFORM phase. */
  2804. *done = TRUE;
  2805. switch(conn->alpn) {
  2806. case CURL_HTTP_VERSION_3:
  2807. DEBUGASSERT(Curl_conn_is_http3(data, conn, FIRSTSOCKET));
  2808. break;
  2809. case CURL_HTTP_VERSION_2:
  2810. #ifndef CURL_DISABLE_PROXY
  2811. if(!Curl_conn_is_http2(data, conn, FIRSTSOCKET) &&
  2812. conn->bits.proxy && !conn->bits.tunnel_proxy
  2813. ) {
  2814. result = Curl_http2_switch(data, conn, FIRSTSOCKET);
  2815. if(result)
  2816. return result;
  2817. }
  2818. else
  2819. #endif
  2820. DEBUGASSERT(Curl_conn_is_http2(data, conn, FIRSTSOCKET));
  2821. break;
  2822. case CURL_HTTP_VERSION_1_1:
  2823. /* continue with HTTP/1.1 when explicitly requested */
  2824. break;
  2825. default:
  2826. /* Check if user wants to use HTTP/2 with clear TCP */
  2827. if(Curl_http2_may_switch(data, conn, FIRSTSOCKET)) {
  2828. DEBUGF(infof(data, "HTTP/2 over clean TCP"));
  2829. result = Curl_http2_switch(data, conn, FIRSTSOCKET);
  2830. if(result)
  2831. return result;
  2832. }
  2833. break;
  2834. }
  2835. http = data->req.p.http;
  2836. DEBUGASSERT(http);
  2837. result = Curl_http_host(data, conn);
  2838. if(result)
  2839. return result;
  2840. result = Curl_http_useragent(data);
  2841. if(result)
  2842. return result;
  2843. Curl_http_method(data, conn, &request, &httpreq);
  2844. /* setup the authentication headers */
  2845. {
  2846. char *pq = NULL;
  2847. if(data->state.up.query) {
  2848. pq = aprintf("%s?%s", data->state.up.path, data->state.up.query);
  2849. if(!pq)
  2850. return CURLE_OUT_OF_MEMORY;
  2851. }
  2852. result = Curl_http_output_auth(data, conn, request, httpreq,
  2853. (pq ? pq : data->state.up.path), FALSE);
  2854. free(pq);
  2855. if(result)
  2856. return result;
  2857. }
  2858. Curl_safefree(data->state.aptr.ref);
  2859. if(data->state.referer && !Curl_checkheaders(data, STRCONST("Referer"))) {
  2860. data->state.aptr.ref = aprintf("Referer: %s\r\n", data->state.referer);
  2861. if(!data->state.aptr.ref)
  2862. return CURLE_OUT_OF_MEMORY;
  2863. }
  2864. if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
  2865. data->set.str[STRING_ENCODING]) {
  2866. Curl_safefree(data->state.aptr.accept_encoding);
  2867. data->state.aptr.accept_encoding =
  2868. aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
  2869. if(!data->state.aptr.accept_encoding)
  2870. return CURLE_OUT_OF_MEMORY;
  2871. }
  2872. else
  2873. Curl_safefree(data->state.aptr.accept_encoding);
  2874. #ifdef HAVE_LIBZ
  2875. /* we only consider transfer-encoding magic if libz support is built-in */
  2876. result = Curl_transferencode(data);
  2877. if(result)
  2878. return result;
  2879. #endif
  2880. result = Curl_http_body(data, conn, httpreq, &te);
  2881. if(result)
  2882. return result;
  2883. p_accept = Curl_checkheaders(data,
  2884. STRCONST("Accept"))?NULL:"Accept: */*\r\n";
  2885. result = Curl_http_resume(data, conn, httpreq);
  2886. if(result)
  2887. return result;
  2888. result = Curl_http_range(data, httpreq);
  2889. if(result)
  2890. return result;
  2891. httpstring = get_http_string(data, conn);
  2892. /* initialize a dynamic send-buffer */
  2893. Curl_dyn_init(&req, DYN_HTTP_REQUEST);
  2894. /* make sure the header buffer is reset - if there are leftovers from a
  2895. previous transfer */
  2896. Curl_dyn_reset(&data->state.headerb);
  2897. /* add the main request stuff */
  2898. /* GET/HEAD/POST/PUT */
  2899. result = Curl_dyn_addf(&req, "%s ", request);
  2900. if(!result)
  2901. result = Curl_http_target(data, conn, &req);
  2902. if(result) {
  2903. Curl_dyn_free(&req);
  2904. return result;
  2905. }
  2906. #ifndef CURL_DISABLE_ALTSVC
  2907. if(conn->bits.altused && !Curl_checkheaders(data, STRCONST("Alt-Used"))) {
  2908. altused = aprintf("Alt-Used: %s:%d\r\n",
  2909. conn->conn_to_host.name, conn->conn_to_port);
  2910. if(!altused) {
  2911. Curl_dyn_free(&req);
  2912. return CURLE_OUT_OF_MEMORY;
  2913. }
  2914. }
  2915. #endif
  2916. result =
  2917. Curl_dyn_addf(&req,
  2918. " HTTP/%s\r\n" /* HTTP version */
  2919. "%s" /* host */
  2920. "%s" /* proxyuserpwd */
  2921. "%s" /* userpwd */
  2922. "%s" /* range */
  2923. "%s" /* user agent */
  2924. "%s" /* accept */
  2925. "%s" /* TE: */
  2926. "%s" /* accept-encoding */
  2927. "%s" /* referer */
  2928. "%s" /* Proxy-Connection */
  2929. "%s" /* transfer-encoding */
  2930. "%s",/* Alt-Used */
  2931. httpstring,
  2932. (data->state.aptr.host?data->state.aptr.host:""),
  2933. data->state.aptr.proxyuserpwd?
  2934. data->state.aptr.proxyuserpwd:"",
  2935. data->state.aptr.userpwd?data->state.aptr.userpwd:"",
  2936. (data->state.use_range && data->state.aptr.rangeline)?
  2937. data->state.aptr.rangeline:"",
  2938. (data->set.str[STRING_USERAGENT] &&
  2939. *data->set.str[STRING_USERAGENT] &&
  2940. data->state.aptr.uagent)?
  2941. data->state.aptr.uagent:"",
  2942. p_accept?p_accept:"",
  2943. data->state.aptr.te?data->state.aptr.te:"",
  2944. (data->set.str[STRING_ENCODING] &&
  2945. *data->set.str[STRING_ENCODING] &&
  2946. data->state.aptr.accept_encoding)?
  2947. data->state.aptr.accept_encoding:"",
  2948. (data->state.referer && data->state.aptr.ref)?
  2949. data->state.aptr.ref:"" /* Referer: <data> */,
  2950. #ifndef CURL_DISABLE_PROXY
  2951. (conn->bits.httpproxy &&
  2952. !conn->bits.tunnel_proxy &&
  2953. !Curl_checkheaders(data, STRCONST("Proxy-Connection")) &&
  2954. !Curl_checkProxyheaders(data,
  2955. conn,
  2956. STRCONST("Proxy-Connection")))?
  2957. "Proxy-Connection: Keep-Alive\r\n":"",
  2958. #else
  2959. "",
  2960. #endif
  2961. te,
  2962. altused ? altused : ""
  2963. );
  2964. /* clear userpwd and proxyuserpwd to avoid re-using old credentials
  2965. * from re-used connections */
  2966. Curl_safefree(data->state.aptr.userpwd);
  2967. Curl_safefree(data->state.aptr.proxyuserpwd);
  2968. free(altused);
  2969. if(result) {
  2970. Curl_dyn_free(&req);
  2971. return result;
  2972. }
  2973. if(!(conn->handler->flags&PROTOPT_SSL) &&
  2974. conn->httpversion < 20 &&
  2975. (data->state.httpwant == CURL_HTTP_VERSION_2)) {
  2976. /* append HTTP2 upgrade magic stuff to the HTTP request if it isn't done
  2977. over SSL */
  2978. result = Curl_http2_request_upgrade(&req, data);
  2979. if(result) {
  2980. Curl_dyn_free(&req);
  2981. return result;
  2982. }
  2983. }
  2984. result = Curl_http_cookies(data, conn, &req);
  2985. #ifdef USE_WEBSOCKETS
  2986. if(!result && conn->handler->protocol&(CURLPROTO_WS|CURLPROTO_WSS))
  2987. result = Curl_ws_request(data, &req);
  2988. #endif
  2989. if(!result)
  2990. result = Curl_add_timecondition(data, &req);
  2991. if(!result)
  2992. result = Curl_add_custom_headers(data, FALSE, &req);
  2993. if(!result) {
  2994. http->postdata = NULL; /* nothing to post at this point */
  2995. if((httpreq == HTTPREQ_GET) ||
  2996. (httpreq == HTTPREQ_HEAD))
  2997. Curl_pgrsSetUploadSize(data, 0); /* nothing */
  2998. /* bodysend takes ownership of the 'req' memory on success */
  2999. result = Curl_http_bodysend(data, conn, &req, httpreq);
  3000. }
  3001. if(result) {
  3002. Curl_dyn_free(&req);
  3003. return result;
  3004. }
  3005. if((http->postsize > -1) &&
  3006. (http->postsize <= data->req.writebytecount) &&
  3007. (http->sending != HTTPSEND_REQUEST))
  3008. data->req.upload_done = TRUE;
  3009. if(data->req.writebytecount) {
  3010. /* if a request-body has been sent off, we make sure this progress is noted
  3011. properly */
  3012. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  3013. if(Curl_pgrsUpdate(data))
  3014. result = CURLE_ABORTED_BY_CALLBACK;
  3015. if(!http->postsize) {
  3016. /* already sent the entire request body, mark the "upload" as
  3017. complete */
  3018. infof(data, "upload completely sent off: %" CURL_FORMAT_CURL_OFF_T
  3019. " out of %" CURL_FORMAT_CURL_OFF_T " bytes",
  3020. data->req.writebytecount, http->postsize);
  3021. data->req.upload_done = TRUE;
  3022. data->req.keepon &= ~KEEP_SEND; /* we're done writing */
  3023. data->req.exp100 = EXP100_SEND_DATA; /* already sent */
  3024. Curl_expire_done(data, EXPIRE_100_TIMEOUT);
  3025. }
  3026. }
  3027. if(data->req.upload_done)
  3028. Curl_conn_ev_data_done_send(data);
  3029. if((conn->httpversion >= 20) && data->req.upload_chunky)
  3030. /* upload_chunky was set above to set up the request in a chunky fashion,
  3031. but is disabled here again to avoid that the chunked encoded version is
  3032. actually used when sending the request body over h2 */
  3033. data->req.upload_chunky = FALSE;
  3034. return result;
  3035. }
  3036. #endif /* USE_HYPER */
  3037. typedef enum {
  3038. STATUS_UNKNOWN, /* not enough data to tell yet */
  3039. STATUS_DONE, /* a status line was read */
  3040. STATUS_BAD /* not a status line */
  3041. } statusline;
  3042. /* Check a string for a prefix. Check no more than 'len' bytes */
  3043. static bool checkprefixmax(const char *prefix, const char *buffer, size_t len)
  3044. {
  3045. size_t ch = CURLMIN(strlen(prefix), len);
  3046. return curl_strnequal(prefix, buffer, ch);
  3047. }
  3048. /*
  3049. * checkhttpprefix()
  3050. *
  3051. * Returns TRUE if member of the list matches prefix of string
  3052. */
  3053. static statusline
  3054. checkhttpprefix(struct Curl_easy *data,
  3055. const char *s, size_t len)
  3056. {
  3057. struct curl_slist *head = data->set.http200aliases;
  3058. statusline rc = STATUS_BAD;
  3059. statusline onmatch = len >= 5? STATUS_DONE : STATUS_UNKNOWN;
  3060. while(head) {
  3061. if(checkprefixmax(head->data, s, len)) {
  3062. rc = onmatch;
  3063. break;
  3064. }
  3065. head = head->next;
  3066. }
  3067. if((rc != STATUS_DONE) && (checkprefixmax("HTTP/", s, len)))
  3068. rc = onmatch;
  3069. return rc;
  3070. }
  3071. #ifndef CURL_DISABLE_RTSP
  3072. static statusline
  3073. checkrtspprefix(struct Curl_easy *data,
  3074. const char *s, size_t len)
  3075. {
  3076. statusline result = STATUS_BAD;
  3077. statusline onmatch = len >= 5? STATUS_DONE : STATUS_UNKNOWN;
  3078. (void)data; /* unused */
  3079. if(checkprefixmax("RTSP/", s, len))
  3080. result = onmatch;
  3081. return result;
  3082. }
  3083. #endif /* CURL_DISABLE_RTSP */
  3084. static statusline
  3085. checkprotoprefix(struct Curl_easy *data, struct connectdata *conn,
  3086. const char *s, size_t len)
  3087. {
  3088. #ifndef CURL_DISABLE_RTSP
  3089. if(conn->handler->protocol & CURLPROTO_RTSP)
  3090. return checkrtspprefix(data, s, len);
  3091. #else
  3092. (void)conn;
  3093. #endif /* CURL_DISABLE_RTSP */
  3094. return checkhttpprefix(data, s, len);
  3095. }
  3096. /*
  3097. * Curl_http_header() parses a single response header.
  3098. */
  3099. CURLcode Curl_http_header(struct Curl_easy *data, struct connectdata *conn,
  3100. char *headp)
  3101. {
  3102. CURLcode result;
  3103. struct SingleRequest *k = &data->req;
  3104. /* Check for Content-Length: header lines to get size */
  3105. if(!k->http_bodyless &&
  3106. !data->set.ignorecl && checkprefix("Content-Length:", headp)) {
  3107. curl_off_t contentlength;
  3108. CURLofft offt = curlx_strtoofft(headp + strlen("Content-Length:"),
  3109. NULL, 10, &contentlength);
  3110. if(offt == CURL_OFFT_OK) {
  3111. k->size = contentlength;
  3112. k->maxdownload = k->size;
  3113. }
  3114. else if(offt == CURL_OFFT_FLOW) {
  3115. /* out of range */
  3116. if(data->set.max_filesize) {
  3117. failf(data, "Maximum file size exceeded");
  3118. return CURLE_FILESIZE_EXCEEDED;
  3119. }
  3120. streamclose(conn, "overflow content-length");
  3121. infof(data, "Overflow Content-Length: value");
  3122. }
  3123. else {
  3124. /* negative or just rubbish - bad HTTP */
  3125. failf(data, "Invalid Content-Length: value");
  3126. return CURLE_WEIRD_SERVER_REPLY;
  3127. }
  3128. }
  3129. /* check for Content-Type: header lines to get the MIME-type */
  3130. else if(checkprefix("Content-Type:", headp)) {
  3131. char *contenttype = Curl_copy_header_value(headp);
  3132. if(!contenttype)
  3133. return CURLE_OUT_OF_MEMORY;
  3134. if(!*contenttype)
  3135. /* ignore empty data */
  3136. free(contenttype);
  3137. else {
  3138. Curl_safefree(data->info.contenttype);
  3139. data->info.contenttype = contenttype;
  3140. }
  3141. }
  3142. #ifndef CURL_DISABLE_PROXY
  3143. else if((conn->httpversion == 10) &&
  3144. conn->bits.httpproxy &&
  3145. Curl_compareheader(headp,
  3146. STRCONST("Proxy-Connection:"),
  3147. STRCONST("keep-alive"))) {
  3148. /*
  3149. * When an HTTP/1.0 reply comes when using a proxy, the
  3150. * 'Proxy-Connection: keep-alive' line tells us the
  3151. * connection will be kept alive for our pleasure.
  3152. * Default action for 1.0 is to close.
  3153. */
  3154. connkeep(conn, "Proxy-Connection keep-alive"); /* don't close */
  3155. infof(data, "HTTP/1.0 proxy connection set to keep alive");
  3156. }
  3157. else if((conn->httpversion == 11) &&
  3158. conn->bits.httpproxy &&
  3159. Curl_compareheader(headp,
  3160. STRCONST("Proxy-Connection:"),
  3161. STRCONST("close"))) {
  3162. /*
  3163. * We get an HTTP/1.1 response from a proxy and it says it'll
  3164. * close down after this transfer.
  3165. */
  3166. connclose(conn, "Proxy-Connection: asked to close after done");
  3167. infof(data, "HTTP/1.1 proxy connection set close");
  3168. }
  3169. #endif
  3170. else if((conn->httpversion == 10) &&
  3171. Curl_compareheader(headp,
  3172. STRCONST("Connection:"),
  3173. STRCONST("keep-alive"))) {
  3174. /*
  3175. * An HTTP/1.0 reply with the 'Connection: keep-alive' line
  3176. * tells us the connection will be kept alive for our
  3177. * pleasure. Default action for 1.0 is to close.
  3178. *
  3179. * [RFC2068, section 19.7.1] */
  3180. connkeep(conn, "Connection keep-alive");
  3181. infof(data, "HTTP/1.0 connection set to keep alive");
  3182. }
  3183. else if(Curl_compareheader(headp,
  3184. STRCONST("Connection:"), STRCONST("close"))) {
  3185. /*
  3186. * [RFC 2616, section 8.1.2.1]
  3187. * "Connection: close" is HTTP/1.1 language and means that
  3188. * the connection will close when this request has been
  3189. * served.
  3190. */
  3191. streamclose(conn, "Connection: close used");
  3192. }
  3193. else if(!k->http_bodyless && checkprefix("Transfer-Encoding:", headp)) {
  3194. /* One or more encodings. We check for chunked and/or a compression
  3195. algorithm. */
  3196. /*
  3197. * [RFC 2616, section 3.6.1] A 'chunked' transfer encoding
  3198. * means that the server will send a series of "chunks". Each
  3199. * chunk starts with line with info (including size of the
  3200. * coming block) (terminated with CRLF), then a block of data
  3201. * with the previously mentioned size. There can be any amount
  3202. * of chunks, and a chunk-data set to zero signals the
  3203. * end-of-chunks. */
  3204. result = Curl_build_unencoding_stack(data,
  3205. headp + strlen("Transfer-Encoding:"),
  3206. TRUE);
  3207. if(result)
  3208. return result;
  3209. if(!k->chunk && data->set.http_transfer_encoding) {
  3210. /* if this isn't chunked, only close can signal the end of this transfer
  3211. as Content-Length is said not to be trusted for transfer-encoding! */
  3212. connclose(conn, "HTTP/1.1 transfer-encoding without chunks");
  3213. k->ignore_cl = TRUE;
  3214. }
  3215. }
  3216. else if(!k->http_bodyless && checkprefix("Content-Encoding:", headp) &&
  3217. data->set.str[STRING_ENCODING]) {
  3218. /*
  3219. * Process Content-Encoding. Look for the values: identity,
  3220. * gzip, deflate, compress, x-gzip and x-compress. x-gzip and
  3221. * x-compress are the same as gzip and compress. (Sec 3.5 RFC
  3222. * 2616). zlib cannot handle compress. However, errors are
  3223. * handled further down when the response body is processed
  3224. */
  3225. result = Curl_build_unencoding_stack(data,
  3226. headp + strlen("Content-Encoding:"),
  3227. FALSE);
  3228. if(result)
  3229. return result;
  3230. }
  3231. else if(checkprefix("Retry-After:", headp)) {
  3232. /* Retry-After = HTTP-date / delay-seconds */
  3233. curl_off_t retry_after = 0; /* zero for unknown or "now" */
  3234. /* Try it as a decimal number, if it works it is not a date */
  3235. (void)curlx_strtoofft(headp + strlen("Retry-After:"),
  3236. NULL, 10, &retry_after);
  3237. if(!retry_after) {
  3238. time_t date = Curl_getdate_capped(headp + strlen("Retry-After:"));
  3239. if(-1 != date)
  3240. /* convert date to number of seconds into the future */
  3241. retry_after = date - time(NULL);
  3242. }
  3243. data->info.retry_after = retry_after; /* store it */
  3244. }
  3245. else if(!k->http_bodyless && checkprefix("Content-Range:", headp)) {
  3246. /* Content-Range: bytes [num]-
  3247. Content-Range: bytes: [num]-
  3248. Content-Range: [num]-
  3249. Content-Range: [asterisk]/[total]
  3250. The second format was added since Sun's webserver
  3251. JavaWebServer/1.1.1 obviously sends the header this way!
  3252. The third added since some servers use that!
  3253. The fourth means the requested range was unsatisfied.
  3254. */
  3255. char *ptr = headp + strlen("Content-Range:");
  3256. /* Move forward until first digit or asterisk */
  3257. while(*ptr && !ISDIGIT(*ptr) && *ptr != '*')
  3258. ptr++;
  3259. /* if it truly stopped on a digit */
  3260. if(ISDIGIT(*ptr)) {
  3261. if(!curlx_strtoofft(ptr, NULL, 10, &k->offset)) {
  3262. if(data->state.resume_from == k->offset)
  3263. /* we asked for a resume and we got it */
  3264. k->content_range = TRUE;
  3265. }
  3266. }
  3267. else
  3268. data->state.resume_from = 0; /* get everything */
  3269. }
  3270. #if !defined(CURL_DISABLE_COOKIES)
  3271. else if(data->cookies && data->state.cookie_engine &&
  3272. checkprefix("Set-Cookie:", headp)) {
  3273. /* If there is a custom-set Host: name, use it here, or else use real peer
  3274. host name. */
  3275. const char *host = data->state.aptr.cookiehost?
  3276. data->state.aptr.cookiehost:conn->host.name;
  3277. const bool secure_context =
  3278. conn->handler->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS) ||
  3279. strcasecompare("localhost", host) ||
  3280. !strcmp(host, "127.0.0.1") ||
  3281. !strcmp(host, "::1") ? TRUE : FALSE;
  3282. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE,
  3283. CURL_LOCK_ACCESS_SINGLE);
  3284. Curl_cookie_add(data, data->cookies, TRUE, FALSE,
  3285. headp + strlen("Set-Cookie:"), host,
  3286. data->state.up.path, secure_context);
  3287. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  3288. }
  3289. #endif
  3290. else if(!k->http_bodyless && checkprefix("Last-Modified:", headp) &&
  3291. (data->set.timecondition || data->set.get_filetime) ) {
  3292. k->timeofdoc = Curl_getdate_capped(headp + strlen("Last-Modified:"));
  3293. if(data->set.get_filetime)
  3294. data->info.filetime = k->timeofdoc;
  3295. }
  3296. else if((checkprefix("WWW-Authenticate:", headp) &&
  3297. (401 == k->httpcode)) ||
  3298. (checkprefix("Proxy-authenticate:", headp) &&
  3299. (407 == k->httpcode))) {
  3300. bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
  3301. char *auth = Curl_copy_header_value(headp);
  3302. if(!auth)
  3303. return CURLE_OUT_OF_MEMORY;
  3304. result = Curl_http_input_auth(data, proxy, auth);
  3305. free(auth);
  3306. if(result)
  3307. return result;
  3308. }
  3309. #ifdef USE_SPNEGO
  3310. else if(checkprefix("Persistent-Auth:", headp)) {
  3311. struct negotiatedata *negdata = &conn->negotiate;
  3312. struct auth *authp = &data->state.authhost;
  3313. if(authp->picked == CURLAUTH_NEGOTIATE) {
  3314. char *persistentauth = Curl_copy_header_value(headp);
  3315. if(!persistentauth)
  3316. return CURLE_OUT_OF_MEMORY;
  3317. negdata->noauthpersist = checkprefix("false", persistentauth)?
  3318. TRUE:FALSE;
  3319. negdata->havenoauthpersist = TRUE;
  3320. infof(data, "Negotiate: noauthpersist -> %d, header part: %s",
  3321. negdata->noauthpersist, persistentauth);
  3322. free(persistentauth);
  3323. }
  3324. }
  3325. #endif
  3326. else if((k->httpcode >= 300 && k->httpcode < 400) &&
  3327. checkprefix("Location:", headp) &&
  3328. !data->req.location) {
  3329. /* this is the URL that the server advises us to use instead */
  3330. char *location = Curl_copy_header_value(headp);
  3331. if(!location)
  3332. return CURLE_OUT_OF_MEMORY;
  3333. if(!*location)
  3334. /* ignore empty data */
  3335. free(location);
  3336. else {
  3337. data->req.location = location;
  3338. if(data->set.http_follow_location) {
  3339. DEBUGASSERT(!data->req.newurl);
  3340. data->req.newurl = strdup(data->req.location); /* clone */
  3341. if(!data->req.newurl)
  3342. return CURLE_OUT_OF_MEMORY;
  3343. /* some cases of POST and PUT etc needs to rewind the data
  3344. stream at this point */
  3345. result = http_perhapsrewind(data, conn);
  3346. if(result)
  3347. return result;
  3348. /* mark the next request as a followed location: */
  3349. data->state.this_is_a_follow = TRUE;
  3350. }
  3351. }
  3352. }
  3353. #ifndef CURL_DISABLE_HSTS
  3354. /* If enabled, the header is incoming and this is over HTTPS */
  3355. else if(data->hsts && checkprefix("Strict-Transport-Security:", headp) &&
  3356. ((conn->handler->flags & PROTOPT_SSL) ||
  3357. #ifdef CURLDEBUG
  3358. /* allow debug builds to circumvent the HTTPS restriction */
  3359. getenv("CURL_HSTS_HTTP")
  3360. #else
  3361. 0
  3362. #endif
  3363. )) {
  3364. CURLcode check =
  3365. Curl_hsts_parse(data->hsts, conn->host.name,
  3366. headp + strlen("Strict-Transport-Security:"));
  3367. if(check)
  3368. infof(data, "Illegal STS header skipped");
  3369. #ifdef DEBUGBUILD
  3370. else
  3371. infof(data, "Parsed STS header fine (%zu entries)",
  3372. data->hsts->list.size);
  3373. #endif
  3374. }
  3375. #endif
  3376. #ifndef CURL_DISABLE_ALTSVC
  3377. /* If enabled, the header is incoming and this is over HTTPS */
  3378. else if(data->asi && checkprefix("Alt-Svc:", headp) &&
  3379. ((conn->handler->flags & PROTOPT_SSL) ||
  3380. #ifdef CURLDEBUG
  3381. /* allow debug builds to circumvent the HTTPS restriction */
  3382. getenv("CURL_ALTSVC_HTTP")
  3383. #else
  3384. 0
  3385. #endif
  3386. )) {
  3387. /* the ALPN of the current request */
  3388. enum alpnid id = (conn->httpversion == 30)? ALPN_h3 :
  3389. (conn->httpversion == 20) ? ALPN_h2 : ALPN_h1;
  3390. result = Curl_altsvc_parse(data, data->asi,
  3391. headp + strlen("Alt-Svc:"),
  3392. id, conn->host.name,
  3393. curlx_uitous((unsigned int)conn->remote_port));
  3394. if(result)
  3395. return result;
  3396. }
  3397. #endif
  3398. else if(conn->handler->protocol & CURLPROTO_RTSP) {
  3399. result = Curl_rtsp_parseheader(data, headp);
  3400. if(result)
  3401. return result;
  3402. }
  3403. return CURLE_OK;
  3404. }
  3405. /*
  3406. * Called after the first HTTP response line (the status line) has been
  3407. * received and parsed.
  3408. */
  3409. CURLcode Curl_http_statusline(struct Curl_easy *data,
  3410. struct connectdata *conn)
  3411. {
  3412. struct SingleRequest *k = &data->req;
  3413. data->info.httpcode = k->httpcode;
  3414. data->info.httpversion = conn->httpversion;
  3415. if(!data->state.httpversion ||
  3416. data->state.httpversion > conn->httpversion)
  3417. /* store the lowest server version we encounter */
  3418. data->state.httpversion = conn->httpversion;
  3419. /*
  3420. * This code executes as part of processing the header. As a
  3421. * result, it's not totally clear how to interpret the
  3422. * response code yet as that depends on what other headers may
  3423. * be present. 401 and 407 may be errors, but may be OK
  3424. * depending on how authentication is working. Other codes
  3425. * are definitely errors, so give up here.
  3426. */
  3427. if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
  3428. k->httpcode == 416) {
  3429. /* "Requested Range Not Satisfiable", just proceed and
  3430. pretend this is no error */
  3431. k->ignorebody = TRUE; /* Avoid appending error msg to good data. */
  3432. }
  3433. if(conn->httpversion == 10) {
  3434. /* Default action for HTTP/1.0 must be to close, unless
  3435. we get one of those fancy headers that tell us the
  3436. server keeps it open for us! */
  3437. infof(data, "HTTP 1.0, assume close after body");
  3438. connclose(conn, "HTTP/1.0 close after body");
  3439. }
  3440. else if(conn->httpversion == 20 ||
  3441. (k->upgr101 == UPGR101_H2 && k->httpcode == 101)) {
  3442. DEBUGF(infof(data, "HTTP/2 found, allow multiplexing"));
  3443. /* HTTP/2 cannot avoid multiplexing since it is a core functionality
  3444. of the protocol */
  3445. conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  3446. }
  3447. else if(conn->httpversion >= 11 &&
  3448. !conn->bits.close) {
  3449. /* If HTTP version is >= 1.1 and connection is persistent */
  3450. DEBUGF(infof(data,
  3451. "HTTP 1.1 or later with persistent connection"));
  3452. }
  3453. k->http_bodyless = k->httpcode >= 100 && k->httpcode < 200;
  3454. switch(k->httpcode) {
  3455. case 304:
  3456. /* (quote from RFC2616, section 10.3.5): The 304 response
  3457. * MUST NOT contain a message-body, and thus is always
  3458. * terminated by the first empty line after the header
  3459. * fields. */
  3460. if(data->set.timecondition)
  3461. data->info.timecond = TRUE;
  3462. /* FALLTHROUGH */
  3463. case 204:
  3464. /* (quote from RFC2616, section 10.2.5): The server has
  3465. * fulfilled the request but does not need to return an
  3466. * entity-body ... The 204 response MUST NOT include a
  3467. * message-body, and thus is always terminated by the first
  3468. * empty line after the header fields. */
  3469. k->size = 0;
  3470. k->maxdownload = 0;
  3471. k->http_bodyless = TRUE;
  3472. break;
  3473. default:
  3474. break;
  3475. }
  3476. return CURLE_OK;
  3477. }
  3478. /* Content-Length must be ignored if any Transfer-Encoding is present in the
  3479. response. Refer to RFC 7230 section 3.3.3 and RFC2616 section 4.4. This is
  3480. figured out here after all headers have been received but before the final
  3481. call to the user's header callback, so that a valid content length can be
  3482. retrieved by the user in the final call. */
  3483. CURLcode Curl_http_size(struct Curl_easy *data)
  3484. {
  3485. struct SingleRequest *k = &data->req;
  3486. if(data->req.ignore_cl || k->chunk) {
  3487. k->size = k->maxdownload = -1;
  3488. }
  3489. else if(k->size != -1) {
  3490. if(data->set.max_filesize &&
  3491. k->size > data->set.max_filesize) {
  3492. failf(data, "Maximum file size exceeded");
  3493. return CURLE_FILESIZE_EXCEEDED;
  3494. }
  3495. Curl_pgrsSetDownloadSize(data, k->size);
  3496. k->maxdownload = k->size;
  3497. }
  3498. return CURLE_OK;
  3499. }
  3500. static CURLcode verify_header(struct Curl_easy *data)
  3501. {
  3502. struct SingleRequest *k = &data->req;
  3503. const char *header = Curl_dyn_ptr(&data->state.headerb);
  3504. size_t hlen = Curl_dyn_len(&data->state.headerb);
  3505. char *ptr = memchr(header, 0x00, hlen);
  3506. if(ptr) {
  3507. /* this is bad, bail out */
  3508. failf(data, "Nul byte in header");
  3509. return CURLE_WEIRD_SERVER_REPLY;
  3510. }
  3511. if(k->headerline < 2)
  3512. /* the first "header" is the status-line and it has no colon */
  3513. return CURLE_OK;
  3514. if(((header[0] == ' ') || (header[0] == '\t')) && k->headerline > 2)
  3515. /* line folding, can't happen on line 2 */
  3516. ;
  3517. else {
  3518. ptr = memchr(header, ':', hlen);
  3519. if(!ptr) {
  3520. /* this is bad, bail out */
  3521. failf(data, "Header without colon");
  3522. return CURLE_WEIRD_SERVER_REPLY;
  3523. }
  3524. }
  3525. return CURLE_OK;
  3526. }
  3527. /*
  3528. * Read any HTTP header lines from the server and pass them to the client app.
  3529. */
  3530. CURLcode Curl_http_readwrite_headers(struct Curl_easy *data,
  3531. struct connectdata *conn,
  3532. ssize_t *nread,
  3533. bool *stop_reading)
  3534. {
  3535. CURLcode result;
  3536. struct SingleRequest *k = &data->req;
  3537. ssize_t onread = *nread;
  3538. char *ostr = k->str;
  3539. char *headp;
  3540. char *str_start;
  3541. char *end_ptr;
  3542. /* header line within buffer loop */
  3543. do {
  3544. size_t rest_length;
  3545. size_t full_length;
  3546. int writetype;
  3547. /* str_start is start of line within buf */
  3548. str_start = k->str;
  3549. /* data is in network encoding so use 0x0a instead of '\n' */
  3550. end_ptr = memchr(str_start, 0x0a, *nread);
  3551. if(!end_ptr) {
  3552. /* Not a complete header line within buffer, append the data to
  3553. the end of the headerbuff. */
  3554. result = Curl_dyn_addn(&data->state.headerb, str_start, *nread);
  3555. if(result)
  3556. return result;
  3557. if(!k->headerline) {
  3558. /* check if this looks like a protocol header */
  3559. statusline st =
  3560. checkprotoprefix(data, conn,
  3561. Curl_dyn_ptr(&data->state.headerb),
  3562. Curl_dyn_len(&data->state.headerb));
  3563. if(st == STATUS_BAD) {
  3564. /* this is not the beginning of a protocol first header line */
  3565. k->header = FALSE;
  3566. k->badheader = HEADER_ALLBAD;
  3567. streamclose(conn, "bad HTTP: No end-of-message indicator");
  3568. if(!data->set.http09_allowed) {
  3569. failf(data, "Received HTTP/0.9 when not allowed");
  3570. return CURLE_UNSUPPORTED_PROTOCOL;
  3571. }
  3572. break;
  3573. }
  3574. }
  3575. break; /* read more and try again */
  3576. }
  3577. /* decrease the size of the remaining (supposed) header line */
  3578. rest_length = (end_ptr - k->str) + 1;
  3579. *nread -= (ssize_t)rest_length;
  3580. k->str = end_ptr + 1; /* move past new line */
  3581. full_length = k->str - str_start;
  3582. result = Curl_dyn_addn(&data->state.headerb, str_start, full_length);
  3583. if(result)
  3584. return result;
  3585. /****
  3586. * We now have a FULL header line in 'headerb'.
  3587. *****/
  3588. if(!k->headerline) {
  3589. /* the first read header */
  3590. statusline st = checkprotoprefix(data, conn,
  3591. Curl_dyn_ptr(&data->state.headerb),
  3592. Curl_dyn_len(&data->state.headerb));
  3593. if(st == STATUS_BAD) {
  3594. streamclose(conn, "bad HTTP: No end-of-message indicator");
  3595. /* this is not the beginning of a protocol first header line */
  3596. if(!data->set.http09_allowed) {
  3597. failf(data, "Received HTTP/0.9 when not allowed");
  3598. return CURLE_UNSUPPORTED_PROTOCOL;
  3599. }
  3600. k->header = FALSE;
  3601. if(*nread)
  3602. /* since there's more, this is a partial bad header */
  3603. k->badheader = HEADER_PARTHEADER;
  3604. else {
  3605. /* this was all we read so it's all a bad header */
  3606. k->badheader = HEADER_ALLBAD;
  3607. *nread = onread;
  3608. k->str = ostr;
  3609. return CURLE_OK;
  3610. }
  3611. break;
  3612. }
  3613. }
  3614. /* headers are in network encoding so use 0x0a and 0x0d instead of '\n'
  3615. and '\r' */
  3616. headp = Curl_dyn_ptr(&data->state.headerb);
  3617. if((0x0a == *headp) || (0x0d == *headp)) {
  3618. size_t headerlen;
  3619. /* Zero-length header line means end of headers! */
  3620. if('\r' == *headp)
  3621. headp++; /* pass the \r byte */
  3622. if('\n' == *headp)
  3623. headp++; /* pass the \n byte */
  3624. if(100 <= k->httpcode && 199 >= k->httpcode) {
  3625. /* "A user agent MAY ignore unexpected 1xx status responses." */
  3626. switch(k->httpcode) {
  3627. case 100:
  3628. /*
  3629. * We have made an HTTP PUT or POST and this is 1.1-lingo
  3630. * that tells us that the server is OK with this and ready
  3631. * to receive the data.
  3632. * However, we'll get more headers now so we must get
  3633. * back into the header-parsing state!
  3634. */
  3635. k->header = TRUE;
  3636. k->headerline = 0; /* restart the header line counter */
  3637. /* if we did wait for this do enable write now! */
  3638. if(k->exp100 > EXP100_SEND_DATA) {
  3639. k->exp100 = EXP100_SEND_DATA;
  3640. k->keepon |= KEEP_SEND;
  3641. Curl_expire_done(data, EXPIRE_100_TIMEOUT);
  3642. }
  3643. break;
  3644. case 101:
  3645. /* Switching Protocols */
  3646. if(k->upgr101 == UPGR101_H2) {
  3647. /* Switching to HTTP/2 */
  3648. infof(data, "Received 101, Switching to HTTP/2");
  3649. k->upgr101 = UPGR101_RECEIVED;
  3650. /* we'll get more headers (HTTP/2 response) */
  3651. k->header = TRUE;
  3652. k->headerline = 0; /* restart the header line counter */
  3653. /* switch to http2 now. The bytes after response headers
  3654. are also processed here, otherwise they are lost. */
  3655. result = Curl_http2_upgrade(data, conn, FIRSTSOCKET,
  3656. k->str, *nread);
  3657. if(result)
  3658. return result;
  3659. *nread = 0;
  3660. }
  3661. #ifdef USE_WEBSOCKETS
  3662. else if(k->upgr101 == UPGR101_WS) {
  3663. /* verify the response */
  3664. result = Curl_ws_accept(data, k->str, *nread);
  3665. if(result)
  3666. return result;
  3667. k->header = FALSE; /* no more header to parse! */
  3668. if(data->set.connect_only) {
  3669. k->keepon &= ~KEEP_RECV; /* read no more content */
  3670. *nread = 0;
  3671. }
  3672. }
  3673. #endif
  3674. else {
  3675. /* Not switching to another protocol */
  3676. k->header = FALSE; /* no more header to parse! */
  3677. }
  3678. break;
  3679. default:
  3680. /* the status code 1xx indicates a provisional response, so
  3681. we'll get another set of headers */
  3682. k->header = TRUE;
  3683. k->headerline = 0; /* restart the header line counter */
  3684. break;
  3685. }
  3686. }
  3687. else {
  3688. k->header = FALSE; /* no more header to parse! */
  3689. if((k->size == -1) && !k->chunk && !conn->bits.close &&
  3690. (conn->httpversion == 11) &&
  3691. !(conn->handler->protocol & CURLPROTO_RTSP) &&
  3692. data->state.httpreq != HTTPREQ_HEAD) {
  3693. /* On HTTP 1.1, when connection is not to get closed, but no
  3694. Content-Length nor Transfer-Encoding chunked have been
  3695. received, according to RFC2616 section 4.4 point 5, we
  3696. assume that the server will close the connection to
  3697. signal the end of the document. */
  3698. infof(data, "no chunk, no close, no size. Assume close to "
  3699. "signal end");
  3700. streamclose(conn, "HTTP: No end-of-message indicator");
  3701. }
  3702. }
  3703. if(!k->header) {
  3704. result = Curl_http_size(data);
  3705. if(result)
  3706. return result;
  3707. }
  3708. /* At this point we have some idea about the fate of the connection.
  3709. If we are closing the connection it may result auth failure. */
  3710. #if defined(USE_NTLM)
  3711. if(conn->bits.close &&
  3712. (((data->req.httpcode == 401) &&
  3713. (conn->http_ntlm_state == NTLMSTATE_TYPE2)) ||
  3714. ((data->req.httpcode == 407) &&
  3715. (conn->proxy_ntlm_state == NTLMSTATE_TYPE2)))) {
  3716. infof(data, "Connection closure while negotiating auth (HTTP 1.0?)");
  3717. data->state.authproblem = TRUE;
  3718. }
  3719. #endif
  3720. #if defined(USE_SPNEGO)
  3721. if(conn->bits.close &&
  3722. (((data->req.httpcode == 401) &&
  3723. (conn->http_negotiate_state == GSS_AUTHRECV)) ||
  3724. ((data->req.httpcode == 407) &&
  3725. (conn->proxy_negotiate_state == GSS_AUTHRECV)))) {
  3726. infof(data, "Connection closure while negotiating auth (HTTP 1.0?)");
  3727. data->state.authproblem = TRUE;
  3728. }
  3729. if((conn->http_negotiate_state == GSS_AUTHDONE) &&
  3730. (data->req.httpcode != 401)) {
  3731. conn->http_negotiate_state = GSS_AUTHSUCC;
  3732. }
  3733. if((conn->proxy_negotiate_state == GSS_AUTHDONE) &&
  3734. (data->req.httpcode != 407)) {
  3735. conn->proxy_negotiate_state = GSS_AUTHSUCC;
  3736. }
  3737. #endif
  3738. /* now, only output this if the header AND body are requested:
  3739. */
  3740. writetype = CLIENTWRITE_HEADER |
  3741. (data->set.include_header ? CLIENTWRITE_BODY : 0) |
  3742. ((k->httpcode/100 == 1) ? CLIENTWRITE_1XX : 0);
  3743. headerlen = Curl_dyn_len(&data->state.headerb);
  3744. result = Curl_client_write(data, writetype,
  3745. Curl_dyn_ptr(&data->state.headerb),
  3746. headerlen);
  3747. if(result)
  3748. return result;
  3749. data->info.header_size += (long)headerlen;
  3750. data->req.headerbytecount += (long)headerlen;
  3751. /*
  3752. * When all the headers have been parsed, see if we should give
  3753. * up and return an error.
  3754. */
  3755. if(http_should_fail(data)) {
  3756. failf(data, "The requested URL returned error: %d",
  3757. k->httpcode);
  3758. return CURLE_HTTP_RETURNED_ERROR;
  3759. }
  3760. #ifdef USE_WEBSOCKETS
  3761. /* All non-101 HTTP status codes are bad when wanting to upgrade to
  3762. websockets */
  3763. if(data->req.upgr101 == UPGR101_WS) {
  3764. failf(data, "Refused WebSockets upgrade: %d", k->httpcode);
  3765. return CURLE_HTTP_RETURNED_ERROR;
  3766. }
  3767. #endif
  3768. data->req.deductheadercount =
  3769. (100 <= k->httpcode && 199 >= k->httpcode)?data->req.headerbytecount:0;
  3770. /* Curl_http_auth_act() checks what authentication methods
  3771. * that are available and decides which one (if any) to
  3772. * use. It will set 'newurl' if an auth method was picked. */
  3773. result = Curl_http_auth_act(data);
  3774. if(result)
  3775. return result;
  3776. if(k->httpcode >= 300) {
  3777. if((!conn->bits.authneg) && !conn->bits.close &&
  3778. !data->state.rewindbeforesend) {
  3779. /*
  3780. * General treatment of errors when about to send data. Including :
  3781. * "417 Expectation Failed", while waiting for 100-continue.
  3782. *
  3783. * The check for close above is done simply because of something
  3784. * else has already deemed the connection to get closed then
  3785. * something else should've considered the big picture and we
  3786. * avoid this check.
  3787. *
  3788. * rewindbeforesend indicates that something has told libcurl to
  3789. * continue sending even if it gets discarded
  3790. */
  3791. switch(data->state.httpreq) {
  3792. case HTTPREQ_PUT:
  3793. case HTTPREQ_POST:
  3794. case HTTPREQ_POST_FORM:
  3795. case HTTPREQ_POST_MIME:
  3796. /* We got an error response. If this happened before the whole
  3797. * request body has been sent we stop sending and mark the
  3798. * connection for closure after we've read the entire response.
  3799. */
  3800. Curl_expire_done(data, EXPIRE_100_TIMEOUT);
  3801. if(!k->upload_done) {
  3802. if((k->httpcode == 417) && data->state.expect100header) {
  3803. /* 417 Expectation Failed - try again without the Expect
  3804. header */
  3805. infof(data, "Got 417 while waiting for a 100");
  3806. data->state.disableexpect = TRUE;
  3807. DEBUGASSERT(!data->req.newurl);
  3808. data->req.newurl = strdup(data->state.url);
  3809. Curl_done_sending(data, k);
  3810. }
  3811. else if(data->set.http_keep_sending_on_error) {
  3812. infof(data, "HTTP error before end of send, keep sending");
  3813. if(k->exp100 > EXP100_SEND_DATA) {
  3814. k->exp100 = EXP100_SEND_DATA;
  3815. k->keepon |= KEEP_SEND;
  3816. }
  3817. }
  3818. else {
  3819. infof(data, "HTTP error before end of send, stop sending");
  3820. streamclose(conn, "Stop sending data before everything sent");
  3821. result = Curl_done_sending(data, k);
  3822. if(result)
  3823. return result;
  3824. k->upload_done = TRUE;
  3825. if(data->state.expect100header)
  3826. k->exp100 = EXP100_FAILED;
  3827. }
  3828. }
  3829. break;
  3830. default: /* default label present to avoid compiler warnings */
  3831. break;
  3832. }
  3833. }
  3834. if(data->state.rewindbeforesend &&
  3835. (conn->writesockfd != CURL_SOCKET_BAD)) {
  3836. /* We rewind before next send, continue sending now */
  3837. infof(data, "Keep sending data to get tossed away");
  3838. k->keepon |= KEEP_SEND;
  3839. }
  3840. }
  3841. if(!k->header) {
  3842. /*
  3843. * really end-of-headers.
  3844. *
  3845. * If we requested a "no body", this is a good time to get
  3846. * out and return home.
  3847. */
  3848. if(data->req.no_body)
  3849. *stop_reading = TRUE;
  3850. #ifndef CURL_DISABLE_RTSP
  3851. else if((conn->handler->protocol & CURLPROTO_RTSP) &&
  3852. (data->set.rtspreq == RTSPREQ_DESCRIBE) &&
  3853. (k->size <= -1))
  3854. /* Respect section 4.4 of rfc2326: If the Content-Length header is
  3855. absent, a length 0 must be assumed. It will prevent libcurl from
  3856. hanging on DESCRIBE request that got refused for whatever
  3857. reason */
  3858. *stop_reading = TRUE;
  3859. #endif
  3860. /* If max download size is *zero* (nothing) we already have
  3861. nothing and can safely return ok now! But for HTTP/2, we'd
  3862. like to call http2_handle_stream_close to properly close a
  3863. stream. In order to do this, we keep reading until we
  3864. close the stream. */
  3865. if(0 == k->maxdownload
  3866. && !Curl_conn_is_http2(data, conn, FIRSTSOCKET)
  3867. && !Curl_conn_is_http3(data, conn, FIRSTSOCKET))
  3868. *stop_reading = TRUE;
  3869. if(*stop_reading) {
  3870. /* we make sure that this socket isn't read more now */
  3871. k->keepon &= ~KEEP_RECV;
  3872. }
  3873. Curl_debug(data, CURLINFO_HEADER_IN, str_start, headerlen);
  3874. break; /* exit header line loop */
  3875. }
  3876. /* We continue reading headers, reset the line-based header */
  3877. Curl_dyn_reset(&data->state.headerb);
  3878. continue;
  3879. }
  3880. /*
  3881. * Checks for special headers coming up.
  3882. */
  3883. writetype = CLIENTWRITE_HEADER;
  3884. if(!k->headerline++) {
  3885. /* This is the first header, it MUST be the error code line
  3886. or else we consider this to be the body right away! */
  3887. bool fine_statusline = FALSE;
  3888. if(conn->handler->protocol & PROTO_FAMILY_HTTP) {
  3889. /*
  3890. * https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2
  3891. *
  3892. * The response code is always a three-digit number in HTTP as the spec
  3893. * says. We allow any three-digit number here, but we cannot make
  3894. * guarantees on future behaviors since it isn't within the protocol.
  3895. */
  3896. int httpversion = 0;
  3897. char *p = headp;
  3898. while(*p && ISBLANK(*p))
  3899. p++;
  3900. if(!strncmp(p, "HTTP/", 5)) {
  3901. p += 5;
  3902. switch(*p) {
  3903. case '1':
  3904. p++;
  3905. if((p[0] == '.') && (p[1] == '0' || p[1] == '1')) {
  3906. if(ISBLANK(p[2])) {
  3907. httpversion = 10 + (p[1] - '0');
  3908. p += 3;
  3909. if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
  3910. k->httpcode = (p[0] - '0') * 100 + (p[1] - '0') * 10 +
  3911. (p[2] - '0');
  3912. p += 3;
  3913. if(ISSPACE(*p))
  3914. fine_statusline = TRUE;
  3915. }
  3916. }
  3917. }
  3918. if(!fine_statusline) {
  3919. failf(data, "Unsupported HTTP/1 subversion in response");
  3920. return CURLE_UNSUPPORTED_PROTOCOL;
  3921. }
  3922. break;
  3923. case '2':
  3924. case '3':
  3925. if(!ISBLANK(p[1]))
  3926. break;
  3927. httpversion = (*p - '0') * 10;
  3928. p += 2;
  3929. if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
  3930. k->httpcode = (p[0] - '0') * 100 + (p[1] - '0') * 10 +
  3931. (p[2] - '0');
  3932. p += 3;
  3933. if(!ISSPACE(*p))
  3934. break;
  3935. fine_statusline = TRUE;
  3936. }
  3937. break;
  3938. default: /* unsupported */
  3939. failf(data, "Unsupported HTTP version in response");
  3940. return CURLE_UNSUPPORTED_PROTOCOL;
  3941. }
  3942. }
  3943. if(fine_statusline) {
  3944. if(k->httpcode < 100) {
  3945. failf(data, "Unsupported response code in HTTP response");
  3946. return CURLE_UNSUPPORTED_PROTOCOL;
  3947. }
  3948. switch(httpversion) {
  3949. case 10:
  3950. case 11:
  3951. #ifdef USE_HTTP2
  3952. case 20:
  3953. #endif
  3954. #ifdef ENABLE_QUIC
  3955. case 30:
  3956. #endif
  3957. conn->httpversion = (unsigned char)httpversion;
  3958. break;
  3959. default:
  3960. failf(data, "Unsupported HTTP version (%u.%d) in response",
  3961. httpversion/10, httpversion%10);
  3962. return CURLE_UNSUPPORTED_PROTOCOL;
  3963. }
  3964. if(k->upgr101 == UPGR101_RECEIVED) {
  3965. /* supposedly upgraded to http2 now */
  3966. if(conn->httpversion != 20)
  3967. infof(data, "Lying server, not serving HTTP/2");
  3968. }
  3969. if(conn->httpversion < 20) {
  3970. conn->bundle->multiuse = BUNDLE_NO_MULTIUSE;
  3971. }
  3972. }
  3973. else {
  3974. /* If user has set option HTTP200ALIASES,
  3975. compare header line against list of aliases
  3976. */
  3977. statusline check =
  3978. checkhttpprefix(data,
  3979. Curl_dyn_ptr(&data->state.headerb),
  3980. Curl_dyn_len(&data->state.headerb));
  3981. if(check == STATUS_DONE) {
  3982. fine_statusline = TRUE;
  3983. k->httpcode = 200;
  3984. conn->httpversion = 10;
  3985. }
  3986. }
  3987. }
  3988. else if(conn->handler->protocol & CURLPROTO_RTSP) {
  3989. char *p = headp;
  3990. while(*p && ISBLANK(*p))
  3991. p++;
  3992. if(!strncmp(p, "RTSP/", 5)) {
  3993. p += 5;
  3994. if(ISDIGIT(*p)) {
  3995. p++;
  3996. if((p[0] == '.') && ISDIGIT(p[1])) {
  3997. if(ISBLANK(p[2])) {
  3998. p += 3;
  3999. if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
  4000. k->httpcode = (p[0] - '0') * 100 + (p[1] - '0') * 10 +
  4001. (p[2] - '0');
  4002. p += 3;
  4003. if(ISSPACE(*p)) {
  4004. fine_statusline = TRUE;
  4005. conn->httpversion = 11; /* RTSP acts like HTTP 1.1 */
  4006. }
  4007. }
  4008. }
  4009. }
  4010. }
  4011. if(!fine_statusline)
  4012. return CURLE_WEIRD_SERVER_REPLY;
  4013. }
  4014. }
  4015. if(fine_statusline) {
  4016. result = Curl_http_statusline(data, conn);
  4017. if(result)
  4018. return result;
  4019. writetype |= CLIENTWRITE_STATUS;
  4020. }
  4021. else {
  4022. k->header = FALSE; /* this is not a header line */
  4023. break;
  4024. }
  4025. }
  4026. result = verify_header(data);
  4027. if(result)
  4028. return result;
  4029. result = Curl_http_header(data, conn, headp);
  4030. if(result)
  4031. return result;
  4032. /*
  4033. * End of header-checks. Write them to the client.
  4034. */
  4035. if(data->set.include_header)
  4036. writetype |= CLIENTWRITE_BODY;
  4037. if(k->httpcode/100 == 1)
  4038. writetype |= CLIENTWRITE_1XX;
  4039. Curl_debug(data, CURLINFO_HEADER_IN, headp,
  4040. Curl_dyn_len(&data->state.headerb));
  4041. result = Curl_client_write(data, writetype, headp,
  4042. Curl_dyn_len(&data->state.headerb));
  4043. if(result)
  4044. return result;
  4045. data->info.header_size += Curl_dyn_len(&data->state.headerb);
  4046. data->req.headerbytecount += Curl_dyn_len(&data->state.headerb);
  4047. Curl_dyn_reset(&data->state.headerb);
  4048. }
  4049. while(*k->str); /* header line within buffer */
  4050. /* We might have reached the end of the header part here, but
  4051. there might be a non-header part left in the end of the read
  4052. buffer. */
  4053. return CURLE_OK;
  4054. }
  4055. /* Decode HTTP status code string. */
  4056. CURLcode Curl_http_decode_status(int *pstatus, const char *s, size_t len)
  4057. {
  4058. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  4059. int status = 0;
  4060. int i;
  4061. if(len != 3)
  4062. goto out;
  4063. for(i = 0; i < 3; ++i) {
  4064. char c = s[i];
  4065. if(c < '0' || c > '9')
  4066. goto out;
  4067. status *= 10;
  4068. status += c - '0';
  4069. }
  4070. result = CURLE_OK;
  4071. out:
  4072. *pstatus = result? -1 : status;
  4073. return result;
  4074. }
  4075. /* simple implementation of strndup(), which isn't portable */
  4076. static char *my_strndup(const char *ptr, size_t len)
  4077. {
  4078. char *copy = malloc(len + 1);
  4079. if(!copy)
  4080. return NULL;
  4081. memcpy(copy, ptr, len);
  4082. copy[len] = '\0';
  4083. return copy;
  4084. }
  4085. CURLcode Curl_http_req_make(struct httpreq **preq,
  4086. const char *method, size_t m_len,
  4087. const char *scheme, size_t s_len,
  4088. const char *authority, size_t a_len,
  4089. const char *path, size_t p_len)
  4090. {
  4091. struct httpreq *req;
  4092. CURLcode result = CURLE_OUT_OF_MEMORY;
  4093. DEBUGASSERT(method);
  4094. if(m_len + 1 >= sizeof(req->method))
  4095. return CURLE_BAD_FUNCTION_ARGUMENT;
  4096. req = calloc(1, sizeof(*req));
  4097. if(!req)
  4098. goto out;
  4099. memcpy(req->method, method, m_len);
  4100. if(scheme) {
  4101. req->scheme = my_strndup(scheme, s_len);
  4102. if(!req->scheme)
  4103. goto out;
  4104. }
  4105. if(authority) {
  4106. req->authority = my_strndup(authority, a_len);
  4107. if(!req->authority)
  4108. goto out;
  4109. }
  4110. if(path) {
  4111. req->path = my_strndup(path, p_len);
  4112. if(!req->path)
  4113. goto out;
  4114. }
  4115. Curl_dynhds_init(&req->headers, 0, DYN_HTTP_REQUEST);
  4116. Curl_dynhds_init(&req->trailers, 0, DYN_HTTP_REQUEST);
  4117. result = CURLE_OK;
  4118. out:
  4119. if(result && req)
  4120. Curl_http_req_free(req);
  4121. *preq = result? NULL : req;
  4122. return result;
  4123. }
  4124. static CURLcode req_assign_url_authority(struct httpreq *req, CURLU *url)
  4125. {
  4126. char *user, *pass, *host, *port;
  4127. struct dynbuf buf;
  4128. CURLUcode uc;
  4129. CURLcode result = CURLE_URL_MALFORMAT;
  4130. user = pass = host = port = NULL;
  4131. Curl_dyn_init(&buf, DYN_HTTP_REQUEST);
  4132. uc = curl_url_get(url, CURLUPART_HOST, &host, 0);
  4133. if(uc && uc != CURLUE_NO_HOST)
  4134. goto out;
  4135. if(!host) {
  4136. req->authority = NULL;
  4137. result = CURLE_OK;
  4138. goto out;
  4139. }
  4140. uc = curl_url_get(url, CURLUPART_PORT, &port, CURLU_NO_DEFAULT_PORT);
  4141. if(uc && uc != CURLUE_NO_PORT)
  4142. goto out;
  4143. uc = curl_url_get(url, CURLUPART_USER, &user, 0);
  4144. if(uc && uc != CURLUE_NO_USER)
  4145. goto out;
  4146. if(user) {
  4147. uc = curl_url_get(url, CURLUPART_PASSWORD, &pass, 0);
  4148. if(uc && uc != CURLUE_NO_PASSWORD)
  4149. goto out;
  4150. }
  4151. if(user) {
  4152. result = Curl_dyn_add(&buf, user);
  4153. if(result)
  4154. goto out;
  4155. if(pass) {
  4156. result = Curl_dyn_addf(&buf, ":%s", pass);
  4157. if(result)
  4158. goto out;
  4159. }
  4160. result = Curl_dyn_add(&buf, "@");
  4161. if(result)
  4162. goto out;
  4163. }
  4164. result = Curl_dyn_add(&buf, host);
  4165. if(result)
  4166. goto out;
  4167. if(port) {
  4168. result = Curl_dyn_addf(&buf, ":%s", port);
  4169. if(result)
  4170. goto out;
  4171. }
  4172. req->authority = strdup(Curl_dyn_ptr(&buf));
  4173. if(!req->authority)
  4174. goto out;
  4175. result = CURLE_OK;
  4176. out:
  4177. free(user);
  4178. free(pass);
  4179. free(host);
  4180. free(port);
  4181. Curl_dyn_free(&buf);
  4182. return result;
  4183. }
  4184. static CURLcode req_assign_url_path(struct httpreq *req, CURLU *url)
  4185. {
  4186. char *path, *query;
  4187. struct dynbuf buf;
  4188. CURLUcode uc;
  4189. CURLcode result = CURLE_URL_MALFORMAT;
  4190. path = query = NULL;
  4191. Curl_dyn_init(&buf, DYN_HTTP_REQUEST);
  4192. uc = curl_url_get(url, CURLUPART_PATH, &path, CURLU_PATH_AS_IS);
  4193. if(uc)
  4194. goto out;
  4195. uc = curl_url_get(url, CURLUPART_QUERY, &query, 0);
  4196. if(uc && uc != CURLUE_NO_QUERY)
  4197. goto out;
  4198. if(!path && !query) {
  4199. req->path = NULL;
  4200. }
  4201. else if(path && !query) {
  4202. req->path = path;
  4203. path = NULL;
  4204. }
  4205. else {
  4206. if(path) {
  4207. result = Curl_dyn_add(&buf, path);
  4208. if(result)
  4209. goto out;
  4210. }
  4211. if(query) {
  4212. result = Curl_dyn_addf(&buf, "?%s", query);
  4213. if(result)
  4214. goto out;
  4215. }
  4216. req->path = strdup(Curl_dyn_ptr(&buf));
  4217. if(!req->path)
  4218. goto out;
  4219. }
  4220. result = CURLE_OK;
  4221. out:
  4222. free(path);
  4223. free(query);
  4224. Curl_dyn_free(&buf);
  4225. return result;
  4226. }
  4227. CURLcode Curl_http_req_make2(struct httpreq **preq,
  4228. const char *method, size_t m_len,
  4229. CURLU *url, const char *scheme_default)
  4230. {
  4231. struct httpreq *req;
  4232. CURLcode result = CURLE_OUT_OF_MEMORY;
  4233. CURLUcode uc;
  4234. DEBUGASSERT(method);
  4235. if(m_len + 1 >= sizeof(req->method))
  4236. return CURLE_BAD_FUNCTION_ARGUMENT;
  4237. req = calloc(1, sizeof(*req));
  4238. if(!req)
  4239. goto out;
  4240. memcpy(req->method, method, m_len);
  4241. uc = curl_url_get(url, CURLUPART_SCHEME, &req->scheme, 0);
  4242. if(uc && uc != CURLUE_NO_SCHEME)
  4243. goto out;
  4244. if(!req->scheme && scheme_default) {
  4245. req->scheme = strdup(scheme_default);
  4246. if(!req->scheme)
  4247. goto out;
  4248. }
  4249. result = req_assign_url_authority(req, url);
  4250. if(result)
  4251. goto out;
  4252. result = req_assign_url_path(req, url);
  4253. if(result)
  4254. goto out;
  4255. Curl_dynhds_init(&req->headers, 0, DYN_HTTP_REQUEST);
  4256. Curl_dynhds_init(&req->trailers, 0, DYN_HTTP_REQUEST);
  4257. result = CURLE_OK;
  4258. out:
  4259. if(result && req)
  4260. Curl_http_req_free(req);
  4261. *preq = result? NULL : req;
  4262. return result;
  4263. }
  4264. void Curl_http_req_free(struct httpreq *req)
  4265. {
  4266. if(req) {
  4267. free(req->scheme);
  4268. free(req->authority);
  4269. free(req->path);
  4270. Curl_dynhds_free(&req->headers);
  4271. Curl_dynhds_free(&req->trailers);
  4272. free(req);
  4273. }
  4274. }
  4275. struct name_const {
  4276. const char *name;
  4277. size_t namelen;
  4278. };
  4279. static struct name_const H2_NON_FIELD[] = {
  4280. { STRCONST("Host") },
  4281. { STRCONST("Upgrade") },
  4282. { STRCONST("Connection") },
  4283. { STRCONST("Keep-Alive") },
  4284. { STRCONST("Proxy-Connection") },
  4285. { STRCONST("Transfer-Encoding") },
  4286. };
  4287. static bool h2_non_field(const char *name, size_t namelen)
  4288. {
  4289. size_t i;
  4290. for(i = 0; i < sizeof(H2_NON_FIELD)/sizeof(H2_NON_FIELD[0]); ++i) {
  4291. if(namelen < H2_NON_FIELD[i].namelen)
  4292. return FALSE;
  4293. if(namelen == H2_NON_FIELD[i].namelen &&
  4294. strcasecompare(H2_NON_FIELD[i].name, name))
  4295. return TRUE;
  4296. }
  4297. return FALSE;
  4298. }
  4299. CURLcode Curl_http_req_to_h2(struct dynhds *h2_headers,
  4300. struct httpreq *req, struct Curl_easy *data)
  4301. {
  4302. const char *scheme = NULL, *authority = NULL;
  4303. struct dynhds_entry *e;
  4304. size_t i;
  4305. CURLcode result;
  4306. DEBUGASSERT(req);
  4307. DEBUGASSERT(h2_headers);
  4308. if(req->scheme) {
  4309. scheme = req->scheme;
  4310. }
  4311. else if(strcmp("CONNECT", req->method)) {
  4312. scheme = Curl_checkheaders(data, STRCONST(HTTP_PSEUDO_SCHEME));
  4313. if(scheme) {
  4314. scheme += sizeof(HTTP_PSEUDO_SCHEME);
  4315. while(*scheme && ISBLANK(*scheme))
  4316. scheme++;
  4317. infof(data, "set pseudo header %s to %s", HTTP_PSEUDO_SCHEME, scheme);
  4318. }
  4319. else {
  4320. scheme = (data->conn && data->conn->handler->flags & PROTOPT_SSL)?
  4321. "https" : "http";
  4322. }
  4323. }
  4324. if(req->authority) {
  4325. authority = req->authority;
  4326. }
  4327. else {
  4328. e = Curl_dynhds_get(&req->headers, STRCONST("Host"));
  4329. if(e)
  4330. authority = e->value;
  4331. }
  4332. Curl_dynhds_reset(h2_headers);
  4333. Curl_dynhds_set_opts(h2_headers, DYNHDS_OPT_LOWERCASE);
  4334. result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_METHOD),
  4335. req->method, strlen(req->method));
  4336. if(!result && scheme) {
  4337. result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_SCHEME),
  4338. scheme, strlen(scheme));
  4339. }
  4340. if(!result && authority) {
  4341. result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_AUTHORITY),
  4342. authority, strlen(authority));
  4343. }
  4344. if(!result && req->path) {
  4345. result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_PATH),
  4346. req->path, strlen(req->path));
  4347. }
  4348. for(i = 0; !result && i < Curl_dynhds_count(&req->headers); ++i) {
  4349. e = Curl_dynhds_getn(&req->headers, i);
  4350. if(!h2_non_field(e->name, e->namelen)) {
  4351. result = Curl_dynhds_add(h2_headers, e->name, e->namelen,
  4352. e->value, e->valuelen);
  4353. }
  4354. }
  4355. return result;
  4356. }
  4357. CURLcode Curl_http_resp_make(struct http_resp **presp,
  4358. int status,
  4359. const char *description)
  4360. {
  4361. struct http_resp *resp;
  4362. CURLcode result = CURLE_OUT_OF_MEMORY;
  4363. resp = calloc(1, sizeof(*resp));
  4364. if(!resp)
  4365. goto out;
  4366. resp->status = status;
  4367. if(description) {
  4368. resp->description = strdup(description);
  4369. if(!resp->description)
  4370. goto out;
  4371. }
  4372. Curl_dynhds_init(&resp->headers, 0, DYN_HTTP_REQUEST);
  4373. Curl_dynhds_init(&resp->trailers, 0, DYN_HTTP_REQUEST);
  4374. result = CURLE_OK;
  4375. out:
  4376. if(result && resp)
  4377. Curl_http_resp_free(resp);
  4378. *presp = result? NULL : resp;
  4379. return result;
  4380. }
  4381. void Curl_http_resp_free(struct http_resp *resp)
  4382. {
  4383. if(resp) {
  4384. free(resp->description);
  4385. Curl_dynhds_free(&resp->headers);
  4386. Curl_dynhds_free(&resp->trailers);
  4387. if(resp->prev)
  4388. Curl_http_resp_free(resp->prev);
  4389. free(resp);
  4390. }
  4391. }
  4392. #endif /* CURL_DISABLE_HTTP */