sendf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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_write() is an internal write function that sends data to the
  130. * server. Works with plain sockets, SCP, SSL or kerberos.
  131. *
  132. * If the write would block (CURLE_AGAIN), we return CURLE_OK and
  133. * (*written == 0). Otherwise we return regular CURLcode value.
  134. */
  135. CURLcode Curl_write(struct Curl_easy *data,
  136. curl_socket_t sockfd,
  137. const void *mem,
  138. size_t len,
  139. ssize_t *written)
  140. {
  141. ssize_t bytes_written;
  142. CURLcode result = CURLE_OK;
  143. struct connectdata *conn;
  144. int num;
  145. DEBUGASSERT(data);
  146. DEBUGASSERT(data->conn);
  147. conn = data->conn;
  148. num = (sockfd != CURL_SOCKET_BAD && sockfd == conn->sock[SECONDARYSOCKET]);
  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. len = CURLMIN(len, altsize);
  158. }
  159. }
  160. #endif
  161. bytes_written = conn->send[num](data, num, mem, len, &result);
  162. *written = bytes_written;
  163. if(bytes_written >= 0)
  164. /* we completely ignore the curlcode value when subzero is not returned */
  165. return CURLE_OK;
  166. /* handle CURLE_AGAIN or a send failure */
  167. switch(result) {
  168. case CURLE_AGAIN:
  169. *written = 0;
  170. return CURLE_OK;
  171. case CURLE_OK:
  172. /* general send failure */
  173. return CURLE_SEND_ERROR;
  174. default:
  175. /* we got a specific curlcode, forward it */
  176. return result;
  177. }
  178. }
  179. static CURLcode pausewrite(struct Curl_easy *data,
  180. int type, /* what type of data */
  181. const char *ptr,
  182. size_t len)
  183. {
  184. /* signalled to pause sending on this connection, but since we have data
  185. we want to send we need to dup it to save a copy for when the sending
  186. is again enabled */
  187. struct SingleRequest *k = &data->req;
  188. struct UrlState *s = &data->state;
  189. unsigned int i;
  190. bool newtype = TRUE;
  191. Curl_conn_ev_data_pause(data, TRUE);
  192. if(s->tempcount) {
  193. for(i = 0; i< s->tempcount; i++) {
  194. if(s->tempwrite[i].type == type) {
  195. /* data for this type exists */
  196. newtype = FALSE;
  197. break;
  198. }
  199. }
  200. DEBUGASSERT(i < 3);
  201. if(i >= 3)
  202. /* There are more types to store than what fits: very bad */
  203. return CURLE_OUT_OF_MEMORY;
  204. }
  205. else
  206. i = 0;
  207. if(newtype) {
  208. /* store this information in the state struct for later use */
  209. Curl_dyn_init(&s->tempwrite[i].b, DYN_PAUSE_BUFFER);
  210. s->tempwrite[i].type = type;
  211. s->tempcount++;
  212. }
  213. if(Curl_dyn_addn(&s->tempwrite[i].b, (unsigned char *)ptr, len))
  214. return CURLE_OUT_OF_MEMORY;
  215. /* mark the connection as RECV paused */
  216. k->keepon |= KEEP_RECV_PAUSE;
  217. return CURLE_OK;
  218. }
  219. /* chop_write() writes chunks of data not larger than CURL_MAX_WRITE_SIZE via
  220. * client write callback(s) and takes care of pause requests from the
  221. * callbacks.
  222. */
  223. static CURLcode chop_write(struct Curl_easy *data,
  224. int type,
  225. char *optr,
  226. size_t olen)
  227. {
  228. struct connectdata *conn = data->conn;
  229. curl_write_callback writeheader = NULL;
  230. curl_write_callback writebody = NULL;
  231. char *ptr = optr;
  232. size_t len = olen;
  233. void *writebody_ptr = data->set.out;
  234. if(!len)
  235. return CURLE_OK;
  236. /* If reading is paused, append this data to the already held data for this
  237. type. */
  238. if(data->req.keepon & KEEP_RECV_PAUSE)
  239. return pausewrite(data, type, ptr, len);
  240. /* Determine the callback(s) to use. */
  241. if(type & CLIENTWRITE_BODY) {
  242. #ifdef USE_WEBSOCKETS
  243. if(conn->handler->protocol & (CURLPROTO_WS|CURLPROTO_WSS)) {
  244. writebody = Curl_ws_writecb;
  245. writebody_ptr = data;
  246. }
  247. else
  248. #endif
  249. writebody = data->set.fwrite_func;
  250. }
  251. if((type & CLIENTWRITE_HEADER) &&
  252. (data->set.fwrite_header || data->set.writeheader)) {
  253. /*
  254. * Write headers to the same callback or to the especially setup
  255. * header callback function (added after version 7.7.1).
  256. */
  257. writeheader =
  258. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  259. }
  260. /* Chop data, write chunks. */
  261. while(len) {
  262. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  263. if(writebody) {
  264. size_t wrote;
  265. Curl_set_in_callback(data, true);
  266. wrote = writebody(ptr, 1, chunklen, writebody_ptr);
  267. Curl_set_in_callback(data, false);
  268. if(CURL_WRITEFUNC_PAUSE == wrote) {
  269. if(conn->handler->flags & PROTOPT_NONETWORK) {
  270. /* Protocols that work without network cannot be paused. This is
  271. actually only FILE:// just now, and it can't pause since the
  272. transfer isn't done using the "normal" procedure. */
  273. failf(data, "Write callback asked for PAUSE when not supported");
  274. return CURLE_WRITE_ERROR;
  275. }
  276. return pausewrite(data, type, ptr, len);
  277. }
  278. if(wrote != chunklen) {
  279. failf(data, "Failure writing output to destination");
  280. return CURLE_WRITE_ERROR;
  281. }
  282. }
  283. ptr += chunklen;
  284. len -= chunklen;
  285. }
  286. #ifndef CURL_DISABLE_HTTP
  287. /* HTTP header, but not status-line */
  288. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  289. (type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS) ) {
  290. unsigned char htype = (unsigned char)
  291. (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
  292. (type & CLIENTWRITE_1XX ? CURLH_1XX :
  293. (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
  294. CURLH_HEADER)));
  295. CURLcode result = Curl_headers_push(data, optr, htype);
  296. if(result)
  297. return result;
  298. }
  299. #endif
  300. if(writeheader) {
  301. size_t wrote;
  302. Curl_set_in_callback(data, true);
  303. wrote = writeheader(optr, 1, olen, data->set.writeheader);
  304. Curl_set_in_callback(data, false);
  305. if(CURL_WRITEFUNC_PAUSE == wrote)
  306. /* here we pass in the HEADER bit only since if this was body as well
  307. then it was passed already and clearly that didn't trigger the
  308. pause, so this is saved for later with the HEADER bit only */
  309. return pausewrite(data, CLIENTWRITE_HEADER |
  310. (type & (CLIENTWRITE_STATUS|CLIENTWRITE_CONNECT|
  311. CLIENTWRITE_1XX|CLIENTWRITE_TRAILER)),
  312. optr, olen);
  313. if(wrote != olen) {
  314. failf(data, "Failed writing header");
  315. return CURLE_WRITE_ERROR;
  316. }
  317. }
  318. return CURLE_OK;
  319. }
  320. /* Curl_client_write() sends data to the write callback(s)
  321. The bit pattern defines to what "streams" to write to. Body and/or header.
  322. The defines are in sendf.h of course.
  323. If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
  324. local character encoding. This is a problem and should be changed in
  325. the future to leave the original data alone.
  326. */
  327. CURLcode Curl_client_write(struct Curl_easy *data,
  328. int type,
  329. char *ptr,
  330. size_t len)
  331. {
  332. #if !defined(CURL_DISABLE_FTP) && defined(CURL_DO_LINEEND_CONV)
  333. /* FTP data may need conversion. */
  334. if((type & CLIENTWRITE_BODY) &&
  335. (data->conn->handler->protocol & PROTO_FAMILY_FTP) &&
  336. data->conn->proto.ftpc.transfertype == 'A') {
  337. /* convert end-of-line markers */
  338. len = convert_lineends(data, ptr, len);
  339. }
  340. #endif
  341. return chop_write(data, type, ptr, len);
  342. }
  343. /*
  344. * Internal read-from-socket function. This is meant to deal with plain
  345. * sockets, SSL sockets and kerberos sockets.
  346. *
  347. * Returns a regular CURLcode value.
  348. */
  349. CURLcode Curl_read(struct Curl_easy *data, /* transfer */
  350. curl_socket_t sockfd, /* read from this socket */
  351. char *buf, /* store read data here */
  352. size_t sizerequested, /* max amount to read */
  353. ssize_t *n) /* amount bytes read */
  354. {
  355. CURLcode result = CURLE_RECV_ERROR;
  356. ssize_t nread = 0;
  357. size_t bytesfromsocket = 0;
  358. char *buffertofill = NULL;
  359. struct connectdata *conn = data->conn;
  360. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  361. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  362. us use the correct ssl handle. */
  363. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  364. *n = 0; /* reset amount to zero */
  365. bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size);
  366. buffertofill = buf;
  367. nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result);
  368. if(nread < 0)
  369. goto out;
  370. *n += nread;
  371. result = CURLE_OK;
  372. out:
  373. return result;
  374. }