http.c 122 KB

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