Coverage Report

Created: 2026-12-01 06:28

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
86.1k
{
88
86.1k
  struct curl_slist *head;
89
86.1k
  DEBUGASSERT(thislen);
90
86.1k
  DEBUGASSERT(thisheader[thislen - 1] != ':');
91
92
147k
  for(head = data->set.headers; head; head = head->next) {
93
66.1k
    if(curl_strnequal(head->data, thisheader, thislen) &&
94
4.55k
       Curl_headersep(head->data[thislen]))
95
4.27k
      return head->data;
96
66.1k
  }
97
98
81.8k
  return NULL;
99
86.1k
}
100
#endif
101
102
static int data_pending(struct Curl_easy *data, bool rcvd_eagain)
103
44.1k
{
104
44.1k
  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
44.1k
  return (!rcvd_eagain &&
109
0
          conn->scheme->protocol & (CURLPROTO_SCP | CURLPROTO_SFTP)) ||
110
44.1k
         Curl_conn_data_pending(data);
111
44.1k
}
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
71.9k
{
145
71.9k
  if(!data || !data->conn)
146
0
    return FALSE;
147
71.9k
  return CURL_CFCHAIN_IN_SHUTDOWN(&data->conn->chain);
148
71.9k
}
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
72.6k
{
163
72.6k
  CURLcode result;
164
165
72.6k
  DEBUGASSERT(blen > 0);
166
72.6k
  *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
72.6k
  if(!eos_reliable && !data->req.header && data->req.size != -1) {
170
668
    blen = curlx_sotouz_range(data->req.size - data->req.bytecount, 0, blen);
171
668
  }
172
71.9k
  else if(xfer_recv_shutdown_started(data)) {
173
    /* we already received everything. Do not try more. */
174
0
    blen = 0;
175
0
  }
176
177
72.6k
  if(blen) {
178
72.6k
    result = Curl_xfer_recv(data, buf, blen, pnread);
179
72.6k
    if(result)
180
50.8k
      return result;
181
72.6k
  }
182
183
21.8k
  if(*pnread == 0)
184
4.80k
    DEBUGF(infof(data, "sendrecv_dl: we are done"));
185
21.8k
  return CURLE_OK;
186
72.6k
}
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
59.9k
{
196
59.9k
  struct connectdata *conn = data->conn;
197
59.9k
  CURLcode result = CURLE_OK;
198
59.9k
  char *buf, *xfer_buf;
199
59.9k
  size_t blen, xfer_blen;
200
59.9k
  int maxloops = 10;
201
59.9k
  bool safe_eos = FALSE;
202
59.9k
  bool rcvd_eagain = FALSE;
203
59.9k
  bool is_eos = FALSE, rate_limited = FALSE;
204
205
59.9k
  result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
206
59.9k
  if(result)
207
17
    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
72.6k
  do {
212
72.6k
    size_t bytestoread;
213
214
72.6k
    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
72.6k
      safe_eos = Curl_conn_has_safe_eos(conn) &&
221
40.3k
                 (!data->state.use_range ||
222
581
                  (conn->scheme->protocol & PROTO_FAMILY_HTTP));
223
72.6k
    }
224
225
72.6k
    buf = xfer_buf;
226
72.6k
    bytestoread = xfer_blen;
227
228
72.6k
    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
72.6k
    rcvd_eagain = FALSE;
246
72.6k
    result = xfer_recv_resp(data, buf, bytestoread, safe_eos, &blen);
247
72.6k
    if(result) {
248
50.8k
      if(result != CURLE_AGAIN)
249
6.66k
        goto out; /* real error */
250
44.1k
      rcvd_eagain = TRUE;
251
44.1k
      result = CURLE_OK;
252
44.1k
      if(data->req.download_done && data->req.no_body &&
253
2
         !data->req.resp_trailer) {
254
2
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
255
2
                     "not waiting for EOS"));
256
2
        blen = 0;
257
        /* continue as if we received the EOS */
258
2
      }
259
44.1k
      else
260
44.1k
        break; /* get out of loop */
261
44.1k
    }
262
263
    /* We only get a 0-length receive at the end of the response */
264
21.8k
    is_eos = (blen == 0);
