request.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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. #ifdef USE_HYPER
  151. req->bodywritten = FALSE;
  152. #endif
  153. }
  154. void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
  155. {
  156. /* This is a bit ugly. `req->p` is a union and we assume we can
  157. * free this safely without leaks. */
  158. Curl_safefree(req->p.ftp);
  159. Curl_safefree(req->newurl);
  160. if(req->sendbuf_init)
  161. Curl_bufq_free(&req->sendbuf);
  162. Curl_client_cleanup(data);
  163. #ifndef CURL_DISABLE_DOH
  164. Curl_doh_cleanup(data);
  165. #endif
  166. }
  167. static CURLcode xfer_send(struct Curl_easy *data,
  168. const char *buf, size_t blen,
  169. size_t hds_len, size_t *pnwritten)
  170. {
  171. CURLcode result = CURLE_OK;
  172. bool eos = FALSE;
  173. *pnwritten = 0;
  174. DEBUGASSERT(hds_len <= blen);
  175. #ifdef DEBUGBUILD
  176. {
  177. /* Allow debug builds to override this logic to force short initial
  178. sends */
  179. size_t body_len = blen - hds_len;
  180. char *p = getenv("CURL_SMALLREQSEND");
  181. if(p) {
  182. size_t body_small = (size_t)strtoul(p, NULL, 10);
  183. if(body_small && body_small < body_len)
  184. blen = hds_len + body_small;
  185. }
  186. }
  187. #endif
  188. /* Make sure this does not send more body bytes than what the max send
  189. speed says. The headers do not count to the max speed. */
  190. if(data->set.max_send_speed) {
  191. size_t body_bytes = blen - hds_len;
  192. if((curl_off_t)body_bytes > data->set.max_send_speed)
  193. blen = hds_len + (size_t)data->set.max_send_speed;
  194. }
  195. if(data->req.eos_read &&
  196. (Curl_bufq_is_empty(&data->req.sendbuf) ||
  197. Curl_bufq_len(&data->req.sendbuf) == blen)) {
  198. DEBUGF(infof(data, "sending last upload chunk of %zu bytes", blen));
  199. eos = TRUE;
  200. }
  201. result = Curl_xfer_send(data, buf, blen, eos, pnwritten);
  202. if(!result) {
  203. if(eos && (blen == *pnwritten))
  204. data->req.eos_sent = TRUE;
  205. if(*pnwritten) {
  206. if(hds_len)
  207. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)buf,
  208. CURLMIN(hds_len, *pnwritten));
  209. if(*pnwritten > hds_len) {
  210. size_t body_len = *pnwritten - hds_len;
  211. Curl_debug(data, CURLINFO_DATA_OUT, (char *)buf + hds_len, body_len);
  212. data->req.writebytecount += body_len;
  213. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  214. }
  215. }
  216. }
  217. return result;
  218. }
  219. static CURLcode req_send_buffer_flush(struct Curl_easy *data)
  220. {
  221. CURLcode result = CURLE_OK;
  222. const unsigned char *buf;
  223. size_t blen;
  224. while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
  225. size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
  226. result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
  227. if(result)
  228. break;
  229. Curl_bufq_skip(&data->req.sendbuf, nwritten);
  230. if(hds_len) {
  231. data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
  232. }
  233. /* leave if we could not send all. Maybe network blocking or
  234. * speed limits on transfer */
  235. if(nwritten < blen)
  236. break;
  237. }
  238. return result;
  239. }
  240. CURLcode Curl_req_set_upload_done(struct Curl_easy *data)
  241. {
  242. DEBUGASSERT(!data->req.upload_done);
  243. data->req.upload_done = TRUE;
  244. data->req.keepon &= ~(KEEP_SEND|KEEP_SEND_TIMED); /* we are done sending */
  245. Curl_pgrsTime(data, TIMER_POSTRANSFER);
  246. Curl_creader_done(data, data->req.upload_aborted);
  247. if(data->req.upload_aborted) {
  248. Curl_bufq_reset(&data->req.sendbuf);
  249. if(data->req.writebytecount)
  250. infof(data, "abort upload after having sent %" FMT_OFF_T " bytes",
  251. data->req.writebytecount);
  252. else
  253. infof(data, "abort upload");
  254. }
  255. else if(data->req.writebytecount)
  256. infof(data, "upload completely sent off: %" FMT_OFF_T " bytes",
  257. data->req.writebytecount);
  258. else if(!data->req.download_done) {
  259. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  260. infof(data, Curl_creader_total_length(data)?
  261. "We are completely uploaded and fine" :
  262. "Request completely sent off");
  263. }
  264. return Curl_xfer_send_close(data);
  265. }
  266. static CURLcode req_flush(struct Curl_easy *data)
  267. {
  268. CURLcode result;
  269. if(!data || !data->conn)
  270. return CURLE_FAILED_INIT;
  271. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  272. result = req_send_buffer_flush(data);
  273. if(result)
  274. return result;
  275. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  276. DEBUGF(infof(data, "Curl_req_flush(len=%zu) -> EAGAIN",
  277. Curl_bufq_len(&data->req.sendbuf)));
  278. return CURLE_AGAIN;
  279. }
  280. }
  281. else if(Curl_xfer_needs_flush(data)) {
  282. DEBUGF(infof(data, "Curl_req_flush(), xfer send_pending"));
  283. return Curl_xfer_flush(data);
  284. }
  285. if(data->req.eos_read && !data->req.eos_sent) {
  286. char tmp;
  287. size_t nwritten;
  288. result = xfer_send(data, &tmp, 0, 0, &nwritten);
  289. if(result)
  290. return result;
  291. DEBUGASSERT(data->req.eos_sent);
  292. }
  293. if(!data->req.upload_done && data->req.eos_read && data->req.eos_sent) {
  294. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  295. if(data->req.shutdown) {
  296. bool done;
  297. result = Curl_xfer_send_shutdown(data, &done);
  298. if(result)
  299. return result;
  300. if(!done)
  301. return CURLE_AGAIN;
  302. }
  303. return Curl_req_set_upload_done(data);
  304. }
  305. return CURLE_OK;
  306. }
  307. static ssize_t add_from_client(void *reader_ctx,
  308. unsigned char *buf, size_t buflen,
  309. CURLcode *err)
  310. {
  311. struct Curl_easy *data = reader_ctx;
  312. size_t nread;
  313. bool eos;
  314. *err = Curl_client_read(data, (char *)buf, buflen, &nread, &eos);
  315. if(*err)
  316. return -1;
  317. if(eos)
  318. data->req.eos_read = TRUE;
  319. return (ssize_t)nread;
  320. }
  321. #ifndef USE_HYPER
  322. static CURLcode req_send_buffer_add(struct Curl_easy *data,
  323. const char *buf, size_t blen,
  324. size_t hds_len)
  325. {
  326. CURLcode result = CURLE_OK;
  327. ssize_t n;
  328. n = Curl_bufq_write(&data->req.sendbuf,
  329. (const unsigned char *)buf, blen, &result);
  330. if(n < 0)
  331. return result;
  332. /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
  333. DEBUGASSERT((size_t)n == blen);
  334. data->req.sendbuf_hds_len += hds_len;
  335. return CURLE_OK;
  336. }
  337. CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req)
  338. {
  339. CURLcode result;
  340. const char *buf;
  341. size_t blen, nwritten;
  342. if(!data || !data->conn)
  343. return CURLE_FAILED_INIT;
  344. buf = Curl_dyn_ptr(req);
  345. blen = Curl_dyn_len(req);
  346. if(!Curl_creader_total_length(data)) {
  347. /* Request without body. Try to send directly from the buf given. */
  348. data->req.eos_read = TRUE;
  349. result = xfer_send(data, buf, blen, blen, &nwritten);
  350. if(result)
  351. return result;
  352. buf += nwritten;
  353. blen -= nwritten;
  354. }
  355. if(blen) {
  356. /* Either we have a request body, or we could not send the complete
  357. * request in one go. Buffer the remainder and try to add as much
  358. * body bytes as room is left in the buffer. Then flush. */
  359. result = req_send_buffer_add(data, buf, blen, blen);
  360. if(result)
  361. return result;
  362. return Curl_req_send_more(data);
  363. }
  364. return CURLE_OK;
  365. }
  366. #endif /* !USE_HYPER */
  367. bool Curl_req_sendbuf_empty(struct Curl_easy *data)
  368. {
  369. return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf);
  370. }
  371. bool Curl_req_want_send(struct Curl_easy *data)
  372. {
  373. /* Not done and
  374. * - KEEP_SEND and not PAUSEd.
  375. * - or request has buffered data to send
  376. * - or transfer connection has pending data to send */
  377. return !data->req.done &&
  378. (((data->req.keepon & KEEP_SENDBITS) == KEEP_SEND) ||
  379. !Curl_req_sendbuf_empty(data) ||
  380. Curl_xfer_needs_flush(data));
  381. }
  382. bool Curl_req_done_sending(struct Curl_easy *data)
  383. {
  384. return data->req.upload_done && !Curl_req_want_send(data);
  385. }
  386. CURLcode Curl_req_send_more(struct Curl_easy *data)
  387. {
  388. CURLcode result;
  389. /* Fill our send buffer if more from client can be read. */
  390. if(!data->req.upload_aborted &&
  391. !data->req.eos_read &&
  392. !(data->req.keepon & KEEP_SEND_PAUSE) &&
  393. !Curl_bufq_is_full(&data->req.sendbuf)) {
  394. ssize_t nread = Curl_bufq_sipn(&data->req.sendbuf, 0,
  395. add_from_client, data, &result);
  396. if(nread < 0 && result != CURLE_AGAIN)
  397. return result;
  398. }
  399. result = req_flush(data);
  400. if(result == CURLE_AGAIN)
  401. result = CURLE_OK;
  402. return result;
  403. }
  404. CURLcode Curl_req_abort_sending(struct Curl_easy *data)
  405. {
  406. if(!data->req.upload_done) {
  407. Curl_bufq_reset(&data->req.sendbuf);
  408. data->req.upload_aborted = TRUE;
  409. /* no longer KEEP_SEND and KEEP_SEND_PAUSE */
  410. data->req.keepon &= ~KEEP_SENDBITS;
  411. return Curl_req_set_upload_done(data);
  412. }
  413. return CURLE_OK;
  414. }
  415. CURLcode Curl_req_stop_send_recv(struct Curl_easy *data)
  416. {
  417. /* stop receiving and ALL sending as well, including PAUSE and HOLD.
  418. * We might still be paused on receive client writes though, so
  419. * keep those bits around. */
  420. data->req.keepon &= ~(KEEP_RECV|KEEP_SENDBITS);
  421. return Curl_req_abort_sending(data);
  422. }