Coverage Report

Created: 2026-09-01 07:00

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