transfer.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 "vtls/vtls_scache.h"
  69. #include "vquic/vquic.h"
  70. #include "select.h"
  71. #include "multiif.h"
  72. #include "connect.h"
  73. #include "http2.h"
  74. #include "mime.h"
  75. #include "strcase.h"
  76. #include "hsts.h"
  77. #include "setopt.h"
  78. #include "headers.h"
  79. /* The last 3 #include files should be in this order */
  80. #include "curl_printf.h"
  81. #include "curl_memory.h"
  82. #include "memdebug.h"
  83. #if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_SMTP) || \
  84. !defined(CURL_DISABLE_IMAP)
  85. /*
  86. * checkheaders() checks the linked list of custom headers for a
  87. * particular header (prefix). Provide the prefix without colon!
  88. *
  89. * Returns a pointer to the first matching header or NULL if none matched.
  90. */
  91. char *Curl_checkheaders(const struct Curl_easy *data,
  92. const char *thisheader,
  93. const size_t thislen)
  94. {
  95. struct curl_slist *head;
  96. DEBUGASSERT(thislen);
  97. DEBUGASSERT(thisheader[thislen-1] != ':');
  98. for(head = data->set.headers; head; head = head->next) {
  99. if(strncasecompare(head->data, thisheader, thislen) &&
  100. Curl_headersep(head->data[thislen]) )
  101. return head->data;
  102. }
  103. return NULL;
  104. }
  105. #endif
  106. static int data_pending(struct Curl_easy *data)
  107. {
  108. struct connectdata *conn = data->conn;
  109. if(conn->handler->protocol&PROTO_FAMILY_FTP)
  110. return Curl_conn_data_pending(data, SECONDARYSOCKET);
  111. /* in the case of libssh2, we can never be really sure that we have emptied
  112. its internal buffers so we MUST always try until we get EAGAIN back */
  113. return conn->handler->protocol&(CURLPROTO_SCP|CURLPROTO_SFTP) ||
  114. Curl_conn_data_pending(data, FIRSTSOCKET);
  115. }
  116. /*
  117. * Check to see if CURLOPT_TIMECONDITION was met by comparing the time of the
  118. * remote document with the time provided by CURLOPT_TIMEVAL
  119. */
  120. bool Curl_meets_timecondition(struct Curl_easy *data, time_t timeofdoc)
  121. {
  122. if((timeofdoc == 0) || (data->set.timevalue == 0))
  123. return TRUE;
  124. switch(data->set.timecondition) {
  125. case CURL_TIMECOND_IFMODSINCE:
  126. default:
  127. if(timeofdoc <= data->set.timevalue) {
  128. infof(data,
  129. "The requested document is not new enough");
  130. data->info.timecond = TRUE;
  131. return FALSE;
  132. }
  133. break;
  134. case CURL_TIMECOND_IFUNMODSINCE:
  135. if(timeofdoc >= data->set.timevalue) {
  136. infof(data,
  137. "The requested document is not old enough");
  138. data->info.timecond = TRUE;
  139. return FALSE;
  140. }
  141. break;
  142. }
  143. return TRUE;
  144. }
  145. static CURLcode xfer_recv_shutdown(struct Curl_easy *data, bool *done)
  146. {
  147. int sockindex;
  148. if(!data || !data->conn)
  149. return CURLE_FAILED_INIT;
  150. if(data->conn->sockfd == CURL_SOCKET_BAD)
  151. return CURLE_FAILED_INIT;
  152. sockindex = (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]);
  153. return Curl_conn_shutdown(data, sockindex, done);
  154. }
  155. static bool xfer_recv_shutdown_started(struct Curl_easy *data)
  156. {
  157. int sockindex;
  158. if(!data || !data->conn)
  159. return CURLE_FAILED_INIT;
  160. if(data->conn->sockfd == CURL_SOCKET_BAD)
  161. return CURLE_FAILED_INIT;
  162. sockindex = (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]);
  163. return Curl_shutdown_started(data, sockindex);
  164. }
  165. CURLcode Curl_xfer_send_shutdown(struct Curl_easy *data, bool *done)
  166. {
  167. int sockindex;
  168. if(!data || !data->conn)
  169. return CURLE_FAILED_INIT;
  170. if(data->conn->writesockfd == CURL_SOCKET_BAD)
  171. return CURLE_FAILED_INIT;
  172. sockindex = (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]);
  173. return Curl_conn_shutdown(data, sockindex, done);
  174. }
  175. /**
  176. * Receive raw response data for the transfer.
  177. * @param data the transfer
  178. * @param buf buffer to keep response data received
  179. * @param blen length of `buf`
  180. * @param eos_reliable if EOS detection in underlying connection is reliable
  181. * @param err error code in case of -1 return
  182. * @return number of bytes read or -1 for error
  183. */
  184. static ssize_t xfer_recv_resp(struct Curl_easy *data,
  185. char *buf, size_t blen,
  186. bool eos_reliable,
  187. CURLcode *err)
  188. {
  189. ssize_t nread;
  190. DEBUGASSERT(blen > 0);
  191. /* If we are reading BODY data and the connection does NOT handle EOF
  192. * and we know the size of the BODY data, limit the read amount */
  193. if(!eos_reliable && !data->req.header && data->req.size != -1) {
  194. curl_off_t totalleft = data->req.size - data->req.bytecount;
  195. if(totalleft <= 0)
  196. blen = 0;
  197. else if(totalleft < (curl_off_t)blen)
  198. blen = (size_t)totalleft;
  199. }
  200. else if(xfer_recv_shutdown_started(data)) {
  201. /* we already received everything. Do not try more. */
  202. blen = 0;
  203. }
  204. if(!blen) {
  205. /* want nothing more */
  206. *err = CURLE_OK;
  207. nread = 0;
  208. }
  209. else {
  210. *err = Curl_xfer_recv(data, buf, blen, &nread);
  211. }
  212. if(*err)
  213. return -1;
  214. if(nread == 0) {
  215. if(data->req.shutdown) {
  216. bool done;
  217. *err = xfer_recv_shutdown(data, &done);
  218. if(*err)
  219. return -1;
  220. if(!done) {
  221. *err = CURLE_AGAIN;
  222. return -1;
  223. }
  224. }
  225. DEBUGF(infof(data, "sendrecv_dl: we are done"));
  226. }
  227. DEBUGASSERT(nread >= 0);
  228. return nread;
  229. }
  230. /*
  231. * Go ahead and do a read if we have a readable socket or if
  232. * the stream was rewound (in which case we have data in a
  233. * buffer)
  234. */
  235. static CURLcode sendrecv_dl(struct Curl_easy *data,
  236. struct SingleRequest *k,
  237. int *didwhat)
  238. {
  239. struct connectdata *conn = data->conn;
  240. CURLcode result = CURLE_OK;
  241. char *buf, *xfer_buf;
  242. size_t blen, xfer_blen;
  243. int maxloops = 10;
  244. curl_off_t total_received = 0;
  245. bool is_multiplex = FALSE;
  246. result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
  247. if(result)
  248. goto out;
  249. /* This is where we loop until we have read everything there is to
  250. read or we get a CURLE_AGAIN */
  251. do {
  252. bool is_eos = FALSE;
  253. size_t bytestoread;
  254. ssize_t nread;
  255. if(!is_multiplex) {
  256. /* Multiplexed connection have inherent handling of EOF and we do not
  257. * have to carefully restrict the amount we try to read.
  258. * Multiplexed changes only in one direction. */
  259. is_multiplex = Curl_conn_is_multiplex(conn, FIRSTSOCKET);
  260. }
  261. buf = xfer_buf;
  262. bytestoread = xfer_blen;
  263. if(bytestoread && data->set.max_recv_speed > 0) {
  264. /* In case of speed limit on receiving: if this loop already got
  265. * data, break out. If not, limit the amount of bytes to receive.
  266. * The overall, timed, speed limiting is done in multi.c */
  267. if(total_received)
  268. break;
  269. if(data->set.max_recv_speed < (curl_off_t)bytestoread)
  270. bytestoread = (size_t)data->set.max_recv_speed;
  271. }
  272. nread = xfer_recv_resp(data, buf, bytestoread, 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. /* We go ahead and do a read if we have a readable socket or if the stream
  389. was rewound (in which case we have data in a buffer) */
  390. if(k->keepon & KEEP_RECV) {
  391. result = sendrecv_dl(data, k, &didwhat);
  392. if(result || data->req.done)
  393. goto out;
  394. }
  395. /* If we still have writing to do, we check if we have a writable socket. */
  396. if(Curl_req_want_send(data) || (data->req.keepon & KEEP_SEND_TIMED)) {
  397. result = sendrecv_ul(data, &didwhat);
  398. if(result)
  399. goto out;
  400. }
  401. if(!didwhat) {
  402. /* Transfer wanted to send/recv, but nothing was possible. */
  403. result = Curl_conn_ev_data_idle(data);
  404. if(result)
  405. goto out;
  406. }
  407. if(Curl_pgrsUpdate(data))
  408. result = CURLE_ABORTED_BY_CALLBACK;
  409. else
  410. result = Curl_speedcheck(data, *nowp);
  411. if(result)
  412. goto out;
  413. if(k->keepon) {
  414. if(0 > Curl_timeleft(data, nowp, FALSE)) {
  415. if(k->size != -1) {
  416. failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
  417. " milliseconds with %" FMT_OFF_T " out of %"
  418. FMT_OFF_T " bytes received",
  419. Curl_timediff(*nowp, data->progress.t_startsingle),
  420. k->bytecount, k->size);
  421. }
  422. else {
  423. failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
  424. " milliseconds with %" FMT_OFF_T " bytes received",
  425. Curl_timediff(*nowp, data->progress.t_startsingle),
  426. k->bytecount);
  427. }
  428. result = CURLE_OPERATION_TIMEDOUT;
  429. goto out;
  430. }
  431. }
  432. else {
  433. /*
  434. * The transfer has been performed. Just make some general checks before
  435. * returning.
  436. */
  437. if(!(data->req.no_body) && (k->size != -1) &&
  438. (k->bytecount != k->size) && !k->newurl) {
  439. failf(data, "transfer closed with %" FMT_OFF_T
  440. " bytes remaining to read", k->size - k->bytecount);
  441. result = CURLE_PARTIAL_FILE;
  442. goto out;
  443. }
  444. if(Curl_pgrsUpdate(data)) {
  445. result = CURLE_ABORTED_BY_CALLBACK;
  446. goto out;
  447. }
  448. }
  449. /* If there is nothing more to send/recv, the request is done */
  450. if(0 == (k->keepon&(KEEP_RECVBITS|KEEP_SENDBITS)))
  451. data->req.done = TRUE;
  452. out:
  453. if(result)
  454. DEBUGF(infof(data, "Curl_sendrecv() -> %d", result));
  455. return result;
  456. }
  457. /* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
  458. which means this gets called once for each subsequent redirect etc */
  459. void Curl_init_CONNECT(struct Curl_easy *data)
  460. {
  461. data->state.fread_func = data->set.fread_func_set;
  462. data->state.in = data->set.in_set;
  463. data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
  464. }
  465. /*
  466. * Curl_pretransfer() is called immediately before a transfer starts, and only
  467. * once for one transfer no matter if it has redirects or do multi-pass
  468. * authentication etc.
  469. */
  470. CURLcode Curl_pretransfer(struct Curl_easy *data)
  471. {
  472. CURLcode result = CURLE_OK;
  473. if(!data->state.url && !data->set.uh) {
  474. /* we cannot do anything without URL */
  475. failf(data, "No URL set");
  476. return CURLE_URL_MALFORMAT;
  477. }
  478. /* since the URL may have been redirected in a previous use of this handle */
  479. if(data->state.url_alloc) {
  480. /* the already set URL is allocated, free it first! */
  481. Curl_safefree(data->state.url);
  482. data->state.url_alloc = FALSE;
  483. }
  484. if(!data->state.url && data->set.uh) {
  485. CURLUcode uc;
  486. free(data->set.str[STRING_SET_URL]);
  487. uc = curl_url_get(data->set.uh,
  488. CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
  489. if(uc) {
  490. failf(data, "No URL set");
  491. return CURLE_URL_MALFORMAT;
  492. }
  493. }
  494. if(data->set.postfields && data->set.set_resume_from) {
  495. /* we cannot */
  496. failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
  497. return CURLE_BAD_FUNCTION_ARGUMENT;
  498. }
  499. data->state.prefer_ascii = data->set.prefer_ascii;
  500. #ifdef CURL_LIST_ONLY_PROTOCOL
  501. data->state.list_only = data->set.list_only;
  502. #endif
  503. data->state.httpreq = data->set.method;
  504. data->state.url = data->set.str[STRING_SET_URL];
  505. #ifdef USE_SSL
  506. if(!data->state.ssl_scache) {
  507. result = Curl_ssl_scache_create(data->set.general_ssl.max_ssl_sessions,
  508. 2, &data->state.ssl_scache);
  509. if(result)
  510. return result;
  511. }
  512. #endif
  513. data->state.requests = 0;
  514. data->state.followlocation = 0; /* reset the location-follow counter */
  515. data->state.this_is_a_follow = FALSE; /* reset this */
  516. data->state.errorbuf = FALSE; /* no error has occurred */
  517. data->state.httpwant = data->set.httpwant;
  518. data->state.httpversion = 0;
  519. data->state.authproblem = FALSE;
  520. data->state.authhost.want = data->set.httpauth;
  521. data->state.authproxy.want = data->set.proxyauth;
  522. Curl_safefree(data->info.wouldredirect);
  523. Curl_data_priority_clear_state(data);
  524. if(data->state.httpreq == HTTPREQ_PUT)
  525. data->state.infilesize = data->set.filesize;
  526. else if((data->state.httpreq != HTTPREQ_GET) &&
  527. (data->state.httpreq != HTTPREQ_HEAD)) {
  528. data->state.infilesize = data->set.postfieldsize;
  529. if(data->set.postfields && (data->state.infilesize == -1))
  530. data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
  531. }
  532. else
  533. data->state.infilesize = 0;
  534. /* If there is a list of cookie files to read, do it now! */
  535. Curl_cookie_loadfiles(data);
  536. /* If there is a list of host pairs to deal with */
  537. if(data->state.resolve)
  538. result = Curl_loadhostpairs(data);
  539. /* If there is a list of hsts files to read */
  540. Curl_hsts_loadfiles(data);
  541. if(!result) {
  542. /* Allow data->set.use_port to set which port to use. This needs to be
  543. * disabled for example when we follow Location: headers to URLs using
  544. * different ports! */
  545. data->state.allow_port = TRUE;
  546. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  547. /*************************************************************
  548. * Tell signal handler to ignore SIGPIPE
  549. *************************************************************/
  550. if(!data->set.no_signal)
  551. data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
  552. #endif
  553. Curl_initinfo(data); /* reset session-specific information "variables" */
  554. Curl_pgrsResetTransferSizes(data);
  555. Curl_pgrsStartNow(data);
  556. /* In case the handle is reused and an authentication method was picked
  557. in the session we need to make sure we only use the one(s) we now
  558. consider to be fine */
  559. data->state.authhost.picked &= data->state.authhost.want;
  560. data->state.authproxy.picked &= data->state.authproxy.want;
  561. #ifndef CURL_DISABLE_FTP
  562. data->state.wildcardmatch = data->set.wildcard_enabled;
  563. if(data->state.wildcardmatch) {
  564. struct WildcardData *wc;
  565. if(!data->wildcard) {
  566. data->wildcard = calloc(1, sizeof(struct WildcardData));
  567. if(!data->wildcard)
  568. return CURLE_OUT_OF_MEMORY;
  569. }
  570. wc = data->wildcard;
  571. if(wc->state < CURLWC_INIT) {
  572. if(wc->ftpwc)
  573. wc->dtor(wc->ftpwc);
  574. Curl_safefree(wc->pattern);
  575. Curl_safefree(wc->path);
  576. result = Curl_wildcard_init(wc); /* init wildcard structures */
  577. if(result)
  578. return CURLE_OUT_OF_MEMORY;
  579. }
  580. }
  581. #endif
  582. result = Curl_hsts_loadcb(data, data->hsts);
  583. }
  584. /*
  585. * Set user-agent. Used for HTTP, but since we can attempt to tunnel
  586. * basically anything through an HTTP proxy we cannot limit this based on
  587. * protocol.
  588. */
  589. if(data->set.str[STRING_USERAGENT]) {
  590. Curl_safefree(data->state.aptr.uagent);
  591. data->state.aptr.uagent =
  592. aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
  593. if(!data->state.aptr.uagent)
  594. return CURLE_OUT_OF_MEMORY;
  595. }
  596. if(data->set.str[STRING_USERNAME] ||
  597. data->set.str[STRING_PASSWORD])
  598. data->state.creds_from = CREDS_OPTION;
  599. if(!result)
  600. result = Curl_setstropt(&data->state.aptr.user,
  601. data->set.str[STRING_USERNAME]);
  602. if(!result)
  603. result = Curl_setstropt(&data->state.aptr.passwd,
  604. data->set.str[STRING_PASSWORD]);
  605. #ifndef CURL_DISABLE_PROXY
  606. if(!result)
  607. result = Curl_setstropt(&data->state.aptr.proxyuser,
  608. data->set.str[STRING_PROXYUSERNAME]);
  609. if(!result)
  610. result = Curl_setstropt(&data->state.aptr.proxypasswd,
  611. data->set.str[STRING_PROXYPASSWORD]);
  612. #endif
  613. data->req.headerbytecount = 0;
  614. Curl_headers_cleanup(data);
  615. return result;
  616. }
  617. /* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
  618. NOTE: that the *url is malloc()ed. */
  619. CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
  620. {
  621. struct connectdata *conn = data->conn;
  622. bool retry = FALSE;
  623. *url = NULL;
  624. /* if we are talking upload, we cannot do the checks below, unless the
  625. protocol is HTTP as when uploading over HTTP we will still get a
  626. response */
  627. if(data->state.upload &&
  628. !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
  629. return CURLE_OK;
  630. if((data->req.bytecount + data->req.headerbytecount == 0) &&
  631. conn->bits.reuse &&
  632. (!data->req.no_body || (conn->handler->protocol & PROTO_FAMILY_HTTP))
  633. #ifndef CURL_DISABLE_RTSP
  634. && (data->set.rtspreq != RTSPREQ_RECEIVE)
  635. #endif
  636. )
  637. /* We got no data, we attempted to reuse a connection. For HTTP this
  638. can be a retry so we try again regardless if we expected a body.
  639. For other protocols we only try again only if we expected a body.
  640. This might happen if the connection was left alive when we were
  641. done using it before, but that was closed when we wanted to read from
  642. it again. Bad luck. Retry the same request on a fresh connect! */
  643. retry = TRUE;
  644. else if(data->state.refused_stream &&
  645. (data->req.bytecount + data->req.headerbytecount == 0) ) {
  646. /* This was sent on a refused stream, safe to rerun. A refused stream
  647. error can typically only happen on HTTP/2 level if the stream is safe
  648. to issue again, but the nghttp2 API can deliver the message to other
  649. streams as well, which is why this adds the check the data counters
  650. too. */
  651. infof(data, "REFUSED_STREAM, retrying a fresh connect");
  652. data->state.refused_stream = FALSE; /* clear again */
  653. retry = TRUE;
  654. }
  655. if(retry) {
  656. #define CONN_MAX_RETRIES 5
  657. if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
  658. failf(data, "Connection died, tried %d times before giving up",
  659. CONN_MAX_RETRIES);
  660. data->state.retrycount = 0;
  661. return CURLE_SEND_ERROR;
  662. }
  663. infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
  664. data->state.retrycount);
  665. *url = strdup(data->state.url);
  666. if(!*url)
  667. return CURLE_OUT_OF_MEMORY;
  668. connclose(conn, "retry"); /* close this connection */
  669. conn->bits.retry = TRUE; /* mark this as a connection we are about
  670. to retry. Marking it this way should
  671. prevent i.e HTTP transfers to return
  672. error just because nothing has been
  673. transferred! */
  674. Curl_creader_set_rewind(data, TRUE);
  675. }
  676. return CURLE_OK;
  677. }
  678. /*
  679. * xfer_setup() is called to setup basic properties for the transfer.
  680. */
  681. static void xfer_setup(
  682. struct Curl_easy *data, /* transfer */
  683. int sockindex, /* socket index to read from or -1 */
  684. curl_off_t size, /* -1 if unknown at this point */
  685. bool getheader, /* TRUE if header parsing is wanted */
  686. int writesockindex, /* socket index to write to, it may be the same we
  687. read from. -1 disables */
  688. bool shutdown, /* shutdown connection at transfer end. Only
  689. * supported when sending OR receiving. */
  690. bool shutdown_err_ignore /* errors during shutdown do not fail the
  691. * transfer */
  692. )
  693. {
  694. struct SingleRequest *k = &data->req;
  695. struct connectdata *conn = data->conn;
  696. bool want_send = Curl_req_want_send(data);
  697. DEBUGASSERT(conn != NULL);
  698. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  699. DEBUGASSERT((writesockindex <= 1) && (writesockindex >= -1));
  700. DEBUGASSERT(!shutdown || (sockindex == -1) || (writesockindex == -1));
  701. if(conn->bits.multiplex || conn->httpversion >= 20 || want_send) {
  702. /* when multiplexing, the read/write sockets need to be the same! */
  703. conn->sockfd = sockindex == -1 ?
  704. ((writesockindex == -1 ? CURL_SOCKET_BAD : conn->sock[writesockindex])) :
  705. conn->sock[sockindex];
  706. conn->writesockfd = conn->sockfd;
  707. if(want_send)
  708. /* special and HTTP-specific */
  709. writesockindex = FIRSTSOCKET;
  710. }
  711. else {
  712. conn->sockfd = sockindex == -1 ?
  713. CURL_SOCKET_BAD : conn->sock[sockindex];
  714. conn->writesockfd = writesockindex == -1 ?
  715. CURL_SOCKET_BAD : conn->sock[writesockindex];
  716. }
  717. k->getheader = getheader;
  718. k->size = size;
  719. k->shutdown = shutdown;
  720. k->shutdown_err_ignore = shutdown_err_ignore;
  721. /* The code sequence below is placed in this function just because all
  722. necessary input is not always known in do_complete() as this function may
  723. be called after that */
  724. if(!k->getheader) {
  725. k->header = FALSE;
  726. if(size > 0)
  727. Curl_pgrsSetDownloadSize(data, size);
  728. }
  729. /* we want header and/or body, if neither then do not do this! */
  730. if(k->getheader || !data->req.no_body) {
  731. if(sockindex != -1)
  732. k->keepon |= KEEP_RECV;
  733. if(writesockindex != -1)
  734. k->keepon |= KEEP_SEND;
  735. } /* if(k->getheader || !data->req.no_body) */
  736. }
  737. void Curl_xfer_setup_nop(struct Curl_easy *data)
  738. {
  739. xfer_setup(data, -1, -1, FALSE, -1, FALSE, FALSE);
  740. }
  741. void Curl_xfer_setup1(struct Curl_easy *data,
  742. int send_recv,
  743. curl_off_t recv_size,
  744. bool getheader)
  745. {
  746. int recv_index = (send_recv & CURL_XFER_RECV) ? FIRSTSOCKET : -1;
  747. int send_index = (send_recv & CURL_XFER_SEND) ? FIRSTSOCKET : -1;
  748. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  749. xfer_setup(data, recv_index, recv_size, getheader, send_index, FALSE, FALSE);
  750. }
  751. void Curl_xfer_setup2(struct Curl_easy *data,
  752. int send_recv,
  753. curl_off_t recv_size,
  754. bool shutdown,
  755. bool shutdown_err_ignore)
  756. {
  757. int recv_index = (send_recv & CURL_XFER_RECV) ? SECONDARYSOCKET : -1;
  758. int send_index = (send_recv & CURL_XFER_SEND) ? SECONDARYSOCKET : -1;
  759. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  760. xfer_setup(data, recv_index, recv_size, FALSE, send_index,
  761. shutdown, shutdown_err_ignore);
  762. }
  763. CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
  764. const char *buf, size_t blen,
  765. bool is_eos)
  766. {
  767. CURLcode result = CURLE_OK;
  768. if(data->conn->handler->write_resp) {
  769. /* protocol handlers offering this function take full responsibility
  770. * for writing all received download data to the client. */
  771. result = data->conn->handler->write_resp(data, buf, blen, is_eos);
  772. }
  773. else {
  774. /* No special handling by protocol handler, write all received data
  775. * as BODY to the client. */
  776. if(blen || is_eos) {
  777. int cwtype = CLIENTWRITE_BODY;
  778. if(is_eos)
  779. cwtype |= CLIENTWRITE_EOS;
  780. result = Curl_client_write(data, cwtype, buf, blen);
  781. }
  782. }
  783. if(!result && is_eos) {
  784. /* If we wrote the EOS, we are definitely done */
  785. data->req.eos_written = TRUE;
  786. data->req.download_done = TRUE;
  787. }
  788. CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
  789. blen, is_eos, result);
  790. return result;
  791. }
  792. CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
  793. const char *hd0, size_t hdlen, bool is_eos)
  794. {
  795. if(data->conn->handler->write_resp_hd) {
  796. /* protocol handlers offering this function take full responsibility
  797. * for writing all received download data to the client. */
  798. return data->conn->handler->write_resp_hd(data, hd0, hdlen, is_eos);
  799. }
  800. /* No special handling by protocol handler, write as response bytes */
  801. return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
  802. }
  803. CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
  804. {
  805. (void)premature;
  806. return Curl_cw_out_done(data);
  807. }
  808. bool Curl_xfer_needs_flush(struct Curl_easy *data)
  809. {
  810. int sockindex;
  811. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  812. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  813. return Curl_conn_needs_flush(data, sockindex);
  814. }
  815. CURLcode Curl_xfer_flush(struct Curl_easy *data)
  816. {
  817. int sockindex;
  818. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  819. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  820. return Curl_conn_flush(data, sockindex);
  821. }
  822. CURLcode Curl_xfer_send(struct Curl_easy *data,
  823. const void *buf, size_t blen, bool eos,
  824. size_t *pnwritten)
  825. {
  826. CURLcode result;
  827. int sockindex;
  828. DEBUGASSERT(data);
  829. DEBUGASSERT(data->conn);
  830. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  831. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  832. result = Curl_conn_send(data, sockindex, buf, blen, eos, pnwritten);
  833. if(result == CURLE_AGAIN) {
  834. result = CURLE_OK;
  835. *pnwritten = 0;
  836. }
  837. else if(!result && *pnwritten)
  838. data->info.request_size += *pnwritten;
  839. DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
  840. blen, eos, result, *pnwritten));
  841. return result;
  842. }
  843. CURLcode Curl_xfer_recv(struct Curl_easy *data,
  844. char *buf, size_t blen,
  845. ssize_t *pnrcvd)
  846. {
  847. int sockindex;
  848. DEBUGASSERT(data);
  849. DEBUGASSERT(data->conn);
  850. DEBUGASSERT(data->set.buffer_size > 0);
  851. sockindex = ((data->conn->sockfd != CURL_SOCKET_BAD) &&
  852. (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]));
  853. if((size_t)data->set.buffer_size < blen)
  854. blen = (size_t)data->set.buffer_size;
  855. return Curl_conn_recv(data, sockindex, buf, blen, pnrcvd);
  856. }
  857. CURLcode Curl_xfer_send_close(struct Curl_easy *data)
  858. {
  859. Curl_conn_ev_data_done_send(data);
  860. return CURLE_OK;
  861. }
  862. bool Curl_xfer_is_blocked(struct Curl_easy *data)
  863. {
  864. bool want_send = ((data)->req.keepon & KEEP_SEND);
  865. bool want_recv = ((data)->req.keepon & KEEP_RECV);
  866. if(!want_send)
  867. return (want_recv && Curl_cwriter_is_paused(data));
  868. else if(!want_recv)
  869. return (want_send && Curl_creader_is_paused(data));
  870. else
  871. return Curl_creader_is_paused(data) && Curl_cwriter_is_paused(data);
  872. }