Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/transfer.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
#ifdef HAVE_NETINET_IN_H
27
#include <netinet/in.h>
28
#endif
29
#ifdef HAVE_NETDB_H
30
#include <netdb.h>
31
#endif
32
#ifdef HAVE_ARPA_INET_H
33
#include <arpa/inet.h>
34
#endif
35
#ifdef HAVE_NET_IF_H
36
#include <net/if.h>
37
#endif
38
#ifdef HAVE_SYS_IOCTL_H
39
#include <sys/ioctl.h>
40
#endif
41
#include <signal.h>
42
43
#ifdef HAVE_SYS_PARAM_H
44
#include <sys/param.h>
45
#endif
46
47
#ifdef HAVE_SYS_SELECT_H
48
#include <sys/select.h>
49
#elif defined(HAVE_UNISTD_H)
50
#include <unistd.h>
51
#endif
52
53
#ifndef HAVE_SOCKET
54
#error "We cannot compile without socket() support"
55
#endif
56
57
#include "urldata.h"
58
59
#include "cfilters.h"
60
#include "cw-out.h"
61
#include "transfer.h"
62
#include "sendf.h"
63
#include "curl_trc.h"
64
#include "progress.h"
65
#include "http.h"
66
#include "url.h"
67
#include "getinfo.h"
68
#include "multiif.h"
69
#include "connect.h"
70
#include "hsts.h"
71
#include "setopt.h"
72
#include "headers.h"
73
#include "bufref.h"
74
#include "rtsp.h"
75
76
#if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_SMTP) || \
77
  !defined(CURL_DISABLE_IMAP)
78
/*
79
 * checkheaders() checks the linked list of custom headers for a
80
 * particular header (prefix). Provide the prefix without colon!
81
 *
82
 * Returns a pointer to the first matching header or NULL if none matched.
83
 */
84
char *Curl_checkheaders(const struct Curl_easy *data,
85
                        const char *thisheader,
86
                        const size_t thislen)
87
1.74k
{
88
1.74k
  struct curl_slist *head;
89
1.74k
  DEBUGASSERT(thislen);
90
1.74k
  DEBUGASSERT(thisheader[thislen - 1] != ':');
91
92
1.74k
  for(head = data->set.headers; head; head = head->next) {
93
0
    if(curl_strnequal(head->data, thisheader, thislen) &&
94
0
       Curl_headersep(head->data[thislen]))
95
0
      return head->data;
96
0
  }
97
98
1.74k
  return NULL;
99
1.74k
}
100
#endif
101
102
static int data_pending(struct Curl_easy *data, bool rcvd_eagain)
103
785
{
104
785
  struct connectdata *conn = data->conn;
105
106
  /* in the case of libssh2, we can never be really sure that we have emptied
107
     its internal buffers so we MUST always try until we get EAGAIN back */
108
785
  return (!rcvd_eagain &&
109
0
          conn->scheme->protocol & (CURLPROTO_SCP | CURLPROTO_SFTP)) ||
110
785
         Curl_conn_data_pending(data);
111
785
}
112
113
/*
114
 * Check to see if CURLOPT_TIMECONDITION was met by comparing the time of the
115
 * remote document with the time provided by CURLOPT_TIMEVAL
116
 */
117
bool Curl_meets_timecondition(struct Curl_easy *data, time_t timeofdoc)
118
0
{
119
0
  if((timeofdoc == 0) || (data->set.timevalue == 0))
120
0
    return TRUE;
121
122
0
  switch(data->set.timecondition) {
123
0
  case CURL_TIMECOND_IFMODSINCE:
124
0
  default:
125
0
    if(timeofdoc <= data->set.timevalue) {
126
0
      infof(data, "The requested document is not new enough");
127
0
      data->info.timecond = TRUE;
128
0
      return FALSE;
129
0
    }
130
0
    break;
131
0
  case CURL_TIMECOND_IFUNMODSINCE:
132
0
    if(timeofdoc >= data->set.timevalue) {
133
0
      infof(data, "The requested document is not old enough");
134
0
      data->info.timecond = TRUE;
135
0
      return FALSE;
136
0
    }
137
0
    break;
138
0
  }
139
140
0
  return TRUE;
141
0
}
142
143
static bool xfer_recv_shutdown_started(struct Curl_easy *data)
144
0
{
145
0
  if(!data || !data->conn)
146
0
    return FALSE;
147
0
  return CURL_CFCHAIN_IN_SHUTDOWN(&data->conn->chain);
148
0
}
149
150
/**
151
 * Receive raw response data for the transfer.
152
 * @param data         the transfer
153
 * @param buf          buffer to keep response data received
154
 * @param blen         length of `buf`
155
 * @param eos_reliable if EOS detection in underlying connection is reliable
156
 * @return number of bytes read or -1 for error
157
 */
