sendf.c 14 KB

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