Coverage Report

Created: 2026-05-30 06:25

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 "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
415k
{
40
415k
  memset(req, 0, sizeof(*req));
41
415k
}
42
43
CURLcode Curl_req_soft_reset(struct SingleRequest *req,
44
                             struct Curl_easy *data)
45
161k
{
46
161k
  CURLcode result;
47
48
161k
  req->done = FALSE;
49
161k
  req->upload_done = FALSE;
50
161k
  req->upload_aborted = FALSE;
51
161k
  req->download_done = FALSE;
52
161k
  req->eos_written = FALSE;
53
161k
  req->eos_read = FALSE;
54
161k
  req->eos_sent = FALSE;
55
161k
  req->ignorebody = FALSE;
56
161k
  req->shutdown = FALSE;
57
161k
  req->bytecount = 0;
58
161k
  req->writebytecount = 0;
59
161k
  req->header = FALSE;
60
161k
  req->headerline = 0;
61
161k
  req->headerbytecount = 0;
62
161k
  req->allheadercount = 0;
63
161k
  req->deductheadercount = 0;
64
161k
  req->httpversion_sent = 0;
65
161k
  req->httpversion = 0;
66
161k
  req->sendbuf_hds_len = 0;
67
68
161k
  curlx_safefree(req->hd_auth);
69
161k
#ifndef CURL_DISABLE_PROXY
70
161k
  curlx_safefree(req->hd_proxy_auth);
71
161k
#endif
72
73
161k
  result = Curl_client_start(data);
74
161k
  if(result)
75
174
    return result;
76
77
161k
  if(!req->sendbuf_init) {
78
118k
    Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
79
118k
                    BUFQ_OPT_SOFT_LIMIT);
80
118k
    req->sendbuf_init = TRUE;
81
118k
  }
82
42.5k
  else {
83
42.5k
    Curl_bufq_reset(&req->sendbuf);
84
42.5k
    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
42.5k
  }
90
91
161k
  return CURLE_OK;
92
161k
}
93
94
CURLcode Curl_req_start(struct SingleRequest *req,
95
                        struct Curl_easy *data)
96
136k
{
97
136k
  req->start = *Curl_pgrs_now(data);
98
136k
  return Curl_req_soft_reset(req, data);
99
136k
}
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
26.5k
{
106
26.5k
  (void)req;
107
26.5k
  if(!aborted)
108
18.8k
    (void)req_flush(data);
109
26.5k
  Curl_client_reset(data);
110
26.5k
  return CURLE_OK;
111
26.5k
}
112
113
void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
114
161k
{
115
161k
  struct curltime t0 = { 0, 0 };
116
117
161k
  curlx_safefree(req->newurl);
118
161k
  curlx_safefree(req->hd_auth);
119
161k
#ifndef CURL_DISABLE_PROXY
120
161k
  curlx_safefree(req->hd_proxy_auth);
121
161k
#endif
122
161k
#ifndef CURL_DISABLE_COOKIES
123
161k
  curlx_safefree(req->cookiehost);
124
161k
#endif
125
161k
  Curl_client_reset(data);
126
161k
  if(req->sendbuf_init)
127
17.9k
    Curl_bufq_reset(&req->sendbuf);
128
129
  /* clear any resolve data */
130
161k
  Curl_resolv_destroy_all(data);
131
  /* Can no longer memset() this struct as we need to keep some state */
132
161k
  req->size = -1;
133
161k
  req->maxdownload = -1;
134
161k
  req->bytecount = 0;
135
161k
  req->writebytecount = 0;
136
161k
  req->start = t0;
137
161k
  req->headerbytecount = 0;
138
161k
  req->allheadercount = 0;
139
161k
  req->deductheadercount = 0;
140
161k
  req->headerline = 0;
141
161k
  req->offset = 0;
142
161k
  req->httpcode = 0;
143
161k
  req->io_flags = 0;
144
161k
  req->upgr101 = UPGR101_NONE;
145
161k
  req->sendbuf_hds_len = 0;
146
161k
  req->timeofdoc = 0;
147
161k
  req->location = NULL;
148
161k
  req->newurl = NULL;
149
161k
#ifndef CURL_DISABLE_COOKIES
150
161k
  req->setcookies = 0;
151
161k
#endif
152
161k
  req->header = FALSE;
153
161k
  req->content_range = FALSE;
154
161k
  req->download_done = FALSE;
155
161k
  req->eos_written = FALSE;
156
161k
  req->eos_read = FALSE;
157
161k
  req->eos_sent = FALSE;
158
161k
  req->rewind_read = FALSE;
159
161k
  req->upload_done = FALSE;
160
161k
  req->upload_aborted = FALSE;
161
161k
  req->ignorebody = FALSE;
162
161k
  req->http_bodyless = FALSE;
163
161k
  req->chunk = FALSE;
164
161k
  req->resp_trailer = FALSE;
165
161k
  req->ignore_cl = FALSE;
166
161k
  req->upload_chunky = FALSE;
167
161k
  req->no_body = data->set.opt_no_body;
168
161k
  req->authneg = FALSE;
169
161k
  req->shutdown = FALSE;
170
  /* Unpause all directions */
171
161k
  Curl_rlimit_block(&data->progress.dl.rlimit, FALSE, &t0);
172
161k
  Curl_rlimit_block(&data->progress.ul.rlimit, FALSE, &t0);
173
161k
}
174
175
void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
176
415k
{
177
415k
  curlx_safefree(req->newurl);
178
415k
  curlx_safefree(req->hd_auth);
179
415k
#ifndef CURL_DISABLE_PROXY
180
415k
  curlx_safefree(req->hd_proxy_auth);
181
415k
#endif
182
415k
  if(req->sendbuf_init)
183
118k
    Curl_bufq_free(&req->sendbuf);
184
415k
  Curl_client_cleanup(data);
185
415k
}
186
187
static CURLcode xfer_send(struct Curl_easy *data,
188
                          const char *buf, size_t blen,
189
                          size_t hds_len, size_t *pnwritten)
