transfer.c 40 KB

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