request.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "curlx/dynbuf.h"
  28. #include "doh.h"
  29. #include "progress.h"
  30. #include "request.h"
  31. #include "sendf.h"
  32. #include "curl_trc.h"
  33. #include "transfer.h"
  34. #include "url.h"
  35. #include "curlx/strparse.h"
  36. void Curl_req_init(struct SingleRequest *req)
  37. {
  38. memset(req, 0, sizeof(*req));
  39. }
  40. CURLcode Curl_req_soft_reset(struct SingleRequest *req,
  41. struct Curl_easy *data)
  42. {
  43. CURLcode result;
  44. req->done = FALSE;
  45. req->upload_done = FALSE;
  46. req->upload_aborted = FALSE;
  47. req->download_done = FALSE;
  48. req->eos_written = FALSE;
  49. req->eos_read = FALSE;
  50. req->eos_sent = FALSE;
  51. req->ignorebody = FALSE;
  52. req->shutdown = FALSE;
  53. req->bytecount = 0;
  54. req->writebytecount = 0;
  55. req->header = FALSE;
  56. req->headerline = 0;
  57. req->headerbytecount = 0;
  58. req->allheadercount = 0;
  59. req->deductheadercount = 0;
  60. req->httpversion_sent = 0;
  61. req->httpversion = 0;
  62. req->sendbuf_hds_len = 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_pgrs_now(data);
  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. Curl_safefree(req->newurl);
  104. Curl_client_reset(data);
  105. if(req->sendbuf_init)
  106. Curl_bufq_reset(&req->sendbuf);
  107. #ifndef CURL_DISABLE_DOH
  108. Curl_doh_close(data);
  109. #endif
  110. /* Can no longer memset() this struct as we need to keep some state */
  111. req->size = -1;
  112. req->maxdownload = -1;
  113. req->bytecount = 0;
  114. req->writebytecount = 0;
  115. req->start = t0;
  116. req->headerbytecount = 0;
  117. req->allheadercount = 0;
  118. req->deductheadercount = 0;
  119. req->headerline = 0;
  120. req->offset = 0;
  121. req->httpcode = 0;
  122. req->keepon = 0;
  123. req->upgr101 = UPGR101_NONE;
  124. req->sendbuf_hds_len = 0;
  125. req->timeofdoc = 0;
  126. req->location = NULL;
  127. req->newurl = NULL;
  128. #ifndef CURL_DISABLE_COOKIES
  129. req->setcookies = 0;
  130. #endif
  131. req->header = FALSE;
  132. req->content_range = FALSE;
  133. req->download_done = FALSE;
  134. req->eos_written = FALSE;
  135. req->eos_read = FALSE;
  136. req->eos_sent = FALSE;
  137. req->rewind_read = FALSE;
  138. req->upload_done = FALSE;
  139. req->upload_aborted = FALSE;
  140. req->ignorebody = FALSE;
  141. req->http_bodyless = FALSE;
  142. req->chunk = FALSE;
  143. req->ignore_cl = FALSE;
  144. req->upload_chunky = FALSE;
  145. req->no_body = data->set.opt_no_body;
  146. req->authneg = FALSE;
  147. req->shutdown = FALSE;
  148. }
  149. void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
  150. {
  151. Curl_safefree(req->newurl);
  152. if(req->sendbuf_init)
  153. Curl_bufq_free(&req->sendbuf);
  154. Curl_client_cleanup(data);
  155. }
  156. static CURLcode xfer_send(struct Curl_easy *data,
  157. const char *buf, size_t blen,
  158. size_t hds_len, size_t *pnwritten)
  159. {
  160. CURLcode result = CURLE_OK;
  161. bool eos = FALSE;
  162. *pnwritten = 0;
  163. DEBUGASSERT(hds_len <= blen);
  164. #ifdef DEBUGBUILD
  165. {
  166. /* Allow debug builds to override this logic to force short initial
  167. sends */
  168. size_t body_len = blen - hds_len;
  169. if(body_len) {
  170. const char *p = getenv("CURL_SMALLREQSEND");
  171. if(p) {
  172. curl_off_t body_small;
  173. if(!curlx_str_number(&p, &body_small, body_len))
  174. blen = hds_len + (size_t)body_small;
  175. }
  176. }
  177. }
  178. #endif
  179. /* Make sure this does not send more body bytes than what the max send
  180. speed says. The headers do not count to the max speed. */
  181. if(data->set.max_send_speed) {
  182. size_t body_bytes = blen - hds_len;
  183. if((curl_off_t)body_bytes > data->set.max_send_speed)
  184. blen = hds_len + (size_t)data->set.max_send_speed;
  185. }
  186. if(data->req.eos_read &&
  187. (Curl_bufq_is_empty(&data->req.sendbuf) ||
  188. Curl_bufq_len(&data->req.sendbuf) == blen)) {
  189. DEBUGF(infof(data, "sending last upload chunk of %zu bytes", blen));
  190. eos = TRUE;
  191. }
  192. result = Curl_xfer_send(data, buf, blen, eos, pnwritten);
  193. if(!result) {
  194. if(eos && (blen == *pnwritten))
  195. data->req.eos_sent = TRUE;
  196. if(*pnwritten) {
  197. if(hds_len)
  198. Curl_debug(data, CURLINFO_HEADER_OUT, buf,
  199. CURLMIN(hds_len, *pnwritten));
  200. if(*pnwritten > hds_len) {
  201. size_t body_len = *pnwritten - hds_len;
  202. Curl_debug(data, CURLINFO_DATA_OUT, buf + hds_len, body_len);
  203. data->req.writebytecount += body_len;
  204. Curl_pgrs_upload_inc(data, body_len);
  205. }
  206. }
  207. }
  208. return result;
  209. }
  210. static CURLcode req_send_buffer_flush(struct Curl_easy *data)
  211. {
  212. CURLcode result = CURLE_OK;
  213. const unsigned char *buf;
  214. size_t blen;
  215. while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
  216. size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
  217. result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
  218. if(result)
  219. break;
  220. Curl_bufq_skip(&data->req.sendbuf, nwritten);
  221. if(hds_len) {
  222. data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
  223. }
  224. /* leave if we could not send all. Maybe network blocking or
  225. * speed limits on transfer */
  226. if(nwritten < blen)
  227. break;
  228. }
  229. return result;
  230. }
  231. static CURLcode req_set_upload_done(struct Curl_easy *data)
  232. {
  233. DEBUGASSERT(!data->req.upload_done);
  234. data->req.upload_done = TRUE;
  235. data->req.keepon &= ~KEEP_SEND; /* we are done sending */
  236. Curl_pgrsTime(data, TIMER_POSTRANSFER);
  237. Curl_creader_done(data, data->req.upload_aborted);
  238. if(data->req.upload_aborted) {
  239. Curl_bufq_reset(&data->req.sendbuf);
  240. if(data->req.writebytecount)
  241. infof(data, "abort upload after having sent %" FMT_OFF_T " bytes",
  242. data->req.writebytecount);
  243. else
  244. infof(data, "abort upload");
  245. }
  246. else if(data->req.writebytecount)
  247. infof(data, "upload completely sent off: %" FMT_OFF_T " bytes",
  248. data->req.writebytecount);
  249. else if(!data->req.download_done) {
  250. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  251. infof(data, Curl_creader_total_length(data) ?
  252. "We are completely uploaded and fine" :
  253. "Request completely sent off");
  254. }
  255. return Curl_xfer_send_close(data);
  256. }
  257. static CURLcode req_flush(struct Curl_easy *data)
  258. {
  259. CURLcode result;
  260. if(!data || !data->conn)
  261. return CURLE_FAILED_INIT;
  262. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  263. result = req_send_buffer_flush(data);
  264. if(result)
  265. return result;
  266. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  267. DEBUGF(infof(data, "Curl_req_flush(len=%zu) -> EAGAIN",
  268. Curl_bufq_len(&data->req.sendbuf)));
  269. return CURLE_AGAIN;
  270. }
  271. }
  272. else if(Curl_xfer_needs_flush(data)) {
  273. DEBUGF(infof(data, "Curl_req_flush(), xfer send_pending"));
  274. return Curl_xfer_flush(data);
  275. }
  276. if(data->req.eos_read && !data->req.eos_sent) {
  277. char tmp = 0;
  278. size_t nwritten;
  279. result = xfer_send(data, &tmp, 0, 0, &nwritten);
  280. if(result)
  281. return result;
  282. DEBUGASSERT(data->req.eos_sent);
  283. }
  284. if(!data->req.upload_done && data->req.eos_read && data->req.eos_sent) {
  285. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  286. if(data->req.shutdown) {
  287. bool done;
  288. result = Curl_xfer_send_shutdown(data, &done);
  289. if(result && data->req.shutdown_err_ignore) {
  290. infof(data, "Shutdown send direction error: %d. Broken server? "
  291. "Proceeding as if everything is ok.", result);
  292. result = CURLE_OK;
  293. done = TRUE;
  294. }
  295. if(result)
  296. return result;
  297. if(!done)
  298. return CURLE_AGAIN;
  299. }
  300. return req_set_upload_done(data);
  301. }
  302. return CURLE_OK;
  303. }
  304. static CURLcode add_from_client(void *reader_ctx,
  305. unsigned char *buf, size_t buflen,
  306. size_t *pnread)
  307. {
  308. struct Curl_easy *data = reader_ctx;
  309. CURLcode result;
  310. bool eos;
  311. result = Curl_client_read(data, (char *)buf, buflen, pnread, &eos);
  312. if(!result && eos)
  313. data->req.eos_read = TRUE;
  314. return result;
  315. }
  316. static CURLcode req_send_buffer_add(struct Curl_easy *data,
  317. const char *buf, size_t blen,
  318. size_t hds_len)
  319. {
  320. CURLcode result = CURLE_OK;
  321. size_t n;
  322. result = Curl_bufq_cwrite(&data->req.sendbuf, buf, blen, &n);
  323. if(result)
  324. return result;
  325. /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
  326. DEBUGASSERT(n == blen);
  327. data->req.sendbuf_hds_len += hds_len;
  328. return CURLE_OK;
  329. }
  330. CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req,
  331. unsigned char httpversion)
  332. {
  333. CURLcode result;
  334. const char *buf;
  335. size_t blen, nwritten;
  336. if(!data || !data->conn)
  337. return CURLE_FAILED_INIT;
  338. data->req.httpversion_sent = httpversion;
  339. buf = curlx_dyn_ptr(req);
  340. blen = curlx_dyn_len(req);
  341. /* if the sendbuf is empty and the request without body and
  342. * the length to send fits info a sendbuf chunk, we send it directly.
  343. * If `blen` is larger then `chunk_size`, we can not. Because we
  344. * might have to retry a blocked send later from sendbuf and that
  345. * would result in retry sends with a shrunken length. That is trouble. */
  346. if(Curl_bufq_is_empty(&data->req.sendbuf) &&
  347. !Curl_creader_total_length(data) &&
  348. (blen <= data->req.sendbuf.chunk_size)) {
  349. data->req.eos_read = TRUE;
  350. result = xfer_send(data, buf, blen, blen, &nwritten);
  351. if(result)
  352. return result;
  353. buf += nwritten;
  354. blen -= nwritten;
  355. if(!blen) {
  356. result = req_set_upload_done(data);
  357. if(result)
  358. return result;
  359. }
  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. bool Curl_req_sendbuf_empty(struct Curl_easy *data)
  373. {
  374. return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf);
  375. }
  376. bool Curl_req_want_send(struct Curl_easy *data)
  377. {
  378. /* Not done and upload not blocked and either one of
  379. * - KEEP_SEND
  380. * - request has buffered data to send
  381. * - connection has pending data to send */
  382. return !data->req.done &&
  383. !Curl_rlimit_is_blocked(&data->progress.ul.rlimit) &&
  384. ((data->req.keepon & KEEP_SEND) ||
  385. !Curl_req_sendbuf_empty(data) ||
  386. Curl_xfer_needs_flush(data));
  387. }
  388. bool Curl_req_want_recv(struct Curl_easy *data)
  389. {
  390. /* Not done and download not blocked and KEEP_RECV */
  391. return !data->req.done &&
  392. !Curl_rlimit_is_blocked(&data->progress.dl.rlimit) &&
  393. (data->req.keepon & KEEP_RECV);
  394. }
  395. bool Curl_req_done_sending(struct Curl_easy *data)
  396. {
  397. return data->req.upload_done && !Curl_req_want_send(data);
  398. }
  399. CURLcode Curl_req_send_more(struct Curl_easy *data)
  400. {
  401. CURLcode result;
  402. /* Fill our send buffer if more from client can be read. */
  403. if(!data->req.upload_aborted &&
  404. !data->req.eos_read &&
  405. !Curl_xfer_send_is_paused(data) &&
  406. !Curl_bufq_is_full(&data->req.sendbuf)) {
  407. size_t nread;
  408. result = Curl_bufq_sipn(&data->req.sendbuf, 0,
  409. add_from_client, data, &nread);
  410. if(result && result != CURLE_AGAIN)
  411. return result;
  412. }
  413. result = req_flush(data);
  414. if(result == CURLE_AGAIN)
  415. result = CURLE_OK;
  416. return result;
  417. }
  418. CURLcode Curl_req_abort_sending(struct Curl_easy *data)
  419. {
  420. if(!data->req.upload_done) {
  421. Curl_bufq_reset(&data->req.sendbuf);
  422. data->req.upload_aborted = TRUE;
  423. data->req.keepon &= ~KEEP_SEND;
  424. return req_set_upload_done(data);
  425. }
  426. return CURLE_OK;
  427. }
  428. CURLcode Curl_req_stop_send_recv(struct Curl_easy *data)
  429. {
  430. /* stop receiving and ALL sending as well, including PAUSE and HOLD.
  431. * We might still be paused on receive client writes though, so
  432. * keep those bits around. */
  433. CURLcode result = CURLE_OK;
  434. if(data->req.keepon & KEEP_SEND)
  435. result = Curl_req_abort_sending(data);
  436. data->req.keepon &= ~(KEEP_RECV | KEEP_SEND);
  437. return result;
  438. }