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