sendf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. const char *ptr,
  199. size_t len)
  200. {
  201. /* signalled to pause sending on this connection, but since we have data
  202. we want to send we need to dup it to save a copy for when the sending
  203. is again enabled */
  204. struct SingleRequest *k = &data->req;
  205. struct UrlState *s = &data->state;
  206. unsigned int i;
  207. bool newtype = TRUE;
  208. Curl_conn_ev_data_pause(data, TRUE);
  209. if(s->tempcount) {
  210. for(i = 0; i< s->tempcount; i++) {
  211. if(s->tempwrite[i].type == type) {
  212. /* data for this type exists */
  213. newtype = FALSE;
  214. break;
  215. }
  216. }
  217. DEBUGASSERT(i < 3);
  218. if(i >= 3)
  219. /* There are more types to store than what fits: very bad */
  220. return CURLE_OUT_OF_MEMORY;
  221. }
  222. else
  223. i = 0;
  224. if(newtype) {
  225. /* store this information in the state struct for later use */
  226. Curl_dyn_init(&s->tempwrite[i].b, DYN_PAUSE_BUFFER);
  227. s->tempwrite[i].type = type;
  228. s->tempcount++;
  229. }
  230. if(Curl_dyn_addn(&s->tempwrite[i].b, (unsigned char *)ptr, len))
  231. return CURLE_OUT_OF_MEMORY;
  232. /* mark the connection as RECV paused */
  233. k->keepon |= KEEP_RECV_PAUSE;
  234. return CURLE_OK;
  235. }
  236. /* chop_write() writes chunks of data not larger than CURL_MAX_WRITE_SIZE via
  237. * client write callback(s) and takes care of pause requests from the
  238. * callbacks.
  239. */
  240. static CURLcode chop_write(struct Curl_easy *data,
  241. int type,
  242. char *optr,
  243. size_t olen)
  244. {
  245. struct connectdata *conn = data->conn;
  246. curl_write_callback writeheader = NULL;
  247. curl_write_callback writebody = NULL;
  248. char *ptr = optr;
  249. size_t len = olen;
  250. void *writebody_ptr = data->set.out;
  251. if(!len)
  252. return CURLE_OK;
  253. /* If reading is paused, append this data to the already held data for this
  254. type. */
  255. if(data->req.keepon & KEEP_RECV_PAUSE)
  256. return pausewrite(data, type, ptr, len);
  257. /* Determine the callback(s) to use. */
  258. if(type & CLIENTWRITE_BODY) {
  259. #ifdef USE_WEBSOCKETS
  260. if(conn->handler->protocol & (CURLPROTO_WS|CURLPROTO_WSS)) {
  261. writebody = Curl_ws_writecb;
  262. writebody_ptr = data;
  263. }
  264. else
  265. #endif
  266. writebody = data->set.fwrite_func;
  267. }
  268. if((type & CLIENTWRITE_HEADER) &&
  269. (data->set.fwrite_header || data->set.writeheader)) {
  270. /*
  271. * Write headers to the same callback or to the especially setup
  272. * header callback function (added after version 7.7.1).
  273. */
  274. writeheader =
  275. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  276. }
  277. /* Chop data, write chunks. */
  278. while(len) {
  279. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  280. if(writebody) {
  281. size_t wrote;
  282. Curl_set_in_callback(data, true);
  283. wrote = writebody(ptr, 1, chunklen, writebody_ptr);
  284. Curl_set_in_callback(data, false);
  285. if(CURL_WRITEFUNC_PAUSE == wrote) {
  286. if(conn->handler->flags & PROTOPT_NONETWORK) {
  287. /* Protocols that work without network cannot be paused. This is
  288. actually only FILE:// just now, and it can't pause since the
  289. transfer isn't done using the "normal" procedure. */
  290. failf(data, "Write callback asked for PAUSE when not supported");
  291. return CURLE_WRITE_ERROR;
  292. }
  293. return pausewrite(data, type, ptr, len);
  294. }
  295. if(wrote != chunklen) {
  296. failf(data, "Failure writing output to destination");
  297. return CURLE_WRITE_ERROR;
  298. }
  299. }
  300. ptr += chunklen;
  301. len -= chunklen;
  302. }
  303. #ifndef CURL_DISABLE_HTTP
  304. /* HTTP header, but not status-line */
  305. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  306. (type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS) ) {
  307. unsigned char htype = (unsigned char)
  308. (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
  309. (type & CLIENTWRITE_1XX ? CURLH_1XX :
  310. (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
  311. CURLH_HEADER)));
  312. CURLcode result = Curl_headers_push(data, optr, htype);
  313. if(result)
  314. return result;
  315. }
  316. #endif
  317. if(writeheader) {
  318. size_t wrote;
  319. Curl_set_in_callback(data, true);
  320. wrote = writeheader(optr, 1, olen, data->set.writeheader);
  321. Curl_set_in_callback(data, false);
  322. if(CURL_WRITEFUNC_PAUSE == wrote)
  323. /* here we pass in the HEADER bit only since if this was body as well
  324. then it was passed already and clearly that didn't trigger the
  325. pause, so this is saved for later with the HEADER bit only */
  326. return pausewrite(data, CLIENTWRITE_HEADER |
  327. (type & (CLIENTWRITE_STATUS|CLIENTWRITE_CONNECT|
  328. CLIENTWRITE_1XX|CLIENTWRITE_TRAILER)),
  329. 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. return chop_write(data, type, ptr, len);
  359. }
  360. /*
  361. * Internal read-from-socket function. This is meant to deal with plain
  362. * sockets, SSL sockets and kerberos sockets.
  363. *
  364. * Returns a regular CURLcode value.
  365. */
  366. CURLcode Curl_read(struct Curl_easy *data, /* transfer */
  367. curl_socket_t sockfd, /* read from this socket */
  368. char *buf, /* store read data here */
  369. size_t sizerequested, /* max amount to read */
  370. ssize_t *n) /* amount bytes read */
  371. {
  372. CURLcode result = CURLE_RECV_ERROR;
  373. ssize_t nread = 0;
  374. size_t bytesfromsocket = 0;
  375. char *buffertofill = NULL;
  376. struct connectdata *conn = data->conn;
  377. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  378. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  379. us use the correct ssl handle. */
  380. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  381. *n = 0; /* reset amount to zero */
  382. bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size);
  383. buffertofill = buf;
  384. nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result);
  385. if(nread < 0)
  386. goto out;
  387. *n += nread;
  388. result = CURLE_OK;
  389. out:
  390. return result;
  391. }