Coverage Report

Created: 2026-12-01 06:27

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
241k
{
88
241k
  struct curl_slist *head;
89
241k
  DEBUGASSERT(thislen);
90
241k
  DEBUGASSERT(thisheader[thislen - 1] != ':');
91
92
575k
  for(head = data->set.headers; head; head = head->next) {
93
343k
    if(curl_strnequal(head->data, thisheader, thislen) &&
94
9.09k
       Curl_headersep(head->data[thislen]))
95
8.88k
      return head->data;
96
343k
  }
97
98
232k
  return NULL;
99
241k
}
100
#endif
101
102
static int data_pending(struct Curl_easy *data, bool rcvd_eagain)
103
74.0k
{
104
74.0k
  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
74.0k
  return (!rcvd_eagain &&
109
0
          conn->scheme->protocol & (CURLPROTO_SCP | CURLPROTO_SFTP)) ||
110
74.0k
         Curl_conn_data_pending(data);
111
74.0k
}
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
159
{
119
159
  if((timeofdoc == 0) || (data->set.timevalue == 0))
120
151
    return TRUE;
121
122
8
  switch(data->set.timecondition) {
123
7
  case CURL_TIMECOND_IFMODSINCE:
124
7
  default:
125
7
    if(timeofdoc <= data->set.timevalue) {
126
6
      infof(data, "The requested document is not new enough");
127
6
      data->info.timecond = TRUE;
128
6
      return FALSE;
129
6
    }
130
1
    break;
131
1
  case CURL_TIMECOND_IFUNMODSINCE:
132
1
    if(timeofdoc >= data->set.timevalue) {
133
1
      infof(data, "The requested document is not old enough");
134
1
      data->info.timecond = TRUE;
135
1
      return FALSE;
136
1
    }
137
0
    break;
138
8
  }
139
140
1
  return TRUE;
141
8
}
142
143
static bool xfer_recv_shutdown_started(struct Curl_easy *data)
144
143k
{
145
143k
  if(!data || !data->conn)
146
0
    return FALSE;
147
143k
  return CURL_CFCHAIN_IN_SHUTDOWN(&data->conn->chain);
148
143k
}
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
143k
{
163
143k
  CURLcode result;
164
165
143k
  DEBUGASSERT(blen > 0);
166
143k
  *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
143k
  if(!eos_reliable && !data->req.header && data->req.size != -1) {
170
214
    blen = curlx_sotouz_range(data->req.size - data->req.bytecount, 0, blen);
171
214
  }
172
143k
  else if(xfer_recv_shutdown_started(data)) {
173
    /* we already received everything. Do not try more. */
174
0
    blen = 0;
175
0
  }
176
177
143k
  if(blen) {
178
143k
    result = Curl_xfer_recv(data, buf, blen, pnread);
179
143k
    if(result)
180
78.7k
      return result;
181
143k
  }
182
183
64.7k
  if(*pnread == 0)
184
16.6k
    DEBUGF(infof(data, "sendrecv_dl: we are done"));
185
64.7k
  return CURLE_OK;
186
143k
}
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
115k
{
196
115k
  struct connectdata *conn = data->conn;
197
115k
  CURLcode result = CURLE_OK;
198
115k
  char *buf, *xfer_buf;
199
115k
  size_t blen, xfer_blen;
200
115k
  int maxloops = 10;
201
115k
  bool safe_eos = FALSE;
202
115k
  bool rcvd_eagain = FALSE;
203
115k
  bool is_eos = FALSE, rate_limited = FALSE;
204
205
115k
  result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
206
115k
  if(result)
207
29
    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
143k
  do {
212
143k
    size_t bytestoread;
213
214
143k
    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
143k
      safe_eos = Curl_conn_has_safe_eos(conn) &&
221
21.5k
                 (!data->state.use_range ||
222
440
                  (conn->scheme->protocol & PROTO_FAMILY_HTTP));
223
143k
    }
224
225
143k
    buf = xfer_buf;
226
143k
    bytestoread = xfer_blen;
227
228
143k
    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
143k
    rcvd_eagain = FALSE;
246
143k
    result = xfer_recv_resp(data, buf, bytestoread, safe_eos, &blen);
247
143k
    if(result) {
248
78.7k
      if(result != CURLE_AGAIN)
249
4.66k
        goto out; /* real error */
250
74.0k
      rcvd_eagain = TRUE;
251
74.0k
      result = CURLE_OK;
252
74.0k
      if(data->req.download_done && data->req.no_body &&
253
3
         !data->req.resp_trailer) {
254
3
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
255
3
                     "not waiting for EOS"));
256
3
        blen = 0;
257
        /* continue as if we received the EOS */
258
3
      }
