request.c 13 KB

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