transfer.c 28 KB

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