Coverage Report

Created: 2026-10-15 06:20

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