sendf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. struct HTTP *ws = data->req.p.http;
  245. writebody = Curl_ws_writecb;
  246. ws->ws.data = data;
  247. writebody_ptr = ws;
  248. }
  249. else
  250. #endif
  251. writebody = data->set.fwrite_func;
  252. }
  253. if((type & CLIENTWRITE_HEADER) &&
  254. (data->set.fwrite_header || data->set.writeheader)) {
  255. /*
  256. * Write headers to the same callback or to the especially setup
  257. * header callback function (added after version 7.7.1).
  258. */
  259. writeheader =
  260. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  261. }
  262. /* Chop data, write chunks. */
  263. while(len) {
  264. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  265. if(writebody) {
  266. size_t wrote;
  267. Curl_set_in_callback(data, true);
  268. wrote = writebody(ptr, 1, chunklen, writebody_ptr);
  269. Curl_set_in_callback(data, false);
  270. if(CURL_WRITEFUNC_PAUSE == wrote) {
  271. if(conn->handler->flags & PROTOPT_NONETWORK) {
  272. /* Protocols that work without network cannot be paused. This is
  273. actually only FILE:// just now, and it can't pause since the
  274. transfer isn't done using the "normal" procedure. */
  275. failf(data, "Write callback asked for PAUSE when not supported");
  276. return CURLE_WRITE_ERROR;
  277. }
  278. return pausewrite(data, type, ptr, len);
  279. }
  280. if(wrote != chunklen) {
  281. failf(data, "Failure writing output to destination");
  282. return CURLE_WRITE_ERROR;
  283. }
  284. }
  285. ptr += chunklen;
  286. len -= chunklen;
  287. }
  288. #ifndef CURL_DISABLE_HTTP
  289. /* HTTP header, but not status-line */
  290. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  291. (type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS) ) {
  292. unsigned char htype = (unsigned char)
  293. (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
  294. (type & CLIENTWRITE_1XX ? CURLH_1XX :
  295. (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
  296. CURLH_HEADER)));
  297. CURLcode result = Curl_headers_push(data, optr, htype);
  298. if(result)
  299. return result;
  300. }
  301. #endif
  302. if(writeheader) {
  303. size_t wrote;
  304. Curl_set_in_callback(data, true);
  305. wrote = writeheader(optr, 1, olen, data->set.writeheader);
  306. Curl_set_in_callback(data, false);
  307. if(CURL_WRITEFUNC_PAUSE == wrote)
  308. /* here we pass in the HEADER bit only since if this was body as well
  309. then it was passed already and clearly that didn't trigger the
  310. pause, so this is saved for later with the HEADER bit only */
  311. return pausewrite(data, CLIENTWRITE_HEADER |
  312. (type & (CLIENTWRITE_STATUS|CLIENTWRITE_CONNECT|
  313. CLIENTWRITE_1XX|CLIENTWRITE_TRAILER)),
  314. optr, olen);
  315. if(wrote != olen) {
  316. failf(data, "Failed writing header");
  317. return CURLE_WRITE_ERROR;
  318. }
  319. }
  320. return CURLE_OK;
  321. }
  322. /* Curl_client_write() sends data to the write callback(s)
  323. The bit pattern defines to what "streams" to write to. Body and/or header.
  324. The defines are in sendf.h of course.
  325. If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
  326. local character encoding. This is a problem and should be changed in
  327. the future to leave the original data alone.
  328. */
  329. CURLcode Curl_client_write(struct Curl_easy *data,
  330. int type,
  331. char *ptr,
  332. size_t len)
  333. {
  334. #if !defined(CURL_DISABLE_FTP) && defined(CURL_DO_LINEEND_CONV)
  335. /* FTP data may need conversion. */
  336. if((type & CLIENTWRITE_BODY) &&
  337. (data->conn->handler->protocol & PROTO_FAMILY_FTP) &&
  338. data->conn->proto.ftpc.transfertype == 'A') {
  339. /* convert end-of-line markers */
  340. len = convert_lineends(data, ptr, len);
  341. }
  342. #endif
  343. return chop_write(data, type, ptr, len);
  344. }
  345. /*
  346. * Internal read-from-socket function. This is meant to deal with plain
  347. * sockets, SSL sockets and kerberos sockets.
  348. *
  349. * Returns a regular CURLcode value.
  350. */
  351. CURLcode Curl_read(struct Curl_easy *data, /* transfer */
  352. curl_socket_t sockfd, /* read from this socket */
  353. char *buf, /* store read data here */
  354. size_t sizerequested, /* max amount to read */
  355. ssize_t *n) /* amount bytes read */
  356. {
  357. CURLcode result = CURLE_RECV_ERROR;
  358. ssize_t nread = 0;
  359. size_t bytesfromsocket = 0;
  360. char *buffertofill = NULL;
  361. struct connectdata *conn = data->conn;
  362. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  363. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  364. us use the correct ssl handle. */
  365. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  366. *n = 0; /* reset amount to zero */
  367. bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size);
  368. buffertofill = buf;
  369. nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result);
  370. if(nread < 0)
  371. goto out;
  372. *n += nread;
  373. result = CURLE_OK;
  374. out:
  375. /* DEBUGF(infof(data, "Curl_read(handle=%p) -> %d, nread=%ld",
  376. data, result, nread)); */
  377. return result;
  378. }