158
static CURLcode xfer_recv_resp(struct Curl_easy *data,
159
                               char *buf, size_t blen,
160
                               bool eos_reliable,
161
                               size_t *pnread)
162
1.12k
{
163
1.12k
  CURLcode result;
164
165
1.12k
  DEBUGASSERT(blen > 0);
166
1.12k
  *pnread = 0;
167
  /* If we are reading BODY data and the connection does NOT handle EOF
168
   * and we know the size of the BODY data, limit the read amount */
169
1.12k
  if(!eos_reliable && !data->req.header && data->req.size != -1) {
170
1.12k
    blen = curlx_sotouz_range(data->req.size - data->req.bytecount, 0, blen);
171
1.12k
  }
172
0
  else if(xfer_recv_shutdown_started(data)) {
173
    /* we already received everything. Do not try more. */
174
0
    blen = 0;
175
0
  }
176
177
1.12k
  if(blen) {
178
1.09k
    result = Curl_xfer_recv(data, buf, blen, pnread);
179
1.09k
    if(result)
180
785
      return result;
181
1.09k
  }
182
183
337
  if(*pnread == 0)
184
28
    DEBUGF(infof(data, "sendrecv_dl: we are done"));
185
337
  return CURLE_OK;
186
1.12k
}
187
188
/*
189
 * Go ahead and do a read if we have a readable socket or if
190
 * the stream was rewound (in which case we have data in a
191
 * buffer)
192
 */
193
static CURLcode sendrecv_dl(struct Curl_easy *data,
194
                            struct SingleRequest *k)
195
869
{
196
869
  struct connectdata *conn = data->conn;
197
869
  CURLcode result = CURLE_OK;
198
869
  char *buf, *xfer_buf;
199
869
  size_t blen, xfer_blen;
200
869
  int maxloops = 10;
201
869
  bool safe_eos = FALSE;
202
869
  bool rcvd_eagain = FALSE;
203
869
  bool is_eos = FALSE, rate_limited = FALSE;
204
205
869
  result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
206
869
  if(result)
207
0
    goto out;
208
209
  /* This is where we loop until we have read everything there is to
210
     read or we get a CURLE_AGAIN */
211
1.12k
  do {
212
1.12k
    size_t bytestoread;
213
214
1.12k
    if(!safe_eos) {
215
      /* When the connection handles End-Of-Stream (EOS) safely
216
       * AND we either do not do a range request OR we are HTTP
217
       * (in HTTP, ranged downloads retrieve the right amount),
218
       * we do not have to watch the amount we try to receive.
219
       * This changes only in one direction. */
220
1.12k
      safe_eos = Curl_conn_has_safe_eos(conn) &&
221
0
                 (!data->state.use_range ||
222
0
                  (conn->scheme->protocol & PROTO_FAMILY_HTTP));
223
1.12k
    }
224
225
1.12k
    buf = xfer_buf;
226
1.12k
    bytestoread = xfer_blen;
227
228
1.12k
    if(bytestoread && Curl_rlimit_active(&data->progress.dl.rlimit)) {
229
0
      curl_off_t dl_avail = Curl_rlimit_avail(&data->progress.dl.rlimit, NULL);
230
#if 0
231
      DEBUGF(infof(data, "dl_rlimit, available=%" FMT_OFF_T, dl_avail));
232
#endif
233
      /* In case of rate limited downloads: if this loop already got data and
234
       * less than 16k is left in the limit, break out. We want to stutter a
235
       * bit to keep in the limit, but too small receives will cost cpu
236
       * unnecessarily. */
237
0
      if(dl_avail <= 0) {
238
0
        rate_limited = TRUE;
239
0
        break;
240
0
      }
241
0
      if(dl_avail < (curl_off_t)bytestoread)
242
0
        bytestoread = (size_t)dl_avail;
243
0
    }
244
245
1.12k
    rcvd_eagain = FALSE;
246
1.12k
    result = xfer_recv_resp(data, buf, bytestoread, safe_eos, &blen);
247
1.12k
    if(result) {
248
785
      if(result != CURLE_AGAIN)
249
0
        goto out; /* real error */
250
785
      rcvd_eagain = TRUE;
251
785
      result = CURLE_OK;
252
785
      if(data->req.download_done && data->req.no_body &&
253
0
         !data->req.resp_trailer) {
254
0
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
255
0
                     "not waiting for EOS"));
256
0
        blen = 0;
257
        /* continue as if we received the EOS */
258
0
      }
259
785
      else
260
785
        break; /* get out of loop */
261
785
    }