265
266
21.8k
    if(!blen) {
267
4.80k
      result = Curl_req_stop_send_recv(data);
268
4.80k
      if(result)
269
0
        goto out;
270
4.80k
      if(k->eos_written) /* already did write this to client, leave */
271
0
        break;
272
4.80k
    }
273
274
21.8k
    result = Curl_xfer_write_resp(data, buf, blen, is_eos);
275
21.8k
    if(result || data->req.done)
276
1.16k
      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
20.6k
    if((!safe_eos && data->req.download_done) || is_eos) {
282
7.90k
      CURL_REQ_CLEAR_RECV(data);
283
7.90k
    }
284
    /* if we stopped receiving, leave the loop */
285
20.6k
    if(!CURL_REQ_WANT_RECV(data))
286
7.90k
      break;
287
288
20.6k
  } while(maxloops--);
289
290
52.0k
  if(!is_eos && !rate_limited && CURL_REQ_WANT_RECV(data) &&
291
44.1k
     (!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
35
    Curl_multi_mark_dirty(data);
295
35
    CURL_TRC_M(data, "sendrecv_dl() no EAGAIN/pending data, mark as dirty");
296
35
  }
297
298
52.0k
  if(!CURL_REQ_WANT_RECV(data) && CURL_REQ_WANT_SEND(data) &&
299
3
     (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
2
    infof(data, "we are done reading and this is set to close, stop send");
304
2
    Curl_req_abort_sending(data);
305
2
  }
306
307
59.9k
out:
308
59.9k
  Curl_multi_xfer_buf_release(data, xfer_buf);
309
59.9k
  if(result)
310
7.84k
    DEBUGF(infof(data, "sendrecv_dl() -> %d", (int)result));
311
59.9k
  return result;
312
52.0k
}
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
843
{
319
  /* We should not get here when the sending is already done. */
320
843
  DEBUGASSERT(!Curl_req_done_sending(data));
321
322
843
  if(!Curl_req_done_sending(data))
323
843
    return Curl_req_send_more(data);
324
0
  return CURLE_OK;
325
843
}
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
59.9k
{
333
59.9k
  struct SingleRequest *k = &data->req;
334
59.9k
  const struct curltime *pnow = NULL;
335
59.9k
  CURLcode result = CURLE_OK;
336
337
59.9k
  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
59.9k
  if(CURL_REQ_WANT_RECV(data)) {
345
59.9k
    result = sendrecv_dl(data, k);
346
59.9k
    if(result || data->req.done)
347
7.85k
      goto out;
348
59.9k
  }
349
350
  /* If we still have writing to do, we check if we have a writable socket. */
351
52.0k
  if(Curl_req_want_send(data)) {
352
843
    result = sendrecv_ul(data);
353
843
    if(result)
354
4
      goto out;
355
843
  }
356
357
52.0k
  pnow = Curl_pgrs_now(data);
358
52.0k
  if(CURL_REQ_WANT_IO(data)) {
359
44.1k
    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
44.1k
  }
377
7.90k
  else {
378
    /*
379
     * The transfer has been performed. Make some general checks before
380
     * returning.
381
     */
382
7.90k
    if(!data->req.no_body && (k->size != -1) &&
383
3.64k
       (k->bytecount != k->size) && !k->newurl) {
384
85
      failf(data, "transfer closed with %" FMT_OFF_T
385
85
            " bytes remaining to read", k->size - k->bytecount);
386
85
      result = CURLE_PARTIAL_FILE;
387
85
      goto out;
388
85
    }
389
7.90k
  }
390
391
  /* If there is nothing more to send/recv, the request is done */
392
51.9k
  if(!CURL_REQ_WANT_IO(data))
393
7.81k
    data->req.done = TRUE;
394
395
51.9k
  result = Curl_pgrsCheckX(data, pnow);
396
397
59.9k
out:
398
59.9k
  if(result)
399
7.93k
    DEBUGF(infof(data, "Curl_sendrecv() -> %d", (int)result));
400
59.9k
  return result;
401
51.9k
}
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
19.0k
{
407
19.0k
  data->state.fread_func = data->set.fread_func_set;
408
19.0k
  data->state.in = data->set.in_set;
409
19.0k
  data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
410
19.0k
}
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
18.4k
{
419
18.4k
  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
18.4k
  data->state.retrycount = 0;
428
429
18.4k
  if(!CURL_EASY_STR(data, STRING_SET_URL) && !data->set.uh) {
430
    /* we cannot do anything without URL */
431
8
    failf(data, "No URL set");
432
8
    return CURLE_URL_MALFORMAT;
433
8
  }
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
18.4k
  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
18.4k
  Curl_bufref_set(&data->state.url, CURL_EASY_STR(data, STRING_SET_URL),
453
18.4k
                  0, NULL);
454
455
18.4k
  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
18.4k
  data->state.prefer_ascii = data->set.prefer_ascii;
462
18.4k
#ifdef CURL_LIST_ONLY_PROTOCOL
463
18.4k
  data->state.list_only = data->set.list_only;
464
18.4k
#endif
465
18.4k
  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
18.4k
  Curl_peer_unlink(&data->state.initial_origin);
470
18.4k
  Curl_peer_unlink(&data->state.origin);
471
18.4k
  data->state.requests = 0;
472
18.4k
  data->state.still_connecting = FALSE;
473
18.4k
  data->state.followlocation = 0; /* reset the location-follow counter */
474
18.4k
  data->state.this_is_a_follow = FALSE; /* reset this */
475
18.4k
  data->state.http_ignorecustom = FALSE; /* use custom HTTP method */
476
18.4k
  data->state.errorbuf = FALSE; /* no error has occurred */
477
18.4k
#ifndef CURL_DISABLE_HTTP
478
18.4k
  Curl_http_neg_init(data, &data->state.http_neg);
479
18.4k
#endif
480
18.4k
  data->state.authproblem = FALSE;
481
18.4k
  data->state.authhost.want = data->set.httpauth;
482
18.4k
  data->state.authproxy.want = data->set.proxyauth;
483
18.4k
  curlx_safefree(data->info.wouldredirect);
484
18.4k
  curlx_safefree(data->info.effective_url);
485
18.4k
  Curl_data_priority_clear_state(data);
486
18.4k
  if(data->set.http_auto_referer)
487
0
    Curl_bufref_free(&data->state.referer);
488
18.4k
  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
18.4k
  else
492
18.4k
    Curl_bufref_free(&data->state.referer);
493
494
18.4k
  if(data->state.httpreq == HTTPREQ_PUT)
495
0
    data->state.infilesize = data->set.filesize;
496
18.4k
  else if((data->state.httpreq != HTTPREQ_GET) &&
497
4.31k
          (data->state.httpreq != HTTPREQ_HEAD)) {
498
4.20k
    data->state.infilesize = data->set.postfieldsize;
499
4.20k
    if(data->set.postfields && (data->state.infilesize == -1))
500
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
501
4.20k
  }
502
14.2k
  else
503
14.2k
    data->state.infilesize = 0;
504
505
  /* If there is a list of cookie files to read, do it now! */
506
18.4k
  result = Curl_cookie_loadfiles(data,
507
18.4k
                                 data->set.cookiesession ?
508
18.4k
                                 COOKIE_NOSESSION : 0);
509
18.4k
  if(!result)
510
18.4k
    Curl_cookie_run(data); /* activate */
511
512
  /* If there is a list of host pairs to deal with */
513
18.4k
  if(!result && data->set.a.resolve && !data->state.resolve_loaded)
514
11.4k
    result = Curl_loadhostpairs(data);
515
516
18.4k
#ifndef CURL_DISABLE_HSTS
517
18.4k
  if(!result)
518
    /* If there is a list of hsts files to read */
519
18.2k
    result = Curl_hsts_loadfiles(data, data->set.a.hsts_files);
520
18.4k
#endif
521
522
18.4k
  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
18.2k
    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
18.2k
    Curl_initinfo(data); /* reset session-specific information "variables" */
537
18.2k
    Curl_pgrsResetTransferSizes(data);
538
18.2k
    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
18.2k
    Curl_rlimit_start(&data->progress.dl.rlimit, Curl_pgrs_now(data), -1);
542
18.2k
    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
18.2k
    data->state.authhost.picked &= data->state.authhost.want;
548
18.2k
    data->state.authproxy.picked &= data->state.authproxy.want;
549
550
18.2k
#ifndef CURL_DISABLE_FTP
551
18.2k
    data->state.wildcardmatch = data->set.wildcard_enabled;
552
18.2k
#ifdef DEBUGBUILD
553
18.2k
  if(getenv("CURL_DBG_FTP_WILDCARD"))
554
0
    data->state.wildcardmatch = TRUE;
555
18.2k
#endif
556
18.2k
    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
18.2k
#endif
573
18.2k
    result = Curl_hsts_loadcb(data, data->hsts);
574
18.2k
  }
575
576
18.4k
  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
18.4k
  data->req.rewind_read = FALSE;
582
18.4k
  Curl_client_reset(data);
583
18.4k
  Curl_headers_cleanup(data);
584
18.4k
  return result;
585
18.4k
}
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
8.15k
{
592
8.15k
  struct connectdata *conn = data->conn;
593
8.15k
  bool retry = FALSE;
594
8.15k
  *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
8.15k
  if(data->state.upload &&
600
0
     !(conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_RTSP)))