259
74.0k
      else
260
74.0k
        break; /* get out of loop */
261
74.0k
    }
262
263
    /* We only get a 0-length receive at the end of the response */
264
64.7k
    is_eos = (blen == 0);
265
266
64.7k
    if(!blen) {
267
16.6k
      result = Curl_req_stop_send_recv(data);
268
16.6k
      if(result)
269
0
        goto out;
270
16.6k
      if(k->eos_written) /* already did write this to client, leave */
271
0
        break;
272
16.6k
    }
273
274
64.7k
    result = Curl_xfer_write_resp(data, buf, blen, is_eos);
275
64.7k
    if(result || data->req.done)
276
7.70k
      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
57.0k
    if((!safe_eos && data->req.download_done) || is_eos) {
282
29.4k
      CURL_REQ_CLEAR_RECV(data);
283
29.4k
    }
284
    /* if we stopped receiving, leave the loop */
285
57.0k
    if(!CURL_REQ_WANT_RECV(data))
286
29.4k
      break;
287
288
57.0k
  } while(maxloops--);
289
290
103k
  if(!is_eos && !rate_limited && CURL_REQ_WANT_RECV(data) &&
291
74.0k
     (!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
21
    Curl_multi_mark_dirty(data);
295
21
    CURL_TRC_M(data, "sendrecv_dl() no EAGAIN/pending data, mark as dirty");
296
21
  }
297
298
103k
  if(!CURL_REQ_WANT_RECV(data) && CURL_REQ_WANT_SEND(data) &&
299
120
     (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
21
    infof(data, "we are done reading and this is set to close, stop send");
304
21
    Curl_req_abort_sending(data);
305
21
  }
306
307
115k
out:
308
115k
  Curl_multi_xfer_buf_release(data, xfer_buf);
309
115k
  if(result)
310
9.66k
    DEBUGF(infof(data, "sendrecv_dl() -> %d", (int)result));
311
115k
  return result;
312
103k
}
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
21.5k
{
319
  /* We should not get here when the sending is already done. */
320
21.5k
  DEBUGASSERT(!Curl_req_done_sending(data));
321
322
21.5k
  if(!Curl_req_done_sending(data))
323
21.5k
    return Curl_req_send_more(data);
324
0
  return CURLE_OK;
325
21.5k
}
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
118k
{
333
118k
  struct SingleRequest *k = &data->req;
334
118k
  const struct curltime *pnow = NULL;
335
118k
  CURLcode result = CURLE_OK;
336
337
118k
  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
118k
  if(CURL_REQ_WANT_RECV(data)) {
345
115k
    result = sendrecv_dl(data, k);
346
115k
    if(result || data->req.done)
347
12.3k
      goto out;
348
115k
  }
349
350
  /* If we still have writing to do, we check if we have a writable socket. */
351
106k
  if(Curl_req_want_send(data)) {
352
21.5k
    result = sendrecv_ul(data);
353
21.5k
    if(result)
354
256
      goto out;
355
21.5k
  }
356
357
106k
  pnow = Curl_pgrs_now(data);
358
106k
  if(CURL_REQ_WANT_IO(data)) {
359
76.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
76.8k
  }
377
29.3k
  else {
378
    /*
379
     * The transfer has been performed. Make some general checks before
380
     * returning.
381
     */
382
29.3k
    if(!data->req.no_body && (k->size != -1) &&
383
13.1k
       (k->bytecount != k->size) && !k->newurl) {
384
3
      failf(data, "transfer closed with %" FMT_OFF_T
385
3
            " bytes remaining to read", k->size - k->bytecount);
386
3
      result = CURLE_PARTIAL_FILE;
387
3
      goto out;
388
3
    }
389
29.3k
  }
390
391
  /* If there is nothing more to send/recv, the request is done */
392
106k
  if(!CURL_REQ_WANT_IO(data))
393
29.3k
    data->req.done = TRUE;
394
395
106k
  result = Curl_pgrsCheckX(data, pnow);
396
397
118k
out:
398
118k
  if(result)
399
9.92k
    DEBUGF(infof(data, "Curl_sendrecv() -> %d", (int)result));
400
118k
  return result;
401
106k
}
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
49.9k
{
407
49.9k
  data->state.fread_func = data->set.fread_func_set;
408
49.9k
  data->state.in = data->set.in_set;
409
49.9k
  data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
410
49.9k
}
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
29.5k
{
419
29.5k
  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
29.5k
  data->state.retrycount = 0;
428
429
29.5k
  if(!CURL_EASY_STR(data, STRING_SET_URL) && !data->set.uh) {
430
    /* we cannot do anything without URL */
431
28
    failf(data, "No URL set");
432
28
    return CURLE_URL_MALFORMAT;
433
28
  }
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
29.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
29.4k
  Curl_bufref_set(&data->state.url, CURL_EASY_STR(data, STRING_SET_URL),
453
29.4k
                  0, NULL);
454
455
29.4k
  if(data->set.postfields && data->set.set_resume_from) {
456
    /* we cannot */
457
47
    failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
458
47
    return CURLE_BAD_FUNCTION_ARGUMENT;
459
47
  }
460
461
29.4k
  data->state.prefer_ascii = data->set.prefer_ascii;
462
29.4k
#ifdef CURL_LIST_ONLY_PROTOCOL
463
29.4k
  data->state.list_only = data->set.list_only;
464
29.4k
#endif
465
29.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
29.4k
  Curl_peer_unlink(&data->state.initial_origin);
470
29.4k
  Curl_peer_unlink(&data->state.origin);
471
29.4k
  data->state.requests = 0;
472
29.4k
  data->state.still_connecting = FALSE;
473
29.4k
  data->state.followlocation = 0; /* reset the location-follow counter */
474
29.4k
  data->state.this_is_a_follow = FALSE; /* reset this */
475
29.4k
  data->state.http_ignorecustom = FALSE; /* use custom HTTP method */
476
29.4k
  data->state.errorbuf = FALSE; /* no error has occurred */
477
29.4k
#ifndef CURL_DISABLE_HTTP
478
29.4k
  Curl_http_neg_init(data, &data->state.http_neg);
479
29.4k
#endif
480
29.4k
  data->state.authproblem = FALSE;
481
29.4k
  data->state.authhost.want = data->set.httpauth;
482
29.4k
  data->state.authproxy.want = data->set.proxyauth;
483
29.4k
  curlx_safefree(data->info.wouldredirect);
484
29.4k
  curlx_safefree(data->info.effective_url);
485
29.4k
  Curl_data_priority_clear_state(data);
486
29.4k
  if(data->set.http_auto_referer)
487
423
    Curl_bufref_free(&data->state.referer);
488
29.4k
  if(CURL_EASY_STR(data, STRING_SET_REFERER))
489
221
    Curl_bufref_set(&data->state.referer,
490
221
                    CURL_EASY_STR(data, STRING_SET_REFERER), 0, NULL);
491
29.2k
  else
492
29.2k
    Curl_bufref_free(&data->state.referer);
493
494
29.4k
  if(data->state.httpreq == HTTPREQ_PUT)
495
446
    data->state.infilesize = data->set.filesize;
496
29.0k
  else if((data->state.httpreq != HTTPREQ_GET) &&
497
9.76k
          (data->state.httpreq != HTTPREQ_HEAD)) {
498
9.53k
    data->state.infilesize = data->set.postfieldsize;
499
9.53k
    if(data->set.postfields && (data->state.infilesize == -1))
500
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
501
9.53k
  }
502
19.4k
  else
503
19.4k
    data->state.infilesize = 0;
504
505
  /* If there is a list of cookie files to read, do it now! */
506
29.4k
  result = Curl_cookie_loadfiles(data,
507
29.4k
                                 data->set.cookiesession ?
508
29.0k
                                 COOKIE_NOSESSION : 0);
509
29.4k
  if(!result)
510
29.4k
    Curl_cookie_run(data); /* activate */
511
512
  /* If there is a list of host pairs to deal with */
513
29.4k
  if(!result && data->set.a.resolve && !data->state.resolve_loaded)
514
0
    result = Curl_loadhostpairs(data);
515
516
29.4k
#ifndef CURL_DISABLE_HSTS
517
29.4k
  if(!result)
518
    /* If there is a list of hsts files to read */
519
29.4k
    result = Curl_hsts_loadfiles(data, data->set.a.hsts_files);
520
29.4k
#endif
521
522
29.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
29.3k
    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
29.3k
    Curl_initinfo(data); /* reset session-specific information "variables" */
537
29.3k
    Curl_pgrsResetTransferSizes(data);
538
29.3k
    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
29.3k
    Curl_rlimit_start(&data->progress.dl.rlimit, Curl_pgrs_now(data), -1);
542
29.3k
    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
29.3k
    data->state.authhost.picked &= data->state.authhost.want;
548
29.3k
    data->state.authproxy.picked &= data->state.authproxy.want;
549
550
29.3k
#ifndef CURL_DISABLE_FTP
551
29.3k
    data->state.wildcardmatch = data->set.wildcard_enabled;
552
29.3k
#ifdef DEBUGBUILD
553
29.3k
  if(getenv("CURL_DBG_FTP_WILDCARD"))
554
0
    data->state.wildcardmatch = TRUE;
555
29.3k
#endif
556
29.3k
    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
29.3k
#endif
573
29.3k
    result = Curl_hsts_loadcb(data, data->hsts);
574
29.3k
  }
575
576
29.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
29.4k
  data->req.rewind_read = FALSE;
582
29.4k
  Curl_client_reset(data);
583
29.4k
  Curl_headers_cleanup(data);
584
29.4k
  return result;
585
29.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
34.3k
{
592
34.3k
  struct connectdata *conn = data->conn;
593
34.3k
  bool retry = FALSE;
594
34.3k
  *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
34.3k
  if(data->state.upload &&
600
535
     !(conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_RTSP)))
601
0
    return CURLE_OK;
602
603
34.3k
  if(data->set.never_retry)
604
0
    return CURLE_OK;
605
606
34.3k
  if(conn->bits.reuse &&
607
9.24k
     (data->req.bytecount + data->req.headerbytecount == 0) &&
608
413
     ((!data->req.no_body && !data->req.done) ||
609
413
      (conn->scheme->protocol & PROTO_FAMILY_HTTP))
610
413
#ifndef CURL_DISABLE_RTSP
611
413
     && (data->set.rtspreq != RTSPREQ_RECEIVE)
612
34.3k
#endif
613
34.3k
    )
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
413
    retry = TRUE;
622
33.9k
  else if(data->req.refused_stream &&
623
2.18k
          (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
2.18k
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
630
2.18k
    retry = TRUE;
631
2.18k
  }
632
34.3k
  if(retry) {
633
2.59k
#define CONN_MAX_RETRIES 5
634
2.59k
    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
2.59k
    infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
641
2.59k
          data->state.retrycount);
642
2.59k
    *url = Curl_bufref_dup(&data->state.url);
643
2.59k
    if(!*url)
644
1
      return CURLE_OUT_OF_MEMORY;
645
646
2.59k
    connclose(conn); /* close this connection */
647
2.59k
    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
2.59k
    Curl_creader_set_rewind(data, TRUE);
652
2.59k
  }
653
34.3k
  return CURLE_OK;
654
34.3k
}
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
42.3k
{
663
42.3k
  struct SingleRequest *k = &data->req;
664
42.3k
  struct connectdata *conn = data->conn;
665
666
42.3k
  DEBUGASSERT(conn);
667
  /* if request wants to send, switching off the send direction is wrong */
668
42.3k
  DEBUGASSERT(want_send || !Curl_req_want_send(data));
669
670
42.3k
  conn->bits.want_send = want_send;
671
42.3k
  conn->bits.want_recv = want_recv;
672
673
  /* without receiving, there should be not recv_size */
674
42.3k
  DEBUGASSERT(want_recv || (recv_size == -1));
675
42.3k
  k->size = recv_size;
676
42.3k
  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
42.3k
  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
42.3k
  if(conn->scheme->run->write_resp_hd || !data->req.no_body) {
686
42.3k
    if(want_recv)
687
42.3k
      CURL_REQ_SET_RECV(data);
688
42.3k
    if(want_send)
689
42.3k
      CURL_REQ_SET_SEND(data);
690
42.3k
  }
691
42.3k
  CURL_TRC_M(data, "xfer_setup: want_recv=%d, want_send=%d",
692
42.3k
             conn->bits.want_recv, conn->bits.want_send);
693
42.3k
}
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
42.3k
{
703
42.3k
  xfer_setup(data, TRUE, TRUE, recv_size);
704
42.3k
}
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
64.7k
{
721
64.7k
  CURLcode result = CURLE_OK;
722
723
64.7k
  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
64.7k
    result = data->conn->scheme->run->write_resp(data, buf, blen, is_eos);
727
64.7k
  }
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
64.7k
  if(!result && is_eos) {
740
    /* If we wrote the EOS, we are definitely done */
741
16.5k
    data->req.eos_written = TRUE;
742
16.5k
    data->req.download_done = TRUE;
743
16.5k
  }
