2
0

request.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. Curl_doh_close(data);
  105. #endif
  106. /* Can no longer memset() this struct as we need to keep some state */
  107. req->size = -1;
  108. req->maxdownload = -1;
  109. req->bytecount = 0;
  110. req->writebytecount = 0;
  111. req->start = t0;
  112. req->headerbytecount = 0;
  113. req->allheadercount = 0;
  114. req->deductheadercount = 0;
  115. req->headerline = 0;
  116. req->offset = 0;
  117. req->httpcode = 0;
  118. req->keepon = 0;
  119. req->upgr101 = UPGR101_INIT;
  120. req->timeofdoc = 0;
  121. req->location = NULL;
  122. req->newurl = NULL;
  123. #ifndef CURL_DISABLE_COOKIES
  124. req->setcookies = 0;
  125. #endif
  126. req->header = FALSE;
  127. req->content_range = FALSE;
  128. req->download_done = FALSE;
  129. req->eos_written = FALSE;
  130. req->eos_read = FALSE;
  131. req->upload_done = FALSE;
  132. req->upload_aborted = FALSE;
  133. req->ignorebody = FALSE;
  134. req->http_bodyless = FALSE;
  135. req->chunk = FALSE;
  136. req->ignore_cl = FALSE;
  137. req->upload_chunky = FALSE;
  138. req->getheader = FALSE;
  139. req->no_body = data->set.opt_no_body;
  140. req->authneg = FALSE;
  141. req->shutdown = FALSE;
  142. #ifdef USE_HYPER
  143. req->bodywritten = FALSE;
  144. #endif
  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.ftp);
  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. Curl_doh_cleanup(data);
  157. #endif
  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. *pnwritten = 0;
  165. #ifdef DEBUGBUILD
  166. {
  167. /* Allow debug builds to override this logic to force short initial
  168. sends
  169. */
  170. char *p = getenv("CURL_SMALLREQSEND");
  171. if(p) {
  172. size_t altsize = (size_t)strtoul(p, NULL, 10);
  173. if(altsize && altsize < blen)
  174. blen = altsize;
  175. }
  176. }
  177. #endif
  178. /* Make sure this does not send more body bytes than what the max send
  179. speed says. The headers do not count to the max speed. */
  180. if(data->set.max_send_speed) {
  181. size_t body_bytes = blen - hds_len;
  182. if((curl_off_t)body_bytes > data->set.max_send_speed)
  183. blen = hds_len + (size_t)data->set.max_send_speed;
  184. }
  185. result = Curl_xfer_send(data, buf, blen, pnwritten);
  186. if(!result && *pnwritten) {
  187. if(hds_len)
  188. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)buf,
  189. CURLMIN(hds_len, *pnwritten));
  190. if(*pnwritten > hds_len) {
  191. size_t body_len = *pnwritten - hds_len;
  192. Curl_debug(data, CURLINFO_DATA_OUT, (char *)buf + hds_len, body_len);
  193. data->req.writebytecount += body_len;
  194. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  195. }
  196. }
  197. return result;
  198. }
  199. static CURLcode req_send_buffer_flush(struct Curl_easy *data)
  200. {
  201. CURLcode result = CURLE_OK;
  202. const unsigned char *buf;
  203. size_t blen;
  204. while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
  205. size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
  206. result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
  207. if(result)
  208. break;
  209. Curl_bufq_skip(&data->req.sendbuf, nwritten);
  210. if(hds_len) {
  211. data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
  212. }
  213. /* leave if we could not send all. Maybe network blocking or
  214. * speed limits on transfer */
  215. if(nwritten < blen)
  216. break;
  217. }
  218. return result;
  219. }
  220. static CURLcode req_set_upload_done(struct Curl_easy *data)
  221. {
  222. DEBUGASSERT(!data->req.upload_done);
  223. data->req.upload_done = TRUE;
  224. data->req.keepon &= ~(KEEP_SEND|KEEP_SEND_TIMED); /* we are done sending */
  225. Curl_creader_done(data, data->req.upload_aborted);
  226. if(data->req.upload_aborted) {
  227. if(data->req.writebytecount)
  228. infof(data, "abort upload after having sent %" CURL_FORMAT_CURL_OFF_T
  229. " bytes", data->req.writebytecount);
  230. else
  231. infof(data, "abort upload");
  232. }
  233. else if(data->req.writebytecount)
  234. infof(data, "upload completely sent off: %" CURL_FORMAT_CURL_OFF_T
  235. " bytes", data->req.writebytecount);
  236. else if(!data->req.download_done)
  237. infof(data, Curl_creader_total_length(data)?
  238. "We are completely uploaded and fine" :
  239. "Request completely sent off");
  240. return Curl_xfer_send_close(data);
  241. }
  242. static CURLcode req_flush(struct Curl_easy *data)
  243. {
  244. CURLcode result;
  245. if(!data || !data->conn)
  246. return CURLE_FAILED_INIT;
  247. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  248. result = req_send_buffer_flush(data);
  249. if(result)
  250. return result;
  251. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  252. return CURLE_AGAIN;
  253. }
  254. }
  255. if(!data->req.upload_done && data->req.eos_read &&
  256. Curl_bufq_is_empty(&data->req.sendbuf)) {
  257. if(data->req.shutdown) {
  258. bool done;
  259. result = Curl_xfer_send_shutdown(data, &done);
  260. if(result)
  261. return result;
  262. if(!done)
  263. return CURLE_AGAIN;
  264. }
  265. return req_set_upload_done(data);
  266. }
  267. return CURLE_OK;
  268. }
  269. static ssize_t add_from_client(void *reader_ctx,
  270. unsigned char *buf, size_t buflen,
  271. CURLcode *err)
  272. {
  273. struct Curl_easy *data = reader_ctx;
  274. size_t nread;
  275. bool eos;
  276. *err = Curl_client_read(data, (char *)buf, buflen, &nread, &eos);
  277. if(*err)
  278. return -1;
  279. if(eos)
  280. data->req.eos_read = TRUE;
  281. return (ssize_t)nread;
  282. }
  283. #ifndef USE_HYPER
  284. static CURLcode req_send_buffer_add(struct Curl_easy *data,
  285. const char *buf, size_t blen,
  286. size_t hds_len)
  287. {
  288. CURLcode result = CURLE_OK;
  289. ssize_t n;
  290. n = Curl_bufq_write(&data->req.sendbuf,
  291. (const unsigned char *)buf, blen, &result);
  292. if(n < 0)
  293. return result;
  294. /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
  295. DEBUGASSERT((size_t)n == blen);
  296. data->req.sendbuf_hds_len += hds_len;
  297. return CURLE_OK;
  298. }
  299. CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req)
  300. {
  301. CURLcode result;
  302. const char *buf;
  303. size_t blen, nwritten;
  304. if(!data || !data->conn)
  305. return CURLE_FAILED_INIT;
  306. buf = Curl_dyn_ptr(req);
  307. blen = Curl_dyn_len(req);
  308. if(!Curl_creader_total_length(data)) {
  309. /* Request without body. Try to send directly from the buf given. */
  310. data->req.eos_read = TRUE;
  311. result = xfer_send(data, buf, blen, blen, &nwritten);
  312. if(result)
  313. return result;
  314. buf += nwritten;
  315. blen -= nwritten;
  316. }
  317. if(blen) {
  318. /* Either we have a request body, or we could not send the complete
  319. * request in one go. Buffer the remainder and try to add as much
  320. * body bytes as room is left in the buffer. Then flush. */
  321. result = req_send_buffer_add(data, buf, blen, blen);
  322. if(result)
  323. return result;
  324. return Curl_req_send_more(data);
  325. }
  326. return CURLE_OK;
  327. }
  328. #endif /* !USE_HYPER */
  329. bool Curl_req_want_send(struct Curl_easy *data)
  330. {
  331. return data->req.sendbuf_init && !Curl_bufq_is_empty(&data->req.sendbuf);
  332. }
  333. bool Curl_req_done_sending(struct Curl_easy *data)
  334. {
  335. if(data->req.upload_done) {
  336. DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
  337. return TRUE;
  338. }
  339. return FALSE;
  340. }
  341. CURLcode Curl_req_send_more(struct Curl_easy *data)
  342. {
  343. CURLcode result;
  344. /* Fill our send buffer if more from client can be read. */
  345. if(!data->req.eos_read && !Curl_bufq_is_full(&data->req.sendbuf)) {
  346. ssize_t nread = Curl_bufq_sipn(&data->req.sendbuf, 0,
  347. add_from_client, data, &result);
  348. if(nread < 0 && result != CURLE_AGAIN)
  349. return result;
  350. }
  351. result = req_flush(data);
  352. if(result == CURLE_AGAIN)
  353. result = CURLE_OK;
  354. return result;
  355. }
  356. CURLcode Curl_req_abort_sending(struct Curl_easy *data)
  357. {
  358. if(!data->req.upload_done) {
  359. Curl_bufq_reset(&data->req.sendbuf);
  360. data->req.upload_aborted = TRUE;
  361. return req_set_upload_done(data);
  362. }
  363. return CURLE_OK;
  364. }