2
0

request.c 11 KB

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