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