Coverage Report

Created: 2026-09-01 06:58

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