transfer.c 41 KB

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