262
263
    /* We only get a 0-length receive at the end of the response */
264
337
    is_eos = (blen == 0);
265
266
337
    if(!blen) {
267
28
      result = Curl_req_stop_send_recv(data);
268
28
      if(result)
269
0
        goto out;
270
28
      if(k->eos_written) /* already did write this to client, leave */
271
0
        break;
272
28
    }
273
274
337
    result = Curl_xfer_write_resp(data, buf, blen, is_eos);
275
337
    if(result || data->req.done)
276
0
      goto out;
277
278
    /* if we are done, we stop receiving. On multiplexed connections,
279
     * we should read the EOS. Which may arrive as meta data after
280
     * the bytes. Not taking it in might lead to RST of streams. */
281
337
    if((!safe_eos && data->req.download_done) || is_eos) {
282
73
      CURL_REQ_CLEAR_RECV(data);
283
73
    }
284
    /* if we stopped receiving, leave the loop */
285
337
    if(!CURL_REQ_WANT_RECV(data))
286
73
      break;
287
288
337
  } while(maxloops--);
289
290
869
  if(!is_eos && !rate_limited && CURL_REQ_WANT_RECV(data) &&
291
796
     (!rcvd_eagain || data_pending(data, rcvd_eagain))) {
292
    /* Did not read until EAGAIN/EOS or there is still data pending
293
     * in buffers. Mark as read-again via simulated SELECT results. */
294
11
    Curl_multi_mark_dirty(data);
295
11
    CURL_TRC_M(data, "sendrecv_dl() no EAGAIN/pending data, mark as dirty");
296
11
  }
297
298
869
  if(!CURL_REQ_WANT_RECV(data) && CURL_REQ_WANT_SEND(data) &&
299
0
     (conn->bits.close || safe_eos)) {
300
    /* When we have read the entire thing and the close bit is set, the server
301
       may now close the connection. If there is now any kind of sending going
302
       on from our side, we need to stop that immediately. */
303
0
    infof(data, "we are done reading and this is set to close, stop send");
304
0
    Curl_req_abort_sending(data);
305
0
  }
306
307
869
out:
308
869
  Curl_multi_xfer_buf_release(data, xfer_buf);
309
869
  if(result)
310
0
    DEBUGF(infof(data, "sendrecv_dl() -> %d", (int)result));
311
869
  return result;
312
869
}
313
314
/*
315
 * Send data to upload to the server, when the socket is writable.
316
 */
317
static CURLcode sendrecv_ul(struct Curl_easy *data)
318
41.6k
{
319
  /* We should not get here when the sending is already done. */
320
41.6k
  DEBUGASSERT(!Curl_req_done_sending(data));
321
322
41.6k
  if(!Curl_req_done_sending(data))
323
41.6k
    return Curl_req_send_more(data);
324
0
  return CURLE_OK;
325
41.6k
}
326
327
/*
328
 * Curl_sendrecv() is the low-level function to be called when data is to
329
 * be read and written to/from the connection.
330
 */
331
CURLcode Curl_sendrecv(struct Curl_easy *data)
332
42.5k
{
333
42.5k
  struct SingleRequest *k = &data->req;
334
42.5k
  const struct curltime *pnow = NULL;
335
42.5k
  CURLcode result = CURLE_OK;
336
337
42.5k
  if(Curl_xfer_is_blocked(data)) {
338
0
    result = Curl_conn_do_maintenance(data);
339
0
    goto out;
340
0
  }
341
342
  /* We go ahead and do a read if we have a readable socket or if the stream
343
     was rewound (in which case we have data in a buffer) */
344
42.5k
  if(CURL_REQ_WANT_RECV(data)) {
345
869
    result = sendrecv_dl(data, k);
346
869
    if(result || data->req.done)
347
0
      goto out;
348
869
  }
349
350
  /* If we still have writing to do, we check if we have a writable socket. */
351
42.5k
  if(Curl_req_want_send(data)) {
352
41.6k
    result = sendrecv_ul(data);
353
41.6k
    if(result)
354
460
      goto out;
355
41.6k
  }
356
357
42.0k
  pnow = Curl_pgrs_now(data);
358
42.0k
  if(CURL_REQ_WANT_IO(data)) {
359
40.8k
    if(Curl_timeleft_now_ms(data, pnow) < 0) {
360
0
      if(k->size != -1) {
361
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
362
0
              " milliseconds with %" FMT_OFF_T " out of %"
363
0
              FMT_OFF_T " bytes received",
364
0
              Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE),
365
0
              k->bytecount, k->size);
366
0
      }
367
0
      else {
368
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
369
0
              " milliseconds with %" FMT_OFF_T " bytes received",
370
0
              Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE),
371
0
              k->bytecount);
