http.c 120 KB

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