Coverage Report

Created: 2026-05-30 06:06

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
0
{
40
0
  memset(req, 0, sizeof(*req));
41
0
}
42
43
CURLcode Curl_req_soft_reset(struct SingleRequest *req,
44
                             struct Curl_easy *data)
45
0
{
46
0
  CURLcode result;
47
48
0
  req->done = FALSE;
49
0
  req->upload_done = FALSE;
50
0
  req->upload_aborted = FALSE;
51
0
  req->download_done = FALSE;
52
0
  req->eos_written = FALSE;
53
0
  req->eos_read = FALSE;
54
0
  req->eos_sent = FALSE;
55
0
  req->ignorebody = FALSE;
56
0
  req->shutdown = FALSE;
57
0
  req->bytecount = 0;
58
0
  req->writebytecount = 0;
59
0
  req->header = FALSE;
60
0
  req->headerline = 0;
61
0
  req->headerbytecount = 0;
62
0
  req->allheadercount = 0;
63
0
  req->deductheadercount = 0;
64
0
  req->httpversion_sent = 0;
65
0
  req->httpversion = 0;
66
0
  req->sendbuf_hds_len = 0;
67
68
0
  curlx_safefree(req->hd_auth);
69
0
#ifndef CURL_DISABLE_PROXY
70
0
  curlx_safefree(req->hd_proxy_auth);
71
0
#endif
72
73
0
  result = Curl_client_start(data);
74
0
  if(result)
75
0
    return result;
76
77
0
  if(!req->sendbuf_init) {
78
0
    Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
79
0
                    BUFQ_OPT_SOFT_LIMIT);
80
0
    req->sendbuf_init = TRUE;
81
0
  }
82
0
  else {
83
0
    Curl_bufq_reset(&req->sendbuf);
84
0
    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
0
  }
90
91
0
  return CURLE_OK;
92
0
}
93
94
CURLcode Curl_req_start(struct SingleRequest *req,
95
                        struct Curl_easy *data)