372
0
      }
373
0
      result = CURLE_OPERATION_TIMEDOUT;
374
0
      goto out;
375
0
    }
376
40.8k
  }
377
1.25k
  else {
378
    /*
379
     * The transfer has been performed. Make some general checks before
380
     * returning.
381
     */
382
1.25k
    if(!data->req.no_body && (k->size != -1) &&
383
73
       (k->bytecount != k->size) && !k->newurl) {
384
28
      failf(data, "transfer closed with %" FMT_OFF_T
385
28
            " bytes remaining to read", k->size - k->bytecount);
386
28
      result = CURLE_PARTIAL_FILE;
387
28
      goto out;
388
28
    }
389
1.25k
  }
390
391
  /* If there is nothing more to send/recv, the request is done */
392
42.0k
  if(!CURL_REQ_WANT_IO(data))
393
1.22k
    data->req.done = TRUE;
394
395
42.0k
  result = Curl_pgrsCheckX(data, pnow);
396
397
42.5k
out:
398
42.5k
  if(result)
399
488
    DEBUGF(infof(data, "Curl_sendrecv() -> %d", (int)result));
400
42.5k
  return result;
401
42.0k
}
402
403
/* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
404
   which means this gets called once for each subsequent redirect etc */
405
void Curl_init_CONNECT(struct Curl_easy *data)
406
6.39k
{
407
6.39k
  data->state.fread_func = data->set.fread_func_set;
408
6.39k
  data->state.in = data->set.in_set;
409
6.39k
  data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
410
6.39k
}
411
412
/*
413
 * Curl_pretransfer() is called immediately before a transfer starts, and only
414
 * once for one transfer no matter if it has redirects or do multi-pass
415
 * authentication etc.
416
 */
