request.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. CURLcode Curl_req_init(struct SingleRequest *req)
  40. {
  41. memset(req, 0, sizeof(*req));
  42. return CURLE_OK;
  43. }
  44. CURLcode Curl_req_soft_reset(struct SingleRequest *req,
  45. struct Curl_easy *data)
  46. {
  47. CURLcode result;
  48. req->done = FALSE;
  49. req->upload_done = FALSE;
  50. req->download_done = FALSE;
  51. req->ignorebody = 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.http);
  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->bodywrites = 0;
  125. req->location = NULL;
  126. req->newurl = NULL;
  127. #ifndef CURL_DISABLE_COOKIES
  128. req->setcookies = 0;
  129. #endif
  130. req->header = FALSE;
  131. req->content_range = FALSE;
  132. req->download_done = FALSE;
  133. req->eos_written = FALSE;
  134. req->eos_read = FALSE;
  135. req->upload_done = FALSE;
  136. req->upload_aborted = FALSE;
  137. req->ignorebody = FALSE;
  138. req->http_bodyless = FALSE;
  139. req->chunk = FALSE;
  140. req->ignore_cl = FALSE;
  141. req->upload_chunky = FALSE;
  142. req->getheader = FALSE;
  143. req->no_body = data->set.opt_no_body;
  144. req->authneg = FALSE;
  145. }
  146. void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
  147. {
  148. /* This is a bit ugly. `req->p` is a union and we assume we can
  149. * free this safely without leaks. */
  150. Curl_safefree(req->p.http);
  151. Curl_safefree(req->newurl);
  152. if(req->sendbuf_init)
  153. Curl_bufq_free(&req->sendbuf);
  154. Curl_client_cleanup(data);
  155. #ifndef CURL_DISABLE_DOH
  156. if(req->doh) {
  157. Curl_close(&req->doh->probe[0].easy);
  158. Curl_close(&req->doh->probe[1].easy);
  159. Curl_dyn_free(&req->doh->probe[0].serverdoh);
  160. Curl_dyn_free(&req->doh->probe[1].serverdoh);
  161. curl_slist_free_all(req->doh->headers);
  162. Curl_safefree(req->doh);
  163. }
  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. *pnwritten = 0;
  172. #ifdef CURLDEBUG
  173. {
  174. /* Allow debug builds to override this logic to force short initial
  175. sends
  176. */
  177. char *p = getenv("CURL_SMALLREQSEND");
  178. if(p) {
  179. size_t altsize = (size_t)strtoul(p, NULL, 10);
  180. if(altsize && altsize < blen)
  181. blen = altsize;
  182. }
  183. }
  184. #endif
  185. /* Make sure this doesn't send more body bytes than what the max send
  186. speed says. The headers do not count to the max speed. */
  187. if(data->set.max_send_speed) {
  188. size_t body_bytes = blen - hds_len;
  189. if((curl_off_t)body_bytes > data->set.max_send_speed)
  190. blen = hds_len + (size_t)data->set.max_send_speed;
  191. }
  192. result = Curl_xfer_send(data, buf, blen, pnwritten);
  193. if(!result && *pnwritten) {
  194. if(hds_len)
  195. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)buf,
  196. CURLMIN(hds_len, *pnwritten));
  197. if(*pnwritten > hds_len) {
  198. size_t body_len = *pnwritten - hds_len;
  199. Curl_debug(data, CURLINFO_DATA_OUT, (char *)buf + hds_len, body_len);
  200. data->req.writebytecount += body_len;
  201. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  202. }
  203. }
  204. return result;
  205. }
  206. static CURLcode req_send_buffer_flush(struct Curl_easy *data)
  207. {
  208. CURLcode result = CURLE_OK;
  209. const unsigned char *buf;
  210. size_t blen;
  211. while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
  212. size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
  213. result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
  214. if(result)
  215. break;
  216. Curl_bufq_skip(&data->req.sendbuf, nwritten);
  217. if(hds_len) {
  218. data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
  219. }
  220. /* leave if we could not send all. Maybe network blocking or
  221. * speed limits on transfer */
  222. if(nwritten < blen)
  223. break;
  224. }
  225. return result;
  226. }
  227. static CURLcode req_set_upload_done(struct Curl_easy *data)
  228. {
  229. DEBUGASSERT(!data->req.upload_done);
  230. data->req.upload_done = TRUE;
  231. data->req.keepon &= ~(KEEP_SEND|KEEP_SEND_TIMED); /* we're done sending */
  232. Curl_creader_done(data, data->req.upload_aborted);
  233. if(data->req.upload_aborted) {
  234. if(data->req.writebytecount)
  235. infof(data, "abort upload after having sent %" CURL_FORMAT_CURL_OFF_T
  236. " bytes", data->req.writebytecount);
  237. else
  238. infof(data, "abort upload");
  239. }
  240. else if(data->req.writebytecount)
  241. infof(data, "upload completely sent off: %" CURL_FORMAT_CURL_OFF_T
  242. " bytes", data->req.writebytecount);
  243. else
  244. infof(data, Curl_creader_total_length(data)?
  245. "We are completely uploaded and fine" :
  246. "Request completely sent off");
  247. return Curl_xfer_send_close(data);
  248. }
  249. static CURLcode req_flush(struct Curl_easy *data)
  250. {
  251. CURLcode result;
  252. if(!data || !data->conn)
  253. return CURLE_FAILED_INIT;
  254. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  255. result = req_send_buffer_flush(data);
  256. if(result)
  257. return result;
  258. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  259. return CURLE_AGAIN;
  260. }
  261. }
  262. if(!data->req.upload_done && data->req.eos_read &&
  263. Curl_bufq_is_empty(&data->req.sendbuf)) {
  264. return req_set_upload_done(data);
  265. }
  266. return CURLE_OK;
  267. }
  268. static ssize_t add_from_client(void *reader_ctx,
  269. unsigned char *buf, size_t buflen,
  270. CURLcode *err)
  271. {
  272. struct Curl_easy *data = reader_ctx;
  273. size_t nread;
  274. bool eos;
  275. *err = Curl_client_read(data, (char *)buf, buflen, &nread, &eos);
  276. if(*err)
  277. return -1;
  278. if(eos)
  279. data->req.eos_read = TRUE;
  280. return (ssize_t)nread;
  281. }
  282. #ifndef USE_HYPER
  283. static CURLcode req_send_buffer_add(struct Curl_easy *data,
  284. const char *buf, size_t blen,
  285. size_t hds_len)
  286. {
  287. CURLcode result = CURLE_OK;
  288. ssize_t n;
  289. n = Curl_bufq_write(&data->req.sendbuf,
  290. (const unsigned char *)buf, blen, &result);
  291. if(n < 0)
  292. return result;
  293. /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
  294. DEBUGASSERT((size_t)n == blen);
  295. data->req.sendbuf_hds_len += hds_len;
  296. return CURLE_OK;
  297. }
  298. CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req)
  299. {
  300. CURLcode result;
  301. const char *buf;
  302. size_t blen, nwritten;
  303. if(!data || !data->conn)
  304. return CURLE_FAILED_INIT;
  305. buf = Curl_dyn_ptr(req);
  306. blen = Curl_dyn_len(req);
  307. if(!Curl_creader_total_length(data)) {
  308. /* Request without body. Try to send directly from the buf given. */
  309. data->req.eos_read = TRUE;
  310. result = xfer_send(data, buf, blen, blen, &nwritten);
  311. if(result)
  312. return result;
  313. buf += nwritten;
  314. blen -= nwritten;
  315. }
  316. if(blen) {
  317. /* Either we have a request body, or we could not send the complete
  318. * request in one go. Buffer the remainder and try to add as much
  319. * body bytes as room is left in the buffer. Then flush. */
  320. result = req_send_buffer_add(data, buf, blen, blen);
  321. if(result)
  322. return result;
  323. return Curl_req_send_more(data);
  324. }
  325. return CURLE_OK;
  326. }
  327. #endif /* !USE_HYPER */
  328. bool Curl_req_want_send(struct Curl_easy *data)
  329. {
  330. return data->req.sendbuf_init && !Curl_bufq_is_empty(&data->req.sendbuf);
  331. }
  332. bool Curl_req_done_sending(struct Curl_easy *data)
  333. {
  334. if(data->req.upload_done) {
  335. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  336. return TRUE;
  337. }
  338. return FALSE;
  339. }
  340. CURLcode Curl_req_send_more(struct Curl_easy *data)
  341. {
  342. CURLcode result;
  343. /* Fill our send buffer if more from client can be read. */
  344. if(!data->req.eos_read && !Curl_bufq_is_full(&data->req.sendbuf)) {
  345. ssize_t nread = Curl_bufq_sipn(&data->req.sendbuf, 0,
  346. add_from_client, data, &result);
  347. if(nread < 0 && result != CURLE_AGAIN)
  348. return result;
  349. }
  350. result = req_flush(data);
  351. if(result == CURLE_AGAIN)
  352. result = CURLE_OK;
  353. return result;
  354. }
  355. CURLcode Curl_req_abort_sending(struct Curl_easy *data)
  356. {
  357. if(!data->req.upload_done) {
  358. Curl_bufq_reset(&data->req.sendbuf);
  359. data->req.upload_aborted = TRUE;
  360. return req_set_upload_done(data);
  361. }
  362. return CURLE_OK;
  363. }