request.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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->download_done = FALSE;
  50. req->ignorebody = FALSE;
  51. req->shutdown = FALSE;
  52. req->bytecount = 0;
  53. req->writebytecount = 0;
  54. req->header = TRUE; /* assume header */
  55. req->headerline = 0;
  56. req->headerbytecount = 0;
  57. req->allheadercount = 0;
  58. req->deductheadercount = 0;
  59. result = Curl_client_start(data);
  60. if(result)
  61. return result;
  62. if(!req->sendbuf_init) {
  63. Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
  64. BUFQ_OPT_SOFT_LIMIT);
  65. req->sendbuf_init = TRUE;
  66. }
  67. else {
  68. Curl_bufq_reset(&req->sendbuf);
  69. if(data->set.upload_buffer_size != req->sendbuf.chunk_size) {
  70. Curl_bufq_free(&req->sendbuf);
  71. Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
  72. BUFQ_OPT_SOFT_LIMIT);
  73. }
  74. }
  75. return CURLE_OK;
  76. }
  77. CURLcode Curl_req_start(struct SingleRequest *req,
  78. struct Curl_easy *data)
  79. {
  80. req->start = Curl_now();
  81. return Curl_req_soft_reset(req, data);
  82. }
  83. static CURLcode req_flush(struct Curl_easy *data);
  84. CURLcode Curl_req_done(struct SingleRequest *req,
  85. struct Curl_easy *data, bool aborted)
  86. {
  87. (void)req;
  88. if(!aborted)
  89. (void)req_flush(data);
  90. Curl_client_reset(data);
  91. return CURLE_OK;
  92. }
  93. void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
  94. {
  95. struct curltime t0 = {0, 0};
  96. /* This is a bit ugly. `req->p` is a union and we assume we can
  97. * free this safely without leaks. */
  98. Curl_safefree(req->p.ftp);
  99. Curl_safefree(req->newurl);
  100. Curl_client_reset(data);
  101. if(req->sendbuf_init)
  102. Curl_bufq_reset(&req->sendbuf);
  103. #ifndef CURL_DISABLE_DOH
  104. if(req->doh) {
  105. Curl_close(&req->doh->probe[0].easy);
  106. Curl_close(&req->doh->probe[1].easy);
  107. }
  108. #endif
  109. /* Can no longer memset() this struct as we need to keep some state */
  110. req->size = -1;
  111. req->maxdownload = -1;
  112. req->bytecount = 0;
  113. req->writebytecount = 0;
  114. req->start = t0;
  115. req->headerbytecount = 0;
  116. req->allheadercount = 0;
  117. req->deductheadercount = 0;
  118. req->headerline = 0;
  119. req->offset = 0;
  120. req->httpcode = 0;
  121. req->keepon = 0;
  122. req->upgr101 = UPGR101_INIT;
  123. req->timeofdoc = 0;
  124. req->location = NULL;
  125. req->newurl = NULL;
  126. #ifndef CURL_DISABLE_COOKIES
  127. req->setcookies = 0;
  128. #endif
  129. req->header = FALSE;
  130. req->content_range = FALSE;
  131. req->download_done = FALSE;
  132. req->eos_written = FALSE;
  133. req->eos_read = FALSE;
  134. req->upload_done = FALSE;
  135. req->upload_aborted = FALSE;
  136. req->ignorebody = FALSE;
  137. req->http_bodyless = FALSE;
  138. req->chunk = FALSE;
  139. req->ignore_cl = FALSE;
  140. req->upload_chunky = FALSE;
  141. req->getheader = FALSE;
  142. req->no_body = data->set.opt_no_body;
  143. req->authneg = FALSE;
  144. req->shutdown = FALSE;
  145. #ifdef USE_HYPER
  146. req->bodywritten = FALSE;
  147. #endif
  148. }
  149. void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
  150. {
  151. /* This is a bit ugly. `req->p` is a union and we assume we can
  152. * free this safely without leaks. */
  153. Curl_safefree(req->p.ftp);
  154. Curl_safefree(req->newurl);
  155. if(req->sendbuf_init)
  156. Curl_bufq_free(&req->sendbuf);
  157. Curl_client_cleanup(data);
  158. #ifndef CURL_DISABLE_DOH
  159. if(req->doh) {
  160. Curl_close(&req->doh->probe[0].easy);
  161. Curl_close(&req->doh->probe[1].easy);
  162. Curl_dyn_free(&req->doh->probe[0].serverdoh);
  163. Curl_dyn_free(&req->doh->probe[1].serverdoh);
  164. curl_slist_free_all(req->doh->headers);
  165. Curl_safefree(req->doh);
  166. }
  167. #endif
  168. }
  169. static CURLcode xfer_send(struct Curl_easy *data,
  170. const char *buf, size_t blen,
  171. size_t hds_len, size_t *pnwritten)
  172. {
  173. CURLcode result = CURLE_OK;
  174. *pnwritten = 0;
  175. #ifdef DEBUGBUILD
  176. {
  177. /* Allow debug builds to override this logic to force short initial
  178. sends
  179. */
  180. char *p = getenv("CURL_SMALLREQSEND");
  181. if(p) {
  182. size_t altsize = (size_t)strtoul(p, NULL, 10);
  183. if(altsize && altsize < blen)
  184. blen = altsize;
  185. }
  186. }
  187. #endif
  188. /* Make sure this doesn't 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. result = Curl_xfer_send(data, buf, blen, pnwritten);
  196. if(!result && *pnwritten) {
  197. if(hds_len)
  198. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)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, (char *)buf + hds_len, body_len);
  203. data->req.writebytecount += body_len;
  204. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  205. }
  206. }
  207. return result;
  208. }
  209. static CURLcode req_send_buffer_flush(struct Curl_easy *data)
  210. {
  211. CURLcode result = CURLE_OK;
  212. const unsigned char *buf;
  213. size_t blen;
  214. while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
  215. size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
  216. result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
  217. if(result)
  218. break;
  219. Curl_bufq_skip(&data->req.sendbuf, nwritten);
  220. if(hds_len) {
  221. data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
  222. }
  223. /* leave if we could not send all. Maybe network blocking or
  224. * speed limits on transfer */
  225. if(nwritten < blen)
  226. break;
  227. }
  228. return result;
  229. }
  230. static CURLcode req_set_upload_done(struct Curl_easy *data)
  231. {
  232. DEBUGASSERT(!data->req.upload_done);
  233. data->req.upload_done = TRUE;
  234. data->req.keepon &= ~(KEEP_SEND|KEEP_SEND_TIMED); /* we're done sending */
  235. Curl_creader_done(data, data->req.upload_aborted);
  236. if(data->req.upload_aborted) {
  237. if(data->req.writebytecount)
  238. infof(data, "abort upload after having sent %" CURL_FORMAT_CURL_OFF_T
  239. " bytes", data->req.writebytecount);
  240. else
  241. infof(data, "abort upload");
  242. }
  243. else if(data->req.writebytecount)
  244. infof(data, "upload completely sent off: %" CURL_FORMAT_CURL_OFF_T
  245. " bytes", data->req.writebytecount);
  246. else if(!data->req.download_done)
  247. infof(data, Curl_creader_total_length(data)?
  248. "We are completely uploaded and fine" :
  249. "Request completely sent off");
  250. return Curl_xfer_send_close(data);
  251. }
  252. static CURLcode req_flush(struct Curl_easy *data)
  253. {
  254. CURLcode result;
  255. if(!data || !data->conn)
  256. return CURLE_FAILED_INIT;
  257. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  258. result = req_send_buffer_flush(data);
  259. if(result)
  260. return result;
  261. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  262. return CURLE_AGAIN;
  263. }
  264. }
  265. if(!data->req.upload_done && data->req.eos_read &&
  266. Curl_bufq_is_empty(&data->req.sendbuf)) {
  267. if(data->req.shutdown) {
  268. bool done;
  269. result = Curl_xfer_send_shutdown(data, &done);
  270. if(result)
  271. return result;
  272. if(!done)
  273. return CURLE_AGAIN;
  274. }
  275. return req_set_upload_done(data);
  276. }
  277. return CURLE_OK;
  278. }
  279. static ssize_t add_from_client(void *reader_ctx,
  280. unsigned char *buf, size_t buflen,
  281. CURLcode *err)
  282. {
  283. struct Curl_easy *data = reader_ctx;
  284. size_t nread;
  285. bool eos;
  286. *err = Curl_client_read(data, (char *)buf, buflen, &nread, &eos);
  287. if(*err)
  288. return -1;
  289. if(eos)
  290. data->req.eos_read = TRUE;
  291. return (ssize_t)nread;
  292. }
  293. #ifndef USE_HYPER
  294. static CURLcode req_send_buffer_add(struct Curl_easy *data,
  295. const char *buf, size_t blen,
  296. size_t hds_len)
  297. {
  298. CURLcode result = CURLE_OK;
  299. ssize_t n;
  300. n = Curl_bufq_write(&data->req.sendbuf,
  301. (const unsigned char *)buf, blen, &result);
  302. if(n < 0)
  303. return result;
  304. /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
  305. DEBUGASSERT((size_t)n == blen);
  306. data->req.sendbuf_hds_len += hds_len;
  307. return CURLE_OK;
  308. }
  309. CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req)
  310. {
  311. CURLcode result;
  312. const char *buf;
  313. size_t blen, nwritten;
  314. if(!data || !data->conn)
  315. return CURLE_FAILED_INIT;
  316. buf = Curl_dyn_ptr(req);
  317. blen = Curl_dyn_len(req);
  318. if(!Curl_creader_total_length(data)) {
  319. /* Request without body. Try to send directly from the buf given. */
  320. data->req.eos_read = TRUE;
  321. result = xfer_send(data, buf, blen, blen, &nwritten);
  322. if(result)
  323. return result;
  324. buf += nwritten;
  325. blen -= nwritten;
  326. }
  327. if(blen) {
  328. /* Either we have a request body, or we could not send the complete
  329. * request in one go. Buffer the remainder and try to add as much
  330. * body bytes as room is left in the buffer. Then flush. */
  331. result = req_send_buffer_add(data, buf, blen, blen);
  332. if(result)
  333. return result;
  334. return Curl_req_send_more(data);
  335. }
  336. return CURLE_OK;
  337. }
  338. #endif /* !USE_HYPER */
  339. bool Curl_req_want_send(struct Curl_easy *data)
  340. {
  341. return data->req.sendbuf_init && !Curl_bufq_is_empty(&data->req.sendbuf);
  342. }
  343. bool Curl_req_done_sending(struct Curl_easy *data)
  344. {
  345. if(data->req.upload_done) {
  346. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  347. return TRUE;
  348. }
  349. return FALSE;
  350. }
  351. CURLcode Curl_req_send_more(struct Curl_easy *data)
  352. {
  353. CURLcode result;
  354. /* Fill our send buffer if more from client can be read. */
  355. if(!data->req.eos_read && !Curl_bufq_is_full(&data->req.sendbuf)) {
  356. ssize_t nread = Curl_bufq_sipn(&data->req.sendbuf, 0,
  357. add_from_client, data, &result);
  358. if(nread < 0 && result != CURLE_AGAIN)
  359. return result;
  360. }
  361. result = req_flush(data);
  362. if(result == CURLE_AGAIN)
  363. result = CURLE_OK;
  364. return result;
  365. }
  366. CURLcode Curl_req_abort_sending(struct Curl_easy *data)
  367. {
  368. if(!data->req.upload_done) {
  369. Curl_bufq_reset(&data->req.sendbuf);
  370. data->req.upload_aborted = TRUE;
  371. return req_set_upload_done(data);
  372. }
  373. return CURLE_OK;
  374. }