request.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "dynbuf.h"
  27. #include "doh.h"
  28. #include "progress.h"
  29. #include "request.h"
  30. #include "sendf.h"
  31. #include "transfer.h"
  32. #include "url.h"
  33. /* The last 3 #include files should be in this order */
  34. #include "curl_printf.h"
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. CURLcode Curl_req_init(struct SingleRequest *req)
  38. {
  39. memset(req, 0, sizeof(*req));
  40. return CURLE_OK;
  41. }
  42. CURLcode Curl_req_start(struct SingleRequest *req,
  43. struct Curl_easy *data)
  44. {
  45. req->start = Curl_now();
  46. Curl_cw_reset(data);
  47. if(!req->sendbuf_init) {
  48. Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
  49. BUFQ_OPT_SOFT_LIMIT);
  50. req->sendbuf_init = TRUE;
  51. }
  52. else {
  53. Curl_bufq_reset(&req->sendbuf);
  54. if(data->set.upload_buffer_size != req->sendbuf.chunk_size) {
  55. Curl_bufq_free(&req->sendbuf);
  56. Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
  57. BUFQ_OPT_SOFT_LIMIT);
  58. }
  59. }
  60. return CURLE_OK;
  61. }
  62. CURLcode Curl_req_done(struct SingleRequest *req,
  63. struct Curl_easy *data, bool aborted)
  64. {
  65. (void)req;
  66. if(!aborted)
  67. (void)Curl_req_flush(data);
  68. Curl_cw_reset(data);
  69. return CURLE_OK;
  70. }
  71. void Curl_req_reset(struct SingleRequest *req, struct Curl_easy *data)
  72. {
  73. struct bufq savebuf;
  74. bool save_init;
  75. /* This is a bit ugly. `req->p` is a union and we assume we can
  76. * free this safely without leaks. */
  77. Curl_safefree(req->p.http);
  78. Curl_safefree(req->newurl);
  79. Curl_cw_reset(data);
  80. #ifndef CURL_DISABLE_DOH
  81. if(req->doh) {
  82. Curl_close(&req->doh->probe[0].easy);
  83. Curl_close(&req->doh->probe[1].easy);
  84. }
  85. #endif
  86. savebuf = req->sendbuf;
  87. save_init = req->sendbuf_init;
  88. memset(req, 0, sizeof(*req));
  89. data->req.size = data->req.maxdownload = -1;
  90. data->req.no_body = data->set.opt_no_body;
  91. if(save_init) {
  92. req->sendbuf = savebuf;
  93. req->sendbuf_init = save_init;
  94. }
  95. }
  96. void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
  97. {
  98. /* This is a bit ugly. `req->p` is a union and we assume we can
  99. * free this safely without leaks. */
  100. Curl_safefree(req->p.http);
  101. Curl_safefree(req->newurl);
  102. if(req->sendbuf_init)
  103. Curl_bufq_free(&req->sendbuf);
  104. Curl_cw_reset(data);
  105. #ifndef CURL_DISABLE_DOH
  106. if(req->doh) {
  107. Curl_close(&req->doh->probe[0].easy);
  108. Curl_close(&req->doh->probe[1].easy);
  109. Curl_dyn_free(&req->doh->probe[0].serverdoh);
  110. Curl_dyn_free(&req->doh->probe[1].serverdoh);
  111. curl_slist_free_all(req->doh->headers);
  112. Curl_safefree(req->doh);
  113. }
  114. #endif
  115. }
  116. static CURLcode req_send(struct Curl_easy *data,
  117. const char *buf, size_t blen,
  118. size_t hds_len, size_t *pnwritten)
  119. {
  120. CURLcode result = CURLE_OK;
  121. *pnwritten = 0;
  122. #ifdef CURLDEBUG
  123. {
  124. /* Allow debug builds to override this logic to force short initial
  125. sends
  126. */
  127. char *p = getenv("CURL_SMALLREQSEND");
  128. if(p) {
  129. size_t altsize = (size_t)strtoul(p, NULL, 10);
  130. if(altsize && altsize < blen)
  131. blen = altsize;
  132. }
  133. }
  134. #endif
  135. /* Make sure this doesn't send more body bytes than what the max send
  136. speed says. The headers do not count to the max speed. */
  137. if(data->set.max_send_speed) {
  138. size_t body_bytes = blen - hds_len;
  139. if((curl_off_t)body_bytes > data->set.max_send_speed)
  140. blen = hds_len + (size_t)data->set.max_send_speed;
  141. }
  142. result = Curl_xfer_send(data, buf, blen, pnwritten);
  143. if(!result && *pnwritten) {
  144. if(hds_len)
  145. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)buf,
  146. CURLMIN(hds_len, *pnwritten));
  147. if(*pnwritten > hds_len) {
  148. size_t body_len = *pnwritten - hds_len;
  149. Curl_debug(data, CURLINFO_DATA_OUT, (char *)buf + hds_len, body_len);
  150. data->req.writebytecount += body_len;
  151. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  152. }
  153. }
  154. return result;
  155. }
  156. static CURLcode req_send_buffer_add(struct Curl_easy *data,
  157. const char *buf, size_t blen,
  158. size_t hds_len)
  159. {
  160. CURLcode result = CURLE_OK;
  161. ssize_t n;
  162. n = Curl_bufq_write(&data->req.sendbuf,
  163. (const unsigned char *)buf, blen, &result);
  164. if(n < 0)
  165. return result;
  166. /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
  167. DEBUGASSERT((size_t)n == blen);
  168. data->req.sendbuf_hds_len += hds_len;
  169. return CURLE_OK;
  170. }
  171. static CURLcode req_send_buffer_flush(struct Curl_easy *data)
  172. {
  173. CURLcode result = CURLE_OK;
  174. const unsigned char *buf;
  175. size_t blen;
  176. while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
  177. size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
  178. result = req_send(data, (const char *)buf, blen, hds_len, &nwritten);
  179. if(result)
  180. break;
  181. Curl_bufq_skip(&data->req.sendbuf, nwritten);
  182. if(hds_len)
  183. data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
  184. /* leave if we could not send all. Maybe network blocking or
  185. * speed limits on transfer */
  186. if(nwritten < blen)
  187. break;
  188. }
  189. return result;
  190. }
  191. CURLcode Curl_req_flush(struct Curl_easy *data)
  192. {
  193. CURLcode result;
  194. if(!data || !data->conn)
  195. return CURLE_FAILED_INIT;
  196. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  197. result = req_send_buffer_flush(data);
  198. if(result)
  199. return result;
  200. if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
  201. return CURLE_AGAIN;
  202. }
  203. }
  204. return CURLE_OK;
  205. }
  206. CURLcode Curl_req_send(struct Curl_easy *data,
  207. const char *buf, size_t blen,
  208. size_t hds_len)
  209. {
  210. CURLcode result;
  211. if(!data || !data->conn)
  212. return CURLE_FAILED_INIT;
  213. /* We always buffer and send from there. The reason is that on
  214. * blocking, we can retry using the same memory address. This is
  215. * important for TLS libraries that expect this.
  216. * We *could* optimized for non-TLS transfers, but that would mean
  217. * separate code paths and seems not worth it. */
  218. result = req_send_buffer_add(data, buf, blen, hds_len);
  219. if(result)
  220. return result;
  221. result = req_send_buffer_flush(data);
  222. if(result == CURLE_AGAIN)
  223. result = CURLE_OK;
  224. return result;
  225. }
  226. bool Curl_req_want_send(struct Curl_easy *data)
  227. {
  228. return data->req.sendbuf_init && !Curl_bufq_is_empty(&data->req.sendbuf);
  229. }