417
CURLcode Curl_pretransfer(struct Curl_easy *data)
418
6.43k
{
419
6.43k
  CURLcode result = CURLE_OK;
420
421
  /* Reset the retry count at the start of each request.
422
   * If the retry count is not reset, when the connection drops,
423
   * it will not enter the retry mechanism on CONN_MAX_RETRIES + 1 attempts
424
   * and will immediately throw
425
   * "Connection died, tried CONN_MAX_RETRIES times before giving up".
426
   * By resetting it here, we ensure each new request starts fresh. */
427
6.43k
  data->state.retrycount = 0;
428
429
6.43k
  if(!CURL_EASY_STR(data, STRING_SET_URL) && !data->set.uh) {
430
    /* we cannot do anything without URL */
431
9
    failf(data, "No URL set");
432
9
    return CURLE_URL_MALFORMAT;
433
9
  }
434
435
  /* CURLOPT_CURLU overrides CURLOPT_URL and the contents of the CURLU handle
436
     is allowed to be changed by the user between transfers */
437
6.42k
  if(data->set.uh) {
438
0
    char *url = NULL;
439
0
    CURLUcode uc;
440
0
    uc = curl_url_get(data->set.uh, CURLUPART_URL, &url, 0);
441
0
    if(uc) {
442
      /* clear the pointer to not point to freed memory anymore */
443
0
      Curl_bufref_set(&data->state.url, NULL, 0, NULL);
444
0
      failf(data, "No URL set");
445
0
      return CURLE_URL_MALFORMAT;
446
0
    }
447
0
    result = CURL_EASY_STR_SETN(data, STRING_SET_URL, url);
448
0
    if(result)
449
0
      return result;
450
0
  }
451
452
6.42k
  Curl_bufref_set(&data->state.url, CURL_EASY_STR(data, STRING_SET_URL),
453
6.42k
                  0, NULL);
454
455
6.42k
  if(data->set.postfields && data->set.set_resume_from) {
456
    /* we cannot */
457
0
    failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
458
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
459
0
  }
460
461
6.42k
  data->state.prefer_ascii = data->set.prefer_ascii;
462
6.42k
#ifdef CURL_LIST_ONLY_PROTOCOL
463
6.42k
  data->state.list_only = data->set.list_only;
464
6.42k
#endif
465
6.42k
  data->state.httpreq = data->set.method;
466
467
  /* initial transfer request coming up, forget the initial origin
468
   * from a previous perform() on this handle. */
469
6.42k
  Curl_peer_unlink(&data->state.initial_origin);
470
6.42k
  Curl_peer_unlink(&data->state.origin);
471
6.42k
  data->state.requests = 0;
472
6.42k
  data->state.still_connecting = FALSE;
473
6.42k
  data->state.followlocation = 0; /* reset the location-follow counter */
474
6.42k
  data->state.this_is_a_follow = FALSE; /* reset this */
475
6.42k
  data->state.http_ignorecustom = FALSE; /* use custom HTTP method */
476
6.42k
  data->state.errorbuf = FALSE; /* no error has occurred */
477
6.42k
#ifndef CURL_DISABLE_HTTP
478
6.42k
  Curl_http_neg_init(data, &data->state.http_neg);
479
6.42k
#endif
480
6.42k
  data->state.authproblem = FALSE;
481
6.42k
  data->state.authhost.want = data->set.httpauth;
482
6.42k
  data->state.authproxy.want = data->set.proxyauth;
483
6.42k
  curlx_safefree(data->info.wouldredirect);
484
6.42k
  curlx_safefree(data->info.effective_url);
485
6.42k
  Curl_data_priority_clear_state(data);
486
6.42k
  if(data->set.http_auto_referer)
487
0
    Curl_bufref_free(&data->state.referer);
488
6.42k
  if(CURL_EASY_STR(data, STRING_SET_REFERER))
489
0
    Curl_bufref_set(&data->state.referer,
490
0
                    CURL_EASY_STR(data, STRING_SET_REFERER), 0, NULL);
491
6.42k
  else
492
6.42k
    Curl_bufref_free(&data->state.referer);
493
494
6.42k
  if(data->state.httpreq == HTTPREQ_PUT)
495
230
    data->state.infilesize = data->set.filesize;
496
6.19k
  else if((data->state.httpreq != HTTPREQ_GET) &&
497
2.66k
          (data->state.httpreq != HTTPREQ_HEAD)) {
498
2.62k
    data->state.infilesize = data->set.postfieldsize;
499
2.62k
    if(data->set.postfields && (data->state.infilesize == -1))
500
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
501
2.62k
  }
502
3.56k
  else
503
3.56k
    data->state.infilesize = 0;
504
505
  /* If there is a list of cookie files to read, do it now! */
506
6.42k
  result = Curl_cookie_loadfiles(data,
507
6.42k
                                 data->set.cookiesession ?
508
6.42k
                                 COOKIE_NOSESSION : 0);
509
6.42k
  if(!result)
510
6.41k
    Curl_cookie_run(data); /* activate */
511
512
  /* If there is a list of host pairs to deal with */
513
6.42k
  if(!result && data->set.a.resolve && !data->state.resolve_loaded)
514
0
    result = Curl_loadhostpairs(data);
515
516
6.42k
#ifndef CURL_DISABLE_HSTS
517
6.42k
  if(!result)
518
    /* If there is a list of hsts files to read */
519
6.41k
    result = Curl_hsts_loadfiles(data, data->set.a.hsts_files);
520
6.42k
#endif
521
522
6.42k
  if(!result) {
523
    /* Allow data->set.use_port to set which port to use. This needs to be
524
     * disabled for example when we follow Location: headers to URLs using
525
     * different ports! */
526
6.39k
    data->state.allow_port = TRUE;
527
528
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(MSG_NOSIGNAL)
529
    /*************************************************************
530
     * Tell signal handler to ignore SIGPIPE
531
     *************************************************************/
532
    if(!data->set.no_signal)
533
      data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
534
#endif
535
536
6.39k
    Curl_initinfo(data); /* reset session-specific information "variables" */
537
6.39k
    Curl_pgrsResetTransferSizes(data);
538
6.39k
    Curl_pgrsStart(data, NULL);
539
    /* Start rate limiting afresh: a handle reused for another transfer
540
       must not carry over the time step and tokens of an earlier one */
541
6.39k
    Curl_rlimit_start(&data->progress.dl.rlimit, Curl_pgrs_now(data), -1);
542
6.39k
    Curl_rlimit_start(&data->progress.ul.rlimit, Curl_pgrs_now(data), -1);
543
544
    /* In case the handle is reused and an authentication method was picked
545
       in the session we need to make sure we only use the one(s) we now
546
       consider to be fine */
547
6.39k
    data->state.authhost.picked &= data->state.authhost.want;
548
6.39k
    data->state.authproxy.picked &= data->state.authproxy.want;
549
550
6.39k
#ifndef CURL_DISABLE_FTP
551
6.39k
    data->state.wildcardmatch = data->set.wildcard_enabled;
552
6.39k
#ifdef DEBUGBUILD
553
6.39k
  if(getenv("CURL_DBG_FTP_WILDCARD"))
554
0
    data->state.wildcardmatch = TRUE;
555
6.39k
#endif
556
6.39k
    if(data->state.wildcardmatch) {
557
0
      struct WildcardData *wc;
558
0
      if(!data->wildcard) {
559
0
        data->wildcard = curlx_calloc(1, sizeof(struct WildcardData));
560
0
        if(!data->wildcard)
561
0
          return CURLE_OUT_OF_MEMORY;
562
0
      }
563
0
      wc = data->wildcard;
564
0
      if(wc->state < CURLWC_INIT) {
565
0
        if(wc->ftpwc)
566
0
          wc->dtor(wc->ftpwc);
567
0
        curlx_safefree(wc->pattern);
568
0
        curlx_safefree(wc->path);
569
0
        Curl_wildcard_init(wc); /* init wildcard structures */
570
0
      }
571
0
    }
572
6.39k
#endif
573
6.39k
    result = Curl_hsts_loadcb(data, data->hsts);
574
6.39k
  }
575
576
6.42k
  data->req.headerbytecount = 0;
577
  /* a previous transfer on this handle may have ended with a rewind request
578
   * still pending (a follow or auth retry that failed). This is a new
579
   * transfer, so drop those readers now instead of letting a later upload
580
   * read from them. */
581
6.42k
  data->req.rewind_read = FALSE;
582
6.42k
  Curl_client_reset(data);
583
6.42k
  Curl_headers_cleanup(data);
584
6.42k
  return result;
585
6.42k
}
586
587
/* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
588
589
   NOTE: that the *url is curlx_malloc()ed. */
