Coverage Report

Created: 2026-10-22 06:50

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