http.c 133 KB

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