590
CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
591
1.22k
{
592
1.22k
  struct connectdata *conn = data->conn;
593
1.22k
  bool retry = FALSE;
594
1.22k
  *url = NULL;
595
596
  /* if we are talking upload, we cannot do the checks below, unless the
597
     protocol is HTTP as when uploading over HTTP we will still get a
598
     response */
599
1.22k
  if(data->state.upload &&
600
11
     !(conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_RTSP)))
601
11
    return CURLE_OK;
602
603
1.21k
  if(data->set.never_retry)
604
0
    return CURLE_OK;
605
606
1.21k
  if(conn->bits.reuse &&
607
0
     (data->req.bytecount + data->req.headerbytecount == 0) &&
608
0
     ((!data->req.no_body && !data->req.done) ||
609
0
      (conn->scheme->protocol & PROTO_FAMILY_HTTP))
610
0
#ifndef CURL_DISABLE_RTSP
611
0
     && (data->set.rtspreq != RTSPREQ_RECEIVE)
612
1.21k
#endif
613
1.21k
    )
614
    /* We got no data, we attempted to reuse a connection. For HTTP this
615
       can be a retry so we try again regardless if we expected a body.
616
       For other protocols we only try again only if we expected a body.
617
618
       This might happen if the connection was left alive when we were
619
       done using it before, but that was closed when we wanted to read from
620
       it again. Bad luck. Retry the same request on a fresh connect! */
621
0
    retry = TRUE;
622
1.21k
  else if(data->req.refused_stream &&
623
0
          (data->req.bytecount + data->req.headerbytecount == 0)) {
624
    /* This was sent on a refused stream, safe to rerun. A refused stream
625
       error can only happen on multiplexed protoccols (HTTP/2+3).
626
       While this *should* imply that the server never processed any
627
       of the request data, we only act on this when we have
628
       not received any response headers or content bytes. */
629
0
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
630
0
    retry = TRUE;
631
0
  }
632
1.21k
  if(retry) {
633
0
#define CONN_MAX_RETRIES 5
634
0
    if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
635
0
      failf(data, "Connection died, tried %d times before giving up",
636
0
            CONN_MAX_RETRIES);
637
0
      data->state.retrycount = 0;
638
0
      return CURLE_SEND_ERROR;
639
0
    }
640
0
    infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
641
0
          data->state.retrycount);
642
0
    *url = Curl_bufref_dup(&data->state.url);
643
0
    if(!*url)
644
0
      return CURLE_OUT_OF_MEMORY;
645
646
0
    connclose(conn); /* close this connection */
647
0
    conn->bits.retry = TRUE; /* mark this as a connection we are about to
648
                                retry. Marking it this way should prevent i.e
649
                                HTTP transfers to return error because nothing
650
                                has been transferred! */
651
0
    Curl_creader_set_rewind(data, TRUE);
652
0
  }
653
1.21k
  return CURLE_OK;
654
1.21k
}
655
656
static void xfer_setup(
657
  struct Curl_easy *data,   /* transfer */
658
  bool want_send,
659
  bool want_recv,
660
  curl_off_t recv_size      /* how much to receive, -1 if unknown */
661
  )
