transfer.c 31 KB

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