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