2
0

transfer.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "strtoofft.h"
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_NET_IF_H
  36. #include <net/if.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #include <signal.h>
  42. #ifdef HAVE_SYS_PARAM_H
  43. #include <sys/param.h>
  44. #endif
  45. #ifdef HAVE_SYS_SELECT_H
  46. #include <sys/select.h>
  47. #elif defined(HAVE_UNISTD_H)
  48. #include <unistd.h>
  49. #endif
  50. #ifndef HAVE_SOCKET
  51. #error "We cannot compile without socket() support!"
  52. #endif
  53. #include "urldata.h"
  54. #include <curl/curl.h>
  55. #include "netrc.h"
  56. #include "content_encoding.h"
  57. #include "hostip.h"
  58. #include "cfilters.h"
  59. #include "cw-out.h"
  60. #include "transfer.h"
  61. #include "sendf.h"
  62. #include "speedcheck.h"
  63. #include "progress.h"
  64. #include "http.h"
  65. #include "url.h"
  66. #include "getinfo.h"
  67. #include "vtls/vtls.h"
  68. #include "vquic/vquic.h"
  69. #include "select.h"
  70. #include "multiif.h"
  71. #include "connect.h"
  72. #include "http2.h"
  73. #include "mime.h"
  74. #include "strcase.h"
  75. #include "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 are 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 > 0) {
  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(data->set.max_recv_speed < (curl_off_t)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--);
  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 have read the entire thing and the close bit is set, the server
  313. may now close the connection. If there is 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. 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 do not adjust their file SIZE response for CRLFs,
  467. so we will check to see if the discrepancy can be explained
  468. by the number of CRLFs we have 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 cannot 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 cannot */
  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 cannot 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_follow() handles the URL redirect magic. Pass in the 'newurl' string
  648. * as given by the remote server and set up the new URL to request.
  649. *
  650. * This function DOES NOT FREE the given url.
  651. */
  652. CURLcode Curl_follow(struct Curl_easy *data,
  653. char *newurl, /* the Location: string */
  654. followtype type) /* see transfer.h */
  655. {
  656. #ifdef CURL_DISABLE_HTTP
  657. (void)data;
  658. (void)newurl;
  659. (void)type;
  660. /* Location: following will not happen when HTTP is disabled */
  661. return CURLE_TOO_MANY_REDIRECTS;
  662. #else
  663. /* Location: redirect */
  664. bool disallowport = FALSE;
  665. bool reachedmax = FALSE;
  666. CURLUcode uc;
  667. DEBUGASSERT(type != FOLLOW_NONE);
  668. if(type != FOLLOW_FAKE)
  669. data->state.requests++; /* count all real follows */
  670. if(type == FOLLOW_REDIR) {
  671. if((data->set.maxredirs != -1) &&
  672. (data->state.followlocation >= data->set.maxredirs)) {
  673. reachedmax = TRUE;
  674. type = FOLLOW_FAKE; /* switch to fake to store the would-be-redirected
  675. to URL */
  676. }
  677. else {
  678. data->state.followlocation++; /* count redirect-followings, including
  679. auth reloads */
  680. if(data->set.http_auto_referer) {
  681. CURLU *u;
  682. char *referer = NULL;
  683. /* We are asked to automatically set the previous URL as the referer
  684. when we get the next URL. We pick the ->url field, which may or may
  685. not be 100% correct */
  686. if(data->state.referer_alloc) {
  687. Curl_safefree(data->state.referer);
  688. data->state.referer_alloc = FALSE;
  689. }
  690. /* Make a copy of the URL without credentials and fragment */
  691. u = curl_url();
  692. if(!u)
  693. return CURLE_OUT_OF_MEMORY;
  694. uc = curl_url_set(u, CURLUPART_URL, data->state.url, 0);
  695. if(!uc)
  696. uc = curl_url_set(u, CURLUPART_FRAGMENT, NULL, 0);
  697. if(!uc)
  698. uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
  699. if(!uc)
  700. uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
  701. if(!uc)
  702. uc = curl_url_get(u, CURLUPART_URL, &referer, 0);
  703. curl_url_cleanup(u);
  704. if(uc || !referer)
  705. return CURLE_OUT_OF_MEMORY;
  706. data->state.referer = referer;
  707. data->state.referer_alloc = TRUE; /* yes, free this later */
  708. }
  709. }
  710. }
  711. if((type != FOLLOW_RETRY) &&
  712. (data->req.httpcode != 401) && (data->req.httpcode != 407) &&
  713. Curl_is_absolute_url(newurl, NULL, 0, FALSE)) {
  714. /* If this is not redirect due to a 401 or 407 response and an absolute
  715. URL: do not allow a custom port number */
  716. disallowport = TRUE;
  717. }
  718. DEBUGASSERT(data->state.uh);
  719. uc = curl_url_set(data->state.uh, CURLUPART_URL, newurl, (unsigned int)
  720. ((type == FOLLOW_FAKE) ? CURLU_NON_SUPPORT_SCHEME :
  721. ((type == FOLLOW_REDIR) ? CURLU_URLENCODE : 0) |
  722. CURLU_ALLOW_SPACE |
  723. (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)));
  724. if(uc) {
  725. if(type != FOLLOW_FAKE) {
  726. failf(data, "The redirect target URL could not be parsed: %s",
  727. curl_url_strerror(uc));
  728. return Curl_uc_to_curlcode(uc);
  729. }
  730. /* the URL could not be parsed for some reason, but since this is FAKE
  731. mode, just duplicate the field as-is */
  732. newurl = strdup(newurl);
  733. if(!newurl)
  734. return CURLE_OUT_OF_MEMORY;
  735. }
  736. else {
  737. uc = curl_url_get(data->state.uh, CURLUPART_URL, &newurl, 0);
  738. if(uc)
  739. return Curl_uc_to_curlcode(uc);
  740. /* Clear auth if this redirects to a different port number or protocol,
  741. unless permitted */
  742. if(!data->set.allow_auth_to_other_hosts && (type != FOLLOW_FAKE)) {
  743. char *portnum;
  744. int port;
  745. bool clear = FALSE;
  746. if(data->set.use_port && data->state.allow_port)
  747. /* a custom port is used */
  748. port = (int)data->set.use_port;
  749. else {
  750. uc = curl_url_get(data->state.uh, CURLUPART_PORT, &portnum,
  751. CURLU_DEFAULT_PORT);
  752. if(uc) {
  753. free(newurl);
  754. return Curl_uc_to_curlcode(uc);
  755. }
  756. port = atoi(portnum);
  757. free(portnum);
  758. }
  759. if(port != data->info.conn_remote_port) {
  760. infof(data, "Clear auth, redirects to port from %u to %u",
  761. data->info.conn_remote_port, port);
  762. clear = TRUE;
  763. }
  764. else {
  765. char *scheme;
  766. const struct Curl_handler *p;
  767. uc = curl_url_get(data->state.uh, CURLUPART_SCHEME, &scheme, 0);
  768. if(uc) {
  769. free(newurl);
  770. return Curl_uc_to_curlcode(uc);
  771. }
  772. p = Curl_get_scheme_handler(scheme);
  773. if(p && (p->protocol != data->info.conn_protocol)) {
  774. infof(data, "Clear auth, redirects scheme from %s to %s",
  775. data->info.conn_scheme, scheme);
  776. clear = TRUE;
  777. }
  778. free(scheme);
  779. }
  780. if(clear) {
  781. Curl_safefree(data->state.aptr.user);
  782. Curl_safefree(data->state.aptr.passwd);
  783. }
  784. }
  785. }
  786. if(type == FOLLOW_FAKE) {
  787. /* we are only figuring out the new URL if we would have followed locations
  788. but now we are done so we can get out! */
  789. data->info.wouldredirect = newurl;
  790. if(reachedmax) {
  791. failf(data, "Maximum (%ld) redirects followed", data->set.maxredirs);
  792. return CURLE_TOO_MANY_REDIRECTS;
  793. }
  794. return CURLE_OK;
  795. }
  796. if(disallowport)
  797. data->state.allow_port = FALSE;
  798. if(data->state.url_alloc)
  799. Curl_safefree(data->state.url);
  800. data->state.url = newurl;
  801. data->state.url_alloc = TRUE;
  802. Curl_req_soft_reset(&data->req, data);
  803. infof(data, "Issue another request to this URL: '%s'", data->state.url);
  804. /*
  805. * We get here when the HTTP code is 300-399 (and 401). We need to perform
  806. * differently based on exactly what return code there was.
  807. *
  808. * News from 7.10.6: we can also get here on a 401 or 407, in case we act on
  809. * an HTTP (proxy-) authentication scheme other than Basic.
  810. */
  811. switch(data->info.httpcode) {
  812. /* 401 - Act on a WWW-Authenticate, we keep on moving and do the
  813. Authorization: XXXX header in the HTTP request code snippet */
  814. /* 407 - Act on a Proxy-Authenticate, we keep on moving and do the
  815. Proxy-Authorization: XXXX header in the HTTP request code snippet */
  816. /* 300 - Multiple Choices */
  817. /* 306 - Not used */
  818. /* 307 - Temporary Redirect */
  819. default: /* for all above (and the unknown ones) */
  820. /* Some codes are explicitly mentioned since I have checked RFC2616 and
  821. * they seem to be OK to POST to.
  822. */
  823. break;
  824. case 301: /* Moved Permanently */
  825. /* (quote from RFC7231, section 6.4.2)
  826. *
  827. * Note: For historical reasons, a user agent MAY change the request
  828. * method from POST to GET for the subsequent request. If this
  829. * behavior is undesired, the 307 (Temporary Redirect) status code
  830. * can be used instead.
  831. *
  832. * ----
  833. *
  834. * Many webservers expect this, so these servers often answers to a POST
  835. * request with an error page. To be sure that libcurl gets the page that
  836. * most user agents would get, libcurl has to force GET.
  837. *
  838. * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
  839. * can be overridden with CURLOPT_POSTREDIR.
  840. */
  841. if((data->state.httpreq == HTTPREQ_POST
  842. || data->state.httpreq == HTTPREQ_POST_FORM
  843. || data->state.httpreq == HTTPREQ_POST_MIME)
  844. && !(data->set.keep_post & CURL_REDIR_POST_301)) {
  845. infof(data, "Switch from POST to GET");
  846. data->state.httpreq = HTTPREQ_GET;
  847. Curl_creader_set_rewind(data, FALSE);
  848. }
  849. break;
  850. case 302: /* Found */
  851. /* (quote from RFC7231, section 6.4.3)
  852. *
  853. * Note: For historical reasons, a user agent MAY change the request
  854. * method from POST to GET for the subsequent request. If this
  855. * behavior is undesired, the 307 (Temporary Redirect) status code
  856. * can be used instead.
  857. *
  858. * ----
  859. *
  860. * Many webservers expect this, so these servers often answers to a POST
  861. * request with an error page. To be sure that libcurl gets the page that
  862. * most user agents would get, libcurl has to force GET.
  863. *
  864. * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
  865. * can be overridden with CURLOPT_POSTREDIR.
  866. */
  867. if((data->state.httpreq == HTTPREQ_POST
  868. || data->state.httpreq == HTTPREQ_POST_FORM
  869. || data->state.httpreq == HTTPREQ_POST_MIME)
  870. && !(data->set.keep_post & CURL_REDIR_POST_302)) {
  871. infof(data, "Switch from POST to GET");
  872. data->state.httpreq = HTTPREQ_GET;
  873. Curl_creader_set_rewind(data, FALSE);
  874. }
  875. break;
  876. case 303: /* See Other */
  877. /* 'See Other' location is not the resource but a substitute for the
  878. * resource. In this case we switch the method to GET/HEAD, unless the
  879. * method is POST and the user specified to keep it as POST.
  880. * https://github.com/curl/curl/issues/5237#issuecomment-614641049
  881. */
  882. if(data->state.httpreq != HTTPREQ_GET &&
  883. ((data->state.httpreq != HTTPREQ_POST &&
  884. data->state.httpreq != HTTPREQ_POST_FORM &&
  885. data->state.httpreq != HTTPREQ_POST_MIME) ||
  886. !(data->set.keep_post & CURL_REDIR_POST_303))) {
  887. data->state.httpreq = HTTPREQ_GET;
  888. infof(data, "Switch to %s",
  889. data->req.no_body?"HEAD":"GET");
  890. }
  891. break;
  892. case 304: /* Not Modified */
  893. /* 304 means we did a conditional request and it was "Not modified".
  894. * We should not get any Location: header in this response!
  895. */
  896. break;
  897. case 305: /* Use Proxy */
  898. /* (quote from RFC2616, section 10.3.6):
  899. * "The requested resource MUST be accessed through the proxy given
  900. * by the Location field. The Location field gives the URI of the
  901. * proxy. The recipient is expected to repeat this single request
  902. * via the proxy. 305 responses MUST only be generated by origin
  903. * servers."
  904. */
  905. break;
  906. }
  907. Curl_pgrsTime(data, TIMER_REDIRECT);
  908. Curl_pgrsResetTransferSizes(data);
  909. return CURLE_OK;
  910. #endif /* CURL_DISABLE_HTTP */
  911. }
  912. /* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
  913. NOTE: that the *url is malloc()ed. */
  914. CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
  915. {
  916. struct connectdata *conn = data->conn;
  917. bool retry = FALSE;
  918. *url = NULL;
  919. /* if we are talking upload, we cannot do the checks below, unless the
  920. protocol is HTTP as when uploading over HTTP we will still get a
  921. response */
  922. if(data->state.upload &&
  923. !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
  924. return CURLE_OK;
  925. if((data->req.bytecount + data->req.headerbytecount == 0) &&
  926. conn->bits.reuse &&
  927. (!data->req.no_body || (conn->handler->protocol & PROTO_FAMILY_HTTP))
  928. #ifndef CURL_DISABLE_RTSP
  929. && (data->set.rtspreq != RTSPREQ_RECEIVE)
  930. #endif
  931. )
  932. /* We got no data, we attempted to reuse a connection. For HTTP this
  933. can be a retry so we try again regardless if we expected a body.
  934. For other protocols we only try again only if we expected a body.
  935. This might happen if the connection was left alive when we were
  936. done using it before, but that was closed when we wanted to read from
  937. it again. Bad luck. Retry the same request on a fresh connect! */
  938. retry = TRUE;
  939. else if(data->state.refused_stream &&
  940. (data->req.bytecount + data->req.headerbytecount == 0) ) {
  941. /* This was sent on a refused stream, safe to rerun. A refused stream
  942. error can typically only happen on HTTP/2 level if the stream is safe
  943. to issue again, but the nghttp2 API can deliver the message to other
  944. streams as well, which is why this adds the check the data counters
  945. too. */
  946. infof(data, "REFUSED_STREAM, retrying a fresh connect");
  947. data->state.refused_stream = FALSE; /* clear again */
  948. retry = TRUE;
  949. }
  950. if(retry) {
  951. #define CONN_MAX_RETRIES 5
  952. if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
  953. failf(data, "Connection died, tried %d times before giving up",
  954. CONN_MAX_RETRIES);
  955. data->state.retrycount = 0;
  956. return CURLE_SEND_ERROR;
  957. }
  958. infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
  959. data->state.retrycount);
  960. *url = strdup(data->state.url);
  961. if(!*url)
  962. return CURLE_OUT_OF_MEMORY;
  963. connclose(conn, "retry"); /* close this connection */
  964. conn->bits.retry = TRUE; /* mark this as a connection we are about
  965. to retry. Marking it this way should
  966. prevent i.e HTTP transfers to return
  967. error just because nothing has been
  968. transferred! */
  969. Curl_creader_set_rewind(data, TRUE);
  970. }
  971. return CURLE_OK;
  972. }
  973. /*
  974. * xfer_setup() is called to setup basic properties for the transfer.
  975. */
  976. static void xfer_setup(
  977. struct Curl_easy *data, /* transfer */
  978. int sockindex, /* socket index to read from or -1 */
  979. curl_off_t size, /* -1 if unknown at this point */
  980. bool getheader, /* TRUE if header parsing is wanted */
  981. int writesockindex, /* socket index to write to, it may very well be
  982. the same we read from. -1 disables */
  983. bool shutdown /* shutdown connection at transfer end. Only
  984. * supported when sending OR receiving. */
  985. )
  986. {
  987. struct SingleRequest *k = &data->req;
  988. struct connectdata *conn = data->conn;
  989. bool want_send = Curl_req_want_send(data);
  990. DEBUGASSERT(conn != NULL);
  991. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  992. DEBUGASSERT((writesockindex <= 1) && (writesockindex >= -1));
  993. DEBUGASSERT(!shutdown || (sockindex == -1) || (writesockindex == -1));
  994. if(conn->bits.multiplex || conn->httpversion >= 20 || want_send) {
  995. /* when multiplexing, the read/write sockets need to be the same! */
  996. conn->sockfd = sockindex == -1 ?
  997. ((writesockindex == -1 ? CURL_SOCKET_BAD : conn->sock[writesockindex])) :
  998. conn->sock[sockindex];
  999. conn->writesockfd = conn->sockfd;
  1000. if(want_send)
  1001. /* special and very HTTP-specific */
  1002. writesockindex = FIRSTSOCKET;
  1003. }
  1004. else {
  1005. conn->sockfd = sockindex == -1 ?
  1006. CURL_SOCKET_BAD : conn->sock[sockindex];
  1007. conn->writesockfd = writesockindex == -1 ?
  1008. CURL_SOCKET_BAD:conn->sock[writesockindex];
  1009. }
  1010. k->getheader = getheader;
  1011. k->size = size;
  1012. k->shutdown = shutdown;
  1013. /* The code sequence below is placed in this function just because all
  1014. necessary input is not always known in do_complete() as this function may
  1015. be called after that */
  1016. if(!k->getheader) {
  1017. k->header = FALSE;
  1018. if(size > 0)
  1019. Curl_pgrsSetDownloadSize(data, size);
  1020. }
  1021. /* we want header and/or body, if neither then do not do this! */
  1022. if(k->getheader || !data->req.no_body) {
  1023. if(sockindex != -1)
  1024. k->keepon |= KEEP_RECV;
  1025. if(writesockindex != -1)
  1026. k->keepon |= KEEP_SEND;
  1027. } /* if(k->getheader || !data->req.no_body) */
  1028. }
  1029. void Curl_xfer_setup_nop(struct Curl_easy *data)
  1030. {
  1031. xfer_setup(data, -1, -1, FALSE, -1, FALSE);
  1032. }
  1033. void Curl_xfer_setup1(struct Curl_easy *data,
  1034. int send_recv,
  1035. curl_off_t recv_size,
  1036. bool getheader)
  1037. {
  1038. int recv_index = (send_recv & CURL_XFER_RECV)? FIRSTSOCKET : -1;
  1039. int send_index = (send_recv & CURL_XFER_SEND)? FIRSTSOCKET : -1;
  1040. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  1041. xfer_setup(data, recv_index, recv_size, getheader, send_index, FALSE);
  1042. }
  1043. void Curl_xfer_setup2(struct Curl_easy *data,
  1044. int send_recv,
  1045. curl_off_t recv_size,
  1046. bool shutdown)
  1047. {
  1048. int recv_index = (send_recv & CURL_XFER_RECV)? SECONDARYSOCKET : -1;
  1049. int send_index = (send_recv & CURL_XFER_SEND)? SECONDARYSOCKET : -1;
  1050. DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
  1051. xfer_setup(data, recv_index, recv_size, FALSE, send_index, shutdown);
  1052. }
  1053. CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
  1054. const char *buf, size_t blen,
  1055. bool is_eos)
  1056. {
  1057. CURLcode result = CURLE_OK;
  1058. if(data->conn->handler->write_resp) {
  1059. /* protocol handlers offering this function take full responsibility
  1060. * for writing all received download data to the client. */
  1061. result = data->conn->handler->write_resp(data, buf, blen, is_eos);
  1062. }
  1063. else {
  1064. /* No special handling by protocol handler, write all received data
  1065. * as BODY to the client. */
  1066. if(blen || is_eos) {
  1067. int cwtype = CLIENTWRITE_BODY;
  1068. if(is_eos)
  1069. cwtype |= CLIENTWRITE_EOS;
  1070. #ifndef CURL_DISABLE_POP3
  1071. if(blen && data->conn->handler->protocol & PROTO_FAMILY_POP3) {
  1072. result = data->req.ignorebody? CURLE_OK :
  1073. Curl_pop3_write(data, buf, blen);
  1074. }
  1075. else
  1076. #endif /* CURL_DISABLE_POP3 */
  1077. result = Curl_client_write(data, cwtype, buf, blen);
  1078. }
  1079. }
  1080. if(!result && is_eos) {
  1081. /* If we wrote the EOS, we are definitely done */
  1082. data->req.eos_written = TRUE;
  1083. data->req.download_done = TRUE;
  1084. }
  1085. CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
  1086. blen, is_eos, result);
  1087. return result;
  1088. }
  1089. CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
  1090. const char *hd0, size_t hdlen, bool is_eos)
  1091. {
  1092. if(data->conn->handler->write_resp_hd) {
  1093. /* protocol handlers offering this function take full responsibility
  1094. * for writing all received download data to the client. */
  1095. return data->conn->handler->write_resp_hd(data, hd0, hdlen, is_eos);
  1096. }
  1097. /* No special handling by protocol handler, write as response bytes */
  1098. return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
  1099. }
  1100. CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
  1101. {
  1102. (void)premature;
  1103. return Curl_cw_out_done(data);
  1104. }
  1105. CURLcode Curl_xfer_send(struct Curl_easy *data,
  1106. const void *buf, size_t blen,
  1107. size_t *pnwritten)
  1108. {
  1109. CURLcode result;
  1110. int sockindex;
  1111. if(!data || !data->conn)
  1112. return CURLE_FAILED_INIT;
  1113. /* FIXME: would like to enable this, but some protocols (MQTT) do not
  1114. * setup the transfer correctly, it seems
  1115. if(data->conn->writesockfd == CURL_SOCKET_BAD) {
  1116. failf(data, "transfer not setup for sending");
  1117. DEBUGASSERT(0);
  1118. return CURLE_SEND_ERROR;
  1119. } */
  1120. sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
  1121. (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
  1122. result = Curl_conn_send(data, sockindex, buf, blen, pnwritten);
  1123. if(result == CURLE_AGAIN) {
  1124. result = CURLE_OK;
  1125. *pnwritten = 0;
  1126. }
  1127. else if(!result && *pnwritten)
  1128. data->info.request_size += *pnwritten;
  1129. return result;
  1130. }
  1131. CURLcode Curl_xfer_recv(struct Curl_easy *data,
  1132. char *buf, size_t blen,
  1133. ssize_t *pnrcvd)
  1134. {
  1135. int sockindex;
  1136. if(!data || !data->conn)
  1137. return CURLE_FAILED_INIT;
  1138. /* FIXME: would like to enable this, but some protocols (MQTT) do not
  1139. * setup the transfer correctly, it seems
  1140. if(data->conn->sockfd == CURL_SOCKET_BAD) {
  1141. failf(data, "transfer not setup for receiving");
  1142. DEBUGASSERT(0);
  1143. return CURLE_RECV_ERROR;
  1144. } */
  1145. sockindex = ((data->conn->sockfd != CURL_SOCKET_BAD) &&
  1146. (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]));
  1147. if(data->set.buffer_size > 0 && (size_t)data->set.buffer_size < blen)
  1148. blen = (size_t)data->set.buffer_size;
  1149. return Curl_conn_recv(data, sockindex, buf, blen, pnrcvd);
  1150. }
  1151. CURLcode Curl_xfer_send_close(struct Curl_easy *data)
  1152. {
  1153. Curl_conn_ev_data_done_send(data);
  1154. return CURLE_OK;
  1155. }
  1156. CURLcode Curl_xfer_send_shutdown(struct Curl_easy *data, bool *done)
  1157. {
  1158. int sockindex;
  1159. if(!data || !data->conn)
  1160. return CURLE_FAILED_INIT;
  1161. if(data->conn->writesockfd == CURL_SOCKET_BAD)
  1162. return CURLE_FAILED_INIT;
  1163. sockindex = (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]);
  1164. return Curl_conn_shutdown(data, sockindex, done);
  1165. }
  1166. bool Curl_xfer_is_blocked(struct Curl_easy *data)
  1167. {
  1168. bool want_send = ((data)->req.keepon & KEEP_SEND);
  1169. bool want_recv = ((data)->req.keepon & KEEP_RECV);
  1170. if(!want_send)
  1171. return (want_recv && Curl_cwriter_is_paused(data));
  1172. else if(!want_recv)
  1173. return (want_send && Curl_creader_is_paused(data));
  1174. else
  1175. return Curl_creader_is_paused(data) && Curl_cwriter_is_paused(data);
  1176. }