601
0
    return CURLE_OK;
602
603
8.15k
  if(data->set.never_retry)
604
0
    return CURLE_OK;
605
606
8.15k
  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
8.15k
#endif
613
8.15k
    )
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
8.15k
  else if(data->req.refused_stream &&
623
117
          (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
116
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
630
116
    retry = TRUE;
631
116
  }
632
8.15k
  if(retry) {
633
116
#define CONN_MAX_RETRIES 5
634
116
    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
116
    infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
641
116
          data->state.retrycount);
642
116
    *url = Curl_bufref_dup(&data->state.url);
643
116
    if(!*url)
644
2
      return CURLE_OUT_OF_MEMORY;
645
646
114
    connclose(conn); /* close this connection */
647
114
    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
114
    Curl_creader_set_rewind(data, TRUE);
652
114
  }
653
8.15k
  return CURLE_OK;
654
8.15k
}
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
16.7k
{
663
16.7k
  struct SingleRequest *k = &data->req;
664
16.7k
  struct connectdata *conn = data->conn;
665
666
16.7k
  DEBUGASSERT(conn);
667
  /* if request wants to send, switching off the send direction is wrong */
668
16.7k
  DEBUGASSERT(want_send || !Curl_req_want_send(data));
669
670
16.7k
  conn->bits.want_send = want_send;
671
16.7k
  conn->bits.want_recv = want_recv;
672
673
  /* without receiving, there should be not recv_size */
674
16.7k
  DEBUGASSERT(want_recv || (recv_size == -1));
675
16.7k
  k->size = recv_size;
676
16.7k
  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
16.7k
  if(!k->header && (recv_size > 0))
682
0
    Curl_pgrsSetDownloadSize(data, recv_size);
683
684
  /* we want header and/or body, if neither then do not do this! */
685
16.7k
  if(conn->scheme->run->write_resp_hd || !data->req.no_body) {
686
16.7k
    if(want_recv)
687
16.7k
      CURL_REQ_SET_RECV(data);
688
16.7k
    if(want_send)
689
16.7k
      CURL_REQ_SET_SEND(data);
690
16.7k
  }
691
16.7k
  CURL_TRC_M(data, "xfer_setup: want_recv=%d, want_send=%d",
692
16.7k
             conn->bits.want_recv, conn->bits.want_send);
693
16.7k
}
694
695
void Curl_xfer_setup_nop(struct Curl_easy *data)
696
0
{
697
0
  xfer_setup(data, FALSE, FALSE, -1);
698
0
}
699
700
void Curl_xfer_setup_sendrecv(struct Curl_easy *data,
701
                              curl_off_t recv_size)