96
0
{
97
0
  req->start = *Curl_pgrs_now(data);
98
0
  return Curl_req_soft_reset(req, data);
99
0
}
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
0
{
106
0
  (void)req;
107
0
  if(!aborted)
108
0
    (void)req_flush(data);
109
0
  Curl_client_reset(data);
110
0
  return CURLE_OK;
111
0
}
112
113
void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
114
0
{
115
0
  struct curltime t0 = { 0, 0 };
116
117
0
  curlx_safefree(req->newurl);
118
0
  curlx_safefree(req->hd_auth);
119
0
#ifndef CURL_DISABLE_PROXY
120
0
  curlx_safefree(req->hd_proxy_auth);
121
0
#endif
122
0
#ifndef CURL_DISABLE_COOKIES
123
0
  curlx_safefree(req->cookiehost);
124
0
#endif
125
0
  Curl_client_reset(data);
126
0
  if(req->sendbuf_init)
127
0
    Curl_bufq_reset(&req->sendbuf);
128
129
  /* clear any resolve data */
130
0
  Curl_resolv_destroy_all(data);
131
  /* Can no longer memset() this struct as we need to keep some state */
132
0
  req->size = -1;
133
0
  req->maxdownload = -1;
134
0
  req->bytecount = 0;
135
0
  req->writebytecount = 0;
136
0
  req->start = t0;
137
0
  req->headerbytecount = 0;
138
0
  req->allheadercount = 0;
139
0
  req->deductheadercount = 0;
140
0
  req->headerline = 0;
141
0
  req->offset = 0;
142
0
  req->httpcode = 0;
143
0
  req->io_flags = 0;
144
0
  req->upgr101 = UPGR101_NONE;
145
0
  req->sendbuf_hds_len = 0;
146
0
  req->timeofdoc = 0;
147
0
  req->location = NULL;
148
0
  req->newurl = NULL;
149
0
#ifndef CURL_DISABLE_COOKIES
150
0
  req->setcookies = 0;
151
0
#endif
152
0
  req->header = FALSE;
153
0
  req->content_range = FALSE;
154
0
  req->download_done = FALSE;
155
0
  req->eos_written = FALSE;
156
0
  req->eos_read = FALSE;
157
0
  req->eos_sent = FALSE;
158
0
  req->rewind_read = FALSE;
159
0
  req->upload_done = FALSE;
160
0
  req->upload_aborted = FALSE;
161
0
  req->ignorebody = FALSE;
162
0
  req->http_bodyless = FALSE;
163
0
  req->chunk = FALSE;
164
0
  req->resp_trailer = FALSE;
165
0
  req->ignore_cl = FALSE;
166
0
  req->upload_chunky = FALSE;
167
0
  req->no_body = data->set.opt_no_body;
168
0
  req->authneg = FALSE;
169
0
  req->shutdown = FALSE;
170
  /* Unpause all directions */
171
0
  Curl_rlimit_block(&data->progress.dl.rlimit, FALSE, &t0);
172
0
  Curl_rlimit_block(&data->progress.ul.rlimit, FALSE, &t0);
173
0
}
174
175
void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
176
0
{
177
0
  curlx_safefree(req->newurl);
178
0
  curlx_safefree(req->hd_auth);
179
0
#ifndef CURL_DISABLE_PROXY
180
0
  curlx_safefree(req->hd_proxy_auth);
181
0
#endif
182
0
  if(req->sendbuf_init)
183
0
    Curl_bufq_free(&req->sendbuf);
184
0
  Curl_client_cleanup(data);
185
0
}
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
0
{
191
0
  CURLcode result = CURLE_OK;
192
0
  bool eos = FALSE;
193
194
0
  *pnwritten = 0;
195
0
  DEBUGASSERT(hds_len <= blen);
196
0
#ifdef DEBUGBUILD
197
0
  {
198
    /* Allow debug builds to override this logic to force short initial
199
       sends */
200
0
    size_t body_len = blen - hds_len;
201
0
    if(body_len) {
202
0
      const char *p = getenv("CURL_SMALLREQSEND");
203
0
      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
0
    }
209
0
  }
210
0
#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
0
  if(data->set.max_send_speed) {
214
0
    size_t body_bytes = blen - hds_len;
215
0
    if((curl_off_t)body_bytes > data->set.max_send_speed)
216
0
      blen = hds_len + (size_t)data->set.max_send_speed;
217
0
  }
218
219
0
  if(data->req.eos_read &&
220
0
    (Curl_bufq_is_empty(&data->req.sendbuf) ||
221
0
     Curl_bufq_len(&data->req.sendbuf) == blen)) {
222
0
    DEBUGF(infof(data, "sending last upload chunk of %zu bytes", blen));
223
0
    eos = TRUE;
224
0
  }
225
0
  result = Curl_xfer_send(data, buf, blen, eos, pnwritten);
226
0
  if(!result) {
227
0
    if(eos && (blen == *pnwritten))
228
0
      data->req.eos_sent = TRUE;
229
0
    if(*pnwritten) {
230
0
      if(hds_len)
231
0
        Curl_debug(data, CURLINFO_HEADER_OUT, buf,
232
0
                   CURLMIN(hds_len, *pnwritten));
233
0
      if(*pnwritten > hds_len) {
234
0
        size_t body_len = *pnwritten - hds_len;
235
0
        Curl_debug(data, CURLINFO_DATA_OUT, buf + hds_len, body_len);
236
0
        data->req.writebytecount += body_len;
237
0
        Curl_pgrs_upload_inc(data, body_len);
238
0
      }
239
0
    }
240
0
  }
241
0
  return result;
242
0
}
243
244
static CURLcode req_send_buffer_flush(struct Curl_easy *data)
245
0
{
246
0
  CURLcode result = CURLE_OK;
247
0
  const unsigned char *buf;
248
0
  size_t blen;
249
250
0
  while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
251
0
    size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
252
0
    result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
253
0
    if(result)
254
0
      break;
255
256
0
    Curl_bufq_skip(&data->req.sendbuf, nwritten);
257
0
    if(hds_len) {
258
0
      data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
259
0
    }
260
    /* leave if we could not send all. Maybe network blocking or
261
     * speed limits on transfer */
262
0
    if(nwritten < blen)
263
0
      break;
264
0
  }
265
0
  return result;
