Coverage Report

Created: 2026-01-09 07:14

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
6.13k
{
40
6.13k
  memset(req, 0, sizeof(*req));
41
6.13k
}
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
  result = Curl_client_start(data);
69
0
  if(result)
70
0
    return result;
71
72
0
  if(!req->sendbuf_init) {
73
0
    Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
74
0
                    BUFQ_OPT_SOFT_LIMIT);
75
0
    req->sendbuf_init = TRUE;
76
0
  }
77
0
  else {
78
0
    Curl_bufq_reset(&req->sendbuf);
79
0
    if(data->set.upload_buffer_size != req->sendbuf.chunk_size) {
80
0
      Curl_bufq_free(&req->sendbuf);
81
0
      Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
82
0
                      BUFQ_OPT_SOFT_LIMIT);
83
0
    }
84
0
  }
85
86
0
  return CURLE_OK;
87
0
}
88
89
CURLcode Curl_req_start(struct SingleRequest *req,
90
                        struct Curl_easy *data)
91
0
{
92
0
  req->start = *Curl_pgrs_now(data);
93
0
  return Curl_req_soft_reset(req, data);
94
0
}
95
96
static CURLcode req_flush(struct Curl_easy *data);
97
98
CURLcode Curl_req_done(struct SingleRequest *req,
99
                       struct Curl_easy *data, bool aborted)
100
0
{
101
0
  (void)req;
102
0
  if(!aborted)
103
0
    (void)req_flush(data);
104
0
  Curl_client_reset(data);
105
0
#ifndef CURL_DISABLE_DOH
106
0
  Curl_doh_close(data);
107
0
#endif
108
0
  return CURLE_OK;
109
0
}
110
111
void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
112
0
{
113
0
  struct curltime t0 = { 0, 0 };
114
115
0
  Curl_safefree(req->newurl);
116
0
  Curl_client_reset(data);
117
0
  if(req->sendbuf_init)
118
0
    Curl_bufq_reset(&req->sendbuf);
119
120
0
#ifndef CURL_DISABLE_DOH
121
0
  Curl_doh_close(data);
122
0
#endif
123
  /* Can no longer memset() this struct as we need to keep some state */
124
0
  req->size = -1;
125
0
  req->maxdownload = -1;
126
0
  req->bytecount = 0;
127
0
  req->writebytecount = 0;
128
0
  req->start = t0;
129
0
  req->headerbytecount = 0;
130
0
  req->allheadercount = 0;
131
0
  req->deductheadercount = 0;
132
0
  req->headerline = 0;
133
0
  req->offset = 0;
134
0
  req->httpcode = 0;
135
0
  req->keepon = 0;
136
0
  req->upgr101 = UPGR101_NONE;
137
0
  req->sendbuf_hds_len = 0;
138
0
  req->timeofdoc = 0;
139
0
  req->location = NULL;
140
0
  req->newurl = NULL;
141
0
#ifndef CURL_DISABLE_COOKIES
142
0
  req->setcookies = 0;
143
0
#endif
144
0
  req->header = FALSE;
145
0
  req->content_range = FALSE;
146
0
  req->download_done = FALSE;
147
0
  req->eos_written = FALSE;
148
0
  req->eos_read = FALSE;
149
0
  req->eos_sent = FALSE;
150
0
  req->rewind_read = FALSE;
151
0
  req->upload_done = FALSE;
152
0
  req->upload_aborted = FALSE;
153
0
  req->ignorebody = FALSE;
154
0
  req->http_bodyless = FALSE;
155
0
  req->chunk = FALSE;
156
0
  req->ignore_cl = FALSE;
157
0
  req->upload_chunky = FALSE;
158
0
  req->no_body = data->set.opt_no_body;
159
0
  req->authneg = FALSE;
160
0
  req->shutdown = FALSE;
161
0
}
162
163
void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
164
6.13k
{
165
6.13k
  Curl_safefree(req->newurl);
166
6.13k
  if(req->sendbuf_init)
167
0
    Curl_bufq_free(&req->sendbuf);
168
6.13k
  Curl_client_cleanup(data);
169
6.13k
}
170
171
static CURLcode xfer_send(struct Curl_easy *data,
172
                          const char *buf, size_t blen,
173
                          size_t hds_len, size_t *pnwritten)