190
900k
{
191
900k
  CURLcode result = CURLE_OK;
192
900k
  bool eos = FALSE;
193
194
900k
  *pnwritten = 0;
195
900k
  DEBUGASSERT(hds_len <= blen);
196
900k
#ifdef DEBUGBUILD
197
900k
  {
198
    /* Allow debug builds to override this logic to force short initial
199
       sends */
200
900k
    size_t body_len = blen - hds_len;
201
900k
    if(body_len) {
202
843k
      const char *p = getenv("CURL_SMALLREQSEND");
203
843k
      if(p) {
204
0
        curl_off_t body_small;
205
0
        if(!curlx_str_number(&p, &body_small, body_len))
206
0
          blen = hds_len + (size_t)body_small;
207
0
      }
208
843k
    }
209
900k
  }
210
900k
#endif
211
  /* Make sure this does not send more body bytes than what the max send
212
     speed says. The headers do not count to the max speed. */
213
900k
  if(data->set.max_send_speed) {
214
238k
    size_t body_bytes = blen - hds_len;
215
238k
    if((curl_off_t)body_bytes > data->set.max_send_speed)
216
93.1k
      blen = hds_len + (size_t)data->set.max_send_speed;
217
238k
  }
218
219
900k
  if(data->req.eos_read &&
220
697k
    (Curl_bufq_is_empty(&data->req.sendbuf) ||
221
646k
     Curl_bufq_len(&data->req.sendbuf) == blen)) {
222
604k
    DEBUGF(infof(data, "sending last upload chunk of %zu bytes", blen));
223
604k
    eos = TRUE;
224
604k
  }
225
900k
  result = Curl_xfer_send(data, buf, blen, eos, pnwritten);
226
900k
  if(!result) {
227
900k
    if(eos && (blen == *pnwritten))
228
55.9k
      data->req.eos_sent = TRUE;
229
900k
    if(*pnwritten) {
230
60.6k
      if(hds_len)
231
57.2k
        Curl_debug(data, CURLINFO_HEADER_OUT, buf,
232
57.2k
                   CURLMIN(hds_len, *pnwritten));
233
60.6k
      if(*pnwritten > hds_len) {
234
8.05k
        size_t body_len = *pnwritten - hds_len;
235
8.05k
        Curl_debug(data, CURLINFO_DATA_OUT, buf + hds_len, body_len);
236
8.05k
        data->req.writebytecount += body_len;
237
8.05k
        Curl_pgrs_upload_inc(data, body_len);
238
8.05k
      }
239
60.6k
    }
240
900k
  }
241
900k
  return result;
242
900k
}
243
244
static CURLcode req_send_buffer_flush(struct Curl_easy *data)
245
848k
{
246
848k
  CURLcode result = CURLE_OK;
247
848k
  const unsigned char *buf;
248
848k
  size_t blen;
249
250
857k
  while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
251
849k
    size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
252
849k
    result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
253
849k
    if(result)
254
125
      break;
255
256
849k
    Curl_bufq_skip(&data->req.sendbuf, nwritten);
257
849k
    if(hds_len) {
258
10.9k
      data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
259
10.9k
    }
260
    /* leave if we could not send all. Maybe network blocking or
261
     * speed limits on transfer */
262
849k
    if(nwritten < blen)
263
840k
      break;
264
849k
  }