744
64.7k
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
745
64.7k
                 blen, is_eos, (int)result);
746
64.7k
  return result;
747
64.7k
}
748
749
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
750
81
{
751
81
  return Curl_cwriter_is_paused(data);
752
81
}
753
754
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
755
                                 const char *hd0, size_t hdlen, bool is_eos)
756
3.38k
{
757
3.38k
  if(data->conn->scheme->run->write_resp_hd) {
758
3.38k
    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
3.38k
    return data->conn->scheme->run->write_resp_hd(data, hd0, hdlen, is_eos);
762
3.38k
  }
763
  /* No special handling by protocol handler, write as response bytes */
764
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
765
3.38k
}
766
767
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
768
49.3k
{
769
49.3k
  (void)premature;
770
49.3k
  return Curl_cw_out_done(data);
771
49.3k
}
772
773
bool Curl_xfer_needs_flush(struct Curl_easy *data)
774
122k
{
775
122k
  return Curl_cfchain_needs_flush(data, &data->conn->chain);
776
122k
}
777
778
CURLcode Curl_xfer_flush(struct Curl_easy *data)
779
1.08k
{
780
1.08k
  return Curl_cfchain_flush(data, &data->conn->chain);
781
1.08k
}
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
52.7k
{
787
52.7k
  CURLcode result;
788
789
52.7k
  DEBUGASSERT(data);
790
52.7k
  DEBUGASSERT(data->conn);
791
792
52.7k
  result = Curl_conn_send(data, buf, blen, eos, pnwritten);
793
52.7k
  if(!result && *pnwritten)
794
49.0k
    data->info.request_size += *pnwritten;
795
796
52.7k
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
797
52.7k
               blen, eos, (int)result, *pnwritten));