174
0
{
175
0
  CURLcode result = CURLE_OK;
176
0
  bool eos = FALSE;
177
178
0
  *pnwritten = 0;
179
0
  DEBUGASSERT(hds_len <= blen);
180
0
#ifdef DEBUGBUILD
181
0
  {
182
    /* Allow debug builds to override this logic to force short initial
183
       sends */
184
0
    size_t body_len = blen - hds_len;
185
0
    if(body_len) {
186
0
      const char *p = getenv("CURL_SMALLREQSEND");
187
0
      if(p) {
188
0
        curl_off_t body_small;
189
0
        if(!curlx_str_number(&p, &body_small, body_len))
190
0
          blen = hds_len + (size_t)body_small;
191
0
      }
192
0
    }
193
0
  }
194
0
#endif
195
  /* Make sure this does not send more body bytes than what the max send
196
     speed says. The headers do not count to the max speed. */
197
0
  if(data->set.max_send_speed) {
198
0
    size_t body_bytes = blen - hds_len;
199
0
    if((curl_off_t)body_bytes > data->set.max_send_speed)
200
0
      blen = hds_len + (size_t)data->set.max_send_speed;
201
0
  }
202
203
0
  if(data->req.eos_read &&
204
0
    (Curl_bufq_is_empty(&data->req.sendbuf) ||
205
0
     Curl_bufq_len(&data->req.sendbuf) == blen)) {
206
0
    DEBUGF(infof(data, "sending last upload chunk of %zu bytes", blen));
207
0
    eos = TRUE;
208
0
  }
209
0
  result = Curl_xfer_send(data, buf, blen, eos, pnwritten);
210
0
  if(!result) {
211
0
    if(eos && (blen == *pnwritten))
212
0
      data->req.eos_sent = TRUE;
213
0
    if(*pnwritten) {
214
0
      if(hds_len)
215
0
        Curl_debug(data, CURLINFO_HEADER_OUT, buf,
216
0
                   CURLMIN(hds_len, *pnwritten));
217
0
      if(*pnwritten > hds_len) {
218
0
        size_t body_len = *pnwritten - hds_len;
219
0
        Curl_debug(data, CURLINFO_DATA_OUT, buf + hds_len, body_len);
220
0
        data->req.writebytecount += body_len;
221
0
        Curl_pgrs_upload_inc(data, body_len);
222
0
      }
223
0
    }
224
0
  }
225
0
  return result;
226
0
}
227
228
static CURLcode req_send_buffer_flush(struct Curl_easy *data)
229
0
{
230
0
  CURLcode result = CURLE_OK;
231
0
  const unsigned char *buf;
232
0
  size_t blen;
233
234
0
  while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
235
0
    size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
236
0
    result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
237
0
    if(result)
238
0
      break;
239
240
0
    Curl_bufq_skip(&data->req.sendbuf, nwritten);
241
0
    if(hds_len) {
242
0
      data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
243
0
    }
244
    /* leave if we could not send all. Maybe network blocking or
245
     * speed limits on transfer */
246
0
    if(nwritten < blen)
247
0
      break;
248
0
  }
249
0
  return result;
250
0
}
251
252
static CURLcode req_set_upload_done(struct Curl_easy *data)
253
0
{
254
0
  DEBUGASSERT(!data->req.upload_done);
255
0
  data->req.upload_done = TRUE;
256
0
  data->req.keepon &= ~KEEP_SEND; /* we are done sending */
257
258
0
  Curl_pgrsTime(data, TIMER_POSTRANSFER);
259
0
  Curl_creader_done(data, data->req.upload_aborted);
260
261
0
  if(data->req.upload_aborted) {
262
0
    Curl_bufq_reset(&data->req.sendbuf);
263
0
    if(data->req.writebytecount)
264
0
      infof(data, "abort upload after having sent %" FMT_OFF_T " bytes",
265
0
            data->req.writebytecount);
266
0
    else
267
0
      infof(data, "abort upload");
268
0
  }
269
0
  else if(data->req.writebytecount)
270
0
    infof(data, "upload completely sent off: %" FMT_OFF_T " bytes",
271
0
          data->req.writebytecount);
272
0
  else if(!data->req.download_done) {
273
0
    DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
274
0
    infof(data, Curl_creader_total_length(data) ?
275
0
          "We are completely uploaded and fine" :
276
0
          "Request completely sent off");
277
0
  }
278
279
0
  return Curl_xfer_send_close(data);
280
0
}
281
282
static CURLcode req_flush(struct Curl_easy *data)
283
0
{
284
0
  CURLcode result;
285
286
0
  if(!data || !data->conn)
287
0
    return CURLE_FAILED_INIT;
288
289
0
  if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
290
0
    result = req_send_buffer_flush(data);
291
0
    if(result)
292
0
      return result;
293
0
    if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
294
0
      DEBUGF(infof(data, "Curl_req_flush(len=%zu) -> EAGAIN",
295
0
             Curl_bufq_len(&data->req.sendbuf)));
296
0
      return CURLE_AGAIN;
297
0
    }
