transfer.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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 can't 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 "urlapi-int.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. /**
  166. * Receive raw response data for the transfer.
  167. * @param data the transfer
  168. * @param buf buffer to keep response data received
  169. * @param blen length of `buf`
  170. * @param eos_reliable if EOS detection in underlying connection is reliable
  171. * @param err error code in case of -1 return
  172. * @return number of bytes read or -1 for error
  173. */
  174. static ssize_t Curl_xfer_recv_resp(struct Curl_easy *data,
  175. char *buf, size_t blen,
  176. bool eos_reliable,
  177. CURLcode *err)
  178. {
  179. ssize_t nread;
  180. DEBUGASSERT(blen > 0);
  181. /* If we are reading BODY data and the connection does NOT handle EOF
  182. * and we know the size of the BODY data, limit the read amount */
  183. if(!eos_reliable && !data->req.header && data->req.size != -1) {
  184. curl_off_t totalleft = data->req.size - data->req.bytecount;
  185. if(totalleft <= 0)
  186. blen = 0;
  187. else if(totalleft < (curl_off_t)blen)
  188. blen = (size_t)totalleft;
  189. }
  190. else if(xfer_recv_shutdown_started(data)) {
  191. /* we already reveived everything. Do not try more. */
  192. blen = 0;
  193. }
  194. if(!blen) {
  195. /* want nothing more */
  196. *err = CURLE_OK;
  197. nread = 0;
  198. }
  199. else {
  200. *err = Curl_xfer_recv(data, buf, blen, &nread);
  201. }
  202. if(*err)
  203. return -1;
  204. if(nread == 0) {
  205. if(data->req.shutdown) {
  206. bool done;
  207. *err = xfer_recv_shutdown(data, &done);
  208. if(*err)
  209. return -1;
  210. if(!done) {
  211. *err = CURLE_AGAIN;
  212. return -1;
  213. }
  214. }
  215. DEBUGF(infof(data, "readwrite_data: we're done"));
  216. }
  217. DEBUGASSERT(nread >= 0);
  218. return nread;
  219. }
  220. /*
  221. * Go ahead and do a read if we have a readable socket or if
  222. * the stream was rewound (in which case we have data in a
  223. * buffer)
  224. */
  225. static CURLcode readwrite_data(struct Curl_easy *data,
  226. struct SingleRequest *k,
  227. int *didwhat)
  228. {
  229. struct connectdata *conn = data->conn;
  230. CURLcode result = CURLE_OK;
  231. char *buf, *xfer_buf;
  232. size_t blen, xfer_blen;
  233. int maxloops = 10;
  234. curl_off_t total_received = 0;
  235. bool is_multiplex = FALSE;
  236. result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
  237. if(result)
  238. goto out;
  239. /* This is where we loop until we have read everything there is to
  240. read or we get a CURLE_AGAIN */
  241. do {
  242. bool is_eos = FALSE;
  243. size_t bytestoread;
  244. ssize_t nread;
  245. if(!is_multiplex) {
  246. /* Multiplexed connection have inherent handling of EOF and we do not
  247. * have to carefully restrict the amount we try to read.
  248. * Multiplexed changes only in one direction. */
  249. is_multiplex = Curl_conn_is_multiplex(conn, FIRSTSOCKET);
  250. }
  251. buf = xfer_buf;
  252. bytestoread = xfer_blen;
  253. if(bytestoread && data->set.max_recv_speed) {
  254. /* In case of speed limit on receiving: if this loop already got
  255. * data, break out. If not, limit the amount of bytes to receive.
  256. * The overall, timed, speed limiting is done in multi.c */
  257. if(total_received)
  258. break;
  259. if((size_t)data->set.max_recv_speed < bytestoread)
  260. bytestoread = (size_t)data->set.max_recv_speed;
  261. }
  262. nread = Curl_xfer_recv_resp(data, buf, bytestoread,
  263. is_multiplex, &result);
  264. if(nread < 0) {
  265. if(CURLE_AGAIN == result) {
  266. result = CURLE_OK;
  267. break; /* get out of loop */
  268. }
  269. goto out; /* real error */
  270. }
  271. /* We only get a 0-length read on EndOfStream */
  272. blen = (size_t)nread;
  273. is_eos = (blen == 0);
  274. *didwhat |= KEEP_RECV;
  275. if(!blen) {
  276. /* if we receive 0 or less here, either the data transfer is done or the
  277. server closed the connection and we bail out from this! */
  278. if(is_multiplex)
  279. DEBUGF(infof(data, "nread == 0, stream closed, bailing"));
  280. else
  281. DEBUGF(infof(data, "nread <= 0, server closed connection, bailing"));
  282. /* stop receiving and ALL sending as well, including PAUSE and HOLD.
  283. * We might still be paused on receive client writes though, so
  284. * keep those bits around. */
  285. k->keepon &= ~(KEEP_RECV|KEEP_SENDBITS);
  286. if(k->eos_written) /* already did write this to client, leave */
  287. break;
  288. }
  289. total_received += blen;
  290. result = Curl_xfer_write_resp(data, buf, blen, is_eos);
  291. if(result || data->req.done)
  292. goto out;
  293. /* if we are done, we stop receiving. On multiplexed connections,
  294. * we should read the EOS. Which may arrive as meta data after
  295. * the bytes. Not taking it in might lead to RST of streams. */
  296. if((!is_multiplex && data->req.download_done) || is_eos) {
  297. data->req.keepon &= ~KEEP_RECV;
  298. }
  299. /* if we are PAUSEd or stopped receiving, leave the loop */
  300. if((k->keepon & KEEP_RECV_PAUSE) || !(k->keepon & KEEP_RECV))
  301. break;
  302. } while(maxloops-- && data_pending(data));
  303. if((maxloops <= 0) || data_pending(data)) {
  304. /* did not read until EAGAIN or there is still pending data, mark as
  305. read-again-please */
  306. data->state.select_bits = CURL_CSELECT_IN;
  307. if((k->keepon & KEEP_SENDBITS) == KEEP_SEND)
  308. data->state.select_bits |= CURL_CSELECT_OUT;
  309. }
  310. if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) &&
  311. (conn->bits.close || is_multiplex)) {
  312. /* When we've read the entire thing and the close bit is set, the server
  313. may now close the connection. If there's now any kind of sending going
  314. on from our side, we need to stop that immediately. */
  315. infof(data, "we are done reading and this is set to close, stop send");
  316. k->keepon &= ~KEEP_SEND; /* no writing anymore either */
  317. k->keepon &= ~KEEP_SEND_PAUSE; /* no pausing anymore either */
  318. }
  319. out:
  320. Curl_multi_xfer_buf_release(data, xfer_buf);
  321. if(result)
  322. DEBUGF(infof(data, "readwrite_data() -> %d", result));
  323. return result;
  324. }
  325. /*
  326. * Send data to upload to the server, when the socket is writable.
  327. */
  328. static CURLcode readwrite_upload(struct Curl_easy *data, int *didwhat)
  329. {
  330. if((data->req.keepon & KEEP_SEND_PAUSE))
  331. return CURLE_OK;
  332. /* We should not get here when the sending is already done. It
  333. * probably means that someone set `data-req.keepon |= KEEP_SEND`
  334. * when it should not. */
  335. DEBUGASSERT(!Curl_req_done_sending(data));
  336. if(!Curl_req_done_sending(data)) {
  337. *didwhat |= KEEP_SEND;
  338. return Curl_req_send_more(data);
  339. }
  340. return CURLE_OK;
  341. }
  342. static int select_bits_paused(struct Curl_easy *data, int select_bits)
  343. {
  344. /* See issue #11982: we really need to be careful not to progress
  345. * a transfer direction when that direction is paused. Not all parts
  346. * of our state machine are handling PAUSED transfers correctly. So, we
  347. * do not want to go there.
  348. * NOTE: we are only interested in PAUSE, not HOLD. */
  349. /* if there is data in a direction not paused, return false */
  350. if(((select_bits & CURL_CSELECT_IN) &&
  351. !(data->req.keepon & KEEP_RECV_PAUSE)) ||
  352. ((select_bits & CURL_CSELECT_OUT) &&
  353. !(data->req.keepon & KEEP_SEND_PAUSE)))
  354. return FALSE;
  355. return (data->req.keepon & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE));
  356. }
  357. /*
  358. * Curl_readwrite() is the low-level function to be called when data is to
  359. * be read and written to/from the connection.
  360. */
  361. CURLcode Curl_readwrite(struct Curl_easy *data)
  362. {
  363. struct connectdata *conn = data->conn;
  364. struct SingleRequest *k = &data->req;
  365. CURLcode result;
  366. struct curltime now;
  367. int didwhat = 0;
  368. int select_bits;
  369. /* Check if client writes had been paused and can resume now. */
  370. if(!(k->keepon & KEEP_RECV_PAUSE) && Curl_cwriter_is_paused(data)) {
  371. Curl_conn_ev_data_pause(data, FALSE);
  372. result = Curl_cwriter_unpause(data);
  373. if(result)
  374. goto out;
  375. }
  376. if(data->state.select_bits) {
  377. if(select_bits_paused(data, data->state.select_bits)) {
  378. /* leave the bits unchanged, so they'll tell us what to do when
  379. * this transfer gets unpaused. */
  380. DEBUGF(infof(data, "readwrite, select_bits, early return on PAUSED"));
  381. result = CURLE_OK;
  382. goto out;
  383. }
  384. select_bits = data->state.select_bits;
  385. data->state.select_bits = 0;
  386. }
  387. else {
  388. curl_socket_t fd_read;
  389. curl_socket_t fd_write;
  390. /* only use the proper socket if the *_HOLD bit is not set simultaneously
  391. as then we are in rate limiting state in that transfer direction */
  392. if((k->keepon & KEEP_RECVBITS) == KEEP_RECV)
  393. fd_read = conn->sockfd;
  394. else
  395. fd_read = CURL_SOCKET_BAD;
  396. if((k->keepon & KEEP_SENDBITS) == KEEP_SEND)
  397. fd_write = conn->writesockfd;
  398. else
  399. fd_write = CURL_SOCKET_BAD;
  400. select_bits = Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write, 0);
  401. }
  402. if(select_bits == CURL_CSELECT_ERR) {
  403. failf(data, "select/poll returned error");
  404. result = CURLE_SEND_ERROR;
  405. goto out;
  406. }
  407. #ifdef USE_HYPER
  408. if(conn->datastream) {
  409. result = conn->datastream(data, conn, &didwhat, select_bits);
  410. if(result || data->req.done)
  411. goto out;
  412. }
  413. else {
  414. #endif
  415. /* We go ahead and do a read if we have a readable socket or if
  416. the stream was rewound (in which case we have data in a
  417. buffer) */
  418. if((k->keepon & KEEP_RECV) && (select_bits & CURL_CSELECT_IN)) {
  419. result = readwrite_data(data, k, &didwhat);
  420. if(result || data->req.done)
  421. goto out;
  422. }
  423. /* If we still have writing to do, we check if we have a writable socket. */
  424. if(((k->keepon & KEEP_SEND) && (select_bits & CURL_CSELECT_OUT)) ||
  425. (k->keepon & KEEP_SEND_TIMED)) {
  426. /* write */
  427. result = readwrite_upload(data, &didwhat);
  428. if(result)
  429. goto out;
  430. }
  431. #ifdef USE_HYPER
  432. }
  433. #endif
  434. now = Curl_now();
  435. if(!didwhat) {
  436. result = Curl_conn_ev_data_idle(data);
  437. if(result)
  438. goto out;
  439. }
  440. if(Curl_pgrsUpdate(data))
  441. result = CURLE_ABORTED_BY_CALLBACK;
  442. else
  443. result = Curl_speedcheck(data, now);
  444. if(result)
  445. goto out;
  446. if(k->keepon) {
  447. if(0 > Curl_timeleft(data, &now, FALSE)) {
  448. if(k->size != -1) {
  449. failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T
  450. " milliseconds with %" CURL_FORMAT_CURL_OFF_T " out of %"
  451. CURL_FORMAT_CURL_OFF_T " bytes received",
  452. Curl_timediff(now, data->progress.t_startsingle),
  453. k->bytecount, k->size);
  454. }
  455. else {
  456. failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T
  457. " milliseconds with %" CURL_FORMAT_CURL_OFF_T " bytes received",
  458. Curl_timediff(now, data->progress.t_startsingle),
  459. k->bytecount);
  460. }
  461. result = CURLE_OPERATION_TIMEDOUT;
  462. goto out;
  463. }
  464. }
  465. else {
  466. /*
  467. * The transfer has been performed. Just make some general checks before
  468. * returning.
  469. */
  470. if(!(data->req.no_body) && (k->size != -1) &&
  471. (k->bytecount != k->size) &&
  472. #ifdef CURL_DO_LINEEND_CONV
  473. /* Most FTP servers don't adjust their file SIZE response for CRLFs,
  474. so we'll check to see if the discrepancy can be explained
  475. by the number of CRLFs we've changed to LFs.
  476. */
  477. (k->bytecount != (k->size + data->state.crlf_conversions)) &&
  478. #endif /* CURL_DO_LINEEND_CONV */
  479. !k->newurl) {
  480. failf(data, "transfer closed with %" CURL_FORMAT_CURL_OFF_T
  481. " bytes remaining to read", k->size - k->bytecount);
  482. result = CURLE_PARTIAL_FILE;
  483. goto out;
  484. }
  485. if(Curl_pgrsUpdate(data)) {
  486. result = CURLE_ABORTED_BY_CALLBACK;
  487. goto out;
  488. }
  489. }
  490. /* If there is nothing more to send/recv, the request is done */
  491. if(0 == (k->keepon&(KEEP_RECVBITS|KEEP_SENDBITS)))
  492. data->req.done = TRUE;
  493. out:
  494. if(result)
  495. DEBUGF(infof(data, "Curl_readwrite() -> %d", result));
  496. return result;
  497. }
  498. /* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
  499. which means this gets called once for each subsequent redirect etc */
  500. void Curl_init_CONNECT(struct Curl_easy *data)
  501. {
  502. data->state.fread_func = data->set.fread_func_set;
  503. data->state.in = data->set.in_set;
  504. data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
  505. }
  506. /*
  507. * Curl_pretransfer() is called immediately before a transfer starts, and only
  508. * once for one transfer no matter if it has redirects or do multi-pass
  509. * authentication etc.
  510. */
  511. CURLcode Curl_pretransfer(struct Curl_easy *data)
  512. {
  513. CURLcode result;
  514. if(!data->state.url && !data->set.uh) {
  515. /* we can't do anything without URL */
  516. failf(data, "No URL set");
  517. return CURLE_URL_MALFORMAT;
  518. }
  519. /* since the URL may have been redirected in a previous use of this handle */
  520. if(data->state.url_alloc) {
  521. /* the already set URL is allocated, free it first! */
  522. Curl_safefree(data->state.url);
  523. data->state.url_alloc = FALSE;
  524. }
  525. if(!data->state.url && data->set.uh) {
  526. CURLUcode uc;
  527. free(data->set.str[STRING_SET_URL]);
  528. uc = curl_url_get(data->set.uh,
  529. CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
  530. if(uc) {
  531. failf(data, "No URL set");
  532. return CURLE_URL_MALFORMAT;
  533. }
  534. }
  535. if(data->set.postfields && data->set.set_resume_from) {
  536. /* we can't */
  537. failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
  538. return CURLE_BAD_FUNCTION_ARGUMENT;
  539. }
  540. data->state.prefer_ascii = data->set.prefer_ascii;
  541. #ifdef CURL_LIST_ONLY_PROTOCOL
  542. data->state.list_only = data->set.list_only;
  543. #endif
  544. data->state.httpreq = data->set.method;
  545. data->state.url = data->set.str[STRING_SET_URL];
  546. /* Init the SSL session ID cache here. We do it here since we want to do it
  547. after the *_setopt() calls (that could specify the size of the cache) but
  548. before any transfer takes place. */
  549. result = Curl_ssl_initsessions(data, data->set.general_ssl.max_ssl_sessions);
  550. if(result)
  551. return result;
  552. data->state.requests = 0;
  553. data->state.followlocation = 0; /* reset the location-follow counter */
  554. data->state.this_is_a_follow = FALSE; /* reset this */
  555. data->state.errorbuf = FALSE; /* no error has occurred */
  556. data->state.httpwant = data->set.httpwant;
  557. data->state.httpversion = 0;
  558. data->state.authproblem = FALSE;
  559. data->state.authhost.want = data->set.httpauth;
  560. data->state.authproxy.want = data->set.proxyauth;
  561. Curl_safefree(data->info.wouldredirect);
  562. Curl_data_priority_clear_state(data);
  563. if(data->state.httpreq == HTTPREQ_PUT)
  564. data->state.infilesize = data->set.filesize;
  565. else if((data->state.httpreq != HTTPREQ_GET) &&
  566. (data->state.httpreq != HTTPREQ_HEAD)) {
  567. data->state.infilesize = data->set.postfieldsize;
  568. if(data->set.postfields && (data->state.infilesize == -1))
  569. data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
  570. }
  571. else
  572. data->state.infilesize = 0;
  573. /* If there is a list of cookie files to read, do it now! */
  574. Curl_cookie_loadfiles(data);
  575. /* If there is a list of host pairs to deal with */
  576. if(data->state.resolve)
  577. result = Curl_loadhostpairs(data);
  578. /* If there is a list of hsts files to read */
  579. Curl_hsts_loadfiles(data);
  580. if(!result) {
  581. /* Allow data->set.use_port to set which port to use. This needs to be
  582. * disabled for example when we follow Location: headers to URLs using
  583. * different ports! */
  584. data->state.allow_port = TRUE;
  585. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  586. /*************************************************************
  587. * Tell signal handler to ignore SIGPIPE
  588. *************************************************************/
  589. if(!data->set.no_signal)
  590. data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
  591. #endif
  592. Curl_initinfo(data); /* reset session-specific information "variables" */
  593. Curl_pgrsResetTransferSizes(data);
  594. Curl_pgrsStartNow(data);
  595. /* In case the handle is reused and an authentication method was picked
  596. in the session we need to make sure we only use the one(s) we now
  597. consider to be fine */
  598. data->state.authhost.picked &= data->state.authhost.want;
  599. data->state.authproxy.picked &= data->state.authproxy.want;
  600. #ifndef CURL_DISABLE_FTP
  601. data->state.wildcardmatch = data->set.wildcard_enabled;
  602. if(data->state.wildcardmatch) {
  603. struct WildcardData *wc;
  604. if(!data->wildcard) {
  605. data->wildcard = calloc(1, sizeof(struct WildcardData));
  606. if(!data->wildcard)
  607. return CURLE_OUT_OF_MEMORY;
  608. }
  609. wc = data->wildcard;
  610. if(wc->state < CURLWC_INIT) {
  611. if(wc->ftpwc)
  612. wc->dtor(wc->ftpwc);
  613. Curl_safefree(wc->pattern);
  614. Curl_safefree(wc->path);
  615. result = Curl_wildcard_init(wc); /* init wildcard structures */
  616. if(result)
  617. return CURLE_OUT_OF_MEMORY;
  618. }
  619. }
  620. #endif
  621. result = Curl_hsts_loadcb(data, data->hsts);
  622. }
  623. /*
  624. * Set user-agent. Used for HTTP, but since we can attempt to tunnel
  625. * basically anything through an HTTP proxy we can't limit this based on
  626. * protocol.
  627. */
  628. if(data->set.str[STRING_USERAGENT]) {
  629. Curl_safefree(data->state.aptr.uagent);
  630. data->state.aptr.uagent =
  631. aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
  632. if(!data->state.aptr.uagent)
  633. return CURLE_OUT_OF_MEMORY;
  634. }
  635. if(!result)
  636. result = Curl_setstropt(&data->state.aptr.user,
  637. data->set.str[STRING_USERNAME]);
  638. if(!result)
  639. result = Curl_setstropt(&data->state.aptr.passwd,
  640. data->set.str[STRING_PASSWORD]);
  641. #ifndef CURL_DISABLE_PROXY
  642. if(!result)
  643. result = Curl_setstropt(&data->state.aptr.proxyuser,
  644. data->set.str[STRING_PROXYUSERNAME]);
  645. if(!result)
  646. result = Curl_setstropt(&data->state.aptr.proxypasswd,
  647. data->set.str[STRING_PROXYPASSWORD]);
  648. #endif
  649. data->req.headerbytecount = 0;
  650. Curl_headers_cleanup(data);
  651. return result;
  652. }
  653. /*
  654. * Curl_posttransfer() is called immediately after a transfer ends
  655. */
  656. CURLcode Curl_posttransfer(struct Curl_easy *data)
  657. {
  658. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  659. /* restore the signal handler for SIGPIPE before we get back */
  660. if(!data->set.no_signal)
  661. signal(SIGPIPE, data->state.prev_signal);
  662. #else
  663. (void)data; /* unused parameter */
  664. #endif
  665. return CURLE_OK;
  666. }
  667. /*
  668. * Curl_follow() handles the URL redirect magic. Pass in the 'newurl' string
  669. * as given by the remote server and set up the new URL to request.
  670. *
  671. * This function DOES NOT FREE the given url.
  672. */
  673. CURLcode Curl_follow(struct Curl_easy *data,
  674. char *newurl, /* the Location: string */
  675. followtype type) /* see transfer.h */
  676. {
  677. #ifdef CURL_DISABLE_HTTP
  678. (void)data;
  679. (void)newurl;
  680. (void)type;
  681. /* Location: following will not happen when HTTP is disabled */
  682. return CURLE_TOO_MANY_REDIRECTS;
  683. #else
  684. /* Location: redirect */
  685. bool disallowport = FALSE;
  686. bool reachedmax = FALSE;
  687. CURLUcode uc;
  688. DEBUGASSERT(type != FOLLOW_NONE);
  689. if(type != FOLLOW_FAKE)
  690. data->state.requests++; /* count all real follows */
  691. if(type == FOLLOW_REDIR) {
  692. if((data->set.maxredirs != -1) &&
  693. (data->state.followlocation >= data->set.maxredirs)) {
  694. reachedmax = TRUE;
  695. type = FOLLOW_FAKE; /* switch to fake to store the would-be-redirected
  696. to URL */
  697. }
  698. else {
  699. data->state.followlocation++; /* count redirect-followings, including
  700. auth reloads */
  701. if(data->set.http_auto_referer) {
  702. CURLU *u;
  703. char *referer = NULL;
  704. /* We are asked to automatically set the previous URL as the referer
  705. when we get the next URL. We pick the ->url field, which may or may
  706. not be 100% correct */
  707. if(data->state.referer_alloc) {
  708. Curl_safefree(data->state.referer);
  709. data->state.referer_alloc = FALSE;
  710. }
  711. /* Make a copy of the URL without credentials and fragment */
  712. u = curl_url();
  713. if(!u)
  714. return CURLE_OUT_OF_MEMORY;
  715. uc = curl_url_set(u, CURLUPART_URL, data->state.url, 0);
  716. if(!uc)
  717. uc = curl_url_set(u, CURLUPART_FRAGMENT, NULL, 0);
  718. if(!uc)
  719. uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
  720. if(!uc)
  721. uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
  722. if(!uc)
  723. uc = curl_url_get(u, CURLUPART_URL, &referer, 0);
  724. curl_url_cleanup(u);
  725. if(uc || !referer)
  726. return CURLE_OUT_OF_MEMORY;
  727. data->state.referer = referer;
  728. data->state.referer_alloc = TRUE; /* yes, free this later */
  729. }
  730. }
  731. }
  732. if((type != FOLLOW_RETRY) &&
  733. (data->req.httpcode != 401) && (data->req.httpcode != 407) &&
  734. Curl_is_absolute_url(newurl, NULL, 0, FALSE)) {
  735. /* If this is not redirect due to a 401 or 407 response and an absolute
  736. URL: don't allow a custom port number */
  737. disallowport = TRUE;
  738. }
  739. DEBUGASSERT(data->state.uh);
  740. uc = curl_url_set(data->state.uh, CURLUPART_URL, newurl, (unsigned int)
  741. ((type == FOLLOW_FAKE) ? CURLU_NON_SUPPORT_SCHEME :
  742. ((type == FOLLOW_REDIR) ? CURLU_URLENCODE : 0) |
  743. CURLU_ALLOW_SPACE |
  744. (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)));
  745. if(uc) {
  746. if(type != FOLLOW_FAKE) {
  747. failf(data, "The redirect target URL could not be parsed: %s",
  748. curl_url_strerror(uc));
  749. return Curl_uc_to_curlcode(uc);
  750. }
  751. /* the URL could not be parsed for some reason, but since this is FAKE
  752. mode, just duplicate the field as-is */
  753. newurl = strdup(newurl);
  754. if(!newurl)
  755. return CURLE_OUT_OF_MEMORY;
  756. }
  757. else {
  758. uc = curl_url_get(data->state.uh, CURLUPART_URL, &newurl, 0);
  759. if(uc)
  760. return Curl_uc_to_curlcode(uc);
  761. /* Clear auth if this redirects to a different port number or protocol,
  762. unless permitted */
  763. if(!data->set.allow_auth_to_other_hosts && (type != FOLLOW_FAKE)) {
  764. char *portnum;
  765. int port;
  766. bool clear = FALSE;
  767. if(data->set.use_port && data->state.allow_port)
  768. /* a custom port is used */
  769. port = (int)data->set.use_port;
  770. else {
  771. uc = curl_url_get(data->state.uh, CURLUPART_PORT, &portnum,
  772. CURLU_DEFAULT_PORT);
  773. if(uc) {
  774. free(newurl);
  775. return Curl_uc_to_curlcode(uc);
  776. }
  777. port = atoi(portnum);
  778. free(portnum);
  779. }
  780. if(port != data->info.conn_remote_port) {
  781. infof(data, "Clear auth, redirects to port from %u to %u",
  782. data->info.conn_remote_port, port);
  783. clear = TRUE;
  784. }
  785. else {
  786. char *scheme;
  787. const struct Curl_handler *p;
  788. uc = curl_url_get(data->state.uh, CURLUPART_SCHEME, &scheme, 0);
  789. if(uc) {
  790. free(newurl);
  791. return Curl_uc_to_curlcode(uc);
  792. }
  793. p = Curl_get_scheme_handler(scheme);
  794. if(p && (p->protocol != data->info.conn_protocol)) {
  795. infof(data, "Clear auth, redirects scheme from %s to %s",
  796. data->info.conn_scheme, scheme);
  797. clear = TRUE;
  798. }
  799. free(scheme);
  800. }
  801. if(clear) {
  802. Curl_safefree(data->state.aptr.user);
  803. Curl_safefree(data->state.aptr.passwd);
  804. }
  805. }
  806. }
  807. if(type == FOLLOW_FAKE) {
  808. /* we're only figuring out the new url if we would've followed locations
  809. but now we're done so we can get out! */
  810. data->info.wouldredirect = newurl;
  811. if(reachedmax) {
  812. failf(data, "Maximum (%ld) redirects followed", data->set.maxredirs);
  813. return CURLE_TOO_MANY_REDIRECTS;
  814. }
  815. return CURLE_OK;
  816. }
  817. if(disallowport)
  818. data->state.allow_port = FALSE;
  819. if(data->state.url_alloc)
  820. Curl_safefree(data->state.url);
  821. data->state.url = newurl;
  822. data->state.url_alloc = TRUE;
  823. Curl_req_soft_reset(&data->req, data);
  824. infof(data, "Issue another request to this URL: '%s'", data->state.url);
  825. /*
  826. * We get here when the HTTP code is 300-399 (and 401). We need to perform
  827. * differently based on exactly what return code there was.
  828. *
  829. * News from 7.10.6: we can also get here on a 401 or 407, in case we act on
  830. * an HTTP (proxy-) authentication scheme other than Basic.
  831. */
  832. switch(data->info.httpcode) {
  833. /* 401 - Act on a WWW-Authenticate, we keep on moving and do the
  834. Authorization: XXXX header in the HTTP request code snippet */
  835. /* 407 - Act on a Proxy-Authenticate, we keep on moving and do the
  836. Proxy-Authorization: XXXX header in the HTTP request code snippet */
  837. /* 300 - Multiple Choices */
  838. /* 306 - Not used */
  839. /* 307 - Temporary Redirect */
  840. default: /* for all above (and the unknown ones) */
  841. /* Some codes are explicitly mentioned since I've checked RFC2616 and they
  842. * seem to be OK to POST to.
  843. */
  844. break;
  845. case 301: /* Moved Permanently */
  846. /* (quote from RFC7231, section 6.4.2)
  847. *
  848. * Note: For historical reasons, a user agent MAY change the request
  849. * method from POST to GET for the subsequent request. If this
  850. * behavior is undesired, the 307 (Temporary Redirect) status code
  851. * can be used instead.
  852. *
  853. * ----
  854. *
  855. * Many webservers expect this, so these servers often answers to a POST
  856. * request with an error page. To be sure that libcurl gets the page that
  857. * most user agents would get, libcurl has to force GET.
  858. *
  859. * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
  860. * can be overridden with CURLOPT_POSTREDIR.
  861. */
  862. if((data->state.httpreq == HTTPREQ_POST
  863. || data->state.httpreq == HTTPREQ_POST_FORM
  864. || data->state.httpreq == HTTPREQ_POST_MIME)
  865. && !(data->set.keep_post & CURL_REDIR_POST_301)) {
  866. infof(data, "Switch from POST to GET");
  867. data->state.httpreq = HTTPREQ_GET;
  868. Curl_creader_set_rewind(data, FALSE);
  869. }
  870. break;
  871. case 302: /* Found */
  872. /* (quote from RFC7231, section 6.4.3)
  873. *
  874. * Note: For historical reasons, a user agent MAY change the request
  875. * method from POST to GET for the subsequent request. If this
  876. * behavior is undesired, the 307 (Temporary Redirect) status code
  877. * can be used instead.
  878. *
  879. * ----
  880. *
  881. * Many webservers expect this, so these servers often answers to a POST
  882. * request with an error page. To be sure that libcurl gets the page that
  883. * most user agents would get, libcurl has to force GET.
  884. *
  885. * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
  886. * can be overridden with CURLOPT_POSTREDIR.
  887. */
  888. if((data->state.httpreq == HTTPREQ_POST
  889. || data->state.httpreq == HTTPREQ_POST_FORM
  890. || data->state.httpreq == HTTPREQ_POST_MIME)
  891. && !(data->set.keep_post & CURL_REDIR_POST_302)) {
  892. infof(data, "Switch from POST to GET");
  893. data->state.httpreq = HTTPREQ_GET;
  894. Curl_creader_set_rewind(data, FALSE);
  895. }
  896. break;
  897. case 303: /* See Other */
  898. /* 'See Other' location is not the resource but a substitute for the
  899. * resource. In this case we switch the method to GET/HEAD, unless the
  900. * method is POST and the user specified to keep it as POST.
  901. * https://github.com/curl/curl/issues/5237#issuecomment-614641049
  902. */
  903. if(data->state.httpreq != HTTPREQ_GET &&
  904. ((data->state.httpreq != HTTPREQ_POST &&
  905. data->state.httpreq != HTTPREQ_POST_FORM &&
  906. data->state.httpreq != HTTPREQ_POST_MIME) ||
  907. !(data->set.keep_post & CURL_REDIR_POST_303))) {
  908. data->state.httpreq = HTTPREQ_GET;
  909. infof(data, "Switch to %s",
  910. data->req.no_body?"HEAD":"GET");
  911. }
  912. break;
  913. case 304: /* Not Modified */
  914. /* 304 means we did a conditional request and it was "Not modified".
  915. * We shouldn't get any Location: header in this response!
  916. */
  917. break;
  918. case 305: /* Use Proxy */
  919. /* (quote from RFC2616, section 10.3.6):
  920. * "The requested resource MUST be accessed through the proxy given
  921. * by the Location field. The Location field gives the URI of the
  922. * proxy. The recipient is expected to repeat this single request
  923. * via the proxy. 305 responses MUST only be generated by origin
  924. * servers."
  925. */
  926. break;
  927. }
  928. Curl_pgrsTime(data, TIMER_REDIRECT);
  929. Curl_pgrsResetTransferSizes(data);
  930. return CURLE_OK;
  931. #endif /* CURL_DISABLE_HTTP */
  932. }
  933. /* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
  934. NOTE: that the *url is malloc()ed. */
  935. CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
  936. {
  937. struct connectdata *conn = data->conn;
  938. bool retry = FALSE;
  939. *url = NULL;
  940. /* if we're talking upload, we can't do the checks below, unless the protocol
  941. is HTTP as when uploading over HTTP we will still get a response */
  942. if(data->state.upload &&
  943. !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
  944. return CURLE_OK;
  945. if((data->req.bytecount + data->req.headerbytecount == 0) &&
  946. conn->bits.reuse &&
  947. (!data->req.no_body || (conn->handler->protocol & PROTO_FAMILY_HTTP))
  948. #ifndef CURL_DISABLE_RTSP
  949. && (data->set.rtspreq != RTSPREQ_RECEIVE)
  950. #endif
  951. )
  952. /* We got no data, we attempted to reuse a connection. For HTTP this
  953. can be a retry so we try again regardless if we expected a body.
  954. For other protocols we only try again only if we expected a body.
  955. This might happen if the connection was left alive when we were
  956. done using it before, but that was closed when we wanted to read from
  957. it again. Bad luck. Retry the same request on a fresh connect! */
  958. retry = TRUE;
  959. else if(data->state.refused_stream &&
  960. (data->req.bytecount + data->req.headerbytecount == 0) ) {
  961. /* This was sent on a refused stream, safe to rerun. A refused stream
  962. error can typically only happen on HTTP/2 level if the stream is safe
  963. to issue again, but the nghttp2 API can deliver the message to other
  964. streams as well, which is why this adds the check the data counters
  965. too. */
  966. infof(data, "REFUSED_STREAM, retrying a fresh connect");
  967. data->state.refused_stream = FALSE; /* clear again */
  968. retry = TRUE;
  969. }
  970. if(retry) {
  971. #define CONN_MAX_RETRIES 5
  972. if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
  973. failf(data, "Connection died, tried %d times before giving up",
  974. CONN_MAX_RETRIES);
  975. data->state.retrycount = 0;
  976. return CURLE_SEND_ERROR;
  977. }
  978. infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
  979. data->state.retrycount);
  980. *url = strdup(data->state.url);
  981. if(!*url)
  982. return CURLE_OUT_OF_MEMORY;
  983. connclose(conn, "retry"); /* close this connection */
  984. conn->bits.retry = TRUE; /* mark this as a connection we're about
  985. to retry. Marking it this way should
  986. prevent i.e HTTP transfers to return
  987. error just because nothing has been
  988. transferred! */
  989. Curl_creader_set_rewind(data, TRUE);
  990. }
  991. return CURLE_OK;
  992. }
  993. /*
  994. * xfer_setup() is called to setup basic properties for the transfer.
  995. */
  996. static void xfer_setup(
  997. struct Curl_easy *data, /* transfer */
  998. int sockindex, /* socket index to read from or -1 */
  999. curl_off_t size, /* -1 if unknown at this point */
  1000. bool getheader, /* TRUE if header parsing is wanted */
  1001. int writesockindex, /* socket index to write to, it may very well be
  1002. the same we read from. -1 disables */
  1003. bool shutdown /* shutdown connection at transfer end. Only
  1004. * supported when sending OR receiving. */
  1005. )
  1006. {
  1007. struct SingleRequest *k = &data->req;
  1008. struct connectdata *conn = data->conn;
  1009. bool want_send = Curl_req_want_send(data);
  1010. DEBUGASSERT(conn != NULL);
  1011. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  1012. DEBUGASSERT((writesockindex <= 1) && (writesockindex >= -1));
  1013. DEBUGASSERT(!shutdown || (sockindex == -1) || (writesockindex == -1));
  1014. if(conn->bits.multiplex || conn->httpversion >= 20 || want_send) {
  1015. /* when multiplexing, the read/write sockets need to be the same! */
  1016. conn->sockfd = sockindex == -1 ?
  1017. ((writesockindex == -1 ? CURL_SOCKET_BAD : conn->sock[writesockindex])) :
  1018. conn->sock[sockindex];
  1019. conn->writesockfd = conn->sockfd;
  1020. if(want_send)
  1021. /* special and very HTTP-specific */
  1022. writesockindex = FIRSTSOCKET;
  1023. }
  1024. else {
  1025. conn->sockfd = sockindex == -1 ?
  1026. CURL_SOCKET_BAD : conn->sock[sockindex];
  1027. conn->writesockfd = writesockindex == -1 ?
  1028. CURL_SOCKET_BAD:conn->sock[writesockindex];
  1029. }
  1030. k->getheader = getheader;
  1031. k->size = size;
  1032. k->shutdown = shutdown;
  1033. /* The code sequence below is placed in this function just because all
  1034. necessary input is not always known in do_complete() as this function may
  1035. be called after that */
  1036. if(!k->getheader) {
  1037. k->header = FALSE;
  1038. if(size > 0)
  1039. Curl_pgrsSetDownloadSize(data, size);
  1040. }
  1041. /* we want header and/or body, if neither then don't do this! */
  1042. if(k->getheader || !data->req.no_body) {
  1043. if(sockindex != -1)
  1044. k->keepon |= KEEP_RECV;
  1045. if(writesockindex != -1)
  1046. k->keepon |= KEEP_SEND;
  1047. } /* if(k->getheader || !data->req.no_body) */
  1048. }
  1049. void Curl_xfer_setup_nop(struct Curl_easy *data)
  1050. {
  1051. xfer_setup(data, -1, -1, FALSE, -1, FALSE);
  1052. }
  1053. void Curl_xfer_setup1(struct Curl_easy *data,
  1054. int send_recv,
  1055. curl_off_t recv_size,
  1056. bool getheader)
  1057. {
  1058. int recv_index = (send_recv & CURL_XFER_RECV)? FIRSTSOCKET : -1;
  1059. int send_index = (send_recv & CURL_XFER_SEND)? FIRSTSOCKET : -1;
  1060. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  1061. xfer_setup(data, recv_index, recv_size, getheader, send_index, FALSE);
  1062. }
  1063. void Curl_xfer_setup2(struct Curl_easy *data,
  1064. int send_recv,
  1065. curl_off_t recv_size,
  1066. bool shutdown)
  1067. {
  1068. int recv_index = (send_recv & CURL_XFER_RECV)? SECONDARYSOCKET : -1;
  1069. int send_index = (send_recv & CURL_XFER_SEND)? SECONDARYSOCKET : -1;
  1070. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  1071. xfer_setup(data, recv_index, recv_size, FALSE, send_index, shutdown);
  1072. }
  1073. CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
  1074. const char *buf, size_t blen,
  1075. bool is_eos)
  1076. {
  1077. CURLcode result = CURLE_OK;
  1078. if(data->conn->handler->write_resp) {
  1079. /* protocol handlers offering this function take full responsibility
  1080. * for writing all received download data to the client. */
  1081. result = data->conn->handler->write_resp(data, buf, blen, is_eos);
  1082. }
  1083. else {
  1084. /* No special handling by protocol handler, write all received data
  1085. * as BODY to the client. */
  1086. if(blen || is_eos) {
  1087. int cwtype = CLIENTWRITE_BODY;
  1088. if(is_eos)
  1089. cwtype |= CLIENTWRITE_EOS;
  1090. #ifndef CURL_DISABLE_POP3
  1091. if(blen && data->conn->handler->protocol & PROTO_FAMILY_POP3) {
  1092. result = data->req.ignorebody? CURLE_OK :
  1093. Curl_pop3_write(data, buf, blen);
  1094. }
  1095. else
  1096. #endif /* CURL_DISABLE_POP3 */
  1097. result = Curl_client_write(data, cwtype, buf, blen);
  1098. }
  1099. }
  1100. if(!result && is_eos) {
  1101. /* If we wrote the EOS, we are definitely done */
  1102. data->req.eos_written = TRUE;
  1103. data->req.download_done = TRUE;
  1104. }
  1105. CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
  1106. blen, is_eos, result);
  1107. return result;
  1108. }
  1109. CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
  1110. const char *hd0, size_t hdlen, bool is_eos)
  1111. {
  1112. if(data->conn->handler->write_resp_hd) {
  1113. /* protocol handlers offering this function take full responsibility
  1114. * for writing all received download data to the client. */
  1115. return data->conn->handler->write_resp_hd(data, hd0, hdlen, is_eos);
  1116. }
  1117. /* No special handling by protocol handler, write as response bytes */
  1118. return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
  1119. }
  1120. CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
  1121. {
  1122. (void)premature;
  1123. return Curl_cw_out_done(data);
  1124. }
  1125. CURLcode Curl_xfer_send(struct Curl_easy *data,
  1126. const void *buf, size_t blen,
  1127. size_t *pnwritten)
  1128. {
  1129. CURLcode result;
  1130. int sockindex;
  1131. if(!data || !data->conn)
  1132. return CURLE_FAILED_INIT;
  1133. /* FIXME: would like to enable this, but some protocols (MQTT) do not
  1134. * setup the transfer correctly, it seems
  1135. if(data->conn->writesockfd == CURL_SOCKET_BAD) {
  1136. failf(data, "transfer not setup for sending");
  1137. DEBUGASSERT(0);
  1138. return CURLE_SEND_ERROR;
  1139. } */
  1140. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  1141. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  1142. result = Curl_conn_send(data, sockindex, buf, blen, pnwritten);
  1143. if(result == CURLE_AGAIN) {
  1144. result = CURLE_OK;
  1145. *pnwritten = 0;
  1146. }
  1147. else if(!result && *pnwritten)
  1148. data->info.request_size += *pnwritten;
  1149. return result;
  1150. }
  1151. CURLcode Curl_xfer_recv(struct Curl_easy *data,
  1152. char *buf, size_t blen,
  1153. ssize_t *pnrcvd)
  1154. {
  1155. int sockindex;
  1156. if(!data || !data->conn)
  1157. return CURLE_FAILED_INIT;
  1158. /* FIXME: would like to enable this, but some protocols (MQTT) do not
  1159. * setup the transfer correctly, it seems
  1160. if(data->conn->sockfd == CURL_SOCKET_BAD) {
  1161. failf(data, "transfer not setup for receiving");
  1162. DEBUGASSERT(0);
  1163. return CURLE_RECV_ERROR;
  1164. } */
  1165. sockindex = ((data->conn->sockfd != CURL_SOCKET_BAD) &&
  1166. (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]));
  1167. if(data->set.buffer_size > 0 && (size_t)data->set.buffer_size < blen)
  1168. blen = (size_t)data->set.buffer_size;
  1169. return Curl_conn_recv(data, sockindex, buf, blen, pnrcvd);
  1170. }
  1171. CURLcode Curl_xfer_send_close(struct Curl_easy *data)
  1172. {
  1173. Curl_conn_ev_data_done_send(data);
  1174. return CURLE_OK;
  1175. }
  1176. CURLcode Curl_xfer_send_shutdown(struct Curl_easy *data, bool *done)
  1177. {
  1178. int sockindex;
  1179. if(!data || !data->conn)
  1180. return CURLE_FAILED_INIT;
  1181. if(data->conn->writesockfd == CURL_SOCKET_BAD)
  1182. return CURLE_FAILED_INIT;
  1183. sockindex = (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]);
  1184. return Curl_conn_shutdown(data, sockindex, done);
  1185. }
  1186. bool Curl_xfer_is_blocked(struct Curl_easy *data)
  1187. {
  1188. bool want_send = ((data)->req.keepon & KEEP_SEND);
  1189. bool want_recv = ((data)->req.keepon & KEEP_RECV);
  1190. if(!want_send)
  1191. return (want_recv && Curl_cwriter_is_paused(data));
  1192. else if(!want_recv)
  1193. return (want_send && Curl_creader_is_paused(data));
  1194. else
  1195. return Curl_creader_is_paused(data) && Curl_cwriter_is_paused(data);
  1196. }