transfer.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "strtoofft.h"
  24. #ifdef HAVE_NETINET_IN_H
  25. #include <netinet/in.h>
  26. #endif
  27. #ifdef HAVE_NETDB_H
  28. #include <netdb.h>
  29. #endif
  30. #ifdef HAVE_ARPA_INET_H
  31. #include <arpa/inet.h>
  32. #endif
  33. #ifdef HAVE_NET_IF_H
  34. #include <net/if.h>
  35. #endif
  36. #ifdef HAVE_SYS_IOCTL_H
  37. #include <sys/ioctl.h>
  38. #endif
  39. #ifdef HAVE_SIGNAL_H
  40. #include <signal.h>
  41. #endif
  42. #ifdef HAVE_SYS_PARAM_H
  43. #include <sys/param.h>
  44. #endif
  45. #ifdef HAVE_SYS_SELECT_H
  46. #include <sys/select.h>
  47. #elif defined(HAVE_UNISTD_H)
  48. #include <unistd.h>
  49. #endif
  50. #ifndef HAVE_SOCKET
  51. #error "We can't compile without socket() support!"
  52. #endif
  53. #include "urldata.h"
  54. #include <curl/curl.h>
  55. #include "netrc.h"
  56. #include "content_encoding.h"
  57. #include "hostip.h"
  58. #include "transfer.h"
  59. #include "sendf.h"
  60. #include "speedcheck.h"
  61. #include "progress.h"
  62. #include "http.h"
  63. #include "url.h"
  64. #include "getinfo.h"
  65. #include "vtls/vtls.h"
  66. #include "select.h"
  67. #include "multiif.h"
  68. #include "connect.h"
  69. #include "non-ascii.h"
  70. #include "http2.h"
  71. #include "mime.h"
  72. #include "strcase.h"
  73. #include "urlapi-int.h"
  74. #include "hsts.h"
  75. /* The last 3 #include files should be in this order */
  76. #include "curl_printf.h"
  77. #include "curl_memory.h"
  78. #include "memdebug.h"
  79. #if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_SMTP) || \
  80. !defined(CURL_DISABLE_IMAP)
  81. /*
  82. * checkheaders() checks the linked list of custom headers for a
  83. * particular header (prefix). Provide the prefix without colon!
  84. *
  85. * Returns a pointer to the first matching header or NULL if none matched.
  86. */
  87. char *Curl_checkheaders(const struct Curl_easy *data,
  88. const char *thisheader)
  89. {
  90. struct curl_slist *head;
  91. size_t thislen = strlen(thisheader);
  92. for(head = data->set.headers; head; head = head->next) {
  93. if(strncasecompare(head->data, thisheader, thislen) &&
  94. Curl_headersep(head->data[thislen]) )
  95. return head->data;
  96. }
  97. return NULL;
  98. }
  99. #endif
  100. CURLcode Curl_get_upload_buffer(struct Curl_easy *data)
  101. {
  102. if(!data->state.ulbuf) {
  103. data->state.ulbuf = malloc(data->set.upload_buffer_size);
  104. if(!data->state.ulbuf)
  105. return CURLE_OUT_OF_MEMORY;
  106. }
  107. return CURLE_OK;
  108. }
  109. #ifndef CURL_DISABLE_HTTP
  110. /*
  111. * This function will be called to loop through the trailers buffer
  112. * until no more data is available for sending.
  113. */
  114. static size_t trailers_read(char *buffer, size_t size, size_t nitems,
  115. void *raw)
  116. {
  117. struct Curl_easy *data = (struct Curl_easy *)raw;
  118. struct dynbuf *trailers_buf = &data->state.trailers_buf;
  119. size_t bytes_left = Curl_dyn_len(trailers_buf) -
  120. data->state.trailers_bytes_sent;
  121. size_t to_copy = (size*nitems < bytes_left) ? size*nitems : bytes_left;
  122. if(to_copy) {
  123. memcpy(buffer,
  124. Curl_dyn_ptr(trailers_buf) + data->state.trailers_bytes_sent,
  125. to_copy);
  126. data->state.trailers_bytes_sent += to_copy;
  127. }
  128. return to_copy;
  129. }
  130. static size_t trailers_left(void *raw)
  131. {
  132. struct Curl_easy *data = (struct Curl_easy *)raw;
  133. struct dynbuf *trailers_buf = &data->state.trailers_buf;
  134. return Curl_dyn_len(trailers_buf) - data->state.trailers_bytes_sent;
  135. }
  136. #endif
  137. /*
  138. * This function will call the read callback to fill our buffer with data
  139. * to upload.
  140. */
  141. CURLcode Curl_fillreadbuffer(struct Curl_easy *data, size_t bytes,
  142. size_t *nreadp)
  143. {
  144. size_t buffersize = bytes;
  145. size_t nread;
  146. curl_read_callback readfunc = NULL;
  147. void *extra_data = NULL;
  148. #ifdef CURL_DOES_CONVERSIONS
  149. bool sending_http_headers = FALSE;
  150. struct connectdata *conn = data->conn;
  151. if(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)) {
  152. const struct HTTP *http = data->req.p.http;
  153. if(http->sending == HTTPSEND_REQUEST)
  154. /* We're sending the HTTP request headers, not the data.
  155. Remember that so we don't re-translate them into garbage. */
  156. sending_http_headers = TRUE;
  157. }
  158. #endif
  159. #ifndef CURL_DISABLE_HTTP
  160. if(data->state.trailers_state == TRAILERS_INITIALIZED) {
  161. struct curl_slist *trailers = NULL;
  162. CURLcode result;
  163. int trailers_ret_code;
  164. /* at this point we already verified that the callback exists
  165. so we compile and store the trailers buffer, then proceed */
  166. infof(data,
  167. "Moving trailers state machine from initialized to sending.\n");
  168. data->state.trailers_state = TRAILERS_SENDING;
  169. Curl_dyn_init(&data->state.trailers_buf, DYN_TRAILERS);
  170. data->state.trailers_bytes_sent = 0;
  171. Curl_set_in_callback(data, true);
  172. trailers_ret_code = data->set.trailer_callback(&trailers,
  173. data->set.trailer_data);
  174. Curl_set_in_callback(data, false);
  175. if(trailers_ret_code == CURL_TRAILERFUNC_OK) {
  176. result = Curl_http_compile_trailers(trailers, &data->state.trailers_buf,
  177. data);
  178. }
  179. else {
  180. failf(data, "operation aborted by trailing headers callback");
  181. *nreadp = 0;
  182. result = CURLE_ABORTED_BY_CALLBACK;
  183. }
  184. if(result) {
  185. Curl_dyn_free(&data->state.trailers_buf);
  186. curl_slist_free_all(trailers);
  187. return result;
  188. }
  189. infof(data, "Successfully compiled trailers.\r\n");
  190. curl_slist_free_all(trailers);
  191. }
  192. #endif
  193. /* if we are transmitting trailing data, we don't need to write
  194. a chunk size so we skip this */
  195. if(data->req.upload_chunky &&
  196. data->state.trailers_state == TRAILERS_NONE) {
  197. /* if chunked Transfer-Encoding */
  198. buffersize -= (8 + 2 + 2); /* 32bit hex + CRLF + CRLF */
  199. data->req.upload_fromhere += (8 + 2); /* 32bit hex + CRLF */
  200. }
  201. #ifndef CURL_DISABLE_HTTP
  202. if(data->state.trailers_state == TRAILERS_SENDING) {
  203. /* if we're here then that means that we already sent the last empty chunk
  204. but we didn't send a final CR LF, so we sent 0 CR LF. We then start
  205. pulling trailing data until we have no more at which point we
  206. simply return to the previous point in the state machine as if
  207. nothing happened.
  208. */
  209. readfunc = trailers_read;
  210. extra_data = (void *)data;
  211. }
  212. else
  213. #endif
  214. {
  215. readfunc = data->state.fread_func;
  216. extra_data = data->state.in;
  217. }
  218. Curl_set_in_callback(data, true);
  219. nread = readfunc(data->req.upload_fromhere, 1,
  220. buffersize, extra_data);
  221. Curl_set_in_callback(data, false);
  222. if(nread == CURL_READFUNC_ABORT) {
  223. failf(data, "operation aborted by callback");
  224. *nreadp = 0;
  225. return CURLE_ABORTED_BY_CALLBACK;
  226. }
  227. if(nread == CURL_READFUNC_PAUSE) {
  228. struct SingleRequest *k = &data->req;
  229. if(data->conn->handler->flags & PROTOPT_NONETWORK) {
  230. /* protocols that work without network cannot be paused. This is
  231. actually only FILE:// just now, and it can't pause since the transfer
  232. isn't done using the "normal" procedure. */
  233. failf(data, "Read callback asked for PAUSE when not supported!");
  234. return CURLE_READ_ERROR;
  235. }
  236. /* CURL_READFUNC_PAUSE pauses read callbacks that feed socket writes */
  237. k->keepon |= KEEP_SEND_PAUSE; /* mark socket send as paused */
  238. if(data->req.upload_chunky) {
  239. /* Back out the preallocation done above */
  240. data->req.upload_fromhere -= (8 + 2);
  241. }
  242. *nreadp = 0;
  243. return CURLE_OK; /* nothing was read */
  244. }
  245. else if(nread > buffersize) {
  246. /* the read function returned a too large value */
  247. *nreadp = 0;
  248. failf(data, "read function returned funny value");
  249. return CURLE_READ_ERROR;
  250. }
  251. if(!data->req.forbidchunk && data->req.upload_chunky) {
  252. /* if chunked Transfer-Encoding
  253. * build chunk:
  254. *
  255. * <HEX SIZE> CRLF
  256. * <DATA> CRLF
  257. */
  258. /* On non-ASCII platforms the <DATA> may or may not be
  259. translated based on set.prefer_ascii while the protocol
  260. portion must always be translated to the network encoding.
  261. To further complicate matters, line end conversion might be
  262. done later on, so we need to prevent CRLFs from becoming
  263. CRCRLFs if that's the case. To do this we use bare LFs
  264. here, knowing they'll become CRLFs later on.
  265. */
  266. bool added_crlf = FALSE;
  267. int hexlen = 0;
  268. const char *endofline_native;
  269. const char *endofline_network;
  270. if(
  271. #ifdef CURL_DO_LINEEND_CONV
  272. (data->set.prefer_ascii) ||
  273. #endif
  274. (data->set.crlf)) {
  275. /* \n will become \r\n later on */
  276. endofline_native = "\n";
  277. endofline_network = "\x0a";
  278. }
  279. else {
  280. endofline_native = "\r\n";
  281. endofline_network = "\x0d\x0a";
  282. }
  283. /* if we're not handling trailing data, proceed as usual */
  284. if(data->state.trailers_state != TRAILERS_SENDING) {
  285. char hexbuffer[11] = "";
  286. hexlen = msnprintf(hexbuffer, sizeof(hexbuffer),
  287. "%zx%s", nread, endofline_native);
  288. /* move buffer pointer */
  289. data->req.upload_fromhere -= hexlen;
  290. nread += hexlen;
  291. /* copy the prefix to the buffer, leaving out the NUL */
  292. memcpy(data->req.upload_fromhere, hexbuffer, hexlen);
  293. /* always append ASCII CRLF to the data unless
  294. we have a valid trailer callback */
  295. #ifndef CURL_DISABLE_HTTP
  296. if((nread-hexlen) == 0 &&
  297. data->set.trailer_callback != NULL &&
  298. data->state.trailers_state == TRAILERS_NONE) {
  299. data->state.trailers_state = TRAILERS_INITIALIZED;
  300. }
  301. else
  302. #endif
  303. {
  304. memcpy(data->req.upload_fromhere + nread,
  305. endofline_network,
  306. strlen(endofline_network));
  307. added_crlf = TRUE;
  308. }
  309. }
  310. #ifdef CURL_DOES_CONVERSIONS
  311. {
  312. CURLcode result;
  313. size_t length;
  314. if(data->set.prefer_ascii)
  315. /* translate the protocol and data */
  316. length = nread;
  317. else
  318. /* just translate the protocol portion */
  319. length = hexlen;
  320. if(length) {
  321. result = Curl_convert_to_network(data, data->req.upload_fromhere,
  322. length);
  323. /* Curl_convert_to_network calls failf if unsuccessful */
  324. if(result)
  325. return result;
  326. }
  327. }
  328. #endif /* CURL_DOES_CONVERSIONS */
  329. #ifndef CURL_DISABLE_HTTP
  330. if(data->state.trailers_state == TRAILERS_SENDING &&
  331. !trailers_left(data)) {
  332. Curl_dyn_free(&data->state.trailers_buf);
  333. data->state.trailers_state = TRAILERS_DONE;
  334. data->set.trailer_data = NULL;
  335. data->set.trailer_callback = NULL;
  336. /* mark the transfer as done */
  337. data->req.upload_done = TRUE;
  338. infof(data, "Signaling end of chunked upload after trailers.\n");
  339. }
  340. else
  341. #endif
  342. if((nread - hexlen) == 0 &&
  343. data->state.trailers_state != TRAILERS_INITIALIZED) {
  344. /* mark this as done once this chunk is transferred */
  345. data->req.upload_done = TRUE;
  346. infof(data,
  347. "Signaling end of chunked upload via terminating chunk.\n");
  348. }
  349. if(added_crlf)
  350. nread += strlen(endofline_network); /* for the added end of line */
  351. }
  352. #ifdef CURL_DOES_CONVERSIONS
  353. else if((data->set.prefer_ascii) && (!sending_http_headers)) {
  354. CURLcode result;
  355. result = Curl_convert_to_network(data, data->req.upload_fromhere, nread);
  356. /* Curl_convert_to_network calls failf if unsuccessful */
  357. if(result)
  358. return result;
  359. }
  360. #endif /* CURL_DOES_CONVERSIONS */
  361. *nreadp = nread;
  362. return CURLE_OK;
  363. }
  364. /*
  365. * Curl_readrewind() rewinds the read stream. This is typically used for HTTP
  366. * POST/PUT with multi-pass authentication when a sending was denied and a
  367. * resend is necessary.
  368. */
  369. CURLcode Curl_readrewind(struct Curl_easy *data)
  370. {
  371. struct connectdata *conn = data->conn;
  372. curl_mimepart *mimepart = &data->set.mimepost;
  373. conn->bits.rewindaftersend = FALSE; /* we rewind now */
  374. /* explicitly switch off sending data on this connection now since we are
  375. about to restart a new transfer and thus we want to avoid inadvertently
  376. sending more data on the existing connection until the next transfer
  377. starts */
  378. data->req.keepon &= ~KEEP_SEND;
  379. /* We have sent away data. If not using CURLOPT_POSTFIELDS or
  380. CURLOPT_HTTPPOST, call app to rewind
  381. */
  382. if(conn->handler->protocol & PROTO_FAMILY_HTTP) {
  383. struct HTTP *http = data->req.p.http;
  384. if(http->sendit)
  385. mimepart = http->sendit;
  386. }
  387. if(data->set.postfields)
  388. ; /* do nothing */
  389. else if(data->state.httpreq == HTTPREQ_POST_MIME ||
  390. data->state.httpreq == HTTPREQ_POST_FORM) {
  391. CURLcode result = Curl_mime_rewind(mimepart);
  392. if(result) {
  393. failf(data, "Cannot rewind mime/post data");
  394. return result;
  395. }
  396. }
  397. else {
  398. if(data->set.seek_func) {
  399. int err;
  400. Curl_set_in_callback(data, true);
  401. err = (data->set.seek_func)(data->set.seek_client, 0, SEEK_SET);
  402. Curl_set_in_callback(data, false);
  403. if(err) {
  404. failf(data, "seek callback returned error %d", (int)err);
  405. return CURLE_SEND_FAIL_REWIND;
  406. }
  407. }
  408. else if(data->set.ioctl_func) {
  409. curlioerr err;
  410. Curl_set_in_callback(data, true);
  411. err = (data->set.ioctl_func)(data, CURLIOCMD_RESTARTREAD,
  412. data->set.ioctl_client);
  413. Curl_set_in_callback(data, false);
  414. infof(data, "the ioctl callback returned %d\n", (int)err);
  415. if(err) {
  416. failf(data, "ioctl callback returned error %d", (int)err);
  417. return CURLE_SEND_FAIL_REWIND;
  418. }
  419. }
  420. else {
  421. /* If no CURLOPT_READFUNCTION is used, we know that we operate on a
  422. given FILE * stream and we can actually attempt to rewind that
  423. ourselves with fseek() */
  424. if(data->state.fread_func == (curl_read_callback)fread) {
  425. if(-1 != fseek(data->state.in, 0, SEEK_SET))
  426. /* successful rewind */
  427. return CURLE_OK;
  428. }
  429. /* no callback set or failure above, makes us fail at once */
  430. failf(data, "necessary data rewind wasn't possible");
  431. return CURLE_SEND_FAIL_REWIND;
  432. }
  433. }
  434. return CURLE_OK;
  435. }
  436. static int data_pending(const struct Curl_easy *data)
  437. {
  438. struct connectdata *conn = data->conn;
  439. #ifdef ENABLE_QUIC
  440. if(conn->transport == TRNSPRT_QUIC)
  441. return Curl_quic_data_pending(data);
  442. #endif
  443. /* in the case of libssh2, we can never be really sure that we have emptied
  444. its internal buffers so we MUST always try until we get EAGAIN back */
  445. return conn->handler->protocol&(CURLPROTO_SCP|CURLPROTO_SFTP) ||
  446. #if defined(USE_NGHTTP2)
  447. Curl_ssl_data_pending(conn, FIRSTSOCKET) ||
  448. /* For HTTP/2, we may read up everything including response body
  449. with header fields in Curl_http_readwrite_headers. If no
  450. content-length is provided, curl waits for the connection
  451. close, which we emulate it using conn->proto.httpc.closed =
  452. TRUE. The thing is if we read everything, then http2_recv won't
  453. be called and we cannot signal the HTTP/2 stream has closed. As
  454. a workaround, we return nonzero here to call http2_recv. */
  455. ((conn->handler->protocol&PROTO_FAMILY_HTTP) && conn->httpversion >= 20);
  456. #else
  457. Curl_ssl_data_pending(conn, FIRSTSOCKET);
  458. #endif
  459. }
  460. /*
  461. * Check to see if CURLOPT_TIMECONDITION was met by comparing the time of the
  462. * remote document with the time provided by CURLOPT_TIMEVAL
  463. */
  464. bool Curl_meets_timecondition(struct Curl_easy *data, time_t timeofdoc)
  465. {
  466. if((timeofdoc == 0) || (data->set.timevalue == 0))
  467. return TRUE;
  468. switch(data->set.timecondition) {
  469. case CURL_TIMECOND_IFMODSINCE:
  470. default:
  471. if(timeofdoc <= data->set.timevalue) {
  472. infof(data,
  473. "The requested document is not new enough\n");
  474. data->info.timecond = TRUE;
  475. return FALSE;
  476. }
  477. break;
  478. case CURL_TIMECOND_IFUNMODSINCE:
  479. if(timeofdoc >= data->set.timevalue) {
  480. infof(data,
  481. "The requested document is not old enough\n");
  482. data->info.timecond = TRUE;
  483. return FALSE;
  484. }
  485. break;
  486. }
  487. return TRUE;
  488. }
  489. /*
  490. * Go ahead and do a read if we have a readable socket or if
  491. * the stream was rewound (in which case we have data in a
  492. * buffer)
  493. *
  494. * return '*comeback' TRUE if we didn't properly drain the socket so this
  495. * function should get called again without select() or similar in between!
  496. */
  497. static CURLcode readwrite_data(struct Curl_easy *data,
  498. struct connectdata *conn,
  499. struct SingleRequest *k,
  500. int *didwhat, bool *done,
  501. bool *comeback)
  502. {
  503. CURLcode result = CURLE_OK;
  504. ssize_t nread; /* number of bytes read */
  505. size_t excess = 0; /* excess bytes read */
  506. bool readmore = FALSE; /* used by RTP to signal for more data */
  507. int maxloops = 100;
  508. char *buf = data->state.buffer;
  509. DEBUGASSERT(buf);
  510. *done = FALSE;
  511. *comeback = FALSE;
  512. /* This is where we loop until we have read everything there is to
  513. read or we get a CURLE_AGAIN */
  514. do {
  515. bool is_empty_data = FALSE;
  516. size_t buffersize = data->set.buffer_size;
  517. size_t bytestoread = buffersize;
  518. #ifdef USE_NGHTTP2
  519. bool is_http2 = ((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  520. (conn->httpversion == 20));
  521. #endif
  522. if(
  523. #ifdef USE_NGHTTP2
  524. /* For HTTP/2, read data without caring about the content
  525. length. This is safe because body in HTTP/2 is always
  526. segmented thanks to its framing layer. Meanwhile, we have to
  527. call Curl_read to ensure that http2_handle_stream_close is
  528. called when we read all incoming bytes for a particular
  529. stream. */
  530. !is_http2 &&
  531. #endif
  532. k->size != -1 && !k->header) {
  533. /* make sure we don't read too much */
  534. curl_off_t totalleft = k->size - k->bytecount;
  535. if(totalleft < (curl_off_t)bytestoread)
  536. bytestoread = (size_t)totalleft;
  537. }
  538. if(bytestoread) {
  539. /* receive data from the network! */
  540. result = Curl_read(data, conn->sockfd, buf, bytestoread, &nread);
  541. /* read would've blocked */
  542. if(CURLE_AGAIN == result)
  543. break; /* get out of loop */
  544. if(result>0)
  545. return result;
  546. }
  547. else {
  548. /* read nothing but since we wanted nothing we consider this an OK
  549. situation to proceed from */
  550. DEBUGF(infof(data, "readwrite_data: we're done!\n"));
  551. nread = 0;
  552. }
  553. if(!k->bytecount) {
  554. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  555. if(k->exp100 > EXP100_SEND_DATA)
  556. /* set time stamp to compare with when waiting for the 100 */
  557. k->start100 = Curl_now();
  558. }
  559. *didwhat |= KEEP_RECV;
  560. /* indicates data of zero size, i.e. empty file */
  561. is_empty_data = ((nread == 0) && (k->bodywrites == 0)) ? TRUE : FALSE;
  562. if(0 < nread || is_empty_data) {
  563. buf[nread] = 0;
  564. }
  565. else {
  566. /* if we receive 0 or less here, either the http2 stream is closed or the
  567. server closed the connection and we bail out from this! */
  568. #ifdef USE_NGHTTP2
  569. if(is_http2 && !nread)
  570. DEBUGF(infof(data, "nread == 0, stream closed, bailing\n"));
  571. else
  572. #endif
  573. DEBUGF(infof(data, "nread <= 0, server closed connection, bailing\n"));
  574. k->keepon &= ~KEEP_RECV;
  575. break;
  576. }
  577. /* Default buffer to use when we write the buffer, it may be changed
  578. in the flow below before the actual storing is done. */
  579. k->str = buf;
  580. if(conn->handler->readwrite) {
  581. result = conn->handler->readwrite(data, conn, &nread, &readmore);
  582. if(result)
  583. return result;
  584. if(readmore)
  585. break;
  586. }
  587. #ifndef CURL_DISABLE_HTTP
  588. /* Since this is a two-state thing, we check if we are parsing
  589. headers at the moment or not. */
  590. if(k->header) {
  591. /* we are in parse-the-header-mode */
  592. bool stop_reading = FALSE;
  593. result = Curl_http_readwrite_headers(data, conn, &nread, &stop_reading);
  594. if(result)
  595. return result;
  596. if(conn->handler->readwrite &&
  597. (k->maxdownload <= 0 && nread > 0)) {
  598. result = conn->handler->readwrite(data, conn, &nread, &readmore);
  599. if(result)
  600. return result;
  601. if(readmore)
  602. break;
  603. }
  604. if(stop_reading) {
  605. /* We've stopped dealing with input, get out of the do-while loop */
  606. if(nread > 0) {
  607. infof(data,
  608. "Excess found:"
  609. " excess = %zd"
  610. " url = %s (zero-length body)\n",
  611. nread, data->state.up.path);
  612. }
  613. break;
  614. }
  615. }
  616. #endif /* CURL_DISABLE_HTTP */
  617. /* This is not an 'else if' since it may be a rest from the header
  618. parsing, where the beginning of the buffer is headers and the end
  619. is non-headers. */
  620. if(!k->header && (nread > 0 || is_empty_data)) {
  621. if(data->set.opt_no_body) {
  622. /* data arrives although we want none, bail out */
  623. streamclose(conn, "ignoring body");
  624. *done = TRUE;
  625. return CURLE_WEIRD_SERVER_REPLY;
  626. }
  627. #ifndef CURL_DISABLE_HTTP
  628. if(0 == k->bodywrites && !is_empty_data) {
  629. /* These checks are only made the first time we are about to
  630. write a piece of the body */
  631. if(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)) {
  632. /* HTTP-only checks */
  633. result = Curl_http_firstwrite(data, conn, done);
  634. if(result || *done)
  635. return result;
  636. }
  637. } /* this is the first time we write a body part */
  638. #endif /* CURL_DISABLE_HTTP */
  639. k->bodywrites++;
  640. /* pass data to the debug function before it gets "dechunked" */
  641. if(data->set.verbose) {
  642. if(k->badheader) {
  643. Curl_debug(data, CURLINFO_DATA_IN,
  644. Curl_dyn_ptr(&data->state.headerb),
  645. Curl_dyn_len(&data->state.headerb));
  646. if(k->badheader == HEADER_PARTHEADER)
  647. Curl_debug(data, CURLINFO_DATA_IN,
  648. k->str, (size_t)nread);
  649. }
  650. else
  651. Curl_debug(data, CURLINFO_DATA_IN,
  652. k->str, (size_t)nread);
  653. }
  654. #ifndef CURL_DISABLE_HTTP
  655. if(k->chunk) {
  656. /*
  657. * Here comes a chunked transfer flying and we need to decode this
  658. * properly. While the name says read, this function both reads
  659. * and writes away the data. The returned 'nread' holds the number
  660. * of actual data it wrote to the client.
  661. */
  662. CURLcode extra;
  663. CHUNKcode res =
  664. Curl_httpchunk_read(data, k->str, nread, &nread, &extra);
  665. if(CHUNKE_OK < res) {
  666. if(CHUNKE_PASSTHRU_ERROR == res) {
  667. failf(data, "Failed reading the chunked-encoded stream");
  668. return extra;
  669. }
  670. failf(data, "%s in chunked-encoding", Curl_chunked_strerror(res));
  671. return CURLE_RECV_ERROR;
  672. }
  673. if(CHUNKE_STOP == res) {
  674. /* we're done reading chunks! */
  675. k->keepon &= ~KEEP_RECV; /* read no more */
  676. /* N number of bytes at the end of the str buffer that weren't
  677. written to the client. */
  678. if(conn->chunk.datasize) {
  679. infof(data, "Leftovers after chunking: % "
  680. CURL_FORMAT_CURL_OFF_T "u bytes\n",
  681. conn->chunk.datasize);
  682. }
  683. }
  684. /* If it returned OK, we just keep going */
  685. }
  686. #endif /* CURL_DISABLE_HTTP */
  687. /* Account for body content stored in the header buffer */
  688. if((k->badheader == HEADER_PARTHEADER) && !k->ignorebody) {
  689. size_t headlen = Curl_dyn_len(&data->state.headerb);
  690. DEBUGF(infof(data, "Increasing bytecount by %zu\n", headlen));
  691. k->bytecount += headlen;
  692. }
  693. if((-1 != k->maxdownload) &&
  694. (k->bytecount + nread >= k->maxdownload)) {
  695. excess = (size_t)(k->bytecount + nread - k->maxdownload);
  696. if(excess > 0 && !k->ignorebody) {
  697. infof(data,
  698. "Excess found in a read:"
  699. " excess = %zu"
  700. ", size = %" CURL_FORMAT_CURL_OFF_T
  701. ", maxdownload = %" CURL_FORMAT_CURL_OFF_T
  702. ", bytecount = %" CURL_FORMAT_CURL_OFF_T "\n",
  703. excess, k->size, k->maxdownload, k->bytecount);
  704. connclose(conn, "excess found in a read");
  705. }
  706. nread = (ssize_t) (k->maxdownload - k->bytecount);
  707. if(nread < 0) /* this should be unusual */
  708. nread = 0;
  709. k->keepon &= ~KEEP_RECV; /* we're done reading */
  710. }
  711. k->bytecount += nread;
  712. Curl_pgrsSetDownloadCounter(data, k->bytecount);
  713. if(!k->chunk && (nread || k->badheader || is_empty_data)) {
  714. /* If this is chunky transfer, it was already written */
  715. if(k->badheader && !k->ignorebody) {
  716. /* we parsed a piece of data wrongly assuming it was a header
  717. and now we output it as body instead */
  718. size_t headlen = Curl_dyn_len(&data->state.headerb);
  719. /* Don't let excess data pollute body writes */
  720. if(k->maxdownload == -1 || (curl_off_t)headlen <= k->maxdownload)
  721. result = Curl_client_write(data, CLIENTWRITE_BODY,
  722. Curl_dyn_ptr(&data->state.headerb),
  723. headlen);
  724. else
  725. result = Curl_client_write(data, CLIENTWRITE_BODY,
  726. Curl_dyn_ptr(&data->state.headerb),
  727. (size_t)k->maxdownload);
  728. if(result)
  729. return result;
  730. }
  731. if(k->badheader < HEADER_ALLBAD) {
  732. /* This switch handles various content encodings. If there's an
  733. error here, be sure to check over the almost identical code
  734. in http_chunks.c.
  735. Make sure that ALL_CONTENT_ENCODINGS contains all the
  736. encodings handled here. */
  737. if(data->set.http_ce_skip || !k->writer_stack) {
  738. if(!k->ignorebody) {
  739. #ifndef CURL_DISABLE_POP3
  740. if(conn->handler->protocol & PROTO_FAMILY_POP3)
  741. result = Curl_pop3_write(data, k->str, nread);
  742. else
  743. #endif /* CURL_DISABLE_POP3 */
  744. result = Curl_client_write(data, CLIENTWRITE_BODY, k->str,
  745. nread);
  746. }
  747. }
  748. else if(!k->ignorebody)
  749. result = Curl_unencode_write(data, k->writer_stack, k->str, nread);
  750. }
  751. k->badheader = HEADER_NORMAL; /* taken care of now */
  752. if(result)
  753. return result;
  754. }
  755. } /* if(!header and data to read) */
  756. if(conn->handler->readwrite && excess) {
  757. /* Parse the excess data */
  758. k->str += nread;
  759. if(&k->str[excess] > &buf[data->set.buffer_size]) {
  760. /* the excess amount was too excessive(!), make sure
  761. it doesn't read out of buffer */
  762. excess = &buf[data->set.buffer_size] - k->str;
  763. }
  764. nread = (ssize_t)excess;
  765. result = conn->handler->readwrite(data, conn, &nread, &readmore);
  766. if(result)
  767. return result;
  768. if(readmore)
  769. k->keepon |= KEEP_RECV; /* we're not done reading */
  770. break;
  771. }
  772. if(is_empty_data) {
  773. /* if we received nothing, the server closed the connection and we
  774. are done */
  775. k->keepon &= ~KEEP_RECV;
  776. }
  777. if(k->keepon & KEEP_RECV_PAUSE) {
  778. /* this is a paused transfer */
  779. break;
  780. }
  781. } while(data_pending(data) && maxloops--);
  782. if(maxloops <= 0) {
  783. /* we mark it as read-again-please */
  784. conn->cselect_bits = CURL_CSELECT_IN;
  785. *comeback = TRUE;
  786. }
  787. if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) &&
  788. conn->bits.close) {
  789. /* When we've read the entire thing and the close bit is set, the server
  790. may now close the connection. If there's now any kind of sending going
  791. on from our side, we need to stop that immediately. */
  792. infof(data, "we are done reading and this is set to close, stop send\n");
  793. k->keepon &= ~KEEP_SEND; /* no writing anymore either */
  794. }
  795. return CURLE_OK;
  796. }
  797. CURLcode Curl_done_sending(struct Curl_easy *data,
  798. struct SingleRequest *k)
  799. {
  800. struct connectdata *conn = data->conn;
  801. k->keepon &= ~KEEP_SEND; /* we're done writing */
  802. /* These functions should be moved into the handler struct! */
  803. Curl_http2_done_sending(data, conn);
  804. Curl_quic_done_sending(data);
  805. if(conn->bits.rewindaftersend) {
  806. CURLcode result = Curl_readrewind(data);
  807. if(result)
  808. return result;
  809. }
  810. return CURLE_OK;
  811. }
  812. #if defined(WIN32) && !defined(USE_LWIPSOCK)
  813. #ifndef SIO_IDEAL_SEND_BACKLOG_QUERY
  814. #define SIO_IDEAL_SEND_BACKLOG_QUERY 0x4004747B
  815. #endif
  816. static void win_update_buffer_size(curl_socket_t sockfd)
  817. {
  818. int result;
  819. ULONG ideal;
  820. DWORD ideallen;
  821. result = WSAIoctl(sockfd, SIO_IDEAL_SEND_BACKLOG_QUERY, 0, 0,
  822. &ideal, sizeof(ideal), &ideallen, 0, 0);
  823. if(result == 0) {
  824. setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF,
  825. (const char *)&ideal, sizeof(ideal));
  826. }
  827. }
  828. #else
  829. #define win_update_buffer_size(x)
  830. #endif
  831. /*
  832. * Send data to upload to the server, when the socket is writable.
  833. */
  834. static CURLcode readwrite_upload(struct Curl_easy *data,
  835. struct connectdata *conn,
  836. int *didwhat)
  837. {
  838. ssize_t i, si;
  839. ssize_t bytes_written;
  840. CURLcode result;
  841. ssize_t nread; /* number of bytes read */
  842. bool sending_http_headers = FALSE;
  843. struct SingleRequest *k = &data->req;
  844. if((k->bytecount == 0) && (k->writebytecount == 0))
  845. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  846. *didwhat |= KEEP_SEND;
  847. do {
  848. curl_off_t nbody;
  849. /* only read more data if there's no upload data already
  850. present in the upload buffer */
  851. if(0 == k->upload_present) {
  852. result = Curl_get_upload_buffer(data);
  853. if(result)
  854. return result;
  855. /* init the "upload from here" pointer */
  856. k->upload_fromhere = data->state.ulbuf;
  857. if(!k->upload_done) {
  858. /* HTTP pollution, this should be written nicer to become more
  859. protocol agnostic. */
  860. size_t fillcount;
  861. struct HTTP *http = k->p.http;
  862. if((k->exp100 == EXP100_SENDING_REQUEST) &&
  863. (http->sending == HTTPSEND_BODY)) {
  864. /* If this call is to send body data, we must take some action:
  865. We have sent off the full HTTP 1.1 request, and we shall now
  866. go into the Expect: 100 state and await such a header */
  867. k->exp100 = EXP100_AWAITING_CONTINUE; /* wait for the header */
  868. k->keepon &= ~KEEP_SEND; /* disable writing */
  869. k->start100 = Curl_now(); /* timeout count starts now */
  870. *didwhat &= ~KEEP_SEND; /* we didn't write anything actually */
  871. /* set a timeout for the multi interface */
  872. Curl_expire(data, data->set.expect_100_timeout, EXPIRE_100_TIMEOUT);
  873. break;
  874. }
  875. if(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)) {
  876. if(http->sending == HTTPSEND_REQUEST)
  877. /* We're sending the HTTP request headers, not the data.
  878. Remember that so we don't change the line endings. */
  879. sending_http_headers = TRUE;
  880. else
  881. sending_http_headers = FALSE;
  882. }
  883. result = Curl_fillreadbuffer(data, data->set.upload_buffer_size,
  884. &fillcount);
  885. if(result)
  886. return result;
  887. nread = fillcount;
  888. }
  889. else
  890. nread = 0; /* we're done uploading/reading */
  891. if(!nread && (k->keepon & KEEP_SEND_PAUSE)) {
  892. /* this is a paused transfer */
  893. break;
  894. }
  895. if(nread <= 0) {
  896. result = Curl_done_sending(data, k);
  897. if(result)
  898. return result;
  899. break;
  900. }
  901. /* store number of bytes available for upload */
  902. k->upload_present = nread;
  903. /* convert LF to CRLF if so asked */
  904. if((!sending_http_headers) && (
  905. #ifdef CURL_DO_LINEEND_CONV
  906. /* always convert if we're FTPing in ASCII mode */
  907. (data->set.prefer_ascii) ||
  908. #endif
  909. (data->set.crlf))) {
  910. /* Do we need to allocate a scratch buffer? */
  911. if(!data->state.scratch) {
  912. data->state.scratch = malloc(2 * data->set.upload_buffer_size);
  913. if(!data->state.scratch) {
  914. failf(data, "Failed to alloc scratch buffer!");
  915. return CURLE_OUT_OF_MEMORY;
  916. }
  917. }
  918. /*
  919. * ASCII/EBCDIC Note: This is presumably a text (not binary)
  920. * transfer so the data should already be in ASCII.
  921. * That means the hex values for ASCII CR (0x0d) & LF (0x0a)
  922. * must be used instead of the escape sequences \r & \n.
  923. */
  924. for(i = 0, si = 0; i < nread; i++, si++) {
  925. if(k->upload_fromhere[i] == 0x0a) {
  926. data->state.scratch[si++] = 0x0d;
  927. data->state.scratch[si] = 0x0a;
  928. if(!data->set.crlf) {
  929. /* we're here only because FTP is in ASCII mode...
  930. bump infilesize for the LF we just added */
  931. if(data->state.infilesize != -1)
  932. data->state.infilesize++;
  933. }
  934. }
  935. else
  936. data->state.scratch[si] = k->upload_fromhere[i];
  937. }
  938. if(si != nread) {
  939. /* only perform the special operation if we really did replace
  940. anything */
  941. nread = si;
  942. /* upload from the new (replaced) buffer instead */
  943. k->upload_fromhere = data->state.scratch;
  944. /* set the new amount too */
  945. k->upload_present = nread;
  946. }
  947. }
  948. #ifndef CURL_DISABLE_SMTP
  949. if(conn->handler->protocol & PROTO_FAMILY_SMTP) {
  950. result = Curl_smtp_escape_eob(data, nread);
  951. if(result)
  952. return result;
  953. }
  954. #endif /* CURL_DISABLE_SMTP */
  955. } /* if 0 == k->upload_present */
  956. else {
  957. /* We have a partial buffer left from a previous "round". Use
  958. that instead of reading more data */
  959. }
  960. /* write to socket (send away data) */
  961. result = Curl_write(data,
  962. conn->writesockfd, /* socket to send to */
  963. k->upload_fromhere, /* buffer pointer */
  964. k->upload_present, /* buffer size */
  965. &bytes_written); /* actually sent */
  966. if(result)
  967. return result;
  968. win_update_buffer_size(conn->writesockfd);
  969. if(k->pendingheader) {
  970. /* parts of what was sent was header */
  971. curl_off_t n = CURLMIN(k->pendingheader, bytes_written);
  972. /* show the data before we change the pointer upload_fromhere */
  973. Curl_debug(data, CURLINFO_HEADER_OUT, k->upload_fromhere, (size_t)n);
  974. k->pendingheader -= n;
  975. nbody = bytes_written - n; /* size of the written body part */
  976. }
  977. else
  978. nbody = bytes_written;
  979. if(nbody) {
  980. /* show the data before we change the pointer upload_fromhere */
  981. Curl_debug(data, CURLINFO_DATA_OUT,
  982. &k->upload_fromhere[bytes_written - nbody],
  983. (size_t)nbody);
  984. k->writebytecount += nbody;
  985. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  986. }
  987. if((!k->upload_chunky || k->forbidchunk) &&
  988. (k->writebytecount == data->state.infilesize)) {
  989. /* we have sent all data we were supposed to */
  990. k->upload_done = TRUE;
  991. infof(data, "We are completely uploaded and fine\n");
  992. }
  993. if(k->upload_present != bytes_written) {
  994. /* we only wrote a part of the buffer (if anything), deal with it! */
  995. /* store the amount of bytes left in the buffer to write */
  996. k->upload_present -= bytes_written;
  997. /* advance the pointer where to find the buffer when the next send
  998. is to happen */
  999. k->upload_fromhere += bytes_written;
  1000. }
  1001. else {
  1002. /* we've uploaded that buffer now */
  1003. result = Curl_get_upload_buffer(data);
  1004. if(result)
  1005. return result;
  1006. k->upload_fromhere = data->state.ulbuf;
  1007. k->upload_present = 0; /* no more bytes left */
  1008. if(k->upload_done) {
  1009. result = Curl_done_sending(data, k);
  1010. if(result)
  1011. return result;
  1012. }
  1013. }
  1014. } while(0); /* just to break out from! */
  1015. return CURLE_OK;
  1016. }
  1017. /*
  1018. * Curl_readwrite() is the low-level function to be called when data is to
  1019. * be read and written to/from the connection.
  1020. *
  1021. * return '*comeback' TRUE if we didn't properly drain the socket so this
  1022. * function should get called again without select() or similar in between!
  1023. */
  1024. CURLcode Curl_readwrite(struct connectdata *conn,
  1025. struct Curl_easy *data,
  1026. bool *done,
  1027. bool *comeback)
  1028. {
  1029. struct SingleRequest *k = &data->req;
  1030. CURLcode result;
  1031. int didwhat = 0;
  1032. curl_socket_t fd_read;
  1033. curl_socket_t fd_write;
  1034. int select_res = conn->cselect_bits;
  1035. conn->cselect_bits = 0;
  1036. /* only use the proper socket if the *_HOLD bit is not set simultaneously as
  1037. then we are in rate limiting state in that transfer direction */
  1038. if((k->keepon & KEEP_RECVBITS) == KEEP_RECV)
  1039. fd_read = conn->sockfd;
  1040. else
  1041. fd_read = CURL_SOCKET_BAD;
  1042. if((k->keepon & KEEP_SENDBITS) == KEEP_SEND)
  1043. fd_write = conn->writesockfd;
  1044. else
  1045. fd_write = CURL_SOCKET_BAD;
  1046. if(data->state.drain) {
  1047. select_res |= CURL_CSELECT_IN;
  1048. DEBUGF(infof(data, "Curl_readwrite: forcibly told to drain data\n"));
  1049. }
  1050. if(!select_res) /* Call for select()/poll() only, if read/write/error
  1051. status is not known. */
  1052. select_res = Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write, 0);
  1053. if(select_res == CURL_CSELECT_ERR) {
  1054. failf(data, "select/poll returned error");
  1055. return CURLE_SEND_ERROR;
  1056. }
  1057. #ifdef USE_HYPER
  1058. if(conn->datastream)
  1059. return conn->datastream(data, conn, &didwhat, done, select_res);
  1060. #endif
  1061. /* We go ahead and do a read if we have a readable socket or if
  1062. the stream was rewound (in which case we have data in a
  1063. buffer) */
  1064. if((k->keepon & KEEP_RECV) && (select_res & CURL_CSELECT_IN)) {
  1065. result = readwrite_data(data, conn, k, &didwhat, done, comeback);
  1066. if(result || *done)
  1067. return result;
  1068. }
  1069. /* If we still have writing to do, we check if we have a writable socket. */
  1070. if((k->keepon & KEEP_SEND) && (select_res & CURL_CSELECT_OUT)) {
  1071. /* write */
  1072. result = readwrite_upload(data, conn, &didwhat);
  1073. if(result)
  1074. return result;
  1075. }
  1076. k->now = Curl_now();
  1077. if(!didwhat) {
  1078. /* no read no write, this is a timeout? */
  1079. if(k->exp100 == EXP100_AWAITING_CONTINUE) {
  1080. /* This should allow some time for the header to arrive, but only a
  1081. very short time as otherwise it'll be too much wasted time too
  1082. often. */
  1083. /* Quoting RFC2616, section "8.2.3 Use of the 100 (Continue) Status":
  1084. Therefore, when a client sends this header field to an origin server
  1085. (possibly via a proxy) from which it has never seen a 100 (Continue)
  1086. status, the client SHOULD NOT wait for an indefinite period before
  1087. sending the request body.
  1088. */
  1089. timediff_t ms = Curl_timediff(k->now, k->start100);
  1090. if(ms >= data->set.expect_100_timeout) {
  1091. /* we've waited long enough, continue anyway */
  1092. k->exp100 = EXP100_SEND_DATA;
  1093. k->keepon |= KEEP_SEND;
  1094. Curl_expire_done(data, EXPIRE_100_TIMEOUT);
  1095. infof(data, "Done waiting for 100-continue\n");
  1096. }
  1097. }
  1098. }
  1099. if(Curl_pgrsUpdate(data))
  1100. result = CURLE_ABORTED_BY_CALLBACK;
  1101. else
  1102. result = Curl_speedcheck(data, k->now);
  1103. if(result)
  1104. return result;
  1105. if(k->keepon) {
  1106. if(0 > Curl_timeleft(data, &k->now, FALSE)) {
  1107. if(k->size != -1) {
  1108. failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T
  1109. " milliseconds with %" CURL_FORMAT_CURL_OFF_T " out of %"
  1110. CURL_FORMAT_CURL_OFF_T " bytes received",
  1111. Curl_timediff(k->now, data->progress.t_startsingle),
  1112. k->bytecount, k->size);
  1113. }
  1114. else {
  1115. failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T
  1116. " milliseconds with %" CURL_FORMAT_CURL_OFF_T " bytes received",
  1117. Curl_timediff(k->now, data->progress.t_startsingle),
  1118. k->bytecount);
  1119. }
  1120. return CURLE_OPERATION_TIMEDOUT;
  1121. }
  1122. }
  1123. else {
  1124. /*
  1125. * The transfer has been performed. Just make some general checks before
  1126. * returning.
  1127. */
  1128. if(!(data->set.opt_no_body) && (k->size != -1) &&
  1129. (k->bytecount != k->size) &&
  1130. #ifdef CURL_DO_LINEEND_CONV
  1131. /* Most FTP servers don't adjust their file SIZE response for CRLFs,
  1132. so we'll check to see if the discrepancy can be explained
  1133. by the number of CRLFs we've changed to LFs.
  1134. */
  1135. (k->bytecount != (k->size + data->state.crlf_conversions)) &&
  1136. #endif /* CURL_DO_LINEEND_CONV */
  1137. !k->newurl) {
  1138. failf(data, "transfer closed with %" CURL_FORMAT_CURL_OFF_T
  1139. " bytes remaining to read", k->size - k->bytecount);
  1140. return CURLE_PARTIAL_FILE;
  1141. }
  1142. if(!(data->set.opt_no_body) && k->chunk &&
  1143. (conn->chunk.state != CHUNK_STOP)) {
  1144. /*
  1145. * In chunked mode, return an error if the connection is closed prior to
  1146. * the empty (terminating) chunk is read.
  1147. *
  1148. * The condition above used to check for
  1149. * conn->proto.http->chunk.datasize != 0 which is true after reading
  1150. * *any* chunk, not just the empty chunk.
  1151. *
  1152. */
  1153. failf(data, "transfer closed with outstanding read data remaining");
  1154. return CURLE_PARTIAL_FILE;
  1155. }
  1156. if(Curl_pgrsUpdate(data))
  1157. return CURLE_ABORTED_BY_CALLBACK;
  1158. }
  1159. /* Now update the "done" boolean we return */
  1160. *done = (0 == (k->keepon&(KEEP_RECV|KEEP_SEND|
  1161. KEEP_RECV_PAUSE|KEEP_SEND_PAUSE))) ? TRUE : FALSE;
  1162. return CURLE_OK;
  1163. }
  1164. /*
  1165. * Curl_single_getsock() gets called by the multi interface code when the app
  1166. * has requested to get the sockets for the current connection. This function
  1167. * will then be called once for every connection that the multi interface
  1168. * keeps track of. This function will only be called for connections that are
  1169. * in the proper state to have this information available.
  1170. */
  1171. int Curl_single_getsock(struct Curl_easy *data,
  1172. struct connectdata *conn,
  1173. curl_socket_t *sock)
  1174. {
  1175. int bitmap = GETSOCK_BLANK;
  1176. unsigned sockindex = 0;
  1177. if(conn->handler->perform_getsock)
  1178. return conn->handler->perform_getsock(data, conn, sock);
  1179. /* don't include HOLD and PAUSE connections */
  1180. if((data->req.keepon & KEEP_RECVBITS) == KEEP_RECV) {
  1181. DEBUGASSERT(conn->sockfd != CURL_SOCKET_BAD);
  1182. bitmap |= GETSOCK_READSOCK(sockindex);
  1183. sock[sockindex] = conn->sockfd;
  1184. }
  1185. /* don't include HOLD and PAUSE connections */
  1186. if((data->req.keepon & KEEP_SENDBITS) == KEEP_SEND) {
  1187. if((conn->sockfd != conn->writesockfd) ||
  1188. bitmap == GETSOCK_BLANK) {
  1189. /* only if they are not the same socket and we have a readable
  1190. one, we increase index */
  1191. if(bitmap != GETSOCK_BLANK)
  1192. sockindex++; /* increase index if we need two entries */
  1193. DEBUGASSERT(conn->writesockfd != CURL_SOCKET_BAD);
  1194. sock[sockindex] = conn->writesockfd;
  1195. }
  1196. bitmap |= GETSOCK_WRITESOCK(sockindex);
  1197. }
  1198. return bitmap;
  1199. }
  1200. /* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
  1201. which means this gets called once for each subsequent redirect etc */
  1202. void Curl_init_CONNECT(struct Curl_easy *data)
  1203. {
  1204. data->state.fread_func = data->set.fread_func_set;
  1205. data->state.in = data->set.in_set;
  1206. }
  1207. /*
  1208. * Curl_pretransfer() is called immediately before a transfer starts, and only
  1209. * once for one transfer no matter if it has redirects or do multi-pass
  1210. * authentication etc.
  1211. */
  1212. CURLcode Curl_pretransfer(struct Curl_easy *data)
  1213. {
  1214. CURLcode result;
  1215. if(!data->change.url && !data->set.uh) {
  1216. /* we can't do anything without URL */
  1217. failf(data, "No URL set!");
  1218. return CURLE_URL_MALFORMAT;
  1219. }
  1220. /* since the URL may have been redirected in a previous use of this handle */
  1221. if(data->change.url_alloc) {
  1222. /* the already set URL is allocated, free it first! */
  1223. Curl_safefree(data->change.url);
  1224. data->change.url_alloc = FALSE;
  1225. }
  1226. if(!data->change.url && data->set.uh) {
  1227. CURLUcode uc;
  1228. free(data->set.str[STRING_SET_URL]);
  1229. uc = curl_url_get(data->set.uh,
  1230. CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
  1231. if(uc) {
  1232. failf(data, "No URL set!");
  1233. return CURLE_URL_MALFORMAT;
  1234. }
  1235. }
  1236. data->state.httpreq = data->set.method;
  1237. data->change.url = data->set.str[STRING_SET_URL];
  1238. /* Init the SSL session ID cache here. We do it here since we want to do it
  1239. after the *_setopt() calls (that could specify the size of the cache) but
  1240. before any transfer takes place. */
  1241. result = Curl_ssl_initsessions(data, data->set.general_ssl.max_ssl_sessions);
  1242. if(result)
  1243. return result;
  1244. data->state.wildcardmatch = data->set.wildcard_enabled;
  1245. data->set.followlocation = 0; /* reset the location-follow counter */
  1246. data->state.this_is_a_follow = FALSE; /* reset this */
  1247. data->state.errorbuf = FALSE; /* no error has occurred */
  1248. data->state.httpversion = 0; /* don't assume any particular server version */
  1249. data->state.authproblem = FALSE;
  1250. data->state.authhost.want = data->set.httpauth;
  1251. data->state.authproxy.want = data->set.proxyauth;
  1252. Curl_safefree(data->info.wouldredirect);
  1253. if(data->state.httpreq == HTTPREQ_PUT)
  1254. data->state.infilesize = data->set.filesize;
  1255. else if((data->state.httpreq != HTTPREQ_GET) &&
  1256. (data->state.httpreq != HTTPREQ_HEAD)) {
  1257. data->state.infilesize = data->set.postfieldsize;
  1258. if(data->set.postfields && (data->state.infilesize == -1))
  1259. data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
  1260. }
  1261. else
  1262. data->state.infilesize = 0;
  1263. /* If there is a list of cookie files to read, do it now! */
  1264. if(data->change.cookielist)
  1265. Curl_cookie_loadfiles(data);
  1266. /* If there is a list of host pairs to deal with */
  1267. if(data->change.resolve)
  1268. result = Curl_loadhostpairs(data);
  1269. if(!result) {
  1270. /* Allow data->set.use_port to set which port to use. This needs to be
  1271. * disabled for example when we follow Location: headers to URLs using
  1272. * different ports! */
  1273. data->state.allow_port = TRUE;
  1274. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  1275. /*************************************************************
  1276. * Tell signal handler to ignore SIGPIPE
  1277. *************************************************************/
  1278. if(!data->set.no_signal)
  1279. data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
  1280. #endif
  1281. Curl_initinfo(data); /* reset session-specific information "variables" */
  1282. Curl_pgrsResetTransferSizes(data);
  1283. Curl_pgrsStartNow(data);
  1284. /* In case the handle is re-used and an authentication method was picked
  1285. in the session we need to make sure we only use the one(s) we now
  1286. consider to be fine */
  1287. data->state.authhost.picked &= data->state.authhost.want;
  1288. data->state.authproxy.picked &= data->state.authproxy.want;
  1289. #ifndef CURL_DISABLE_FTP
  1290. if(data->state.wildcardmatch) {
  1291. struct WildcardData *wc = &data->wildcard;
  1292. if(wc->state < CURLWC_INIT) {
  1293. result = Curl_wildcard_init(wc); /* init wildcard structures */
  1294. if(result)
  1295. return CURLE_OUT_OF_MEMORY;
  1296. }
  1297. }
  1298. #endif
  1299. Curl_http2_init_state(&data->state);
  1300. Curl_hsts_loadcb(data, data->hsts);
  1301. }
  1302. /*
  1303. * Set user-agent. Used for HTTP, but since we can attempt to tunnel
  1304. * basically anything through a http proxy we can't limit this based on
  1305. * protocol.
  1306. */
  1307. if(data->set.str[STRING_USERAGENT]) {
  1308. Curl_safefree(data->state.aptr.uagent);
  1309. data->state.aptr.uagent =
  1310. aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
  1311. if(!data->state.aptr.uagent)
  1312. return CURLE_OUT_OF_MEMORY;
  1313. }
  1314. data->req.headerbytecount = 0;
  1315. return result;
  1316. }
  1317. /*
  1318. * Curl_posttransfer() is called immediately after a transfer ends
  1319. */
  1320. CURLcode Curl_posttransfer(struct Curl_easy *data)
  1321. {
  1322. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  1323. /* restore the signal handler for SIGPIPE before we get back */
  1324. if(!data->set.no_signal)
  1325. signal(SIGPIPE, data->state.prev_signal);
  1326. #else
  1327. (void)data; /* unused parameter */
  1328. #endif
  1329. return CURLE_OK;
  1330. }
  1331. /*
  1332. * Curl_follow() handles the URL redirect magic. Pass in the 'newurl' string
  1333. * as given by the remote server and set up the new URL to request.
  1334. *
  1335. * This function DOES NOT FREE the given url.
  1336. */
  1337. CURLcode Curl_follow(struct Curl_easy *data,
  1338. char *newurl, /* the Location: string */
  1339. followtype type) /* see transfer.h */
  1340. {
  1341. #ifdef CURL_DISABLE_HTTP
  1342. (void)data;
  1343. (void)newurl;
  1344. (void)type;
  1345. /* Location: following will not happen when HTTP is disabled */
  1346. return CURLE_TOO_MANY_REDIRECTS;
  1347. #else
  1348. /* Location: redirect */
  1349. bool disallowport = FALSE;
  1350. bool reachedmax = FALSE;
  1351. CURLUcode uc;
  1352. DEBUGASSERT(type != FOLLOW_NONE);
  1353. if(type == FOLLOW_REDIR) {
  1354. if((data->set.maxredirs != -1) &&
  1355. (data->set.followlocation >= data->set.maxredirs)) {
  1356. reachedmax = TRUE;
  1357. type = FOLLOW_FAKE; /* switch to fake to store the would-be-redirected
  1358. to URL */
  1359. }
  1360. else {
  1361. /* mark the next request as a followed location: */
  1362. data->state.this_is_a_follow = TRUE;
  1363. data->set.followlocation++; /* count location-followers */
  1364. if(data->set.http_auto_referer) {
  1365. /* We are asked to automatically set the previous URL as the referer
  1366. when we get the next URL. We pick the ->url field, which may or may
  1367. not be 100% correct */
  1368. if(data->change.referer_alloc) {
  1369. Curl_safefree(data->change.referer);
  1370. data->change.referer_alloc = FALSE;
  1371. }
  1372. data->change.referer = strdup(data->change.url);
  1373. if(!data->change.referer)
  1374. return CURLE_OUT_OF_MEMORY;
  1375. data->change.referer_alloc = TRUE; /* yes, free this later */
  1376. }
  1377. }
  1378. }
  1379. if((type != FOLLOW_RETRY) &&
  1380. (data->req.httpcode != 401) && (data->req.httpcode != 407) &&
  1381. Curl_is_absolute_url(newurl, NULL, MAX_SCHEME_LEN))
  1382. /* If this is not redirect due to a 401 or 407 response and an absolute
  1383. URL: don't allow a custom port number */
  1384. disallowport = TRUE;
  1385. DEBUGASSERT(data->state.uh);
  1386. uc = curl_url_set(data->state.uh, CURLUPART_URL, newurl,
  1387. (type == FOLLOW_FAKE) ? CURLU_NON_SUPPORT_SCHEME :
  1388. ((type == FOLLOW_REDIR) ? CURLU_URLENCODE : 0) );
  1389. if(uc) {
  1390. if(type != FOLLOW_FAKE)
  1391. return Curl_uc_to_curlcode(uc);
  1392. /* the URL could not be parsed for some reason, but since this is FAKE
  1393. mode, just duplicate the field as-is */
  1394. newurl = strdup(newurl);
  1395. if(!newurl)
  1396. return CURLE_OUT_OF_MEMORY;
  1397. }
  1398. else {
  1399. uc = curl_url_get(data->state.uh, CURLUPART_URL, &newurl, 0);
  1400. if(uc)
  1401. return Curl_uc_to_curlcode(uc);
  1402. }
  1403. if(type == FOLLOW_FAKE) {
  1404. /* we're only figuring out the new url if we would've followed locations
  1405. but now we're done so we can get out! */
  1406. data->info.wouldredirect = newurl;
  1407. if(reachedmax) {
  1408. failf(data, "Maximum (%ld) redirects followed", data->set.maxredirs);
  1409. return CURLE_TOO_MANY_REDIRECTS;
  1410. }
  1411. return CURLE_OK;
  1412. }
  1413. if(disallowport)
  1414. data->state.allow_port = FALSE;
  1415. if(data->change.url_alloc)
  1416. Curl_safefree(data->change.url);
  1417. data->change.url = newurl;
  1418. data->change.url_alloc = TRUE;
  1419. infof(data, "Issue another request to this URL: '%s'\n", data->change.url);
  1420. /*
  1421. * We get here when the HTTP code is 300-399 (and 401). We need to perform
  1422. * differently based on exactly what return code there was.
  1423. *
  1424. * News from 7.10.6: we can also get here on a 401 or 407, in case we act on
  1425. * a HTTP (proxy-) authentication scheme other than Basic.
  1426. */
  1427. switch(data->info.httpcode) {
  1428. /* 401 - Act on a WWW-Authenticate, we keep on moving and do the
  1429. Authorization: XXXX header in the HTTP request code snippet */
  1430. /* 407 - Act on a Proxy-Authenticate, we keep on moving and do the
  1431. Proxy-Authorization: XXXX header in the HTTP request code snippet */
  1432. /* 300 - Multiple Choices */
  1433. /* 306 - Not used */
  1434. /* 307 - Temporary Redirect */
  1435. default: /* for all above (and the unknown ones) */
  1436. /* Some codes are explicitly mentioned since I've checked RFC2616 and they
  1437. * seem to be OK to POST to.
  1438. */
  1439. break;
  1440. case 301: /* Moved Permanently */
  1441. /* (quote from RFC7231, section 6.4.2)
  1442. *
  1443. * Note: For historical reasons, a user agent MAY change the request
  1444. * method from POST to GET for the subsequent request. If this
  1445. * behavior is undesired, the 307 (Temporary Redirect) status code
  1446. * can be used instead.
  1447. *
  1448. * ----
  1449. *
  1450. * Many webservers expect this, so these servers often answers to a POST
  1451. * request with an error page. To be sure that libcurl gets the page that
  1452. * most user agents would get, libcurl has to force GET.
  1453. *
  1454. * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
  1455. * can be overridden with CURLOPT_POSTREDIR.
  1456. */
  1457. if((data->state.httpreq == HTTPREQ_POST
  1458. || data->state.httpreq == HTTPREQ_POST_FORM
  1459. || data->state.httpreq == HTTPREQ_POST_MIME)
  1460. && !(data->set.keep_post & CURL_REDIR_POST_301)) {
  1461. infof(data, "Switch from POST to GET\n");
  1462. data->state.httpreq = HTTPREQ_GET;
  1463. }
  1464. break;
  1465. case 302: /* Found */
  1466. /* (quote from RFC7231, section 6.4.3)
  1467. *
  1468. * Note: For historical reasons, a user agent MAY change the request
  1469. * method from POST to GET for the subsequent request. If this
  1470. * behavior is undesired, the 307 (Temporary Redirect) status code
  1471. * can be used instead.
  1472. *
  1473. * ----
  1474. *
  1475. * Many webservers expect this, so these servers often answers to a POST
  1476. * request with an error page. To be sure that libcurl gets the page that
  1477. * most user agents would get, libcurl has to force GET.
  1478. *
  1479. * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
  1480. * can be overridden with CURLOPT_POSTREDIR.
  1481. */
  1482. if((data->state.httpreq == HTTPREQ_POST
  1483. || data->state.httpreq == HTTPREQ_POST_FORM
  1484. || data->state.httpreq == HTTPREQ_POST_MIME)
  1485. && !(data->set.keep_post & CURL_REDIR_POST_302)) {
  1486. infof(data, "Switch from POST to GET\n");
  1487. data->state.httpreq = HTTPREQ_GET;
  1488. }
  1489. break;
  1490. case 303: /* See Other */
  1491. /* 'See Other' location is not the resource but a substitute for the
  1492. * resource. In this case we switch the method to GET/HEAD, unless the
  1493. * method is POST and the user specified to keep it as POST.
  1494. * https://github.com/curl/curl/issues/5237#issuecomment-614641049
  1495. */
  1496. if(data->state.httpreq != HTTPREQ_GET &&
  1497. ((data->state.httpreq != HTTPREQ_POST &&
  1498. data->state.httpreq != HTTPREQ_POST_FORM &&
  1499. data->state.httpreq != HTTPREQ_POST_MIME) ||
  1500. !(data->set.keep_post & CURL_REDIR_POST_303))) {
  1501. data->state.httpreq = HTTPREQ_GET;
  1502. data->set.upload = false;
  1503. infof(data, "Switch to %s\n",
  1504. data->set.opt_no_body?"HEAD":"GET");
  1505. }
  1506. break;
  1507. case 304: /* Not Modified */
  1508. /* 304 means we did a conditional request and it was "Not modified".
  1509. * We shouldn't get any Location: header in this response!
  1510. */
  1511. break;
  1512. case 305: /* Use Proxy */
  1513. /* (quote from RFC2616, section 10.3.6):
  1514. * "The requested resource MUST be accessed through the proxy given
  1515. * by the Location field. The Location field gives the URI of the
  1516. * proxy. The recipient is expected to repeat this single request
  1517. * via the proxy. 305 responses MUST only be generated by origin
  1518. * servers."
  1519. */
  1520. break;
  1521. }
  1522. Curl_pgrsTime(data, TIMER_REDIRECT);
  1523. Curl_pgrsResetTransferSizes(data);
  1524. return CURLE_OK;
  1525. #endif /* CURL_DISABLE_HTTP */
  1526. }
  1527. /* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
  1528. NOTE: that the *url is malloc()ed. */
  1529. CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
  1530. {
  1531. struct connectdata *conn = data->conn;
  1532. bool retry = FALSE;
  1533. *url = NULL;
  1534. /* if we're talking upload, we can't do the checks below, unless the protocol
  1535. is HTTP as when uploading over HTTP we will still get a response */
  1536. if(data->set.upload &&
  1537. !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
  1538. return CURLE_OK;
  1539. if((data->req.bytecount + data->req.headerbytecount == 0) &&
  1540. conn->bits.reuse &&
  1541. (!data->set.opt_no_body
  1542. || (conn->handler->protocol & PROTO_FAMILY_HTTP)) &&
  1543. (data->set.rtspreq != RTSPREQ_RECEIVE))
  1544. /* We got no data, we attempted to re-use a connection. For HTTP this
  1545. can be a retry so we try again regardless if we expected a body.
  1546. For other protocols we only try again only if we expected a body.
  1547. This might happen if the connection was left alive when we were
  1548. done using it before, but that was closed when we wanted to read from
  1549. it again. Bad luck. Retry the same request on a fresh connect! */
  1550. retry = TRUE;
  1551. else if(data->state.refused_stream &&
  1552. (data->req.bytecount + data->req.headerbytecount == 0) ) {
  1553. /* This was sent on a refused stream, safe to rerun. A refused stream
  1554. error can typically only happen on HTTP/2 level if the stream is safe
  1555. to issue again, but the nghttp2 API can deliver the message to other
  1556. streams as well, which is why this adds the check the data counters
  1557. too. */
  1558. infof(data, "REFUSED_STREAM, retrying a fresh connect\n");
  1559. data->state.refused_stream = FALSE; /* clear again */
  1560. retry = TRUE;
  1561. }
  1562. if(retry) {
  1563. #define CONN_MAX_RETRIES 5
  1564. if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
  1565. failf(data, "Connection died, tried %d times before giving up",
  1566. CONN_MAX_RETRIES);
  1567. data->state.retrycount = 0;
  1568. return CURLE_SEND_ERROR;
  1569. }
  1570. infof(data, "Connection died, retrying a fresh connect\
  1571. (retry count: %d)\n", data->state.retrycount);
  1572. *url = strdup(data->change.url);
  1573. if(!*url)
  1574. return CURLE_OUT_OF_MEMORY;
  1575. connclose(conn, "retry"); /* close this connection */
  1576. conn->bits.retry = TRUE; /* mark this as a connection we're about
  1577. to retry. Marking it this way should
  1578. prevent i.e HTTP transfers to return
  1579. error just because nothing has been
  1580. transferred! */
  1581. if(conn->handler->protocol&PROTO_FAMILY_HTTP) {
  1582. if(data->req.writebytecount) {
  1583. CURLcode result = Curl_readrewind(data);
  1584. if(result) {
  1585. Curl_safefree(*url);
  1586. return result;
  1587. }
  1588. }
  1589. }
  1590. }
  1591. return CURLE_OK;
  1592. }
  1593. /*
  1594. * Curl_setup_transfer() is called to setup some basic properties for the
  1595. * upcoming transfer.
  1596. */
  1597. void
  1598. Curl_setup_transfer(
  1599. struct Curl_easy *data, /* transfer */
  1600. int sockindex, /* socket index to read from or -1 */
  1601. curl_off_t size, /* -1 if unknown at this point */
  1602. bool getheader, /* TRUE if header parsing is wanted */
  1603. int writesockindex /* socket index to write to, it may very well be
  1604. the same we read from. -1 disables */
  1605. )
  1606. {
  1607. struct SingleRequest *k = &data->req;
  1608. struct connectdata *conn = data->conn;
  1609. struct HTTP *http = data->req.p.http;
  1610. bool httpsending = ((conn->handler->protocol&PROTO_FAMILY_HTTP) &&
  1611. (http->sending == HTTPSEND_REQUEST));
  1612. DEBUGASSERT(conn != NULL);
  1613. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  1614. if(conn->bits.multiplex || conn->httpversion == 20 || httpsending) {
  1615. /* when multiplexing, the read/write sockets need to be the same! */
  1616. conn->sockfd = sockindex == -1 ?
  1617. ((writesockindex == -1 ? CURL_SOCKET_BAD : conn->sock[writesockindex])) :
  1618. conn->sock[sockindex];
  1619. conn->writesockfd = conn->sockfd;
  1620. if(httpsending)
  1621. /* special and very HTTP-specific */
  1622. writesockindex = FIRSTSOCKET;
  1623. }
  1624. else {
  1625. conn->sockfd = sockindex == -1 ?
  1626. CURL_SOCKET_BAD : conn->sock[sockindex];
  1627. conn->writesockfd = writesockindex == -1 ?
  1628. CURL_SOCKET_BAD:conn->sock[writesockindex];
  1629. }
  1630. k->getheader = getheader;
  1631. k->size = size;
  1632. /* The code sequence below is placed in this function just because all
  1633. necessary input is not always known in do_complete() as this function may
  1634. be called after that */
  1635. if(!k->getheader) {
  1636. k->header = FALSE;
  1637. if(size > 0)
  1638. Curl_pgrsSetDownloadSize(data, size);
  1639. }
  1640. /* we want header and/or body, if neither then don't do this! */
  1641. if(k->getheader || !data->set.opt_no_body) {
  1642. if(sockindex != -1)
  1643. k->keepon |= KEEP_RECV;
  1644. if(writesockindex != -1) {
  1645. /* HTTP 1.1 magic:
  1646. Even if we require a 100-return code before uploading data, we might
  1647. need to write data before that since the REQUEST may not have been
  1648. finished sent off just yet.
  1649. Thus, we must check if the request has been sent before we set the
  1650. state info where we wait for the 100-return code
  1651. */
  1652. if((data->state.expect100header) &&
  1653. (conn->handler->protocol&PROTO_FAMILY_HTTP) &&
  1654. (http->sending == HTTPSEND_BODY)) {
  1655. /* wait with write until we either got 100-continue or a timeout */
  1656. k->exp100 = EXP100_AWAITING_CONTINUE;
  1657. k->start100 = Curl_now();
  1658. /* Set a timeout for the multi interface. Add the inaccuracy margin so
  1659. that we don't fire slightly too early and get denied to run. */
  1660. Curl_expire(data, data->set.expect_100_timeout, EXPIRE_100_TIMEOUT);
  1661. }
  1662. else {
  1663. if(data->state.expect100header)
  1664. /* when we've sent off the rest of the headers, we must await a
  1665. 100-continue but first finish sending the request */
  1666. k->exp100 = EXP100_SENDING_REQUEST;
  1667. /* enable the write bit when we're not waiting for continue */
  1668. k->keepon |= KEEP_SEND;
  1669. }
  1670. } /* if(writesockindex != -1) */
  1671. } /* if(k->getheader || !data->set.opt_no_body) */
  1672. }