transfer.c 41 KB

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