298
0
  }
299
0
  else if(Curl_xfer_needs_flush(data)) {
300
0
    DEBUGF(infof(data, "Curl_req_flush(), xfer send_pending"));
301
0
    return Curl_xfer_flush(data);
302
0
  }
303
304
0
  if(data->req.eos_read && !data->req.eos_sent) {
305
0
    char tmp = 0;
306
0
    size_t nwritten;
307
0
    result = xfer_send(data, &tmp, 0, 0, &nwritten);
308
0
    if(result)
309
0
      return result;
310
0
    DEBUGASSERT(data->req.eos_sent);
311
0
  }
312
313
0
  if(!data->req.upload_done && data->req.eos_read && data->req.eos_sent) {
314
0
    DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
315
0
    if(data->req.shutdown) {
316
0
      bool done;
317
0
      result = Curl_xfer_send_shutdown(data, &done);
318
0
      if(result && data->req.shutdown_err_ignore) {
319
0
        infof(data, "Shutdown send direction error: %d. Broken server? "
320
0
              "Proceeding as if everything is ok.", result);
321
0
        result = CURLE_OK;
322
0
        done = TRUE;
323
0
      }
324
325
0
      if(result)
326
0
        return result;
327
0
      if(!done)
328
0
        return CURLE_AGAIN;
329
0
    }
330
0
    return req_set_upload_done(data);
331
0
  }
332
0
  return CURLE_OK;
333
0
}
334
335
static CURLcode add_from_client(void *reader_ctx,
336
                                unsigned char *buf, size_t buflen,
337
                                size_t *pnread)
338
0
{
339
0
  struct Curl_easy *data = reader_ctx;
340
0
  CURLcode result;
341
0
  bool eos;
342
343
0
  result = Curl_client_read(data, (char *)buf, buflen, pnread, &eos);
344
0
  if(!result && eos)
345
0
    data->req.eos_read = TRUE;
346
0
  return result;
347
0
}
348
349
static CURLcode req_send_buffer_add(struct Curl_easy *data,
350
                                    const char *buf, size_t blen,
351
                                    size_t hds_len)
352
0
{
353
0
  CURLcode result = CURLE_OK;
354
0
  size_t n;
355
0
  result = Curl_bufq_cwrite(&data->req.sendbuf, buf, blen, &n);
356
0
  if(result)
357
0
    return result;
358
  /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
359
0
  DEBUGASSERT(n == blen);
360
0
  data->req.sendbuf_hds_len += hds_len;
361
0
  return CURLE_OK;
362
0
}
363
364
CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req,
365
                       unsigned char httpversion)
