1
0

request.c 13 KB

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