266
0
}
267
268
static CURLcode req_set_upload_done(struct Curl_easy *data)
269
0
{
270
0
  DEBUGASSERT(!data->req.upload_done);
271
0
  data->req.upload_done = TRUE;
272
0
  CURL_REQ_CLEAR_SEND(data);
273
274
0
  Curl_pgrsTime(data, TIMER_POSTRANSFER);
275
0
  Curl_creader_done(data, data->req.upload_aborted);
276
277
0
  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
0
  else if(data->req.writebytecount)
286
0
    infof(data, "upload completely sent off: %" FMT_OFF_T " bytes",
287
0
          data->req.writebytecount);
288
0
  else if(!data->req.download_done) {
289
0
    DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
290
0
    infof(data, Curl_creader_total_length(data) ?
291
0
          "We are completely uploaded and fine" :
292
0
          "Request completely sent off");
293
0
  }
294
295
0
  return Curl_xfer_send_close(data);
296
0
}
297
298
static CURLcode req_flush(struct Curl_easy *data)
299
0
{
300
0
  CURLcode result;
301
302
0
  if(!data || !data->conn)
303
0
    return CURLE_FAILED_INIT;
304
305
0
  if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
306
0
    result = req_send_buffer_flush(data);
307
0
    if(result)
308
0
      return result;
309
0
    if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
310
0
      DEBUGF(infof(data, "Curl_req_flush(len=%zu) -> EAGAIN",
311
0
                   Curl_bufq_len(&data->req.sendbuf)));
312
0
      return CURLE_AGAIN;
313
0
    }
314
0
  }
315
0
  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
0
  if(data->req.eos_read && !data->req.eos_sent) {
321
0
    char tmp = 0;
322
0
    size_t nwritten;
323
0
    result = xfer_send(data, &tmp, 0, 0, &nwritten);
324
0
    if(result)
325
0
      return result;
326
0
    DEBUGASSERT(data->req.eos_sent);
327
0
  }
328
329
0
  if(!data->req.upload_done && data->req.eos_read && data->req.eos_sent) {
330
0
    DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
331
0
    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.", 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
0
    return req_set_upload_done(data);
347
0
  }
348
0
  return CURLE_OK;
349
0
}
350
351
static CURLcode add_from_client(void *reader_ctx,
352
                                unsigned char *buf, size_t buflen,
353
                                size_t *pnread)
354
0
{
355
0
  struct Curl_easy *data = reader_ctx;
356
0
  CURLcode result;
357
0
  bool eos;
358
359
0
  result = Curl_client_read(data, (char *)buf, buflen, pnread, &eos);
360
0
  if(!result && eos)
361
0
    data->req.eos_read = TRUE;
362
0
  return result;
363
0
}
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 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
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
0
{
429
0
  return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf);
430
0
}
431
432
bool Curl_req_want_send(struct Curl_easy *data)
433
0
{
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
0
  return !data->req.done &&
439
0
         !Curl_rlimit_is_blocked(&data->progress.ul.rlimit) &&
440
0
         (CURL_REQ_WANT_SEND(data) ||
441
0
          !Curl_req_sendbuf_empty(data) ||
442
0
          Curl_xfer_needs_flush(data));
443
0
}
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
0
{
455
0
  return data->req.upload_done && !Curl_req_want_send(data);
456
0
}
457
458
CURLcode Curl_req_send_more(struct Curl_easy *data)
459
0
{
460
0
  CURLcode result;
461
462
  /* Fill our send buffer if more from client can be read. */
463
0
  if(!data->req.upload_aborted &&
464
0
     !data->req.eos_read &&
465
0
     !Curl_xfer_send_is_paused(data) &&
466
0
     !Curl_bufq_is_full(&data->req.sendbuf)) {
467
0
    size_t nread;
468
0
    result = Curl_bufq_sipn(&data->req.sendbuf, 0,
469
0
                            add_from_client, data, &nread);
470
0
    if(result && result != CURLE_AGAIN)
471
0
      return result;
472
0
  }
473
474
0
  result = req_flush(data);
475
0
  if(result == CURLE_AGAIN)
476
0
    result = CURLE_OK;
477
478
0
  return result;
479
0
}
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
0
{
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
0
  CURLcode result = CURLE_OK;
498
0
  if(CURL_REQ_WANT_SEND(data))
499
0
    result = Curl_req_abort_sending(data);
500
0
  CURL_REQ_CLEAR_IO(data);
501
0
  return result;
502
0
}