request.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. #include "urldata.h"
  26. #include "cfilters.h"
  27. #include "dynbuf.h"
  28. #include "doh.h"
  29. #include "multiif.h"
  30. #include "progress.h"
  31. #include "request.h"
  32. #include "sendf.h"
  33. #include "transfer.h"
  34. #include "url.h"
  35. /* The last 3 #include files should be in this order */
  36. #include "curl_printf.h"
  37. #include "curl_memory.h"
  38. #include "memdebug.h"
  39. void Curl_req_init(struct SingleRequest *req)
  40. {
  41. memset(req, 0, sizeof(*req));
  42. }
  43. CURLcode Curl_req_soft_reset(struct SingleRequest *req,
  44. struct Curl_easy *data)
  45. {
  46. CURLcode result;
  47. req->done = FALSE;
  48. req->upload_done = FALSE;
  49. req->upload_aborted = FALSE;
  50. req->download_done = FALSE;
  51. req->eos_written = FALSE;
  52. req->eos_read = FALSE;
  53. req->eos_sent = FALSE;
  54. req->ignorebody = FALSE;
  55. req->shutdown = FALSE;
  56. req->bytecount = 0;
  57. req->writebytecount = 0;
  58. req->header = TRUE; /* assume header */
  59. req->headerline = 0;
  60. req->headerbytecount = 0;
  61. req->allheadercount = 0;
  62. req->deductheadercount = 0;
  63. result = Curl_client_start(data);
  64. if(result)
  65. return result;
  66. if(!req->sendbuf_init) {
  67. Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
  68. BUFQ_OPT_SOFT_LIMIT);
  69. req->sendbuf_init = TRUE;
  70. }
  71. else {
  72. Curl_bufq_reset(&req->sendbuf);
  73. if(data->set.upload_buffer_size != req->sendbuf.chunk_size) {
  74. Curl_bufq_free(&req->sendbuf);
  75. Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
  76. BUFQ_OPT_SOFT_LIMIT);
  77. }
  78. }
  79. return CURLE_OK;
  80. }
  81. CURLcode Curl_req_start(struct SingleRequest *req,
  82. struct Curl_easy *data)
  83. {
  84. req->start = Curl_now();
  85. return Curl_req_soft_reset(req, data);
  86. }
  87. static CURLcode req_flush(struct Curl_easy *data);
  88. CURLcode Curl_req_done(struct SingleRequest *req,
  89. struct Curl_easy *data, bool aborted)
  90. {
  91. (void)req;
  92. if(!aborted)
  93. (void)req_flush(data);
  94. Curl_client_reset(data);
  95. #ifndef CURL_DISABLE_DOH
  96. Curl_doh_close(data);
  97. #endif
  98. return CURLE_OK;
  99. }
  100. void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
  101. {
  102. struct curltime t0 = {0, 0};
  103. /* This is a bit ugly. `req->p` is a union and we assume we can
  104. * free this safely without leaks. */
  105. Curl_safefree(req->p.ftp);
  106. Curl_safefree(req->newurl);
  107. Curl_client_reset(data);
  108. if(req->sendbuf_init)
  109. Curl_bufq_reset(&req->sendbuf);
  110. #ifndef CURL_DISABLE_DOH
  111. Curl_doh_close(data);
  112. #endif
  113. /* Can no longer memset() this struct as we need to keep some state */
  114. req->size = -1;
  115. req->maxdownload = -1;
  116. req->bytecount = 0;
  117. req->writebytecount = 0;
  118. req->start = t0;
  119. req->headerbytecount = 0;
  120. req->allheadercount = 0;
  121. req->deductheadercount = 0;
  122. req->headerline = 0;
  123. req->offset = 0;
  124. req->httpcode = 0;
  125. req->keepon = 0;
  126. req->upgr101 = UPGR101_INIT;
  127. req->timeofdoc = 0;
  128. req->location = NULL;
  129. req->newurl = NULL;
  130. #ifndef CURL_DISABLE_COOKIES
  131. req->setcookies = 0;
  132. #endif
  133. req->header = FALSE;
  134. req->content_range = FALSE;
  135. req->download_done = FALSE;
  136. req->eos_written = FALSE;
  137. req->eos_read = FALSE;
  138. req->eos_sent = FALSE;
  139. req->upload_done = FALSE;
  140. req->upload_aborted = FALSE;
  141. req->ignorebody = FALSE;
  142. req->http_bodyless = FALSE;
  143. req->chunk = FALSE;
  144. req->ignore_cl = FALSE;
  145. req->upload_chunky = FALSE;
  146. req->getheader = FALSE;
  147. req->no_body = data->set.opt_no_body;
  148. req->authneg = FALSE;
  149. req->shutdown = FALSE;
  150. }
  151. void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
  152. {
  153. /* This is a bit ugly. `req->p` is a union and we assume we can
  154. * free this safely without leaks. */
  155. Curl_safefree(req->p.ftp);
  156. Curl_safefree(req->newurl);
  157. if(req->sendbuf_init)
  158. Curl_bufq_free(&req->sendbuf);
  159. Curl_client_cleanup(data);
  160. #ifndef CURL_DISABLE_DOH
  161. Curl_doh_cleanup(data);
  162. #endif
  163. }
  164. static CURLcode xfer_send(struct Curl_easy *data,
  165. const char *buf, size_t blen,
  166. size_t hds_len, size_t *pnwritten)
  167. {
  168. CURLcode result = CURLE_OK;
  169. bool eos = FALSE;
  170. *pnwritten = 0;
  171. DEBUGASSERT(hds_len <= blen);
  172. #ifdef DEBUGBUILD
  173. {
  174. /* Allow debug builds to override this logic to force short initial
  175. sends */
  176. size_t body_len = blen - hds_len;
  177. char *p = getenv("CURL_SMALLREQSEND");
  178. if(p) {
  179. size_t body_small = (size_t)strtoul(p, NULL, 10);
  180. if(body_small && body_small < body_len)
  181. blen = hds_len + body_small;
  182. }
  183. }
  184. #endif
  185. /* Make sure this does not send more body bytes than what the max send
  186. speed says. The headers do not count to the max speed. */
  187. if(data->set.max_send_speed) {
  188. size_t body_bytes = blen - hds_len;
  189. if((curl_off_t)body_bytes > data->set.max_send_speed)
  190. blen = hds_len + (size_t)data->set.max_send_speed;
  191. }
  192. if(data->req.eos_read &&
  193. (Curl_bufq_is_empty(&data->req.sendbuf) ||
  194. Curl_bufq_len(&data->req.sendbuf) == blen)) {
  195. DEBUGF(infof(data, "sending last upload chunk of %zu bytes", blen));
  196. eos = TRUE;
  197. }
  198. result = Curl_xfer_send(data, buf, blen, eos, pnwritten);
  199. if(!result) {
  200. if(eos && (blen == *pnwritten))
  201. data->req.eos_sent = TRUE;
  202. if(*pnwritten) {
  203. if(hds_len)
  204. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)buf,
  205. CURLMIN(hds_len, *pnwritten));
  206. if(*pnwritten > hds_len) {
  207. size_t body_len = *pnwritten - hds_len;
  208. Curl_debug(data, CURLINFO_DATA_OUT, (char *)buf + hds_len, body_len);
  209. data->req.writebytecount += body_len;
  210. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  211. }
  212. }
  213. }
  214. return result;
  215. }
  216. static CURLcode req_send_buffer_flush(struct Curl_easy *data)
  217. {
  218. CURLcode result = CURLE_OK;
  219. const unsigned char *buf;
  220. size_t blen;
  221. while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
  222. size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
  223. result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
  224. if(result)
  225. break;
  226. Curl_bufq_skip(&data->req.sendbuf, nwritten);
  227. if(hds_len) {
  228. data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
  229. }
  230. /* leave if we could not send all. Maybe network blocking or
  231. * speed limits on transfer */
  232. if(nwritten < blen)
  233. break;
  234. }
  235. return result;
  236. }
  237. static CURLcode req_set_upload_done(struct Curl_easy *data)
  238. {
  239. DEBUGASSERT(!data->req.upload_done);
  240. data->req.upload_done = TRUE;
  241. data->req.keepon &= ~(KEEP_SEND|KEEP_SEND_TIMED); /* we are done sending */
  242. Curl_pgrsTime(data, TIMER_POSTRANSFER);
  243. Curl_creader_done(data, data->req.upload_aborted);
  244. if(data->req.upload_aborted) {
  245. Curl_bufq_reset(&data->req.sendbuf);
  246. if(data->req.writebytecount)
  247. infof(data, "abort upload after having sent %" FMT_OFF_T " bytes",
  248. data->req.writebytecount);
  249. else
  250. infof(data, "abort upload");
  251. }
  252. else if(data->req.writebytecount)
  253. infof(data, "upload completely sent off: %" FMT_OFF_T " bytes",
  254. data->req.writebytecount);
  255. else if(!data->req.download_done) {
  256. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  257. infof(data, Curl_creader_total_length(data) ?
  258. "We are completely uploaded and fine" :
  259. "Request completely sent off");
  260. }
  261. return Curl_xfer_send_close(data);
  262. }
  263. static CURLcode req_flush(struct Curl_easy *data)
  264. {
  265. CURLcode result;
  266. if(!data || !data->conn)
  267. return CURLE_FAILED_INIT;
  268. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  269. result = req_send_buffer_flush(data);
  270. if(result)
  271. return result;
  272. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  273. DEBUGF(infof(data, "Curl_req_flush(len=%zu) -> EAGAIN",
  274. Curl_bufq_len(&data->req.sendbuf)));
  275. return CURLE_AGAIN;
  276. }
  277. }
  278. else if(Curl_xfer_needs_flush(data)) {
  279. DEBUGF(infof(data, "Curl_req_flush(), xfer send_pending"));
  280. return Curl_xfer_flush(data);
  281. }
  282. if(data->req.eos_read && !data->req.eos_sent) {
  283. char tmp;
  284. size_t nwritten;
  285. result = xfer_send(data, &tmp, 0, 0, &nwritten);
  286. if(result)
  287. return result;
  288. DEBUGASSERT(data->req.eos_sent);
  289. }
  290. if(!data->req.upload_done && data->req.eos_read && data->req.eos_sent) {
  291. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  292. if(data->req.shutdown) {
  293. bool done;
  294. result = Curl_xfer_send_shutdown(data, &done);
  295. if(result && data->req.shutdown_err_ignore) {
  296. infof(data, "Shutdown send direction error: %d. Broken server? "
  297. "Proceeding as if everything is ok.", result);
  298. result = CURLE_OK;
  299. done = TRUE;
  300. }
  301. if(result)
  302. return result;
  303. if(!done)
  304. return CURLE_AGAIN;
  305. }
  306. return req_set_upload_done(data);
  307. }
  308. return CURLE_OK;
  309. }
  310. static ssize_t add_from_client(void *reader_ctx,
  311. unsigned char *buf, size_t buflen,
  312. CURLcode *err)
  313. {
  314. struct Curl_easy *data = reader_ctx;
  315. size_t nread;
  316. bool eos;
  317. *err = Curl_client_read(data, (char *)buf, buflen, &nread, &eos);
  318. if(*err)
  319. return -1;
  320. if(eos)
  321. data->req.eos_read = TRUE;
  322. return (ssize_t)nread;
  323. }
  324. static CURLcode req_send_buffer_add(struct Curl_easy *data,
  325. const char *buf, size_t blen,
  326. size_t hds_len)
  327. {
  328. CURLcode result = CURLE_OK;
  329. ssize_t n;
  330. n = Curl_bufq_write(&data->req.sendbuf,
  331. (const unsigned char *)buf, blen, &result);
  332. if(n < 0)
  333. return result;
  334. /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
  335. DEBUGASSERT((size_t)n == blen);
  336. data->req.sendbuf_hds_len += hds_len;
  337. return CURLE_OK;
  338. }
  339. CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req)
  340. {
  341. CURLcode result;
  342. const char *buf;
  343. size_t blen, nwritten;
  344. if(!data || !data->conn)
  345. return CURLE_FAILED_INIT;
  346. buf = Curl_dyn_ptr(req);
  347. blen = Curl_dyn_len(req);
  348. if(!Curl_creader_total_length(data)) {
  349. /* Request without body. Try to send directly from the buf given. */
  350. data->req.eos_read = TRUE;
  351. result = xfer_send(data, buf, blen, blen, &nwritten);
  352. if(result)
  353. return result;
  354. buf += nwritten;
  355. blen -= nwritten;
  356. }
  357. if(blen) {
  358. /* Either we have a request body, or we could not send the complete
  359. * request in one go. Buffer the remainder and try to add as much
  360. * body bytes as room is left in the buffer. Then flush. */
  361. result = req_send_buffer_add(data, buf, blen, blen);
  362. if(result)
  363. return result;
  364. return Curl_req_send_more(data);
  365. }
  366. return CURLE_OK;
  367. }
  368. bool Curl_req_sendbuf_empty(struct Curl_easy *data)
  369. {
  370. return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf);
  371. }
  372. bool Curl_req_want_send(struct Curl_easy *data)
  373. {
  374. /* Not done and
  375. * - KEEP_SEND and not PAUSEd.
  376. * - or request has buffered data to send
  377. * - or transfer connection has pending data to send */
  378. return !data->req.done &&
  379. (((data->req.keepon & KEEP_SENDBITS) == KEEP_SEND) ||
  380. !Curl_req_sendbuf_empty(data) ||
  381. Curl_xfer_needs_flush(data));
  382. }
  383. bool Curl_req_done_sending(struct Curl_easy *data)
  384. {
  385. return data->req.upload_done && !Curl_req_want_send(data);
  386. }
  387. CURLcode Curl_req_send_more(struct Curl_easy *data)
  388. {
  389. CURLcode result;
  390. /* Fill our send buffer if more from client can be read. */
  391. if(!data->req.upload_aborted &&
  392. !data->req.eos_read &&
  393. !(data->req.keepon & KEEP_SEND_PAUSE) &&
  394. !Curl_bufq_is_full(&data->req.sendbuf)) {
  395. ssize_t nread = Curl_bufq_sipn(&data->req.sendbuf, 0,
  396. add_from_client, data, &result);
  397. if(nread < 0 && result != CURLE_AGAIN)
  398. return result;
  399. }
  400. result = req_flush(data);
  401. if(result == CURLE_AGAIN)
  402. result = CURLE_OK;
  403. return result;
  404. }
  405. CURLcode Curl_req_abort_sending(struct Curl_easy *data)
  406. {
  407. if(!data->req.upload_done) {
  408. Curl_bufq_reset(&data->req.sendbuf);
  409. data->req.upload_aborted = TRUE;
  410. /* no longer KEEP_SEND and KEEP_SEND_PAUSE */
  411. data->req.keepon &= ~KEEP_SENDBITS;
  412. return req_set_upload_done(data);
  413. }
  414. return CURLE_OK;
  415. }
  416. CURLcode Curl_req_stop_send_recv(struct Curl_easy *data)
  417. {
  418. /* stop receiving and ALL sending as well, including PAUSE and HOLD.
  419. * We might still be paused on receive client writes though, so
  420. * keep those bits around. */
  421. data->req.keepon &= ~(KEEP_RECV|KEEP_SENDBITS);
  422. return Curl_req_abort_sending(data);
  423. }