transfer.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "strtoofft.h"
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_NET_IF_H
  36. #include <net/if.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #include <signal.h>
  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 cannot 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 "cfilters.h"
  59. #include "cw-out.h"
  60. #include "transfer.h"
  61. #include "sendf.h"
  62. #include "speedcheck.h"
  63. #include "progress.h"
  64. #include "http.h"
  65. #include "url.h"
  66. #include "getinfo.h"
  67. #include "vtls/vtls.h"
  68. #include "vquic/vquic.h"
  69. #include "select.h"
  70. #include "multiif.h"
  71. #include "connect.h"
  72. #include "http2.h"
  73. #include "mime.h"
  74. #include "strcase.h"
  75. #include "hsts.h"
  76. #include "setopt.h"
  77. #include "headers.h"
  78. /* The last 3 #include files should be in this order */
  79. #include "curl_printf.h"
  80. #include "curl_memory.h"
  81. #include "memdebug.h"
  82. #if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_SMTP) || \
  83. !defined(CURL_DISABLE_IMAP)
  84. /*
  85. * checkheaders() checks the linked list of custom headers for a
  86. * particular header (prefix). Provide the prefix without colon!
  87. *
  88. * Returns a pointer to the first matching header or NULL if none matched.
  89. */
  90. char *Curl_checkheaders(const struct Curl_easy *data,
  91. const char *thisheader,
  92. const size_t thislen)
  93. {
  94. struct curl_slist *head;
  95. DEBUGASSERT(thislen);
  96. DEBUGASSERT(thisheader[thislen-1] != ':');
  97. for(head = data->set.headers; head; head = head->next) {
  98. if(strncasecompare(head->data, thisheader, thislen) &&
  99. Curl_headersep(head->data[thislen]) )
  100. return head->data;
  101. }
  102. return NULL;
  103. }
  104. #endif
  105. static int data_pending(struct Curl_easy *data)
  106. {
  107. struct connectdata *conn = data->conn;
  108. if(conn->handler->protocol&PROTO_FAMILY_FTP)
  109. return Curl_conn_data_pending(data, SECONDARYSOCKET);
  110. /* in the case of libssh2, we can never be really sure that we have emptied
  111. its internal buffers so we MUST always try until we get EAGAIN back */
  112. return conn->handler->protocol&(CURLPROTO_SCP|CURLPROTO_SFTP) ||
  113. Curl_conn_data_pending(data, FIRSTSOCKET);
  114. }
  115. /*
  116. * Check to see if CURLOPT_TIMECONDITION was met by comparing the time of the
  117. * remote document with the time provided by CURLOPT_TIMEVAL
  118. */
  119. bool Curl_meets_timecondition(struct Curl_easy *data, time_t timeofdoc)
  120. {
  121. if((timeofdoc == 0) || (data->set.timevalue == 0))
  122. return TRUE;
  123. switch(data->set.timecondition) {
  124. case CURL_TIMECOND_IFMODSINCE:
  125. default:
  126. if(timeofdoc <= data->set.timevalue) {
  127. infof(data,
  128. "The requested document is not new enough");
  129. data->info.timecond = TRUE;
  130. return FALSE;
  131. }
  132. break;
  133. case CURL_TIMECOND_IFUNMODSINCE:
  134. if(timeofdoc >= data->set.timevalue) {
  135. infof(data,
  136. "The requested document is not old enough");
  137. data->info.timecond = TRUE;
  138. return FALSE;
  139. }
  140. break;
  141. }
  142. return TRUE;
  143. }
  144. static CURLcode xfer_recv_shutdown(struct Curl_easy *data, bool *done)
  145. {
  146. int sockindex;
  147. if(!data || !data->conn)
  148. return CURLE_FAILED_INIT;
  149. if(data->conn->sockfd == CURL_SOCKET_BAD)
  150. return CURLE_FAILED_INIT;
  151. sockindex = (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]);
  152. return Curl_conn_shutdown(data, sockindex, done);
  153. }
  154. static bool xfer_recv_shutdown_started(struct Curl_easy *data)
  155. {
  156. int sockindex;
  157. if(!data || !data->conn)
  158. return CURLE_FAILED_INIT;
  159. if(data->conn->sockfd == CURL_SOCKET_BAD)
  160. return CURLE_FAILED_INIT;
  161. sockindex = (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]);
  162. return Curl_shutdown_started(data, sockindex);
  163. }
  164. CURLcode Curl_xfer_send_shutdown(struct Curl_easy *data, bool *done)
  165. {
  166. int sockindex;
  167. if(!data || !data->conn)
  168. return CURLE_FAILED_INIT;
  169. if(data->conn->writesockfd == CURL_SOCKET_BAD)
  170. return CURLE_FAILED_INIT;
  171. sockindex = (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]);
  172. return Curl_conn_shutdown(data, sockindex, done);
  173. }
  174. /**
  175. * Receive raw response data for the transfer.
  176. * @param data the transfer
  177. * @param buf buffer to keep response data received
  178. * @param blen length of `buf`
  179. * @param eos_reliable if EOS detection in underlying connection is reliable
  180. * @param err error code in case of -1 return
  181. * @return number of bytes read or -1 for error
  182. */
  183. static ssize_t Curl_xfer_recv_resp(struct Curl_easy *data,
  184. char *buf, size_t blen,
  185. bool eos_reliable,
  186. CURLcode *err)
  187. {
  188. ssize_t nread;
  189. DEBUGASSERT(blen > 0);
  190. /* If we are reading BODY data and the connection does NOT handle EOF
  191. * and we know the size of the BODY data, limit the read amount */
  192. if(!eos_reliable && !data->req.header && data->req.size != -1) {
  193. curl_off_t totalleft = data->req.size - data->req.bytecount;
  194. if(totalleft <= 0)
  195. blen = 0;
  196. else if(totalleft < (curl_off_t)blen)
  197. blen = (size_t)totalleft;
  198. }
  199. else if(xfer_recv_shutdown_started(data)) {
  200. /* we already received everything. Do not try more. */
  201. blen = 0;
  202. }
  203. if(!blen) {
  204. /* want nothing more */
  205. *err = CURLE_OK;
  206. nread = 0;
  207. }
  208. else {
  209. *err = Curl_xfer_recv(data, buf, blen, &nread);
  210. }
  211. if(*err)
  212. return -1;
  213. if(nread == 0) {
  214. if(data->req.shutdown) {
  215. bool done;
  216. *err = xfer_recv_shutdown(data, &done);
  217. if(*err)
  218. return -1;
  219. if(!done) {
  220. *err = CURLE_AGAIN;
  221. return -1;
  222. }
  223. }
  224. DEBUGF(infof(data, "sendrecv_dl: we are done"));
  225. }
  226. DEBUGASSERT(nread >= 0);
  227. return nread;
  228. }
  229. /*
  230. * Go ahead and do a read if we have a readable socket or if
  231. * the stream was rewound (in which case we have data in a
  232. * buffer)
  233. */
  234. static CURLcode sendrecv_dl(struct Curl_easy *data,
  235. struct SingleRequest *k,
  236. int *didwhat)
  237. {
  238. struct connectdata *conn = data->conn;
  239. CURLcode result = CURLE_OK;
  240. char *buf, *xfer_buf;
  241. size_t blen, xfer_blen;
  242. int maxloops = 10;
  243. curl_off_t total_received = 0;
  244. bool is_multiplex = FALSE;
  245. result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
  246. if(result)
  247. goto out;
  248. /* This is where we loop until we have read everything there is to
  249. read or we get a CURLE_AGAIN */
  250. do {
  251. bool is_eos = FALSE;
  252. size_t bytestoread;
  253. ssize_t nread;
  254. if(!is_multiplex) {
  255. /* Multiplexed connection have inherent handling of EOF and we do not
  256. * have to carefully restrict the amount we try to read.
  257. * Multiplexed changes only in one direction. */
  258. is_multiplex = Curl_conn_is_multiplex(conn, FIRSTSOCKET);
  259. }
  260. buf = xfer_buf;
  261. bytestoread = xfer_blen;
  262. if(bytestoread && data->set.max_recv_speed > 0) {
  263. /* In case of speed limit on receiving: if this loop already got
  264. * data, break out. If not, limit the amount of bytes to receive.
  265. * The overall, timed, speed limiting is done in multi.c */
  266. if(total_received)
  267. break;
  268. if(data->set.max_recv_speed < (curl_off_t)bytestoread)
  269. bytestoread = (size_t)data->set.max_recv_speed;
  270. }
  271. nread = Curl_xfer_recv_resp(data, buf, bytestoread,
  272. is_multiplex, &result);
  273. if(nread < 0) {
  274. if(CURLE_AGAIN != result)
  275. goto out; /* real error */
  276. result = CURLE_OK;
  277. if(data->req.download_done && data->req.no_body &&
  278. !data->req.resp_trailer) {
  279. DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
  280. "not waiting for EOS"));
  281. nread = 0;
  282. /* continue as if we read the EOS */
  283. }
  284. else
  285. break; /* get out of loop */
  286. }
  287. /* We only get a 0-length read on EndOfStream */
  288. blen = (size_t)nread;
  289. is_eos = (blen == 0);
  290. *didwhat |= KEEP_RECV;
  291. if(!blen) {
  292. /* if we receive 0 or less here, either the data transfer is done or the
  293. server closed the connection and we bail out from this! */
  294. if(is_multiplex)
  295. DEBUGF(infof(data, "nread == 0, stream closed, bailing"));
  296. else
  297. DEBUGF(infof(data, "nread <= 0, server closed connection, bailing"));
  298. result = Curl_req_stop_send_recv(data);
  299. if(result)
  300. goto out;
  301. if(k->eos_written) /* already did write this to client, leave */
  302. break;
  303. }
  304. total_received += blen;
  305. result = Curl_xfer_write_resp(data, buf, blen, is_eos);
  306. if(result || data->req.done)
  307. goto out;
  308. /* if we are done, we stop receiving. On multiplexed connections,
  309. * we should read the EOS. Which may arrive as meta data after
  310. * the bytes. Not taking it in might lead to RST of streams. */
  311. if((!is_multiplex && data->req.download_done) || is_eos) {
  312. data->req.keepon &= ~KEEP_RECV;
  313. }
  314. /* if we are PAUSEd or stopped receiving, leave the loop */
  315. if((k->keepon & KEEP_RECV_PAUSE) || !(k->keepon & KEEP_RECV))
  316. break;
  317. } while(maxloops--);
  318. if((maxloops <= 0) || data_pending(data)) {
  319. /* did not read until EAGAIN or there is still pending data, mark as
  320. read-again-please */
  321. data->state.select_bits = CURL_CSELECT_IN;
  322. if((k->keepon & KEEP_SENDBITS) == KEEP_SEND)
  323. data->state.select_bits |= CURL_CSELECT_OUT;
  324. }
  325. if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) &&
  326. (conn->bits.close || is_multiplex)) {
  327. /* When we have read the entire thing and the close bit is set, the server
  328. may now close the connection. If there is now any kind of sending going
  329. on from our side, we need to stop that immediately. */
  330. infof(data, "we are done reading and this is set to close, stop send");
  331. Curl_req_abort_sending(data);
  332. }
  333. out:
  334. Curl_multi_xfer_buf_release(data, xfer_buf);
  335. if(result)
  336. DEBUGF(infof(data, "sendrecv_dl() -> %d", result));
  337. return result;
  338. }
  339. /*
  340. * Send data to upload to the server, when the socket is writable.
  341. */
  342. static CURLcode sendrecv_ul(struct Curl_easy *data, int *didwhat)
  343. {
  344. /* We should not get here when the sending is already done. It
  345. * probably means that someone set `data-req.keepon |= KEEP_SEND`
  346. * when it should not. */
  347. DEBUGASSERT(!Curl_req_done_sending(data));
  348. if(!Curl_req_done_sending(data)) {
  349. *didwhat |= KEEP_SEND;
  350. return Curl_req_send_more(data);
  351. }
  352. return CURLE_OK;
  353. }
  354. static int select_bits_paused(struct Curl_easy *data, int select_bits)
  355. {
  356. /* See issue #11982: we really need to be careful not to progress
  357. * a transfer direction when that direction is paused. Not all parts
  358. * of our state machine are handling PAUSED transfers correctly. So, we
  359. * do not want to go there.
  360. * NOTE: we are only interested in PAUSE, not HOLD. */
  361. /* if there is data in a direction not paused, return false */
  362. if(((select_bits & CURL_CSELECT_IN) &&
  363. !(data->req.keepon & KEEP_RECV_PAUSE)) ||
  364. ((select_bits & CURL_CSELECT_OUT) &&
  365. !(data->req.keepon & KEEP_SEND_PAUSE)))
  366. return FALSE;
  367. return (data->req.keepon & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE));
  368. }
  369. /*
  370. * Curl_sendrecv() is the low-level function to be called when data is to
  371. * be read and written to/from the connection.
  372. */
  373. CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
  374. {
  375. struct SingleRequest *k = &data->req;
  376. CURLcode result = CURLE_OK;
  377. int didwhat = 0;
  378. DEBUGASSERT(nowp);
  379. if(data->state.select_bits) {
  380. if(select_bits_paused(data, data->state.select_bits)) {
  381. /* leave the bits unchanged, so they'll tell us what to do when
  382. * this transfer gets unpaused. */
  383. result = CURLE_OK;
  384. goto out;
  385. }
  386. data->state.select_bits = 0;
  387. }
  388. #ifdef USE_HYPER
  389. if(data->conn->datastream) {
  390. result = data->conn->datastream(data, data->conn, &didwhat,
  391. CURL_CSELECT_OUT|CURL_CSELECT_IN);
  392. if(result || data->req.done)
  393. goto out;
  394. }
  395. else {
  396. #endif
  397. /* We go ahead and do a read if we have a readable socket or if the stream
  398. was rewound (in which case we have data in a buffer) */
  399. if(k->keepon & KEEP_RECV) {
  400. result = sendrecv_dl(data, k, &didwhat);
  401. if(result || data->req.done)
  402. goto out;
  403. }
  404. /* If we still have writing to do, we check if we have a writable socket. */
  405. if(Curl_req_want_send(data) || (data->req.keepon & KEEP_SEND_TIMED)) {
  406. result = sendrecv_ul(data, &didwhat);
  407. if(result)
  408. goto out;
  409. }
  410. #ifdef USE_HYPER
  411. }
  412. #endif
  413. if(!didwhat) {
  414. /* Transfer wanted to send/recv, but nothing was possible. */
  415. result = Curl_conn_ev_data_idle(data);
  416. if(result)
  417. goto out;
  418. }
  419. if(Curl_pgrsUpdate(data))
  420. result = CURLE_ABORTED_BY_CALLBACK;
  421. else
  422. result = Curl_speedcheck(data, *nowp);
  423. if(result)
  424. goto out;
  425. if(k->keepon) {
  426. if(0 > Curl_timeleft(data, nowp, FALSE)) {
  427. if(k->size != -1) {
  428. failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
  429. " milliseconds with %" FMT_OFF_T " out of %"
  430. FMT_OFF_T " bytes received",
  431. Curl_timediff(*nowp, data->progress.t_startsingle),
  432. k->bytecount, k->size);
  433. }
  434. else {
  435. failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
  436. " milliseconds with %" FMT_OFF_T " bytes received",
  437. Curl_timediff(*nowp, data->progress.t_startsingle),
  438. k->bytecount);
  439. }
  440. result = CURLE_OPERATION_TIMEDOUT;
  441. goto out;
  442. }
  443. }
  444. else {
  445. /*
  446. * The transfer has been performed. Just make some general checks before
  447. * returning.
  448. */
  449. if(!(data->req.no_body) && (k->size != -1) &&
  450. (k->bytecount != k->size) && !k->newurl) {
  451. failf(data, "transfer closed with %" FMT_OFF_T
  452. " bytes remaining to read", k->size - k->bytecount);
  453. result = CURLE_PARTIAL_FILE;
  454. goto out;
  455. }
  456. if(Curl_pgrsUpdate(data)) {
  457. result = CURLE_ABORTED_BY_CALLBACK;
  458. goto out;
  459. }
  460. }
  461. /* If there is nothing more to send/recv, the request is done */
  462. if(0 == (k->keepon&(KEEP_RECVBITS|KEEP_SENDBITS)))
  463. data->req.done = TRUE;
  464. out:
  465. if(result)
  466. DEBUGF(infof(data, "Curl_sendrecv() -> %d", result));
  467. return result;
  468. }
  469. /* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
  470. which means this gets called once for each subsequent redirect etc */
  471. void Curl_init_CONNECT(struct Curl_easy *data)
  472. {
  473. data->state.fread_func = data->set.fread_func_set;
  474. data->state.in = data->set.in_set;
  475. data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
  476. }
  477. /*
  478. * Curl_pretransfer() is called immediately before a transfer starts, and only
  479. * once for one transfer no matter if it has redirects or do multi-pass
  480. * authentication etc.
  481. */
  482. CURLcode Curl_pretransfer(struct Curl_easy *data)
  483. {
  484. CURLcode result;
  485. if(!data->state.url && !data->set.uh) {
  486. /* we cannot do anything without URL */
  487. failf(data, "No URL set");
  488. return CURLE_URL_MALFORMAT;
  489. }
  490. /* since the URL may have been redirected in a previous use of this handle */
  491. if(data->state.url_alloc) {
  492. /* the already set URL is allocated, free it first! */
  493. Curl_safefree(data->state.url);
  494. data->state.url_alloc = FALSE;
  495. }
  496. if(!data->state.url && data->set.uh) {
  497. CURLUcode uc;
  498. free(data->set.str[STRING_SET_URL]);
  499. uc = curl_url_get(data->set.uh,
  500. CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
  501. if(uc) {
  502. failf(data, "No URL set");
  503. return CURLE_URL_MALFORMAT;
  504. }
  505. }
  506. if(data->set.postfields && data->set.set_resume_from) {
  507. /* we cannot */
  508. failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
  509. return CURLE_BAD_FUNCTION_ARGUMENT;
  510. }
  511. data->state.prefer_ascii = data->set.prefer_ascii;
  512. #ifdef CURL_LIST_ONLY_PROTOCOL
  513. data->state.list_only = data->set.list_only;
  514. #endif
  515. data->state.httpreq = data->set.method;
  516. data->state.url = data->set.str[STRING_SET_URL];
  517. /* Init the SSL session ID cache here. We do it here since we want to do it
  518. after the *_setopt() calls (that could specify the size of the cache) but
  519. before any transfer takes place. */
  520. result = Curl_ssl_initsessions(data, data->set.general_ssl.max_ssl_sessions);
  521. if(result)
  522. return result;
  523. data->state.requests = 0;
  524. data->state.followlocation = 0; /* reset the location-follow counter */
  525. data->state.this_is_a_follow = FALSE; /* reset this */
  526. data->state.errorbuf = FALSE; /* no error has occurred */
  527. data->state.httpwant = data->set.httpwant;
  528. data->state.httpversion = 0;
  529. data->state.authproblem = FALSE;
  530. data->state.authhost.want = data->set.httpauth;
  531. data->state.authproxy.want = data->set.proxyauth;
  532. Curl_safefree(data->info.wouldredirect);
  533. Curl_data_priority_clear_state(data);
  534. if(data->state.httpreq == HTTPREQ_PUT)
  535. data->state.infilesize = data->set.filesize;
  536. else if((data->state.httpreq != HTTPREQ_GET) &&
  537. (data->state.httpreq != HTTPREQ_HEAD)) {
  538. data->state.infilesize = data->set.postfieldsize;
  539. if(data->set.postfields && (data->state.infilesize == -1))
  540. data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
  541. }
  542. else
  543. data->state.infilesize = 0;
  544. /* If there is a list of cookie files to read, do it now! */
  545. Curl_cookie_loadfiles(data);
  546. /* If there is a list of host pairs to deal with */
  547. if(data->state.resolve)
  548. result = Curl_loadhostpairs(data);
  549. /* If there is a list of hsts files to read */
  550. Curl_hsts_loadfiles(data);
  551. if(!result) {
  552. /* Allow data->set.use_port to set which port to use. This needs to be
  553. * disabled for example when we follow Location: headers to URLs using
  554. * different ports! */
  555. data->state.allow_port = TRUE;
  556. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  557. /*************************************************************
  558. * Tell signal handler to ignore SIGPIPE
  559. *************************************************************/
  560. if(!data->set.no_signal)
  561. data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
  562. #endif
  563. Curl_initinfo(data); /* reset session-specific information "variables" */
  564. Curl_pgrsResetTransferSizes(data);
  565. Curl_pgrsStartNow(data);
  566. /* In case the handle is reused and an authentication method was picked
  567. in the session we need to make sure we only use the one(s) we now
  568. consider to be fine */
  569. data->state.authhost.picked &= data->state.authhost.want;
  570. data->state.authproxy.picked &= data->state.authproxy.want;
  571. #ifndef CURL_DISABLE_FTP
  572. data->state.wildcardmatch = data->set.wildcard_enabled;
  573. if(data->state.wildcardmatch) {
  574. struct WildcardData *wc;
  575. if(!data->wildcard) {
  576. data->wildcard = calloc(1, sizeof(struct WildcardData));
  577. if(!data->wildcard)
  578. return CURLE_OUT_OF_MEMORY;
  579. }
  580. wc = data->wildcard;
  581. if(wc->state < CURLWC_INIT) {
  582. if(wc->ftpwc)
  583. wc->dtor(wc->ftpwc);
  584. Curl_safefree(wc->pattern);
  585. Curl_safefree(wc->path);
  586. result = Curl_wildcard_init(wc); /* init wildcard structures */
  587. if(result)
  588. return CURLE_OUT_OF_MEMORY;
  589. }
  590. }
  591. #endif
  592. result = Curl_hsts_loadcb(data, data->hsts);
  593. }
  594. /*
  595. * Set user-agent. Used for HTTP, but since we can attempt to tunnel
  596. * basically anything through an HTTP proxy we cannot limit this based on
  597. * protocol.
  598. */
  599. if(data->set.str[STRING_USERAGENT]) {
  600. Curl_safefree(data->state.aptr.uagent);
  601. data->state.aptr.uagent =
  602. aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
  603. if(!data->state.aptr.uagent)
  604. return CURLE_OUT_OF_MEMORY;
  605. }
  606. if(data->set.str[STRING_USERNAME] ||
  607. data->set.str[STRING_PASSWORD])
  608. data->state.creds_from = CREDS_OPTION;
  609. if(!result)
  610. result = Curl_setstropt(&data->state.aptr.user,
  611. data->set.str[STRING_USERNAME]);
  612. if(!result)
  613. result = Curl_setstropt(&data->state.aptr.passwd,
  614. data->set.str[STRING_PASSWORD]);
  615. #ifndef CURL_DISABLE_PROXY
  616. if(!result)
  617. result = Curl_setstropt(&data->state.aptr.proxyuser,
  618. data->set.str[STRING_PROXYUSERNAME]);
  619. if(!result)
  620. result = Curl_setstropt(&data->state.aptr.proxypasswd,
  621. data->set.str[STRING_PROXYPASSWORD]);
  622. #endif
  623. data->req.headerbytecount = 0;
  624. Curl_headers_cleanup(data);
  625. return result;
  626. }
  627. /* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
  628. NOTE: that the *url is malloc()ed. */
  629. CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
  630. {
  631. struct connectdata *conn = data->conn;
  632. bool retry = FALSE;
  633. *url = NULL;
  634. /* if we are talking upload, we cannot do the checks below, unless the
  635. protocol is HTTP as when uploading over HTTP we will still get a
  636. response */
  637. if(data->state.upload &&
  638. !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
  639. return CURLE_OK;
  640. if((data->req.bytecount + data->req.headerbytecount == 0) &&
  641. conn->bits.reuse &&
  642. (!data->req.no_body || (conn->handler->protocol & PROTO_FAMILY_HTTP))
  643. #ifndef CURL_DISABLE_RTSP
  644. && (data->set.rtspreq != RTSPREQ_RECEIVE)
  645. #endif
  646. )
  647. /* We got no data, we attempted to reuse a connection. For HTTP this
  648. can be a retry so we try again regardless if we expected a body.
  649. For other protocols we only try again only if we expected a body.
  650. This might happen if the connection was left alive when we were
  651. done using it before, but that was closed when we wanted to read from
  652. it again. Bad luck. Retry the same request on a fresh connect! */
  653. retry = TRUE;
  654. else if(data->state.refused_stream &&
  655. (data->req.bytecount + data->req.headerbytecount == 0) ) {
  656. /* This was sent on a refused stream, safe to rerun. A refused stream
  657. error can typically only happen on HTTP/2 level if the stream is safe
  658. to issue again, but the nghttp2 API can deliver the message to other
  659. streams as well, which is why this adds the check the data counters
  660. too. */
  661. infof(data, "REFUSED_STREAM, retrying a fresh connect");
  662. data->state.refused_stream = FALSE; /* clear again */
  663. retry = TRUE;
  664. }
  665. if(retry) {
  666. #define CONN_MAX_RETRIES 5
  667. if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
  668. failf(data, "Connection died, tried %d times before giving up",
  669. CONN_MAX_RETRIES);
  670. data->state.retrycount = 0;
  671. return CURLE_SEND_ERROR;
  672. }
  673. infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
  674. data->state.retrycount);
  675. *url = strdup(data->state.url);
  676. if(!*url)
  677. return CURLE_OUT_OF_MEMORY;
  678. connclose(conn, "retry"); /* close this connection */
  679. conn->bits.retry = TRUE; /* mark this as a connection we are about
  680. to retry. Marking it this way should
  681. prevent i.e HTTP transfers to return
  682. error just because nothing has been
  683. transferred! */
  684. Curl_creader_set_rewind(data, TRUE);
  685. }
  686. return CURLE_OK;
  687. }
  688. /*
  689. * xfer_setup() is called to setup basic properties for the transfer.
  690. */
  691. static void xfer_setup(
  692. struct Curl_easy *data, /* transfer */
  693. int sockindex, /* socket index to read from or -1 */
  694. curl_off_t size, /* -1 if unknown at this point */
  695. bool getheader, /* TRUE if header parsing is wanted */
  696. int writesockindex, /* socket index to write to, it may be the same we
  697. read from. -1 disables */
  698. bool shutdown, /* shutdown connection at transfer end. Only
  699. * supported when sending OR receiving. */
  700. bool shutdown_err_ignore /* errors during shutdown do not fail the
  701. * transfer */
  702. )
  703. {
  704. struct SingleRequest *k = &data->req;
  705. struct connectdata *conn = data->conn;
  706. bool want_send = Curl_req_want_send(data);
  707. DEBUGASSERT(conn != NULL);
  708. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  709. DEBUGASSERT((writesockindex <= 1) && (writesockindex >= -1));
  710. DEBUGASSERT(!shutdown || (sockindex == -1) || (writesockindex == -1));
  711. if(conn->bits.multiplex || conn->httpversion >= 20 || want_send) {
  712. /* when multiplexing, the read/write sockets need to be the same! */
  713. conn->sockfd = sockindex == -1 ?
  714. ((writesockindex == -1 ? CURL_SOCKET_BAD : conn->sock[writesockindex])) :
  715. conn->sock[sockindex];
  716. conn->writesockfd = conn->sockfd;
  717. if(want_send)
  718. /* special and HTTP-specific */
  719. writesockindex = FIRSTSOCKET;
  720. }
  721. else {
  722. conn->sockfd = sockindex == -1 ?
  723. CURL_SOCKET_BAD : conn->sock[sockindex];
  724. conn->writesockfd = writesockindex == -1 ?
  725. CURL_SOCKET_BAD : conn->sock[writesockindex];
  726. }
  727. k->getheader = getheader;
  728. k->size = size;
  729. k->shutdown = shutdown;
  730. k->shutdown_err_ignore = shutdown_err_ignore;
  731. /* The code sequence below is placed in this function just because all
  732. necessary input is not always known in do_complete() as this function may
  733. be called after that */
  734. if(!k->getheader) {
  735. k->header = FALSE;
  736. if(size > 0)
  737. Curl_pgrsSetDownloadSize(data, size);
  738. }
  739. /* we want header and/or body, if neither then do not do this! */
  740. if(k->getheader || !data->req.no_body) {
  741. if(sockindex != -1)
  742. k->keepon |= KEEP_RECV;
  743. if(writesockindex != -1)
  744. k->keepon |= KEEP_SEND;
  745. } /* if(k->getheader || !data->req.no_body) */
  746. }
  747. void Curl_xfer_setup_nop(struct Curl_easy *data)
  748. {
  749. xfer_setup(data, -1, -1, FALSE, -1, FALSE, FALSE);
  750. }
  751. void Curl_xfer_setup1(struct Curl_easy *data,
  752. int send_recv,
  753. curl_off_t recv_size,
  754. bool getheader)
  755. {
  756. int recv_index = (send_recv & CURL_XFER_RECV) ? FIRSTSOCKET : -1;
  757. int send_index = (send_recv & CURL_XFER_SEND) ? FIRSTSOCKET : -1;
  758. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  759. xfer_setup(data, recv_index, recv_size, getheader, send_index, FALSE, FALSE);
  760. }
  761. void Curl_xfer_setup2(struct Curl_easy *data,
  762. int send_recv,
  763. curl_off_t recv_size,
  764. bool shutdown,
  765. bool shutdown_err_ignore)
  766. {
  767. int recv_index = (send_recv & CURL_XFER_RECV) ? SECONDARYSOCKET : -1;
  768. int send_index = (send_recv & CURL_XFER_SEND) ? SECONDARYSOCKET : -1;
  769. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  770. xfer_setup(data, recv_index, recv_size, FALSE, send_index,
  771. shutdown, shutdown_err_ignore);
  772. }
  773. CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
  774. const char *buf, size_t blen,
  775. bool is_eos)
  776. {
  777. CURLcode result = CURLE_OK;
  778. if(data->conn->handler->write_resp) {
  779. /* protocol handlers offering this function take full responsibility
  780. * for writing all received download data to the client. */
  781. result = data->conn->handler->write_resp(data, buf, blen, is_eos);
  782. }
  783. else {
  784. /* No special handling by protocol handler, write all received data
  785. * as BODY to the client. */
  786. if(blen || is_eos) {
  787. int cwtype = CLIENTWRITE_BODY;
  788. if(is_eos)
  789. cwtype |= CLIENTWRITE_EOS;
  790. result = Curl_client_write(data, cwtype, buf, blen);
  791. }
  792. }
  793. if(!result && is_eos) {
  794. /* If we wrote the EOS, we are definitely done */
  795. data->req.eos_written = TRUE;
  796. data->req.download_done = TRUE;
  797. }
  798. CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
  799. blen, is_eos, result);
  800. return result;
  801. }
  802. CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
  803. const char *hd0, size_t hdlen, bool is_eos)
  804. {
  805. if(data->conn->handler->write_resp_hd) {
  806. /* protocol handlers offering this function take full responsibility
  807. * for writing all received download data to the client. */
  808. return data->conn->handler->write_resp_hd(data, hd0, hdlen, is_eos);
  809. }
  810. /* No special handling by protocol handler, write as response bytes */
  811. return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
  812. }
  813. CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
  814. {
  815. (void)premature;
  816. return Curl_cw_out_done(data);
  817. }
  818. bool Curl_xfer_needs_flush(struct Curl_easy *data)
  819. {
  820. int sockindex;
  821. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  822. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  823. return Curl_conn_needs_flush(data, sockindex);
  824. }
  825. CURLcode Curl_xfer_flush(struct Curl_easy *data)
  826. {
  827. int sockindex;
  828. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  829. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  830. return Curl_conn_flush(data, sockindex);
  831. }
  832. CURLcode Curl_xfer_send(struct Curl_easy *data,
  833. const void *buf, size_t blen, bool eos,
  834. size_t *pnwritten)
  835. {
  836. CURLcode result;
  837. int sockindex;
  838. DEBUGASSERT(data);
  839. DEBUGASSERT(data->conn);
  840. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  841. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  842. result = Curl_conn_send(data, sockindex, buf, blen, eos, pnwritten);
  843. if(result == CURLE_AGAIN) {
  844. result = CURLE_OK;
  845. *pnwritten = 0;
  846. }
  847. else if(!result && *pnwritten)
  848. data->info.request_size += *pnwritten;
  849. DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
  850. blen, eos, result, *pnwritten));
  851. return result;
  852. }
  853. CURLcode Curl_xfer_recv(struct Curl_easy *data,
  854. char *buf, size_t blen,
  855. ssize_t *pnrcvd)
  856. {
  857. int sockindex;
  858. DEBUGASSERT(data);
  859. DEBUGASSERT(data->conn);
  860. DEBUGASSERT(data->set.buffer_size > 0);
  861. sockindex = ((data->conn->sockfd != CURL_SOCKET_BAD) &&
  862. (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]));
  863. if((size_t)data->set.buffer_size < blen)
  864. blen = (size_t)data->set.buffer_size;
  865. return Curl_conn_recv(data, sockindex, buf, blen, pnrcvd);
  866. }
  867. CURLcode Curl_xfer_send_close(struct Curl_easy *data)
  868. {
  869. Curl_conn_ev_data_done_send(data);
  870. return CURLE_OK;
  871. }
  872. bool Curl_xfer_is_blocked(struct Curl_easy *data)
  873. {
  874. bool want_send = ((data)->req.keepon & KEEP_SEND);
  875. bool want_recv = ((data)->req.keepon & KEEP_RECV);
  876. if(!want_send)
  877. return (want_recv && Curl_cwriter_is_paused(data));
  878. else if(!want_recv)
  879. return (want_send && Curl_creader_is_paused(data));
  880. else
  881. return Curl_creader_is_paused(data) && Curl_cwriter_is_paused(data);
  882. }