sendf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. #ifdef HAVE_LINUX_TCP_H
  29. #include <linux/tcp.h>
  30. #elif defined(HAVE_NETINET_TCP_H)
  31. #include <netinet/tcp.h>
  32. #endif
  33. #include <curl/curl.h>
  34. #include "urldata.h"
  35. #include "sendf.h"
  36. #include "cfilters.h"
  37. #include "connect.h"
  38. #include "content_encoding.h"
  39. #include "vtls/vtls.h"
  40. #include "vssh/ssh.h"
  41. #include "easyif.h"
  42. #include "multiif.h"
  43. #include "strerror.h"
  44. #include "select.h"
  45. #include "strdup.h"
  46. #include "http2.h"
  47. #include "headers.h"
  48. #include "ws.h"
  49. /* The last 3 #include files should be in this order */
  50. #include "curl_printf.h"
  51. #include "curl_memory.h"
  52. #include "memdebug.h"
  53. #if defined(CURL_DO_LINEEND_CONV) && !defined(CURL_DISABLE_FTP)
  54. /*
  55. * convert_lineends() changes CRLF (\r\n) end-of-line markers to a single LF
  56. * (\n), with special processing for CRLF sequences that are split between two
  57. * blocks of data. Remaining, bare CRs are changed to LFs. The possibly new
  58. * size of the data is returned.
  59. */
  60. static size_t convert_lineends(struct Curl_easy *data,
  61. char *startPtr, size_t size)
  62. {
  63. char *inPtr, *outPtr;
  64. /* sanity check */
  65. if(!startPtr || (size < 1)) {
  66. return size;
  67. }
  68. if(data->state.prev_block_had_trailing_cr) {
  69. /* The previous block of incoming data
  70. had a trailing CR, which was turned into a LF. */
  71. if(*startPtr == '\n') {
  72. /* This block of incoming data starts with the
  73. previous block's LF so get rid of it */
  74. memmove(startPtr, startPtr + 1, size-1);
  75. size--;
  76. /* and it wasn't a bare CR but a CRLF conversion instead */
  77. data->state.crlf_conversions++;
  78. }
  79. data->state.prev_block_had_trailing_cr = FALSE; /* reset the flag */
  80. }
  81. /* find 1st CR, if any */
  82. inPtr = outPtr = memchr(startPtr, '\r', size);
  83. if(inPtr) {
  84. /* at least one CR, now look for CRLF */
  85. while(inPtr < (startPtr + size-1)) {
  86. /* note that it's size-1, so we'll never look past the last byte */
  87. if(memcmp(inPtr, "\r\n", 2) == 0) {
  88. /* CRLF found, bump past the CR and copy the NL */
  89. inPtr++;
  90. *outPtr = *inPtr;
  91. /* keep track of how many CRLFs we converted */
  92. data->state.crlf_conversions++;
  93. }
  94. else {
  95. if(*inPtr == '\r') {
  96. /* lone CR, move LF instead */
  97. *outPtr = '\n';
  98. }
  99. else {
  100. /* not a CRLF nor a CR, just copy whatever it is */
  101. *outPtr = *inPtr;
  102. }
  103. }
  104. outPtr++;
  105. inPtr++;
  106. } /* end of while loop */
  107. if(inPtr < startPtr + size) {
  108. /* handle last byte */
  109. if(*inPtr == '\r') {
  110. /* deal with a CR at the end of the buffer */
  111. *outPtr = '\n'; /* copy a NL instead */
  112. /* note that a CRLF might be split across two blocks */
  113. data->state.prev_block_had_trailing_cr = TRUE;
  114. }
  115. else {
  116. /* copy last byte */
  117. *outPtr = *inPtr;
  118. }
  119. outPtr++;
  120. }
  121. if(outPtr < startPtr + size)
  122. /* tidy up by null terminating the now shorter data */
  123. *outPtr = '\0';
  124. return (outPtr - startPtr);
  125. }
  126. return size;
  127. }
  128. #endif /* CURL_DO_LINEEND_CONV && !CURL_DISABLE_FTP */
  129. /*
  130. * Curl_nwrite() is an internal write function that sends data to the
  131. * server. Works with a socket index for the connection.
  132. *
  133. * If the write would block (CURLE_AGAIN), it returns CURLE_OK and
  134. * (*nwritten == 0). Otherwise we return regular CURLcode value.
  135. */
  136. CURLcode Curl_nwrite(struct Curl_easy *data,
  137. int sockindex,
  138. const void *buf,
  139. size_t blen,
  140. ssize_t *pnwritten)
  141. {
  142. ssize_t nwritten;
  143. CURLcode result = CURLE_OK;
  144. struct connectdata *conn;
  145. DEBUGASSERT(sockindex >= 0 && sockindex < 2);
  146. DEBUGASSERT(pnwritten);
  147. DEBUGASSERT(data);
  148. DEBUGASSERT(data->conn);
  149. conn = data->conn;
  150. #ifdef CURLDEBUG
  151. {
  152. /* Allow debug builds to override this logic to force short sends
  153. */
  154. char *p = getenv("CURL_SMALLSENDS");
  155. if(p) {
  156. size_t altsize = (size_t)strtoul(p, NULL, 10);
  157. if(altsize)
  158. blen = CURLMIN(blen, altsize);
  159. }
  160. }
  161. #endif
  162. nwritten = conn->send[sockindex](data, sockindex, buf, blen, &result);
  163. if(result == CURLE_AGAIN) {
  164. nwritten = 0;
  165. result = CURLE_OK;
  166. }
  167. else if(result) {
  168. nwritten = -1; /* make sure */
  169. }
  170. else {
  171. DEBUGASSERT(nwritten >= 0);
  172. }
  173. *pnwritten = nwritten;
  174. return result;
  175. }
  176. /*
  177. * Curl_write() is an internal write function that sends data to the
  178. * server. Works with plain sockets, SCP, SSL or kerberos.
  179. *
  180. * If the write would block (CURLE_AGAIN), we return CURLE_OK and
  181. * (*written == 0). Otherwise we return regular CURLcode value.
  182. */
  183. CURLcode Curl_write(struct Curl_easy *data,
  184. curl_socket_t sockfd,
  185. const void *mem,
  186. size_t len,
  187. ssize_t *written)
  188. {
  189. struct connectdata *conn;
  190. int num;
  191. DEBUGASSERT(data);
  192. DEBUGASSERT(data->conn);
  193. conn = data->conn;
  194. num = (sockfd != CURL_SOCKET_BAD && sockfd == conn->sock[SECONDARYSOCKET]);
  195. return Curl_nwrite(data, num, mem, len, written);
  196. }
  197. static CURLcode pausewrite(struct Curl_easy *data,
  198. int type, /* what type of data */
  199. bool paused_body,
  200. const char *ptr,
  201. size_t len)
  202. {
  203. /* signalled to pause sending on this connection, but since we have data
  204. we want to send we need to dup it to save a copy for when the sending
  205. is again enabled */
  206. struct SingleRequest *k = &data->req;
  207. struct UrlState *s = &data->state;
  208. unsigned int i;
  209. bool newtype = TRUE;
  210. Curl_conn_ev_data_pause(data, TRUE);
  211. if(s->tempcount) {
  212. for(i = 0; i< s->tempcount; i++) {
  213. if(s->tempwrite[i].type == type &&
  214. !!s->tempwrite[i].paused_body == !!paused_body) {
  215. /* data for this type exists */
  216. newtype = FALSE;
  217. break;
  218. }
  219. }
  220. DEBUGASSERT(i < 3);
  221. if(i >= 3)
  222. /* There are more types to store than what fits: very bad */
  223. return CURLE_OUT_OF_MEMORY;
  224. }
  225. else
  226. i = 0;
  227. if(newtype) {
  228. /* store this information in the state struct for later use */
  229. Curl_dyn_init(&s->tempwrite[i].b, DYN_PAUSE_BUFFER);
  230. s->tempwrite[i].type = type;
  231. s->tempwrite[i].paused_body = paused_body;
  232. s->tempcount++;
  233. }
  234. if(Curl_dyn_addn(&s->tempwrite[i].b, (unsigned char *)ptr, len))
  235. return CURLE_OUT_OF_MEMORY;
  236. /* mark the connection as RECV paused */
  237. k->keepon |= KEEP_RECV_PAUSE;
  238. return CURLE_OK;
  239. }
  240. /* chop_write() writes chunks of data not larger than CURL_MAX_WRITE_SIZE via
  241. * client write callback(s) and takes care of pause requests from the
  242. * callbacks.
  243. */
  244. static CURLcode chop_write(struct Curl_easy *data,
  245. int type,
  246. bool skip_body_write,
  247. char *optr,
  248. size_t olen)
  249. {
  250. struct connectdata *conn = data->conn;
  251. curl_write_callback writeheader = NULL;
  252. curl_write_callback writebody = NULL;
  253. char *ptr = optr;
  254. size_t len = olen;
  255. void *writebody_ptr = data->set.out;
  256. if(!len)
  257. return CURLE_OK;
  258. /* If reading is paused, append this data to the already held data for this
  259. type. */
  260. if(data->req.keepon & KEEP_RECV_PAUSE)
  261. return pausewrite(data, type, !skip_body_write, ptr, len);
  262. /* Determine the callback(s) to use. */
  263. if(!skip_body_write &&
  264. ((type & CLIENTWRITE_BODY) ||
  265. ((type & CLIENTWRITE_HEADER) && data->set.include_header))) {
  266. #ifdef USE_WEBSOCKETS
  267. if(conn->handler->protocol & (CURLPROTO_WS|CURLPROTO_WSS)) {
  268. writebody = Curl_ws_writecb;
  269. writebody_ptr = data;
  270. }
  271. else
  272. #endif
  273. writebody = data->set.fwrite_func;
  274. }
  275. if((type & (CLIENTWRITE_HEADER|CLIENTWRITE_INFO)) &&
  276. (data->set.fwrite_header || data->set.writeheader)) {
  277. /*
  278. * Write headers to the same callback or to the especially setup
  279. * header callback function (added after version 7.7.1).
  280. */
  281. writeheader =
  282. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  283. }
  284. /* Chop data, write chunks. */
  285. while(len) {
  286. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  287. if(writebody) {
  288. size_t wrote;
  289. Curl_set_in_callback(data, true);
  290. wrote = writebody(ptr, 1, chunklen, writebody_ptr);
  291. Curl_set_in_callback(data, false);
  292. if(CURL_WRITEFUNC_PAUSE == wrote) {
  293. if(conn->handler->flags & PROTOPT_NONETWORK) {
  294. /* Protocols that work without network cannot be paused. This is
  295. actually only FILE:// just now, and it can't pause since the
  296. transfer isn't done using the "normal" procedure. */
  297. failf(data, "Write callback asked for PAUSE when not supported");
  298. return CURLE_WRITE_ERROR;
  299. }
  300. return pausewrite(data, type, TRUE, ptr, len);
  301. }
  302. if(wrote != chunklen) {
  303. failf(data, "Failure writing output to destination");
  304. return CURLE_WRITE_ERROR;
  305. }
  306. }
  307. ptr += chunklen;
  308. len -= chunklen;
  309. }
  310. #ifndef CURL_DISABLE_HTTP
  311. /* HTTP header, but not status-line */
  312. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  313. (type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS) ) {
  314. unsigned char htype = (unsigned char)
  315. (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
  316. (type & CLIENTWRITE_1XX ? CURLH_1XX :
  317. (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
  318. CURLH_HEADER)));
  319. CURLcode result = Curl_headers_push(data, optr, htype);
  320. if(result)
  321. return result;
  322. }
  323. #endif
  324. if(writeheader) {
  325. size_t wrote;
  326. Curl_set_in_callback(data, true);
  327. wrote = writeheader(optr, 1, olen, data->set.writeheader);
  328. Curl_set_in_callback(data, false);
  329. if(CURL_WRITEFUNC_PAUSE == wrote)
  330. return pausewrite(data, type, FALSE, optr, olen);
  331. if(wrote != olen) {
  332. failf(data, "Failed writing header");
  333. return CURLE_WRITE_ERROR;
  334. }
  335. }
  336. return CURLE_OK;
  337. }
  338. /* Curl_client_write() sends data to the write callback(s)
  339. The bit pattern defines to what "streams" to write to. Body and/or header.
  340. The defines are in sendf.h of course.
  341. If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
  342. local character encoding. This is a problem and should be changed in
  343. the future to leave the original data alone.
  344. */
  345. CURLcode Curl_client_write(struct Curl_easy *data,
  346. int type,
  347. char *ptr,
  348. size_t len)
  349. {
  350. #if !defined(CURL_DISABLE_FTP) && defined(CURL_DO_LINEEND_CONV)
  351. /* FTP data may need conversion. */
  352. if((type & CLIENTWRITE_BODY) &&
  353. (data->conn->handler->protocol & PROTO_FAMILY_FTP) &&
  354. data->conn->proto.ftpc.transfertype == 'A') {
  355. /* convert end-of-line markers */
  356. len = convert_lineends(data, ptr, len);
  357. }
  358. #endif
  359. /* it is one of those, at least */
  360. DEBUGASSERT(type & (CLIENTWRITE_BODY|CLIENTWRITE_HEADER|CLIENTWRITE_INFO));
  361. /* BODY is only BODY */
  362. DEBUGASSERT(!(type & CLIENTWRITE_BODY) || (type == CLIENTWRITE_BODY));
  363. /* INFO is only INFO */
  364. DEBUGASSERT(!(type & CLIENTWRITE_INFO) || (type == CLIENTWRITE_INFO));
  365. if(type == CLIENTWRITE_BODY) {
  366. if(data->req.ignorebody)
  367. return CURLE_OK;
  368. if(data->req.writer_stack && !data->set.http_ce_skip)
  369. return Curl_unencode_write(data, data->req.writer_stack, ptr, len);
  370. }
  371. return chop_write(data, type, FALSE, ptr, len);
  372. }
  373. CURLcode Curl_client_unpause(struct Curl_easy *data)
  374. {
  375. CURLcode result = CURLE_OK;
  376. if(data->state.tempcount) {
  377. /* there are buffers for sending that can be delivered as the receive
  378. pausing is lifted! */
  379. unsigned int i;
  380. unsigned int count = data->state.tempcount;
  381. struct tempbuf writebuf[3]; /* there can only be three */
  382. /* copy the structs to allow for immediate re-pausing */
  383. for(i = 0; i < data->state.tempcount; i++) {
  384. writebuf[i] = data->state.tempwrite[i];
  385. Curl_dyn_init(&data->state.tempwrite[i].b, DYN_PAUSE_BUFFER);
  386. }
  387. data->state.tempcount = 0;
  388. for(i = 0; i < count; i++) {
  389. /* even if one function returns error, this loops through and frees
  390. all buffers */
  391. if(!result)
  392. result = chop_write(data, writebuf[i].type,
  393. !writebuf[i].paused_body,
  394. Curl_dyn_ptr(&writebuf[i].b),
  395. Curl_dyn_len(&writebuf[i].b));
  396. Curl_dyn_free(&writebuf[i].b);
  397. }
  398. }
  399. return result;
  400. }
  401. void Curl_client_cleanup(struct Curl_easy *data)
  402. {
  403. struct contenc_writer *writer = data->req.writer_stack;
  404. size_t i;
  405. while(writer) {
  406. data->req.writer_stack = writer->downstream;
  407. writer->handler->close_writer(data, writer);
  408. free(writer);
  409. writer = data->req.writer_stack;
  410. }
  411. for(i = 0; i < data->state.tempcount; i++) {
  412. Curl_dyn_free(&data->state.tempwrite[i].b);
  413. }
  414. data->state.tempcount = 0;
  415. }
  416. /* Real client writer: no downstream. */
  417. static CURLcode client_cew_init(struct Curl_easy *data,
  418. struct contenc_writer *writer)
  419. {
  420. (void) data;
  421. (void)writer;
  422. return CURLE_OK;
  423. }
  424. static CURLcode client_cew_write(struct Curl_easy *data,
  425. struct contenc_writer *writer,
  426. const char *buf, size_t nbytes)
  427. {
  428. (void)writer;
  429. if(!nbytes || data->req.ignorebody)
  430. return CURLE_OK;
  431. return chop_write(data, CLIENTWRITE_BODY, FALSE, (char *)buf, nbytes);
  432. }
  433. static void client_cew_close(struct Curl_easy *data,
  434. struct contenc_writer *writer)
  435. {
  436. (void) data;
  437. (void) writer;
  438. }
  439. static const struct content_encoding client_cew = {
  440. NULL,
  441. NULL,
  442. client_cew_init,
  443. client_cew_write,
  444. client_cew_close,
  445. sizeof(struct contenc_writer)
  446. };
  447. /* Create an unencoding writer stage using the given handler. */
  448. CURLcode Curl_client_create_writer(struct contenc_writer **pwriter,
  449. struct Curl_easy *data,
  450. const struct content_encoding *ce_handler,
  451. int order)
  452. {
  453. struct contenc_writer *writer;
  454. CURLcode result = CURLE_OUT_OF_MEMORY;
  455. DEBUGASSERT(ce_handler->writersize >= sizeof(struct contenc_writer));
  456. writer = (struct contenc_writer *) calloc(1, ce_handler->writersize);
  457. if(!writer)
  458. goto out;
  459. writer->handler = ce_handler;
  460. writer->order = order;
  461. result = ce_handler->init_writer(data, writer);
  462. out:
  463. *pwriter = result? NULL : writer;
  464. if(result)
  465. free(writer);
  466. return result;
  467. }
  468. void Curl_client_free_writer(struct Curl_easy *data,
  469. struct contenc_writer *writer)
  470. {
  471. if(writer) {
  472. writer->handler->close_writer(data, writer);
  473. free(writer);
  474. }
  475. }
  476. /* allow no more than 5 "chained" compression steps */
  477. #define MAX_ENCODE_STACK 5
  478. static CURLcode init_writer_stack(struct Curl_easy *data)
  479. {
  480. DEBUGASSERT(!data->req.writer_stack);
  481. return Curl_client_create_writer(&data->req.writer_stack,
  482. data, &client_cew, 0);
  483. }
  484. CURLcode Curl_client_add_writer(struct Curl_easy *data,
  485. struct contenc_writer *writer)
  486. {
  487. CURLcode result;
  488. if(!data->req.writer_stack) {
  489. result = init_writer_stack(data);
  490. if(result)
  491. return result;
  492. }
  493. if(data->req.writer_stack_depth++ >= MAX_ENCODE_STACK) {
  494. failf(data, "Reject response due to more than %u content encodings",
  495. MAX_ENCODE_STACK);
  496. return CURLE_BAD_CONTENT_ENCODING;
  497. }
  498. /* Stack the unencoding stage. */
  499. if(writer->order >= data->req.writer_stack->order) {
  500. writer->downstream = data->req.writer_stack;
  501. data->req.writer_stack = writer;
  502. }
  503. else {
  504. struct contenc_writer *w = data->req.writer_stack;
  505. while(w->downstream && writer->order < w->downstream->order)
  506. w = w->downstream;
  507. writer->downstream = w->downstream;
  508. w->downstream = writer;
  509. }
  510. return CURLE_OK;
  511. }
  512. /*
  513. * Internal read-from-socket function. This is meant to deal with plain
  514. * sockets, SSL sockets and kerberos sockets.
  515. *
  516. * Returns a regular CURLcode value.
  517. */
  518. CURLcode Curl_read(struct Curl_easy *data, /* transfer */
  519. curl_socket_t sockfd, /* read from this socket */
  520. char *buf, /* store read data here */
  521. size_t sizerequested, /* max amount to read */
  522. ssize_t *n) /* amount bytes read */
  523. {
  524. CURLcode result = CURLE_RECV_ERROR;
  525. ssize_t nread = 0;
  526. size_t bytesfromsocket = 0;
  527. char *buffertofill = NULL;
  528. struct connectdata *conn = data->conn;
  529. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  530. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  531. us use the correct ssl handle. */
  532. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  533. *n = 0; /* reset amount to zero */
  534. bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size);
  535. buffertofill = buf;
  536. nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result);
  537. if(nread < 0)
  538. goto out;
  539. *n += nread;
  540. result = CURLE_OK;
  541. out:
  542. return result;
  543. }