662
1.87k
{
663
1.87k
  struct SingleRequest *k = &data->req;
664
1.87k
  struct connectdata *conn = data->conn;
665
666
1.87k
  DEBUGASSERT(conn);
667
  /* if request wants to send, switching off the send direction is wrong */
668
1.87k
  DEBUGASSERT(want_send || !Curl_req_want_send(data));
669
670
1.87k
  conn->bits.want_send = want_send;
671
1.87k
  conn->bits.want_recv = want_recv;
672
673
  /* without receiving, there should be not recv_size */
674
1.87k
  DEBUGASSERT(want_recv || (recv_size == -1));
675
1.87k
  k->size = recv_size;
676
1.87k
  k->header = !!conn->scheme->run->write_resp_hd;
677
678
  /* The code sequence below is placed in this function because all necessary
679
     input is not always known in do_complete() as this function may be called
680
     after that */
681
1.87k
  if(!k->header && (recv_size > 0))
682
160
    Curl_pgrsSetDownloadSize(data, recv_size);
683
684
  /* we want header and/or body, if neither then do not do this! */
685
1.87k
  if(conn->scheme->run->write_resp_hd || !data->req.no_body) {
686
1.86k
    if(want_recv)
687
158
      CURL_REQ_SET_RECV(data);
688
1.86k
    if(want_send)
689
1.67k
      CURL_REQ_SET_SEND(data);
690
1.86k
  }
691
1.87k
  CURL_TRC_M(data, "xfer_setup: want_recv=%d, want_send=%d",
692
1.87k
             conn->bits.want_recv, conn->bits.want_send);
693
1.87k
}
694
695
void Curl_xfer_setup_nop(struct Curl_easy *data)
696
34
{
697
34
  xfer_setup(data, FALSE, FALSE, -1);
698
34
}
699
700
void Curl_xfer_setup_sendrecv(struct Curl_easy *data,
701
                              curl_off_t recv_size)
702
0
{
703
0
  xfer_setup(data, TRUE, TRUE, recv_size);
704
0
}
705
706
void Curl_xfer_setup_send(struct Curl_easy *data)
707
1.67k
{
708
1.67k
  xfer_setup(data, TRUE, FALSE, -1);
709
1.67k
}
710
711
void Curl_xfer_setup_recv(struct Curl_easy *data,
712
                          curl_off_t recv_size)
713
160
{
714
160
  xfer_setup(data, FALSE, TRUE, recv_size);
715
160
}
716
717
CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
718
                              const char *buf, size_t blen,
719
                              bool is_eos)
720
337
{
721
337
  CURLcode result = CURLE_OK;
722
723
337
  if(data->conn->scheme->run->write_resp) {
724
    /* protocol handlers offering this function take full responsibility
725
     * for writing all received download data to the client. */
726
0
    result = data->conn->scheme->run->write_resp(data, buf, blen, is_eos);
727
0
  }
728
337
  else {
729
    /* No special handling by protocol handler, write all received data
730
     * as BODY to the client. */
731
337
    if(blen || is_eos) {
732
337
      int cwtype = CLIENTWRITE_BODY;
733
337
      if(is_eos)
734
28
        cwtype |= CLIENTWRITE_EOS;
735
337
      result = Curl_client_write(data, cwtype, buf, blen);
736
337
    }
737
337
  }
738
739
337
  if(!result && is_eos) {
740
    /* If we wrote the EOS, we are definitely done */
741
28
    data->req.eos_written = TRUE;
742
28
    data->req.download_done = TRUE;
743
28
  }
744
337
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
745
337
                 blen, is_eos, (int)result);
746
337
  return result;
747
337
}
748
749
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
750
0
{
751
0
  return Curl_cwriter_is_paused(data);
752
0
}
753
754
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
755
                                 const char *hd0, size_t hdlen, bool is_eos)
756
0
{
757
0
  if(data->conn->scheme->run->write_resp_hd) {
758
0
    DEBUGASSERT(!hd0[hdlen]); /* null-terminated */
759
    /* protocol handlers offering this function take full responsibility
760
     * for writing all received download data to the client. */
761
0
    return data->conn->scheme->run->write_resp_hd(data, hd0, hdlen, is_eos);
762
0
  }
763
  /* No special handling by protocol handler, write as response bytes */
764
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
765
0
}
766
767
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
768
6.16k
{
769
6.16k
  (void)premature;
770
6.16k
  return Curl_cw_out_done(data);
771
6.16k
}
772
773
bool Curl_xfer_needs_flush(struct Curl_easy *data)
774
1.46k
{
775
1.46k
  return Curl_cfchain_needs_flush(data, &data->conn->chain);
776
1.46k
}
777
778
CURLcode Curl_xfer_flush(struct Curl_easy *data)
779
0
{
780
0
  return Curl_cfchain_flush(data, &data->conn->chain);
781
0
}
782
783
CURLcode Curl_xfer_try_send(struct Curl_easy *data,
784
                            const void *buf, size_t blen, bool eos,
785
                            size_t *pnwritten)
