2
0

request.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #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 && data->req.shutdown_err_ignore) {
  299. infof(data, "Shutdown send direction error: %d. Broken server? "
  300. "Proceeding as if everything is ok.", result);
  301. result = CURLE_OK;
  302. done = TRUE;
  303. }
  304. if(result)
  305. return result;
  306. if(!done)
  307. return CURLE_AGAIN;
  308. }
  309. return Curl_req_set_upload_done(data);
  310. }
  311. return CURLE_OK;
  312. }
  313. static ssize_t add_from_client(void *reader_ctx,
  314. unsigned char *buf, size_t buflen,
  315. CURLcode *err)
  316. {
  317. struct Curl_easy *data = reader_ctx;
  318. size_t nread;
  319. bool eos;
  320. *err = Curl_client_read(data, (char *)buf, buflen, &nread, &eos);
  321. if(*err)
  322. return -1;
  323. if(eos)
  324. data->req.eos_read = TRUE;
  325. return (ssize_t)nread;
  326. }
  327. #ifndef USE_HYPER
  328. static CURLcode req_send_buffer_add(struct Curl_easy *data,
  329. const char *buf, size_t blen,
  330. size_t hds_len)
  331. {
  332. CURLcode result = CURLE_OK;
  333. ssize_t n;
  334. n = Curl_bufq_write(&data->req.sendbuf,
  335. (const unsigned char *)buf, blen, &result);
  336. if(n < 0)
  337. return result;
  338. /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
  339. DEBUGASSERT((size_t)n == blen);
  340. data->req.sendbuf_hds_len += hds_len;
  341. return CURLE_OK;
  342. }
  343. CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req)
  344. {
  345. CURLcode result;
  346. const char *buf;
  347. size_t blen, nwritten;
  348. if(!data || !data->conn)
  349. return CURLE_FAILED_INIT;
  350. buf = Curl_dyn_ptr(req);
  351. blen = Curl_dyn_len(req);
  352. if(!Curl_creader_total_length(data)) {
  353. /* Request without body. Try to send directly from the buf given. */
  354. data->req.eos_read = TRUE;
  355. result = xfer_send(data, buf, blen, blen, &nwritten);
  356. if(result)
  357. return result;
  358. buf += nwritten;
  359. blen -= nwritten;
  360. }
  361. if(blen) {
  362. /* Either we have a request body, or we could not send the complete
  363. * request in one go. Buffer the remainder and try to add as much
  364. * body bytes as room is left in the buffer. Then flush. */
  365. result = req_send_buffer_add(data, buf, blen, blen);
  366. if(result)
  367. return result;
  368. return Curl_req_send_more(data);
  369. }
  370. return CURLE_OK;
  371. }
  372. #endif /* !USE_HYPER */
  373. bool Curl_req_sendbuf_empty(struct Curl_easy *data)
  374. {
  375. return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf);
  376. }
  377. bool Curl_req_want_send(struct Curl_easy *data)
  378. {
  379. /* Not done and
  380. * - KEEP_SEND and not PAUSEd.
  381. * - or request has buffered data to send
  382. * - or transfer connection has pending data to send */
  383. return !data->req.done &&
  384. (((data->req.keepon & KEEP_SENDBITS) == KEEP_SEND) ||
  385. !Curl_req_sendbuf_empty(data) ||
  386. Curl_xfer_needs_flush(data));
  387. }
  388. bool Curl_req_done_sending(struct Curl_easy *data)
  389. {
  390. return data->req.upload_done && !Curl_req_want_send(data);
  391. }
  392. CURLcode Curl_req_send_more(struct Curl_easy *data)
  393. {
  394. CURLcode result;
  395. /* Fill our send buffer if more from client can be read. */
  396. if(!data->req.upload_aborted &&
  397. !data->req.eos_read &&
  398. !(data->req.keepon & KEEP_SEND_PAUSE) &&
  399. !Curl_bufq_is_full(&data->req.sendbuf)) {
  400. ssize_t nread = Curl_bufq_sipn(&data->req.sendbuf, 0,
  401. add_from_client, data, &result);
  402. if(nread < 0 && result != CURLE_AGAIN)
  403. return result;
  404. }
  405. result = req_flush(data);
  406. if(result == CURLE_AGAIN)
  407. result = CURLE_OK;
  408. return result;
  409. }
  410. CURLcode Curl_req_abort_sending(struct Curl_easy *data)
  411. {
  412. if(!data->req.upload_done) {
  413. Curl_bufq_reset(&data->req.sendbuf);
  414. data->req.upload_aborted = TRUE;
  415. /* no longer KEEP_SEND and KEEP_SEND_PAUSE */
  416. data->req.keepon &= ~KEEP_SENDBITS;
  417. return Curl_req_set_upload_done(data);
  418. }
  419. return CURLE_OK;
  420. }
  421. CURLcode Curl_req_stop_send_recv(struct Curl_easy *data)
  422. {
  423. /* stop receiving and ALL sending as well, including PAUSE and HOLD.
  424. * We might still be paused on receive client writes though, so
  425. * keep those bits around. */
  426. data->req.keepon &= ~(KEEP_RECV|KEEP_SENDBITS);
  427. return Curl_req_abort_sending(data);
  428. }