Coverage Report

Created: 2025-06-20 06:58

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