786
40.9k
{
787
40.9k
  CURLcode result;
788
789
40.9k
  DEBUGASSERT(data);
790
40.9k
  DEBUGASSERT(data->conn);
791
792
40.9k
  result = Curl_conn_send(data, buf, blen, eos, pnwritten);
793
40.9k
  if(!result && *pnwritten)
794
34.4k
    data->info.request_size += *pnwritten;
795
796
40.9k
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
797
40.9k
               blen, eos, (int)result, *pnwritten));
798
40.9k
  return result;
799
40.9k
}
800
801
CURLcode Curl_xfer_send(struct Curl_easy *data,
802
                        const void *buf, size_t blen, bool eos,
803
                        size_t *pnwritten)
804
0
{
805
0
  CURLcode result;
806
807
0
  DEBUGASSERT(data);
808
0
  DEBUGASSERT(data->conn);
809
810
0
  result = Curl_xfer_try_send(data, buf, blen, eos, pnwritten);
811
0
  if(result == CURLE_AGAIN) {
812
0
    result = CURLE_OK;
813
0
    *pnwritten = 0;
814
0
  }
815
0
  return result;
816
0
}
817
818
CURLcode Curl_xfer_recv(struct Curl_easy *data,
819
                        char *buf, size_t blen,
820
                        size_t *pnrcvd)
821
1.09k
{
822
1.09k
  DEBUGASSERT(data);
823
1.09k
  DEBUGASSERT(data->conn);
824
1.09k
  DEBUGASSERT(data->set.buffer_size > 0);
825
826
1.09k
  if(curlx_uitouz(data->set.buffer_size) < blen)
827
0
    blen = curlx_uitouz(data->set.buffer_size);
828
1.09k
  return Curl_conn_recv(data, buf, blen, pnrcvd);
829
1.09k
}
830
831
bool Curl_xfer_is_blocked(struct Curl_easy *data)
832
42.5k
{
833
42.5k
  bool want_send = CURL_REQ_WANT_SEND(data);
834
42.5k
  bool want_recv = CURL_REQ_WANT_RECV(data);
835
42.5k
  if(!want_send)
836
869
    return want_recv && Curl_xfer_recv_is_paused(data);
837
41.6k
  else if(!want_recv)
838
41.6k
    return want_send && Curl_xfer_send_is_paused(data);
839
0
  else
840
0
    return Curl_xfer_recv_is_paused(data) && Curl_xfer_send_is_paused(data);
841
42.5k
}
842
843
bool Curl_xfer_send_is_paused(struct Curl_easy *data)
844
81.8k
{
845
81.8k
  return Curl_rlimit_is_blocked(&data->progress.ul.rlimit);
846
81.8k
}
847
848
bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
849
11.6k
{
850
11.6k
  return Curl_rlimit_is_blocked(&data->progress.dl.rlimit);
851
11.6k
}
852
853
CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable)
854
0
{
855
0
  CURLcode result = CURLE_OK;
856
0
  Curl_rlimit_block(&data->progress.ul.rlimit, enable, Curl_pgrs_now(data));
857
0
  if(!enable && Curl_creader_is_paused(data))
858
0
    result = Curl_creader_unpause(data);
859
0
  Curl_pgrsSendPause(data, enable);
860
0
  return result;
861
0
}
862
863
CURLcode Curl_xfer_pause_recv(struct Curl_easy *data, bool enable, bool flush)
864
0
{
865
0
  CURLcode result = CURLE_OK;
866
0
  Curl_rlimit_block(&data->progress.dl.rlimit, enable, Curl_pgrs_now(data));
867
0
  if(enable)
868
0
    data->req.writer.paused = TRUE;
869
0
  else if(Curl_cwriter_is_paused(data))
870
0
    result = Curl_cwriter_unpause(data, flush);
871
0
  Curl_conn_ev_data_pause(data, enable);
872
0
  Curl_pgrsRecvPause(data, enable);
873
0
  return result;
874
0
}
875
876
bool Curl_xfer_is_secure(struct Curl_easy *data)
877
0
{
878
0
#ifndef CURL_DISABLE_PROXY
879
0
  if(data->conn && data->conn->bits.origin_is_proxy) {
880
    /* talking to a forward proxy, not secure. we do not use
881
     * a forward proxy for https: and other 's' URLs. Let's just check that
882
     * this did not fail somewhere. */
883
0
    DEBUGASSERT(!(data->state.origin->scheme->flags & PROTOPT_SSL));
884
0
    return FALSE;
885
0
  }
886
0
#endif
887
0
  return (data->state.origin->scheme->flags & PROTOPT_SSL);
888
0
}