2
0

transfer.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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 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 = xfer_recv_resp(data, buf, bytestoread, is_multiplex, &result);
  272. if(nread < 0) {
  273. if(CURLE_AGAIN != result)
  274. goto out; /* real error */
  275. result = CURLE_OK;
  276. if(data->req.download_done && data->req.no_body &&
  277. !data->req.resp_trailer) {
  278. DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
  279. "not waiting for EOS"));
  280. nread = 0;
  281. /* continue as if we read the EOS */
  282. }
  283. else
  284. break; /* get out of loop */
  285. }
  286. /* We only get a 0-length read on EndOfStream */
  287. blen = (size_t)nread;
  288. is_eos = (blen == 0);
  289. *didwhat |= KEEP_RECV;
  290. if(!blen) {
  291. /* if we receive 0 or less here, either the data transfer is done or the
  292. server closed the connection and we bail out from this! */
  293. if(is_multiplex)
  294. DEBUGF(infof(data, "nread == 0, stream closed, bailing"));
  295. else
  296. DEBUGF(infof(data, "nread <= 0, server closed connection, bailing"));
  297. result = Curl_req_stop_send_recv(data);
  298. if(result)
  299. goto out;
  300. if(k->eos_written) /* already did write this to client, leave */
  301. break;
  302. }
  303. total_received += blen;
  304. result = Curl_xfer_write_resp(data, buf, blen, is_eos);
  305. if(result || data->req.done)
  306. goto out;
  307. /* if we are done, we stop receiving. On multiplexed connections,
  308. * we should read the EOS. Which may arrive as meta data after
  309. * the bytes. Not taking it in might lead to RST of streams. */
  310. if((!is_multiplex && data->req.download_done) || is_eos) {
  311. data->req.keepon &= ~KEEP_RECV;
  312. }
  313. /* if we are PAUSEd or stopped receiving, leave the loop */
  314. if((k->keepon & KEEP_RECV_PAUSE) || !(k->keepon & KEEP_RECV))
  315. break;
  316. } while(maxloops--);
  317. if((maxloops <= 0) || data_pending(data)) {
  318. /* did not read until EAGAIN or there is still pending data, mark as
  319. read-again-please */
  320. data->state.select_bits = CURL_CSELECT_IN;
  321. if((k->keepon & KEEP_SENDBITS) == KEEP_SEND)
  322. data->state.select_bits |= CURL_CSELECT_OUT;
  323. }
  324. if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) &&
  325. (conn->bits.close || is_multiplex)) {
  326. /* When we have read the entire thing and the close bit is set, the server
  327. may now close the connection. If there is now any kind of sending going
  328. on from our side, we need to stop that immediately. */
  329. infof(data, "we are done reading and this is set to close, stop send");
  330. Curl_req_abort_sending(data);
  331. }
  332. out:
  333. Curl_multi_xfer_buf_release(data, xfer_buf);
  334. if(result)
  335. DEBUGF(infof(data, "sendrecv_dl() -> %d", result));
  336. return result;
  337. }
  338. /*
  339. * Send data to upload to the server, when the socket is writable.
  340. */
  341. static CURLcode sendrecv_ul(struct Curl_easy *data, int *didwhat)
  342. {
  343. /* We should not get here when the sending is already done. It
  344. * probably means that someone set `data-req.keepon |= KEEP_SEND`
  345. * when it should not. */
  346. DEBUGASSERT(!Curl_req_done_sending(data));
  347. if(!Curl_req_done_sending(data)) {
  348. *didwhat |= KEEP_SEND;
  349. return Curl_req_send_more(data);
  350. }
  351. return CURLE_OK;
  352. }
  353. static int select_bits_paused(struct Curl_easy *data, int select_bits)
  354. {
  355. /* See issue #11982: we really need to be careful not to progress
  356. * a transfer direction when that direction is paused. Not all parts
  357. * of our state machine are handling PAUSED transfers correctly. So, we
  358. * do not want to go there.
  359. * NOTE: we are only interested in PAUSE, not HOLD. */
  360. /* if there is data in a direction not paused, return false */
  361. if(((select_bits & CURL_CSELECT_IN) &&
  362. !(data->req.keepon & KEEP_RECV_PAUSE)) ||
  363. ((select_bits & CURL_CSELECT_OUT) &&
  364. !(data->req.keepon & KEEP_SEND_PAUSE)))
  365. return FALSE;
  366. return (data->req.keepon & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE));
  367. }
  368. /*
  369. * Curl_sendrecv() is the low-level function to be called when data is to
  370. * be read and written to/from the connection.
  371. */
  372. CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
  373. {
  374. struct SingleRequest *k = &data->req;
  375. CURLcode result = CURLE_OK;
  376. int didwhat = 0;
  377. DEBUGASSERT(nowp);
  378. if(data->state.select_bits) {
  379. if(select_bits_paused(data, data->state.select_bits)) {
  380. /* leave the bits unchanged, so they'll tell us what to do when
  381. * this transfer gets unpaused. */
  382. result = CURLE_OK;
  383. goto out;
  384. }
  385. data->state.select_bits = 0;
  386. }
  387. #ifdef USE_HYPER
  388. if(data->conn->datastream) {
  389. result = data->conn->datastream(data, data->conn, &didwhat,
  390. CURL_CSELECT_OUT|CURL_CSELECT_IN);
  391. if(result || data->req.done)
  392. goto out;
  393. }
  394. else {
  395. #endif
  396. /* We go ahead and do a read if we have a readable socket or if the stream
  397. was rewound (in which case we have data in a buffer) */
  398. if(k->keepon & KEEP_RECV) {
  399. result = sendrecv_dl(data, k, &didwhat);
  400. if(result || data->req.done)
  401. goto out;
  402. }
  403. /* If we still have writing to do, we check if we have a writable socket. */
  404. if(Curl_req_want_send(data) || (data->req.keepon & KEEP_SEND_TIMED)) {
  405. result = sendrecv_ul(data, &didwhat);
  406. if(result)
  407. goto out;
  408. }
  409. #ifdef USE_HYPER
  410. }
  411. #endif
  412. if(!didwhat) {
  413. /* Transfer wanted to send/recv, but nothing was possible. */
  414. result = Curl_conn_ev_data_idle(data);
  415. if(result)
  416. goto out;
  417. }
  418. if(Curl_pgrsUpdate(data))
  419. result = CURLE_ABORTED_BY_CALLBACK;
  420. else
  421. result = Curl_speedcheck(data, *nowp);
  422. if(result)
  423. goto out;
  424. if(k->keepon) {
  425. if(0 > Curl_timeleft(data, nowp, FALSE)) {
  426. if(k->size != -1) {
  427. failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
  428. " milliseconds with %" FMT_OFF_T " out of %"
  429. FMT_OFF_T " bytes received",
  430. Curl_timediff(*nowp, data->progress.t_startsingle),
  431. k->bytecount, k->size);
  432. }
  433. else {
  434. failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
  435. " milliseconds with %" FMT_OFF_T " bytes received",
  436. Curl_timediff(*nowp, data->progress.t_startsingle),
  437. k->bytecount);
  438. }
  439. result = CURLE_OPERATION_TIMEDOUT;
  440. goto out;
  441. }
  442. }
  443. else {
  444. /*
  445. * The transfer has been performed. Just make some general checks before
  446. * returning.
  447. */
  448. if(!(data->req.no_body) && (k->size != -1) &&
  449. (k->bytecount != k->size) && !k->newurl) {
  450. failf(data, "transfer closed with %" FMT_OFF_T
  451. " bytes remaining to read", k->size - k->bytecount);
  452. result = CURLE_PARTIAL_FILE;
  453. goto out;
  454. }
  455. if(Curl_pgrsUpdate(data)) {
  456. result = CURLE_ABORTED_BY_CALLBACK;
  457. goto out;
  458. }
  459. }
  460. /* If there is nothing more to send/recv, the request is done */
  461. if(0 == (k->keepon&(KEEP_RECVBITS|KEEP_SENDBITS)))
  462. data->req.done = TRUE;
  463. out:
  464. if(result)
  465. DEBUGF(infof(data, "Curl_sendrecv() -> %d", result));
  466. return result;
  467. }
  468. /* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
  469. which means this gets called once for each subsequent redirect etc */
  470. void Curl_init_CONNECT(struct Curl_easy *data)
  471. {
  472. data->state.fread_func = data->set.fread_func_set;
  473. data->state.in = data->set.in_set;
  474. data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
  475. }
  476. /*
  477. * Curl_pretransfer() is called immediately before a transfer starts, and only
  478. * once for one transfer no matter if it has redirects or do multi-pass
  479. * authentication etc.
  480. */
  481. CURLcode Curl_pretransfer(struct Curl_easy *data)
  482. {
  483. CURLcode result;
  484. if(!data->state.url && !data->set.uh) {
  485. /* we cannot do anything without URL */
  486. failf(data, "No URL set");
  487. return CURLE_URL_MALFORMAT;
  488. }
  489. /* since the URL may have been redirected in a previous use of this handle */
  490. if(data->state.url_alloc) {
  491. /* the already set URL is allocated, free it first! */
  492. Curl_safefree(data->state.url);
  493. data->state.url_alloc = FALSE;
  494. }
  495. if(!data->state.url && data->set.uh) {
  496. CURLUcode uc;
  497. free(data->set.str[STRING_SET_URL]);
  498. uc = curl_url_get(data->set.uh,
  499. CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
  500. if(uc) {
  501. failf(data, "No URL set");
  502. return CURLE_URL_MALFORMAT;
  503. }
  504. }
  505. if(data->set.postfields && data->set.set_resume_from) {
  506. /* we cannot */
  507. failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
  508. return CURLE_BAD_FUNCTION_ARGUMENT;
  509. }
  510. data->state.prefer_ascii = data->set.prefer_ascii;
  511. #ifdef CURL_LIST_ONLY_PROTOCOL
  512. data->state.list_only = data->set.list_only;
  513. #endif
  514. data->state.httpreq = data->set.method;
  515. data->state.url = data->set.str[STRING_SET_URL];
  516. /* Init the SSL session ID cache here. We do it here since we want to do it
  517. after the *_setopt() calls (that could specify the size of the cache) but
  518. before any transfer takes place. */
  519. result = Curl_ssl_initsessions(data, data->set.general_ssl.max_ssl_sessions);
  520. if(result)
  521. return result;
  522. data->state.requests = 0;
  523. data->state.followlocation = 0; /* reset the location-follow counter */
  524. data->state.this_is_a_follow = FALSE; /* reset this */
  525. data->state.errorbuf = FALSE; /* no error has occurred */
  526. data->state.httpwant = data->set.httpwant;
  527. data->state.httpversion = 0;
  528. data->state.authproblem = FALSE;
  529. data->state.authhost.want = data->set.httpauth;
  530. data->state.authproxy.want = data->set.proxyauth;
  531. Curl_safefree(data->info.wouldredirect);
  532. Curl_data_priority_clear_state(data);
  533. if(data->state.httpreq == HTTPREQ_PUT)
  534. data->state.infilesize = data->set.filesize;
  535. else if((data->state.httpreq != HTTPREQ_GET) &&
  536. (data->state.httpreq != HTTPREQ_HEAD)) {
  537. data->state.infilesize = data->set.postfieldsize;
  538. if(data->set.postfields && (data->state.infilesize == -1))
  539. data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
  540. }
  541. else
  542. data->state.infilesize = 0;
  543. /* If there is a list of cookie files to read, do it now! */
  544. Curl_cookie_loadfiles(data);
  545. /* If there is a list of host pairs to deal with */
  546. if(data->state.resolve)
  547. result = Curl_loadhostpairs(data);
  548. /* If there is a list of hsts files to read */
  549. Curl_hsts_loadfiles(data);
  550. if(!result) {
  551. /* Allow data->set.use_port to set which port to use. This needs to be
  552. * disabled for example when we follow Location: headers to URLs using
  553. * different ports! */
  554. data->state.allow_port = TRUE;
  555. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  556. /*************************************************************
  557. * Tell signal handler to ignore SIGPIPE
  558. *************************************************************/
  559. if(!data->set.no_signal)
  560. data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
  561. #endif
  562. Curl_initinfo(data); /* reset session-specific information "variables" */
  563. Curl_pgrsResetTransferSizes(data);
  564. Curl_pgrsStartNow(data);
  565. /* In case the handle is reused and an authentication method was picked
  566. in the session we need to make sure we only use the one(s) we now
  567. consider to be fine */
  568. data->state.authhost.picked &= data->state.authhost.want;
  569. data->state.authproxy.picked &= data->state.authproxy.want;
  570. #ifndef CURL_DISABLE_FTP
  571. data->state.wildcardmatch = data->set.wildcard_enabled;
  572. if(data->state.wildcardmatch) {
  573. struct WildcardData *wc;
  574. if(!data->wildcard) {
  575. data->wildcard = calloc(1, sizeof(struct WildcardData));
  576. if(!data->wildcard)
  577. return CURLE_OUT_OF_MEMORY;
  578. }
  579. wc = data->wildcard;
  580. if(wc->state < CURLWC_INIT) {
  581. if(wc->ftpwc)
  582. wc->dtor(wc->ftpwc);
  583. Curl_safefree(wc->pattern);
  584. Curl_safefree(wc->path);
  585. result = Curl_wildcard_init(wc); /* init wildcard structures */
  586. if(result)
  587. return CURLE_OUT_OF_MEMORY;
  588. }
  589. }
  590. #endif
  591. result = Curl_hsts_loadcb(data, data->hsts);
  592. }
  593. /*
  594. * Set user-agent. Used for HTTP, but since we can attempt to tunnel
  595. * basically anything through an HTTP proxy we cannot limit this based on
  596. * protocol.
  597. */
  598. if(data->set.str[STRING_USERAGENT]) {
  599. Curl_safefree(data->state.aptr.uagent);
  600. data->state.aptr.uagent =
  601. aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
  602. if(!data->state.aptr.uagent)
  603. return CURLE_OUT_OF_MEMORY;
  604. }
  605. if(data->set.str[STRING_USERNAME] ||
  606. data->set.str[STRING_PASSWORD])
  607. data->state.creds_from = CREDS_OPTION;
  608. if(!result)
  609. result = Curl_setstropt(&data->state.aptr.user,
  610. data->set.str[STRING_USERNAME]);
  611. if(!result)
  612. result = Curl_setstropt(&data->state.aptr.passwd,
  613. data->set.str[STRING_PASSWORD]);
  614. #ifndef CURL_DISABLE_PROXY
  615. if(!result)
  616. result = Curl_setstropt(&data->state.aptr.proxyuser,
  617. data->set.str[STRING_PROXYUSERNAME]);
  618. if(!result)
  619. result = Curl_setstropt(&data->state.aptr.proxypasswd,
  620. data->set.str[STRING_PROXYPASSWORD]);
  621. #endif
  622. data->req.headerbytecount = 0;
  623. Curl_headers_cleanup(data);
  624. return result;
  625. }
  626. /* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
  627. NOTE: that the *url is malloc()ed. */
  628. CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
  629. {
  630. struct connectdata *conn = data->conn;
  631. bool retry = FALSE;
  632. *url = NULL;
  633. /* if we are talking upload, we cannot do the checks below, unless the
  634. protocol is HTTP as when uploading over HTTP we will still get a
  635. response */
  636. if(data->state.upload &&
  637. !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
  638. return CURLE_OK;
  639. if((data->req.bytecount + data->req.headerbytecount == 0) &&
  640. conn->bits.reuse &&
  641. (!data->req.no_body || (conn->handler->protocol & PROTO_FAMILY_HTTP))
  642. #ifndef CURL_DISABLE_RTSP
  643. && (data->set.rtspreq != RTSPREQ_RECEIVE)
  644. #endif
  645. )
  646. /* We got no data, we attempted to reuse a connection. For HTTP this
  647. can be a retry so we try again regardless if we expected a body.
  648. For other protocols we only try again only if we expected a body.
  649. This might happen if the connection was left alive when we were
  650. done using it before, but that was closed when we wanted to read from
  651. it again. Bad luck. Retry the same request on a fresh connect! */
  652. retry = TRUE;
  653. else if(data->state.refused_stream &&
  654. (data->req.bytecount + data->req.headerbytecount == 0) ) {
  655. /* This was sent on a refused stream, safe to rerun. A refused stream
  656. error can typically only happen on HTTP/2 level if the stream is safe
  657. to issue again, but the nghttp2 API can deliver the message to other
  658. streams as well, which is why this adds the check the data counters
  659. too. */
  660. infof(data, "REFUSED_STREAM, retrying a fresh connect");
  661. data->state.refused_stream = FALSE; /* clear again */
  662. retry = TRUE;
  663. }
  664. if(retry) {
  665. #define CONN_MAX_RETRIES 5
  666. if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
  667. failf(data, "Connection died, tried %d times before giving up",
  668. CONN_MAX_RETRIES);
  669. data->state.retrycount = 0;
  670. return CURLE_SEND_ERROR;
  671. }
  672. infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
  673. data->state.retrycount);
  674. *url = strdup(data->state.url);
  675. if(!*url)
  676. return CURLE_OUT_OF_MEMORY;
  677. connclose(conn, "retry"); /* close this connection */
  678. conn->bits.retry = TRUE; /* mark this as a connection we are about
  679. to retry. Marking it this way should
  680. prevent i.e HTTP transfers to return
  681. error just because nothing has been
  682. transferred! */
  683. Curl_creader_set_rewind(data, TRUE);
  684. }
  685. return CURLE_OK;
  686. }
  687. /*
  688. * xfer_setup() is called to setup basic properties for the transfer.
  689. */
  690. static void xfer_setup(
  691. struct Curl_easy *data, /* transfer */
  692. int sockindex, /* socket index to read from or -1 */
  693. curl_off_t size, /* -1 if unknown at this point */
  694. bool getheader, /* TRUE if header parsing is wanted */
  695. int writesockindex, /* socket index to write to, it may be the same we
  696. read from. -1 disables */
  697. bool shutdown, /* shutdown connection at transfer end. Only
  698. * supported when sending OR receiving. */
  699. bool shutdown_err_ignore /* errors during shutdown do not fail the
  700. * transfer */
  701. )
  702. {
  703. struct SingleRequest *k = &data->req;
  704. struct connectdata *conn = data->conn;
  705. bool want_send = Curl_req_want_send(data);
  706. DEBUGASSERT(conn != NULL);
  707. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  708. DEBUGASSERT((writesockindex <= 1) && (writesockindex >= -1));
  709. DEBUGASSERT(!shutdown || (sockindex == -1) || (writesockindex == -1));
  710. if(conn->bits.multiplex || conn->httpversion >= 20 || want_send) {
  711. /* when multiplexing, the read/write sockets need to be the same! */
  712. conn->sockfd = sockindex == -1 ?
  713. ((writesockindex == -1 ? CURL_SOCKET_BAD : conn->sock[writesockindex])) :
  714. conn->sock[sockindex];
  715. conn->writesockfd = conn->sockfd;
  716. if(want_send)
  717. /* special and HTTP-specific */
  718. writesockindex = FIRSTSOCKET;
  719. }
  720. else {
  721. conn->sockfd = sockindex == -1 ?
  722. CURL_SOCKET_BAD : conn->sock[sockindex];
  723. conn->writesockfd = writesockindex == -1 ?
  724. CURL_SOCKET_BAD : conn->sock[writesockindex];
  725. }
  726. k->getheader = getheader;
  727. k->size = size;
  728. k->shutdown = shutdown;
  729. k->shutdown_err_ignore = shutdown_err_ignore;
  730. /* The code sequence below is placed in this function just because all
  731. necessary input is not always known in do_complete() as this function may
  732. be called after that */
  733. if(!k->getheader) {
  734. k->header = FALSE;
  735. if(size > 0)
  736. Curl_pgrsSetDownloadSize(data, size);
  737. }
  738. /* we want header and/or body, if neither then do not do this! */
  739. if(k->getheader || !data->req.no_body) {
  740. if(sockindex != -1)
  741. k->keepon |= KEEP_RECV;
  742. if(writesockindex != -1)
  743. k->keepon |= KEEP_SEND;
  744. } /* if(k->getheader || !data->req.no_body) */
  745. }
  746. void Curl_xfer_setup_nop(struct Curl_easy *data)
  747. {
  748. xfer_setup(data, -1, -1, FALSE, -1, FALSE, FALSE);
  749. }
  750. void Curl_xfer_setup1(struct Curl_easy *data,
  751. int send_recv,
  752. curl_off_t recv_size,
  753. bool getheader)
  754. {
  755. int recv_index = (send_recv & CURL_XFER_RECV) ? FIRSTSOCKET : -1;
  756. int send_index = (send_recv & CURL_XFER_SEND) ? FIRSTSOCKET : -1;
  757. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  758. xfer_setup(data, recv_index, recv_size, getheader, send_index, FALSE, FALSE);
  759. }
  760. void Curl_xfer_setup2(struct Curl_easy *data,
  761. int send_recv,
  762. curl_off_t recv_size,
  763. bool shutdown,
  764. bool shutdown_err_ignore)
  765. {
  766. int recv_index = (send_recv & CURL_XFER_RECV) ? SECONDARYSOCKET : -1;
  767. int send_index = (send_recv & CURL_XFER_SEND) ? SECONDARYSOCKET : -1;
  768. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  769. xfer_setup(data, recv_index, recv_size, FALSE, send_index,
  770. shutdown, shutdown_err_ignore);
  771. }
  772. CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
  773. const char *buf, size_t blen,
  774. bool is_eos)
  775. {
  776. CURLcode result = CURLE_OK;
  777. if(data->conn->handler->write_resp) {
  778. /* protocol handlers offering this function take full responsibility
  779. * for writing all received download data to the client. */
  780. result = data->conn->handler->write_resp(data, buf, blen, is_eos);
  781. }
  782. else {
  783. /* No special handling by protocol handler, write all received data
  784. * as BODY to the client. */
  785. if(blen || is_eos) {
  786. int cwtype = CLIENTWRITE_BODY;
  787. if(is_eos)
  788. cwtype |= CLIENTWRITE_EOS;
  789. result = Curl_client_write(data, cwtype, buf, blen);
  790. }
  791. }
  792. if(!result && is_eos) {
  793. /* If we wrote the EOS, we are definitely done */
  794. data->req.eos_written = TRUE;
  795. data->req.download_done = TRUE;
  796. }
  797. CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
  798. blen, is_eos, result);
  799. return result;
  800. }
  801. CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
  802. const char *hd0, size_t hdlen, bool is_eos)
  803. {
  804. if(data->conn->handler->write_resp_hd) {
  805. /* protocol handlers offering this function take full responsibility
  806. * for writing all received download data to the client. */
  807. return data->conn->handler->write_resp_hd(data, hd0, hdlen, is_eos);
  808. }
  809. /* No special handling by protocol handler, write as response bytes */
  810. return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
  811. }
  812. CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
  813. {
  814. (void)premature;
  815. return Curl_cw_out_done(data);
  816. }
  817. bool Curl_xfer_needs_flush(struct Curl_easy *data)
  818. {
  819. int sockindex;
  820. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  821. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  822. return Curl_conn_needs_flush(data, sockindex);
  823. }
  824. CURLcode Curl_xfer_flush(struct Curl_easy *data)
  825. {
  826. int sockindex;
  827. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  828. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  829. return Curl_conn_flush(data, sockindex);
  830. }
  831. CURLcode Curl_xfer_send(struct Curl_easy *data,
  832. const void *buf, size_t blen, bool eos,
  833. size_t *pnwritten)
  834. {
  835. CURLcode result;
  836. int sockindex;
  837. DEBUGASSERT(data);
  838. DEBUGASSERT(data->conn);
  839. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  840. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  841. result = Curl_conn_send(data, sockindex, buf, blen, eos, pnwritten);
  842. if(result == CURLE_AGAIN) {
  843. result = CURLE_OK;
  844. *pnwritten = 0;
  845. }
  846. else if(!result && *pnwritten)
  847. data->info.request_size += *pnwritten;
  848. DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
  849. blen, eos, result, *pnwritten));
  850. return result;
  851. }
  852. CURLcode Curl_xfer_recv(struct Curl_easy *data,
  853. char *buf, size_t blen,
  854. ssize_t *pnrcvd)
  855. {
  856. int sockindex;
  857. DEBUGASSERT(data);
  858. DEBUGASSERT(data->conn);
  859. DEBUGASSERT(data->set.buffer_size > 0);
  860. sockindex = ((data->conn->sockfd != CURL_SOCKET_BAD) &&
  861. (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]));
  862. if((size_t)data->set.buffer_size < blen)
  863. blen = (size_t)data->set.buffer_size;
  864. return Curl_conn_recv(data, sockindex, buf, blen, pnrcvd);
  865. }
  866. CURLcode Curl_xfer_send_close(struct Curl_easy *data)
  867. {
  868. Curl_conn_ev_data_done_send(data);
  869. return CURLE_OK;
  870. }
  871. bool Curl_xfer_is_blocked(struct Curl_easy *data)
  872. {
  873. bool want_send = ((data)->req.keepon & KEEP_SEND);
  874. bool want_recv = ((data)->req.keepon & KEEP_RECV);
  875. if(!want_send)
  876. return (want_recv && Curl_cwriter_is_paused(data));
  877. else if(!want_recv)
  878. return (want_send && Curl_creader_is_paused(data));
  879. else
  880. return Curl_creader_is_paused(data) && Curl_cwriter_is_paused(data);
  881. }