265
848k
  return result;
266
848k
}
267
268
static CURLcode req_set_upload_done(struct Curl_easy *data)
269
56.4k
{
270
56.4k
  DEBUGASSERT(!data->req.upload_done);
271
56.4k
  data->req.upload_done = TRUE;
272
56.4k
  CURL_REQ_CLEAR_SEND(data);
273
274
56.4k
  Curl_pgrsTime(data, TIMER_POSTRANSFER);
275
56.4k
  Curl_creader_done(data, data->req.upload_aborted);
276
277
56.4k
  if(data->req.upload_aborted) {
278
472
    Curl_bufq_reset(&data->req.sendbuf);
279
472
    if(data->req.writebytecount)
280
122
      infof(data, "abort upload after having sent %" FMT_OFF_T " bytes",
281
472
            data->req.writebytecount);
282
350
    else
283
350
      infof(data, "abort upload");
284
472
  }
285
55.9k
  else if(data->req.writebytecount)
286
4.91k
    infof(data, "upload completely sent off: %" FMT_OFF_T " bytes",
287
55.9k
          data->req.writebytecount);
288
51.0k
  else if(!data->req.download_done) {
289
50.9k
    DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
290
50.9k
    infof(data, Curl_creader_total_length(data) ?
291
50.9k
          "We are completely uploaded and fine" :
292
50.9k
          "Request completely sent off");
293
50.9k
  }
294
295
56.4k
  return Curl_xfer_send_close(data);
296
56.4k
}
297
298
static CURLcode req_flush(struct Curl_easy *data)
299
6.10M
{
300
6.10M
  CURLcode result;
301
302
6.10M
  if(!data || !data->conn)
303
0
    return CURLE_FAILED_INIT;
304
305
6.10M
  if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
306
848k
    result = req_send_buffer_flush(data);
307
848k
    if(result)
308
125
      return result;
309
848k
    if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
310
840k
      DEBUGF(infof(data, "Curl_req_flush(len=%zu) -> EAGAIN",
311
840k
                   Curl_bufq_len(&data->req.sendbuf)));
312
840k
      return CURLE_AGAIN;
313
840k
    }
314
848k
  }
315
5.25M
  else if(Curl_xfer_needs_flush(data)) {
316
5.22M
    DEBUGF(infof(data, "Curl_req_flush(), xfer send_pending"));
317
5.22M
    return Curl_xfer_flush(data);
318
5.22M
  }
319
320
37.3k
  if(data->req.eos_read && !data->req.eos_sent) {
321
345
    char tmp = 0;
322
345
    size_t nwritten;
323
345
    result = xfer_send(data, &tmp, 0, 0, &nwritten);
324
345
    if(result)
325
23
      return result;
326
322
    DEBUGASSERT(data->req.eos_sent);
327
322
  }
328
329
37.3k
  if(!data->req.upload_done && data->req.eos_read && data->req.eos_sent) {
330
5.13k
    DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
331
5.13k
    if(data->req.shutdown) {
332
27
      bool done;
333
27
      result = Curl_xfer_send_shutdown(data, &done);
334
27
      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.", result);
337
0
        result = CURLE_OK;
338
0
        done = TRUE;
339
0
      }
340
341
27
      if(result)
342
0
        return result;
343
27
      if(!done)
344
0
        return CURLE_AGAIN;
345
27
    }
346
5.13k
    return req_set_upload_done(data);
347
5.13k
  }
348
32.2k
  return CURLE_OK;
349
37.3k
}
350
351
static CURLcode add_from_client(void *reader_ctx,
352
                                unsigned char *buf, size_t buflen,
353
                                size_t *pnread)
354
18.7k
{
355
18.7k
  struct Curl_easy *data = reader_ctx;
356
18.7k
  CURLcode result;
357
18.7k
  bool eos;
358
359
18.7k
  result = Curl_client_read(data, (char *)buf, buflen, pnread, &eos);
360
18.7k
  if(!result && eos)
361
5.45k
    data->req.eos_read = TRUE;
362
18.7k
  return result;
363
18.7k
}
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
5.70k
{
369
5.70k
  CURLcode result = CURLE_OK;
370
5.70k
  size_t n;
371
5.70k
  result = Curl_bufq_cwrite(&data->req.sendbuf, buf, blen, &n);
372
5.70k
  if(result)
373
0
    return result;
374
  /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
375
5.70k
  DEBUGASSERT(n == blen);
376
5.70k
  data->req.sendbuf_hds_len += hds_len;
377
5.70k
  return CURLE_OK;
378
5.70k
}
379
380
CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req,
381
                       unsigned char httpversion)