798
52.7k
  return result;
799
52.7k
}
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
143k
{
822
143k
  DEBUGASSERT(data);
823
143k
  DEBUGASSERT(data->conn);
824
143k
  DEBUGASSERT(data->set.buffer_size > 0);
825
826
143k
  if(curlx_uitouz(data->set.buffer_size) < blen)
827
0
    blen = curlx_uitouz(data->set.buffer_size);
828
143k
  return Curl_conn_recv(data, buf, blen, pnrcvd);
829
143k
}
830
831
bool Curl_xfer_is_blocked(struct Curl_easy *data)
832
118k
{
833
118k
  bool want_send = CURL_REQ_WANT_SEND(data);
834
118k
  bool want_recv = CURL_REQ_WANT_RECV(data);
835
118k
  if(!want_send)
836
87.4k
    return want_recv && Curl_xfer_recv_is_paused(data);
837
31.4k
  else if(!want_recv)
838
3.01k
    return want_send && Curl_xfer_send_is_paused(data);
839
28.4k
  else
840
28.4k
    return Curl_xfer_recv_is_paused(data) && Curl_xfer_send_is_paused(data);
841
118k
}
842
843
bool Curl_xfer_send_is_paused(struct Curl_easy *data)
844
41.1k
{
845
41.1k
  return Curl_rlimit_is_blocked(&data->progress.ul.rlimit);
846
41.1k
}
847
848
bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
849
204k
{
850
204k
  return Curl_rlimit_is_blocked(&data->progress.dl.rlimit);
851
204k
}
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
92.5k
{
878
92.5k
#ifndef CURL_DISABLE_PROXY
879
92.5k
  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
92.5k
#endif
887
92.5k
  return (data->state.origin->scheme->flags & PROTOPT_SSL);
888
92.5k
}