702
16.7k
{
703
16.7k
  xfer_setup(data, TRUE, TRUE, recv_size);
704
16.7k
}
705
706
void Curl_xfer_setup_send(struct Curl_easy *data)
707
0
{
708
0
  xfer_setup(data, TRUE, FALSE, -1);
709
0
}
710
711
void Curl_xfer_setup_recv(struct Curl_easy *data,
712
                          curl_off_t recv_size)
713
0
{
714
0
  xfer_setup(data, FALSE, TRUE, recv_size);
715
0
}
716
717
CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
718
                              const char *buf, size_t blen,
719
                              bool is_eos)
720
22.0k
{
721
22.0k
  CURLcode result = CURLE_OK;
722
723
22.0k
  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
22.0k
    result = data->conn->scheme->run->write_resp(data, buf, blen, is_eos);
727
22.0k
  }
728
0
  else {
729
    /* No special handling by protocol handler, write all received data
730
     * as BODY to the client. */
731
0
    if(blen || is_eos) {
732
0
      int cwtype = CLIENTWRITE_BODY;
733
0
      if(is_eos)
734
0
        cwtype |= CLIENTWRITE_EOS;
735
0
      result = Curl_client_write(data, cwtype, buf, blen);
736
0
    }
737
0
  }
738
739
22.0k
  if(!result && is_eos) {
740
    /* If we wrote the EOS, we are definitely done */
741
4.33k
    data->req.eos_written = TRUE;
742
4.33k
    data->req.download_done = TRUE;
743
4.33k
  }