382
56.6k
{
383
56.6k
  CURLcode result;
384
56.6k
  const char *buf;
385
56.6k
  size_t blen, nwritten;
386
387
56.6k
  if(!data || !data->conn)
388
0
    return CURLE_FAILED_INIT;
389
390
56.6k
  data->req.httpversion_sent = httpversion;
391
56.6k
  buf = curlx_dyn_ptr(req);
392
56.6k
  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 then `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
56.6k
  if(Curl_bufq_is_empty(&data->req.sendbuf) &&
399
56.6k
     !Curl_creader_total_length(data) &&
400
51.3k
     (blen <= data->req.sendbuf.chunk_size)) {
401
51.0k
    data->req.eos_read = TRUE;
402
51.0k
    result = xfer_send(data, buf, blen, blen, &nwritten);
403
51.0k
    if(result)
404
84
      return result;
405
50.9k
    buf += nwritten;
406
50.9k
    blen -= nwritten;
407
50.9k
    if(!blen) {
408
50.8k
      result = req_set_upload_done(data);
409
50.8k
      if(result)
410
0
        return result;
411
50.8k
    }
412
50.9k
  }
413
414
56.5k
  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
5.70k
    result = req_send_buffer_add(data, buf, blen, blen);
419
5.70k
    if(result)
420
0
      return result;
421
422
5.70k
    return Curl_req_send_more(data);
423
5.70k
  }
424
50.8k
  return CURLE_OK;
425
56.5k
}
426
427
bool Curl_req_sendbuf_empty(struct Curl_easy *data)
428
21.5M
{
429
21.5M
  return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf);
430
21.5M
}
431
432
bool Curl_req_want_send(struct Curl_easy *data)
433
23.2M
{
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
23.2M
  return !data->req.done &&
439
23.2M
         !Curl_rlimit_is_blocked(&data->progress.ul.rlimit) &&
440
23.2M
         (CURL_REQ_WANT_SEND(data) ||
441
21.5M
          !Curl_req_sendbuf_empty(data) ||
442
21.5M
          Curl_xfer_needs_flush(data));
443
23.2M
}
444
445
bool Curl_req_want_recv(struct Curl_easy *data)
446
2.88k
{
447
  /* Not done and download not blocked and want RECV */
448
2.88k
  return !data->req.done &&
449
2.88k
         !Curl_rlimit_is_blocked(&data->progress.dl.rlimit) &&
450
2.88k
         CURL_REQ_WANT_RECV(data);
451
2.88k
}
452
453
bool Curl_req_done_sending(struct Curl_easy *data)
454
12.1M
{
455
12.1M
  return data->req.upload_done && !Curl_req_want_send(data);
456
12.1M
}
457
458
CURLcode Curl_req_send_more(struct Curl_easy *data)
459
6.08M
{
460
6.08M
  CURLcode result;
461
462
  /* Fill our send buffer if more from client can be read. */
463
6.08M
  if(!data->req.upload_aborted &&
464
6.08M
     !data->req.eos_read &&
465
218k
     !Curl_xfer_send_is_paused(data) &&
466
218k
     !Curl_bufq_is_full(&data->req.sendbuf)) {
467
18.7k
    size_t nread;
468
18.7k
    result = Curl_bufq_sipn(&data->req.sendbuf, 0,
469
18.7k
                            add_from_client, data, &nread);
470
18.7k
    if(result && result != CURLE_AGAIN)
471
85
      return result;
472
18.7k
  }
473
474
6.08M
  result = req_flush(data);
475
6.08M
  if(result == CURLE_AGAIN)
476
841k
    result = CURLE_OK;
477
478
6.08M
  return result;
479
6.08M
}
480
481
CURLcode Curl_req_abort_sending(struct Curl_easy *data)
482
474
{
483
474
  if(!data->req.upload_done) {
484
472
    Curl_bufq_reset(&data->req.sendbuf);
485
472
    data->req.upload_aborted = TRUE;
486
472
    CURL_REQ_CLEAR_SEND(data);
487
472
    return req_set_upload_done(data);
488
472
  }
489
2
  return CURLE_OK;
490
474
}
491
492
CURLcode Curl_req_stop_send_recv(struct Curl_easy *data)
493
19.2k
{
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
19.2k
  CURLcode result = CURLE_OK;
498
19.2k
  if(CURL_REQ_WANT_SEND(data))
499
430
    result = Curl_req_abort_sending(data);
500
19.2k
  CURL_REQ_CLEAR_IO(data);
501
19.2k
  return result;
502
19.2k
}