sendf.c 12 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 "content_encoding.h"
  39. #include "cw-out.h"
  40. #include "vtls/vtls.h"
  41. #include "vssh/ssh.h"
  42. #include "easyif.h"
  43. #include "multiif.h"
  44. #include "strerror.h"
  45. #include "select.h"
  46. #include "strdup.h"
  47. #include "http2.h"
  48. #include "progress.h"
  49. #include "ws.h"
  50. /* The last 3 #include files should be in this order */
  51. #include "curl_printf.h"
  52. #include "curl_memory.h"
  53. #include "memdebug.h"
  54. static CURLcode do_init_stack(struct Curl_easy *data);
  55. /* Curl_client_write() sends data to the write callback(s)
  56. The bit pattern defines to what "streams" to write to. Body and/or header.
  57. The defines are in sendf.h of course.
  58. */
  59. CURLcode Curl_client_write(struct Curl_easy *data,
  60. int type, const char *buf, size_t blen)
  61. {
  62. CURLcode result;
  63. /* it is one of those, at least */
  64. DEBUGASSERT(type & (CLIENTWRITE_BODY|CLIENTWRITE_HEADER|CLIENTWRITE_INFO));
  65. /* BODY is only BODY (with optional EOS) */
  66. DEBUGASSERT(!(type & CLIENTWRITE_BODY) ||
  67. ((type & ~(CLIENTWRITE_BODY|CLIENTWRITE_EOS)) == 0));
  68. /* INFO is only INFO (with optional EOS) */
  69. DEBUGASSERT(!(type & CLIENTWRITE_INFO) ||
  70. ((type & ~(CLIENTWRITE_INFO|CLIENTWRITE_EOS)) == 0));
  71. if(!data->req.writer_stack) {
  72. result = do_init_stack(data);
  73. if(result)
  74. return result;
  75. DEBUGASSERT(data->req.writer_stack);
  76. }
  77. return Curl_cwriter_write(data, data->req.writer_stack, type, buf, blen);
  78. }
  79. void Curl_cw_reset(struct Curl_easy *data)
  80. {
  81. struct Curl_cwriter *writer = data->req.writer_stack;
  82. while(writer) {
  83. data->req.writer_stack = writer->next;
  84. writer->cwt->do_close(data, writer);
  85. free(writer);
  86. writer = data->req.writer_stack;
  87. }
  88. data->req.bytecount = 0;
  89. data->req.headerline = 0;
  90. }
  91. /* Write data using an unencoding writer stack. "nbytes" is not
  92. allowed to be 0. */
  93. CURLcode Curl_cwriter_write(struct Curl_easy *data,
  94. struct Curl_cwriter *writer, int type,
  95. const char *buf, size_t nbytes)
  96. {
  97. if(!writer)
  98. return CURLE_WRITE_ERROR;
  99. return writer->cwt->do_write(data, writer, type, buf, nbytes);
  100. }
  101. CURLcode Curl_cwriter_def_init(struct Curl_easy *data,
  102. struct Curl_cwriter *writer)
  103. {
  104. (void)data;
  105. (void)writer;
  106. return CURLE_OK;
  107. }
  108. CURLcode Curl_cwriter_def_write(struct Curl_easy *data,
  109. struct Curl_cwriter *writer, int type,
  110. const char *buf, size_t nbytes)
  111. {
  112. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  113. }
  114. void Curl_cwriter_def_close(struct Curl_easy *data,
  115. struct Curl_cwriter *writer)
  116. {
  117. (void) data;
  118. (void) writer;
  119. }
  120. static size_t get_max_body_write_len(struct Curl_easy *data, curl_off_t limit)
  121. {
  122. if(limit != -1) {
  123. /* How much more are we allowed to write? */
  124. curl_off_t remain_diff;
  125. remain_diff = limit - data->req.bytecount;
  126. if(remain_diff < 0) {
  127. /* already written too much! */
  128. return 0;
  129. }
  130. #if SIZEOF_CURL_OFF_T > SIZEOF_SIZE_T
  131. else if(remain_diff > SSIZE_T_MAX) {
  132. return SIZE_T_MAX;
  133. }
  134. #endif
  135. else {
  136. return (size_t)remain_diff;
  137. }
  138. }
  139. return SIZE_T_MAX;
  140. }
  141. /* Download client writer in phase CURL_CW_PROTOCOL that
  142. * sees the "real" download body data. */
  143. static CURLcode cw_download_write(struct Curl_easy *data,
  144. struct Curl_cwriter *writer, int type,
  145. const char *buf, size_t nbytes)
  146. {
  147. CURLcode result;
  148. size_t nwrite, excess_len = 0;
  149. if(!(type & CLIENTWRITE_BODY)) {
  150. if((type & CLIENTWRITE_CONNECT) && data->set.suppress_connect_headers)
  151. return CURLE_OK;
  152. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  153. }
  154. if(!data->req.bytecount) {
  155. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  156. if(data->req.exp100 > EXP100_SEND_DATA)
  157. /* set time stamp to compare with when waiting for the 100 */
  158. data->req.start100 = Curl_now();
  159. }
  160. /* Here, we deal with REAL BODY bytes. All filtering and transfer
  161. * encodings have been applied and only the true content, e.g. BODY,
  162. * bytes are passed here.
  163. * This allows us to check sizes, update stats, etc. independent
  164. * from the protocol in play. */
  165. if(data->req.no_body && nbytes > 0) {
  166. /* BODY arrives although we want none, bail out */
  167. streamclose(data->conn, "ignoring body");
  168. DEBUGF(infof(data, "did not want a BODY, but seeing %zu bytes",
  169. nbytes));
  170. data->req.download_done = TRUE;
  171. if(data->info.header_size)
  172. /* if headers have been received, this is fine */
  173. return CURLE_OK;
  174. return CURLE_WEIRD_SERVER_REPLY;
  175. }
  176. /* Determine if we see any bytes in excess to what is allowed.
  177. * We write the allowed bytes and handle excess further below.
  178. * This gives deterministic BODY writes on varying buffer receive
  179. * lengths. */
  180. nwrite = nbytes;
  181. if(-1 != data->req.maxdownload) {
  182. size_t wmax = get_max_body_write_len(data, data->req.maxdownload);
  183. if(nwrite > wmax) {
  184. excess_len = nbytes - wmax;
  185. nwrite = wmax;
  186. }
  187. if(nwrite == wmax) {
  188. data->req.download_done = TRUE;
  189. }
  190. }
  191. /* Error on too large filesize is handled below, after writing
  192. * the permitted bytes */
  193. if(data->set.max_filesize) {
  194. size_t wmax = get_max_body_write_len(data, data->set.max_filesize);
  195. if(nwrite > wmax) {
  196. nwrite = wmax;
  197. }
  198. }
  199. if(!data->req.ignorebody && (nwrite || (type & CLIENTWRITE_EOS))) {
  200. result = Curl_cwriter_write(data, writer->next, type, buf, nwrite);
  201. if(result)
  202. return result;
  203. }
  204. /* Update stats, write and report progress */
  205. data->req.bytecount += nwrite;
  206. ++data->req.bodywrites;
  207. result = Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
  208. if(result)
  209. return result;
  210. if(excess_len) {
  211. if(!data->req.ignorebody) {
  212. infof(data,
  213. "Excess found writing body:"
  214. " excess = %zu"
  215. ", size = %" CURL_FORMAT_CURL_OFF_T
  216. ", maxdownload = %" CURL_FORMAT_CURL_OFF_T
  217. ", bytecount = %" CURL_FORMAT_CURL_OFF_T,
  218. excess_len, data->req.size, data->req.maxdownload,
  219. data->req.bytecount);
  220. connclose(data->conn, "excess found in a read");
  221. }
  222. }
  223. else if(nwrite < nbytes) {
  224. failf(data, "Exceeded the maximum allowed file size "
  225. "(%" CURL_FORMAT_CURL_OFF_T ") with %"
  226. CURL_FORMAT_CURL_OFF_T " bytes",
  227. data->set.max_filesize, data->req.bytecount);
  228. return CURLE_FILESIZE_EXCEEDED;
  229. }
  230. return CURLE_OK;
  231. }
  232. static const struct Curl_cwtype cw_download = {
  233. "download",
  234. NULL,
  235. Curl_cwriter_def_init,
  236. cw_download_write,
  237. Curl_cwriter_def_close,
  238. sizeof(struct Curl_cwriter)
  239. };
  240. /* RAW client writer in phase CURL_CW_RAW that
  241. * enabled tracing of raw data. */
  242. static CURLcode cw_raw_write(struct Curl_easy *data,
  243. struct Curl_cwriter *writer, int type,
  244. const char *buf, size_t nbytes)
  245. {
  246. if(type & CLIENTWRITE_BODY && data->set.verbose && !data->req.ignorebody) {
  247. Curl_debug(data, CURLINFO_DATA_IN, (char *)buf, nbytes);
  248. }
  249. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  250. }
  251. static const struct Curl_cwtype cw_raw = {
  252. "raw",
  253. NULL,
  254. Curl_cwriter_def_init,
  255. cw_raw_write,
  256. Curl_cwriter_def_close,
  257. sizeof(struct Curl_cwriter)
  258. };
  259. /* Create an unencoding writer stage using the given handler. */
  260. CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
  261. struct Curl_easy *data,
  262. const struct Curl_cwtype *cwt,
  263. Curl_cwriter_phase phase)
  264. {
  265. struct Curl_cwriter *writer;
  266. CURLcode result = CURLE_OUT_OF_MEMORY;
  267. DEBUGASSERT(cwt->cwriter_size >= sizeof(struct Curl_cwriter));
  268. writer = (struct Curl_cwriter *) calloc(1, cwt->cwriter_size);
  269. if(!writer)
  270. goto out;
  271. writer->cwt = cwt;
  272. writer->phase = phase;
  273. result = cwt->do_init(data, writer);
  274. out:
  275. *pwriter = result? NULL : writer;
  276. if(result)
  277. free(writer);
  278. return result;
  279. }
  280. void Curl_cwriter_free(struct Curl_easy *data,
  281. struct Curl_cwriter *writer)
  282. {
  283. if(writer) {
  284. writer->cwt->do_close(data, writer);
  285. free(writer);
  286. }
  287. }
  288. size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase)
  289. {
  290. struct Curl_cwriter *w;
  291. size_t n = 0;
  292. for(w = data->req.writer_stack; w; w = w->next) {
  293. if(w->phase == phase)
  294. ++n;
  295. }
  296. return n;
  297. }
  298. static CURLcode do_init_stack(struct Curl_easy *data)
  299. {
  300. struct Curl_cwriter *writer;
  301. CURLcode result;
  302. DEBUGASSERT(!data->req.writer_stack);
  303. result = Curl_cwriter_create(&data->req.writer_stack,
  304. data, &Curl_cwt_out, CURL_CW_CLIENT);
  305. if(result)
  306. return result;
  307. result = Curl_cwriter_create(&writer, data, &cw_download, CURL_CW_PROTOCOL);
  308. if(result)
  309. return result;
  310. result = Curl_cwriter_add(data, writer);
  311. if(result) {
  312. Curl_cwriter_free(data, writer);
  313. }
  314. result = Curl_cwriter_create(&writer, data, &cw_raw, CURL_CW_RAW);
  315. if(result)
  316. return result;
  317. result = Curl_cwriter_add(data, writer);
  318. if(result) {
  319. Curl_cwriter_free(data, writer);
  320. }
  321. return result;
  322. }
  323. CURLcode Curl_cwriter_add(struct Curl_easy *data,
  324. struct Curl_cwriter *writer)
  325. {
  326. CURLcode result;
  327. struct Curl_cwriter **anchor = &data->req.writer_stack;
  328. if(!*anchor) {
  329. result = do_init_stack(data);
  330. if(result)
  331. return result;
  332. }
  333. /* Insert the writer as first in its phase.
  334. * Skip existing writers of lower phases. */
  335. while(*anchor && (*anchor)->phase < writer->phase)
  336. anchor = &((*anchor)->next);
  337. writer->next = *anchor;
  338. *anchor = writer;
  339. return CURLE_OK;
  340. }
  341. struct Curl_cwriter *Curl_cwriter_get_by_name(struct Curl_easy *data,
  342. const char *name)
  343. {
  344. struct Curl_cwriter *writer;
  345. for(writer = data->req.writer_stack; writer; writer = writer->next) {
  346. if(!strcmp(name, writer->cwt->name))
  347. return writer;
  348. }
  349. return NULL;
  350. }
  351. struct Curl_cwriter *Curl_cwriter_get_by_type(struct Curl_easy *data,
  352. const struct Curl_cwtype *cwt)
  353. {
  354. struct Curl_cwriter *writer;
  355. for(writer = data->req.writer_stack; writer; writer = writer->next) {
  356. if(writer->cwt == cwt)
  357. return writer;
  358. }
  359. return NULL;
  360. }
  361. void Curl_cwriter_remove_by_name(struct Curl_easy *data,
  362. const char *name)
  363. {
  364. struct Curl_cwriter **anchor = &data->req.writer_stack;
  365. while(*anchor) {
  366. if(!strcmp(name, (*anchor)->cwt->name)) {
  367. struct Curl_cwriter *w = (*anchor);
  368. *anchor = w->next;
  369. Curl_cwriter_free(data, w);
  370. continue;
  371. }
  372. anchor = &((*anchor)->next);
  373. }
  374. }