744
22.0k
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
745
22.0k
                 blen, is_eos, (int)result);
746
22.0k
  return result;
747
22.0k
}
748
749
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
750
225
{
751
225
  return Curl_cwriter_is_paused(data);
752
225
}
753
754
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
755
                                 const char *hd0, size_t hdlen, bool is_eos)
756
84.7k
{
757
84.7k
  if(data->conn->scheme->run->write_resp_hd) {
758
84.7k
    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
84.7k
    return data->conn->scheme->run->write_resp_hd(data, hd0, hdlen, is_eos);
762
84.7k
  }
763
  /* No special handling by protocol handler, write as response bytes */
764
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
765
84.7k
}
766
767
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
768
17.9k
{
769
17.9k
  (void)premature;
770
17.9k
  return Curl_cw_out_done(data);
771
17.9k
}
772
773
bool Curl_xfer_needs_flush(struct Curl_easy *data)
774
59.9k
{
775
59.9k
  return Curl_cfchain_needs_flush(data, &data->conn->chain);
776
59.9k
}
777
778
CURLcode Curl_xfer_flush(struct Curl_easy *data)
779
831
{
780
831
  return Curl_cfchain_flush(data, &data->conn->chain);
781
831
}
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
16.8k
{
787
16.8k
  CURLcode result;
788
789
16.8k
  DEBUGASSERT(data);
790
16.8k
  DEBUGASSERT(data->conn);
791
792
16.8k
  result = Curl_conn_send(data, buf, blen, eos, pnwritten);
793
16.8k
  if(!result && *pnwritten)
794
16.6k
    data->info.request_size += *pnwritten;
795
796
16.8k
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
797
16.8k
               blen, eos, (int)result, *pnwritten));
798
16.8k
  return result;
799
16.8k
}
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
72.6k
{
822
72.6k
  DEBUGASSERT(data);
823
72.6k
  DEBUGASSERT(data->conn);
824
72.6k
  DEBUGASSERT(data->set.buffer_size > 0);
825
826
72.6k
  if(curlx_uitouz(data->set.buffer_size) < blen)
827
158
    blen = curlx_uitouz(data->set.buffer_size);
828
72.6k
  return Curl_conn_recv(data, buf, blen, pnrcvd);
829
72.6k
}
830
831
bool Curl_xfer_is_blocked(struct Curl_easy *data)
832
60.0k
{
833
60.0k
  bool want_send = CURL_REQ_WANT_SEND(data);
834
60.0k
  bool want_recv = CURL_REQ_WANT_RECV(data);
835
60.0k
  if(!want_send)
836
59.7k
    return want_recv && Curl_xfer_recv_is_paused(data);
837
218
  else if(!want_recv)
838
0
    return want_send && Curl_xfer_send_is_paused(data);
839
218
  else
840
218
    return Curl_xfer_recv_is_paused(data) && Curl_xfer_send_is_paused(data);
841
60.0k
}
842
843
bool Curl_xfer_send_is_paused(struct Curl_easy *data)
844
4.05k
{
845
4.05k
  return Curl_rlimit_is_blocked(&data->progress.ul.rlimit);
846
4.05k
}
847
848
bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
849
108k
{
850
108k
  return Curl_rlimit_is_blocked(&data->progress.dl.rlimit);
851
108k
}
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
30.6k
{
878
30.6k
#ifndef CURL_DISABLE_PROXY
879
30.6k
  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
30.6k
#endif
887
30.6k
  return (data->state.origin->scheme->flags & PROTOPT_SSL);
888
30.6k
}