/src/CMake/Utilities/cmcurl/lib/request.c
Line | Count | Source |
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 | | |
26 | | #include "urldata.h" |
27 | | #include "cfilters.h" |
28 | | #include "curlx/dynbuf.h" |
29 | | #include "doh.h" |
30 | | #include "progress.h" |
31 | | #include "request.h" |
32 | | #include "sendf.h" |
33 | | #include "curl_trc.h" |
34 | | #include "transfer.h" |
35 | | #include "url.h" |
36 | | #include "curlx/strparse.h" |
37 | | |
38 | | void Curl_req_init(struct SingleRequest *req) |
39 | 0 | { |
40 | 0 | memset(req, 0, sizeof(*req)); |
41 | 0 | } |
42 | | |
43 | | CURLcode Curl_req_soft_reset(struct SingleRequest *req, |
44 | | struct Curl_easy *data) |
45 | 0 | { |
46 | 0 | CURLcode result; |
47 | |
|
48 | 0 | req->done = FALSE; |
49 | 0 | req->upload_done = FALSE; |
50 | 0 | req->upload_aborted = FALSE; |
51 | 0 | req->download_done = FALSE; |
52 | 0 | req->eos_written = FALSE; |
53 | 0 | req->eos_read = FALSE; |
54 | 0 | req->eos_sent = FALSE; |
55 | 0 | req->ignorebody = FALSE; |
56 | 0 | req->shutdown = FALSE; |
57 | 0 | req->bytecount = 0; |
58 | 0 | req->writebytecount = 0; |
59 | 0 | req->header = FALSE; |
60 | 0 | req->headerline = 0; |
61 | 0 | req->headerbytecount = 0; |
62 | 0 | req->allheadercount = 0; |
63 | 0 | req->deductheadercount = 0; |
64 | 0 | req->httpversion_sent = 0; |
65 | 0 | req->httpversion = 0; |
66 | 0 | req->sendbuf_hds_len = 0; |
67 | |
|
68 | 0 | result = Curl_client_start(data); |
69 | 0 | if(result) |
70 | 0 | return result; |
71 | | |
72 | 0 | if(!req->sendbuf_init) { |
73 | 0 | Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1, |
74 | 0 | BUFQ_OPT_SOFT_LIMIT); |
75 | 0 | req->sendbuf_init = TRUE; |
76 | 0 | } |
77 | 0 | else { |
78 | 0 | Curl_bufq_reset(&req->sendbuf); |
79 | 0 | if(data->set.upload_buffer_size != req->sendbuf.chunk_size) { |
80 | 0 | Curl_bufq_free(&req->sendbuf); |
81 | 0 | Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1, |
82 | 0 | BUFQ_OPT_SOFT_LIMIT); |
83 | 0 | } |
84 | 0 | } |
85 | |
|
86 | 0 | return CURLE_OK; |
87 | 0 | } |
88 | | |
89 | | CURLcode Curl_req_start(struct SingleRequest *req, |
90 | | struct Curl_easy *data) |
91 | 0 | { |
92 | 0 | req->start = *Curl_pgrs_now(data); |
93 | 0 | return Curl_req_soft_reset(req, data); |
94 | 0 | } |
95 | | |
96 | | static CURLcode req_flush(struct Curl_easy *data); |
97 | | |
98 | | CURLcode Curl_req_done(struct SingleRequest *req, |
99 | | struct Curl_easy *data, bool aborted) |
100 | 0 | { |
101 | 0 | (void)req; |
102 | 0 | if(!aborted) |
103 | 0 | (void)req_flush(data); |
104 | 0 | Curl_client_reset(data); |
105 | 0 | #ifndef CURL_DISABLE_DOH |
106 | 0 | Curl_doh_close(data); |
107 | 0 | #endif |
108 | 0 | return CURLE_OK; |
109 | 0 | } |
110 | | |
111 | | void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data) |
112 | 0 | { |
113 | 0 | struct curltime t0 = { 0, 0 }; |
114 | |
|
115 | 0 | Curl_safefree(req->newurl); |
116 | 0 | Curl_client_reset(data); |
117 | 0 | if(req->sendbuf_init) |
118 | 0 | Curl_bufq_reset(&req->sendbuf); |
119 | |
|
120 | 0 | #ifndef CURL_DISABLE_DOH |
121 | 0 | Curl_doh_close(data); |
122 | 0 | #endif |
123 | | /* Can no longer memset() this struct as we need to keep some state */ |
124 | 0 | req->size = -1; |
125 | 0 | req->maxdownload = -1; |
126 | 0 | req->bytecount = 0; |
127 | 0 | req->writebytecount = 0; |
128 | 0 | req->start = t0; |
129 | 0 | req->headerbytecount = 0; |
130 | 0 | req->allheadercount = 0; |
131 | 0 | req->deductheadercount = 0; |
132 | 0 | req->headerline = 0; |
133 | 0 | req->offset = 0; |
134 | 0 | req->httpcode = 0; |
135 | 0 | req->keepon = 0; |
136 | 0 | req->upgr101 = UPGR101_NONE; |
137 | 0 | req->sendbuf_hds_len = 0; |
138 | 0 | req->timeofdoc = 0; |
139 | 0 | req->location = NULL; |
140 | 0 | req->newurl = NULL; |
141 | 0 | #ifndef CURL_DISABLE_COOKIES |
142 | 0 | req->setcookies = 0; |
143 | 0 | #endif |
144 | 0 | req->header = FALSE; |
145 | 0 | req->content_range = FALSE; |
146 | 0 | req->download_done = FALSE; |
147 | 0 | req->eos_written = FALSE; |
148 | 0 | req->eos_read = FALSE; |
149 | 0 | req->eos_sent = FALSE; |
150 | 0 | req->rewind_read = FALSE; |
151 | 0 | req->upload_done = FALSE; |
152 | 0 | req->upload_aborted = FALSE; |
153 | 0 | req->ignorebody = FALSE; |
154 | 0 | req->http_bodyless = FALSE; |
155 | 0 | req->chunk = FALSE; |
156 | 0 | req->ignore_cl = FALSE; |
157 | 0 | req->upload_chunky = FALSE; |
158 | 0 | req->no_body = data->set.opt_no_body; |
159 | 0 | req->authneg = FALSE; |
160 | 0 | req->shutdown = FALSE; |
161 | | /* Unpause all directions */ |
162 | 0 | Curl_rlimit_block(&data->progress.dl.rlimit, FALSE, &t0); |
163 | 0 | Curl_rlimit_block(&data->progress.ul.rlimit, FALSE, &t0); |
164 | 0 | } |
165 | | |
166 | | void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data) |
167 | 0 | { |
168 | 0 | Curl_safefree(req->newurl); |
169 | 0 | if(req->sendbuf_init) |
170 | 0 | Curl_bufq_free(&req->sendbuf); |
171 | 0 | Curl_client_cleanup(data); |
172 | 0 | } |
173 | | |
174 | | static CURLcode xfer_send(struct Curl_easy *data, |
175 | | const char *buf, size_t blen, |
176 | | size_t hds_len, size_t *pnwritten) |
177 | 0 | { |
178 | 0 | CURLcode result = CURLE_OK; |
179 | 0 | bool eos = FALSE; |
180 | |
|
181 | 0 | *pnwritten = 0; |
182 | 0 | DEBUGASSERT(hds_len <= blen); |
183 | | #ifdef DEBUGBUILD |
184 | | { |
185 | | /* Allow debug builds to override this logic to force short initial |
186 | | sends */ |
187 | | size_t body_len = blen - hds_len; |
188 | | if(body_len) { |
189 | | const char *p = getenv("CURL_SMALLREQSEND"); |
190 | | if(p) { |
191 | | curl_off_t body_small; |
192 | | if(!curlx_str_number(&p, &body_small, body_len)) |
193 | | blen = hds_len + (size_t)body_small; |
194 | | } |
195 | | } |
196 | | } |
197 | | #endif |
198 | | /* Make sure this does not send more body bytes than what the max send |
199 | | speed says. The headers do not count to the max speed. */ |
200 | 0 | if(data->set.max_send_speed) { |
201 | 0 | size_t body_bytes = blen - hds_len; |
202 | 0 | if((curl_off_t)body_bytes > data->set.max_send_speed) |
203 | 0 | blen = hds_len + (size_t)data->set.max_send_speed; |
204 | 0 | } |
205 | |
|
206 | 0 | if(data->req.eos_read && |
207 | 0 | (Curl_bufq_is_empty(&data->req.sendbuf) || |
208 | 0 | Curl_bufq_len(&data->req.sendbuf) == blen)) { |
209 | 0 | DEBUGF(infof(data, "sending last upload chunk of %zu bytes", blen)); |
210 | 0 | eos = TRUE; |
211 | 0 | } |
212 | 0 | result = Curl_xfer_send(data, buf, blen, eos, pnwritten); |
213 | 0 | if(!result) { |
214 | 0 | if(eos && (blen == *pnwritten)) |
215 | 0 | data->req.eos_sent = TRUE; |
216 | 0 | if(*pnwritten) { |
217 | 0 | if(hds_len) |
218 | 0 | Curl_debug(data, CURLINFO_HEADER_OUT, buf, |
219 | 0 | CURLMIN(hds_len, *pnwritten)); |
220 | 0 | if(*pnwritten > hds_len) { |
221 | 0 | size_t body_len = *pnwritten - hds_len; |
222 | 0 | Curl_debug(data, CURLINFO_DATA_OUT, buf + hds_len, body_len); |
223 | 0 | data->req.writebytecount += body_len; |
224 | 0 | Curl_pgrs_upload_inc(data, body_len); |
225 | 0 | } |
226 | 0 | } |
227 | 0 | } |
228 | 0 | return result; |
229 | 0 | } |
230 | | |
231 | | static CURLcode req_send_buffer_flush(struct Curl_easy *data) |
232 | 0 | { |
233 | 0 | CURLcode result = CURLE_OK; |
234 | 0 | const unsigned char *buf; |
235 | 0 | size_t blen; |
236 | |
|
237 | 0 | while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) { |
238 | 0 | size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen); |
239 | 0 | result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten); |
240 | 0 | if(result) |
241 | 0 | break; |
242 | | |
243 | 0 | Curl_bufq_skip(&data->req.sendbuf, nwritten); |
244 | 0 | if(hds_len) { |
245 | 0 | data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten); |
246 | 0 | } |
247 | | /* leave if we could not send all. Maybe network blocking or |
248 | | * speed limits on transfer */ |
249 | 0 | if(nwritten < blen) |
250 | 0 | break; |
251 | 0 | } |
252 | 0 | return result; |
253 | 0 | } |
254 | | |
255 | | static CURLcode req_set_upload_done(struct Curl_easy *data) |
256 | 0 | { |
257 | 0 | DEBUGASSERT(!data->req.upload_done); |
258 | 0 | data->req.upload_done = TRUE; |
259 | 0 | data->req.keepon &= ~KEEP_SEND; /* we are done sending */ |
260 | |
|
261 | 0 | Curl_pgrsTime(data, TIMER_POSTRANSFER); |
262 | 0 | Curl_creader_done(data, data->req.upload_aborted); |
263 | |
|
264 | 0 | if(data->req.upload_aborted) { |
265 | 0 | Curl_bufq_reset(&data->req.sendbuf); |
266 | 0 | if(data->req.writebytecount) |
267 | 0 | infof(data, "abort upload after having sent %" FMT_OFF_T " bytes", |
268 | 0 | data->req.writebytecount); |
269 | 0 | else |
270 | 0 | infof(data, "abort upload"); |
271 | 0 | } |
272 | 0 | else if(data->req.writebytecount) |
273 | 0 | infof(data, "upload completely sent off: %" FMT_OFF_T " bytes", |
274 | 0 | data->req.writebytecount); |
275 | 0 | else if(!data->req.download_done) { |
276 | 0 | DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf)); |
277 | 0 | infof(data, Curl_creader_total_length(data) ? |
278 | 0 | "We are completely uploaded and fine" : |
279 | 0 | "Request completely sent off"); |
280 | 0 | } |
281 | |
|
282 | 0 | return Curl_xfer_send_close(data); |
283 | 0 | } |
284 | | |
285 | | static CURLcode req_flush(struct Curl_easy *data) |
286 | 0 | { |
287 | 0 | CURLcode result; |
288 | |
|
289 | 0 | if(!data || !data->conn) |
290 | 0 | return CURLE_FAILED_INIT; |
291 | | |
292 | 0 | if(!Curl_bufq_is_empty(&data->req.sendbuf)) { |
293 | 0 | result = req_send_buffer_flush(data); |
294 | 0 | if(result) |
295 | 0 | return result; |
296 | 0 | if(!Curl_bufq_is_empty(&data->req.sendbuf)) { |
297 | 0 | DEBUGF(infof(data, "Curl_req_flush(len=%zu) -> EAGAIN", |
298 | 0 | Curl_bufq_len(&data->req.sendbuf))); |
299 | 0 | return CURLE_AGAIN; |
300 | 0 | } |
301 | 0 | } |
302 | 0 | else if(Curl_xfer_needs_flush(data)) { |
303 | 0 | DEBUGF(infof(data, "Curl_req_flush(), xfer send_pending")); |
304 | 0 | return Curl_xfer_flush(data); |
305 | 0 | } |
306 | | |
307 | 0 | if(data->req.eos_read && !data->req.eos_sent) { |
308 | 0 | char tmp = 0; |
309 | 0 | size_t nwritten; |
310 | 0 | result = xfer_send(data, &tmp, 0, 0, &nwritten); |
311 | 0 | if(result) |
312 | 0 | return result; |
313 | 0 | DEBUGASSERT(data->req.eos_sent); |
314 | 0 | } |
315 | | |
316 | 0 | if(!data->req.upload_done && data->req.eos_read && data->req.eos_sent) { |
317 | 0 | DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf)); |
318 | 0 | if(data->req.shutdown) { |
319 | 0 | bool done; |
320 | 0 | result = Curl_xfer_send_shutdown(data, &done); |
321 | 0 | if(result && data->req.shutdown_err_ignore) { |
322 | 0 | infof(data, "Shutdown send direction error: %d. Broken server? " |
323 | 0 | "Proceeding as if everything is ok.", result); |
324 | 0 | result = CURLE_OK; |
325 | 0 | done = TRUE; |
326 | 0 | } |
327 | |
|
328 | 0 | if(result) |
329 | 0 | return result; |
330 | 0 | if(!done) |
331 | 0 | return CURLE_AGAIN; |
332 | 0 | } |
333 | 0 | return req_set_upload_done(data); |
334 | 0 | } |
335 | 0 | return CURLE_OK; |
336 | 0 | } |
337 | | |
338 | | static CURLcode add_from_client(void *reader_ctx, |
339 | | unsigned char *buf, size_t buflen, |
340 | | size_t *pnread) |
341 | 0 | { |
342 | 0 | struct Curl_easy *data = reader_ctx; |
343 | 0 | CURLcode result; |
344 | 0 | bool eos; |
345 | |
|
346 | 0 | result = Curl_client_read(data, (char *)buf, buflen, pnread, &eos); |
347 | 0 | if(!result && eos) |
348 | 0 | data->req.eos_read = TRUE; |
349 | 0 | return result; |
350 | 0 | } |
351 | | |
352 | | static CURLcode req_send_buffer_add(struct Curl_easy *data, |
353 | | const char *buf, size_t blen, |
354 | | size_t hds_len) |
355 | 0 | { |
356 | 0 | CURLcode result = CURLE_OK; |
357 | 0 | size_t n; |
358 | 0 | result = Curl_bufq_cwrite(&data->req.sendbuf, buf, blen, &n); |
359 | 0 | if(result) |
360 | 0 | return result; |
361 | | /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */ |
362 | 0 | DEBUGASSERT(n == blen); |
363 | 0 | data->req.sendbuf_hds_len += hds_len; |
364 | 0 | return CURLE_OK; |
365 | 0 | } |
366 | | |
367 | | CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req, |
368 | | unsigned char httpversion) |
369 | 0 | { |
370 | 0 | CURLcode result; |
371 | 0 | const char *buf; |
372 | 0 | size_t blen, nwritten; |
373 | |
|
374 | 0 | if(!data || !data->conn) |
375 | 0 | return CURLE_FAILED_INIT; |
376 | | |
377 | 0 | data->req.httpversion_sent = httpversion; |
378 | 0 | buf = curlx_dyn_ptr(req); |
379 | 0 | blen = curlx_dyn_len(req); |
380 | | /* if the sendbuf is empty and the request without body and |
381 | | * the length to send fits info a sendbuf chunk, we send it directly. |
382 | | * If `blen` is larger then `chunk_size`, we can not. Because we |
383 | | * might have to retry a blocked send later from sendbuf and that |
384 | | * would result in retry sends with a shrunken length. That is trouble. */ |
385 | 0 | if(Curl_bufq_is_empty(&data->req.sendbuf) && |
386 | 0 | !Curl_creader_total_length(data) && |
387 | 0 | (blen <= data->req.sendbuf.chunk_size)) { |
388 | 0 | data->req.eos_read = TRUE; |
389 | 0 | result = xfer_send(data, buf, blen, blen, &nwritten); |
390 | 0 | if(result) |
391 | 0 | return result; |
392 | 0 | buf += nwritten; |
393 | 0 | blen -= nwritten; |
394 | 0 | if(!blen) { |
395 | 0 | result = req_set_upload_done(data); |
396 | 0 | if(result) |
397 | 0 | return result; |
398 | 0 | } |
399 | 0 | } |
400 | | |
401 | 0 | if(blen) { |
402 | | /* Either we have a request body, or we could not send the complete |
403 | | * request in one go. Buffer the remainder and try to add as much |
404 | | * body bytes as room is left in the buffer. Then flush. */ |
405 | 0 | result = req_send_buffer_add(data, buf, blen, blen); |
406 | 0 | if(result) |
407 | 0 | return result; |
408 | | |
409 | 0 | return Curl_req_send_more(data); |
410 | 0 | } |
411 | 0 | return CURLE_OK; |
412 | 0 | } |
413 | | |
414 | | bool Curl_req_sendbuf_empty(struct Curl_easy *data) |
415 | 0 | { |
416 | 0 | return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf); |
417 | 0 | } |
418 | | |
419 | | bool Curl_req_want_send(struct Curl_easy *data) |
420 | 0 | { |
421 | | /* Not done and upload not blocked and either one of |
422 | | * - KEEP_SEND |
423 | | * - request has buffered data to send |
424 | | * - connection has pending data to send */ |
425 | 0 | return !data->req.done && |
426 | 0 | !Curl_rlimit_is_blocked(&data->progress.ul.rlimit) && |
427 | 0 | ((data->req.keepon & KEEP_SEND) || |
428 | 0 | !Curl_req_sendbuf_empty(data) || |
429 | 0 | Curl_xfer_needs_flush(data)); |
430 | 0 | } |
431 | | |
432 | | bool Curl_req_want_recv(struct Curl_easy *data) |
433 | 0 | { |
434 | | /* Not done and download not blocked and KEEP_RECV */ |
435 | 0 | return !data->req.done && |
436 | 0 | !Curl_rlimit_is_blocked(&data->progress.dl.rlimit) && |
437 | 0 | (data->req.keepon & KEEP_RECV); |
438 | 0 | } |
439 | | |
440 | | bool Curl_req_done_sending(struct Curl_easy *data) |
441 | 0 | { |
442 | 0 | return data->req.upload_done && !Curl_req_want_send(data); |
443 | 0 | } |
444 | | |
445 | | CURLcode Curl_req_send_more(struct Curl_easy *data) |
446 | 0 | { |
447 | 0 | CURLcode result; |
448 | | |
449 | | /* Fill our send buffer if more from client can be read. */ |
450 | 0 | if(!data->req.upload_aborted && |
451 | 0 | !data->req.eos_read && |
452 | 0 | !Curl_xfer_send_is_paused(data) && |
453 | 0 | !Curl_bufq_is_full(&data->req.sendbuf)) { |
454 | 0 | size_t nread; |
455 | 0 | result = Curl_bufq_sipn(&data->req.sendbuf, 0, |
456 | 0 | add_from_client, data, &nread); |
457 | 0 | if(result && result != CURLE_AGAIN) |
458 | 0 | return result; |
459 | 0 | } |
460 | | |
461 | 0 | result = req_flush(data); |
462 | 0 | if(result == CURLE_AGAIN) |
463 | 0 | result = CURLE_OK; |
464 | |
|
465 | 0 | return result; |
466 | 0 | } |
467 | | |
468 | | CURLcode Curl_req_abort_sending(struct Curl_easy *data) |
469 | 0 | { |
470 | 0 | if(!data->req.upload_done) { |
471 | 0 | Curl_bufq_reset(&data->req.sendbuf); |
472 | 0 | data->req.upload_aborted = TRUE; |
473 | 0 | data->req.keepon &= ~KEEP_SEND; |
474 | 0 | return req_set_upload_done(data); |
475 | 0 | } |
476 | 0 | return CURLE_OK; |
477 | 0 | } |
478 | | |
479 | | CURLcode Curl_req_stop_send_recv(struct Curl_easy *data) |
480 | 0 | { |
481 | | /* stop receiving and ALL sending as well, including PAUSE and HOLD. |
482 | | * We might still be paused on receive client writes though, so |
483 | | * keep those bits around. */ |
484 | 0 | CURLcode result = CURLE_OK; |
485 | 0 | if(data->req.keepon & KEEP_SEND) |
486 | 0 | result = Curl_req_abort_sending(data); |
487 | 0 | data->req.keepon &= ~(KEEP_RECV | KEEP_SEND); |
488 | 0 | return result; |
489 | 0 | } |