366
0
{
367
0
  CURLcode result;
368
0
  const char *buf;
369
0
  size_t blen, nwritten;
370
371
0
  if(!data || !data->conn)
372
0
    return CURLE_FAILED_INIT;
373
374
0
  data->req.httpversion_sent = httpversion;
375
0
  buf = curlx_dyn_ptr(req);
376
0
  blen = curlx_dyn_len(req);
377
  /* if the sendbuf is empty and the request without body and
378
   * the length to send fits info a sendbuf chunk, we send it directly.
379
   * If `blen` is larger then `chunk_size`, we can not. Because we
380
   * might have to retry a blocked send later from sendbuf and that
381
   * would result in retry sends with a shrunken length. That is trouble. */
382
0
  if(Curl_bufq_is_empty(&data->req.sendbuf) &&
383
0
     !Curl_creader_total_length(data) &&
384
0
     (blen <= data->req.sendbuf.chunk_size)) {
385
0
    data->req.eos_read = TRUE;
386
0
    result = xfer_send(data, buf, blen, blen, &nwritten);
387
0
    if(result)
388
0
      return result;
389
0
    buf += nwritten;
390
0
    blen -= nwritten;
391
0
    if(!blen) {
392
0
      result = req_set_upload_done(data);
393
0
      if(result)
394
0
        return result;
395
0
    }
396
0
  }
397
398
0
  if(blen) {
399
    /* Either we have a request body, or we could not send the complete
400
     * request in one go. Buffer the remainder and try to add as much
401
     * body bytes as room is left in the buffer. Then flush. */
402
0
    result = req_send_buffer_add(data, buf, blen, blen);
403
0
    if(result)
404
0
      return result;
405
406
0
    return Curl_req_send_more(data);
407
0
  }
408
0
  return CURLE_OK;
409
0
}
410
411
bool Curl_req_sendbuf_empty(struct Curl_easy *data)
412
0
{
413
0
  return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf);
414
0
}
415
416
bool Curl_req_want_send(struct Curl_easy *data)
417
0
{
418
  /* Not done and upload not blocked and either one of
419
   * - KEEP_SEND
420
   * - request has buffered data to send
421
   * - connection has pending data to send */
422
0
  return !data->req.done &&
423
0
         !Curl_rlimit_is_blocked(&data->progress.ul.rlimit) &&
424
0
         ((data->req.keepon & KEEP_SEND) ||
425
0
          !Curl_req_sendbuf_empty(data) ||
426
0
          Curl_xfer_needs_flush(data));
427
0
}
428
429
bool Curl_req_want_recv(struct Curl_easy *data)
430
0
{
431
  /* Not done and download not blocked and KEEP_RECV */
432
0
  return !data->req.done &&
433
0
         !Curl_rlimit_is_blocked(&data->progress.dl.rlimit) &&
434
0
         (data->req.keepon & KEEP_RECV);
435
0
}
436
437
bool Curl_req_done_sending(struct Curl_easy *data)
438
0
{
439
0
  return data->req.upload_done && !Curl_req_want_send(data);
440
0
}
441
442
CURLcode Curl_req_send_more(struct Curl_easy *data)
443
0
{
444
0
  CURLcode result;
445
446
  /* Fill our send buffer if more from client can be read. */
447
0
  if(!data->req.upload_aborted &&
448
0
     !data->req.eos_read &&
449
0
     !Curl_xfer_send_is_paused(data) &&
450
0
     !Curl_bufq_is_full(&data->req.sendbuf)) {
451
0
    size_t nread;
452
0
    result = Curl_bufq_sipn(&data->req.sendbuf, 0,
453
0
                            add_from_client, data, &nread);
454
0
    if(result && result != CURLE_AGAIN)
455
0
      return result;
456
0
  }
457
458
0
  result = req_flush(data);
459
0
  if(result == CURLE_AGAIN)
460
0
    result = CURLE_OK;
461
462
0
  return result;
463
0
}
464
465
CURLcode Curl_req_abort_sending(struct Curl_easy *data)
466
0
{
467
0
  if(!data->req.upload_done) {
468
0
    Curl_bufq_reset(&data->req.sendbuf);
469
0
    data->req.upload_aborted = TRUE;
470
0
    data->req.keepon &= ~KEEP_SEND;
471
0
    return req_set_upload_done(data);
472
0
  }
473
0
  return CURLE_OK;
474
0
}
475
476
CURLcode Curl_req_stop_send_recv(struct Curl_easy *data)
477
0
{
478
  /* stop receiving and ALL sending as well, including PAUSE and HOLD.
479
   * We might still be paused on receive client writes though, so
480
   * keep those bits around. */
481
0
  CURLcode result = CURLE_OK;
482
0
  if(data->req.keepon & KEEP_SEND)
483
0
    result = Curl_req_abort_sending(data);
484
0
  data->req.keepon &= ~(KEEP_RECV | KEEP_SEND);
485
0
  return result;
486
0
}