Coverage Report

Created: 2026-12-01 06:24

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
54.7k
{
88
54.7k
  struct curl_slist *head;
89
54.7k
  DEBUGASSERT(thislen);
90
54.7k
  DEBUGASSERT(thisheader[thislen - 1] != ':');
91
92
63.0k
  for(head = data->set.headers; head; head = head->next) {
93
8.91k
    if(curl_strnequal(head->data, thisheader, thislen) &&
94
619
       Curl_headersep(head->data[thislen]))
95
577
      return head->data;
96
8.91k
  }
97
98
54.1k
  return NULL;
99
54.7k
}
100
#endif
101
102
static int data_pending(struct Curl_easy *data, bool rcvd_eagain)
103
71.2k
{
104
71.2k
  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
71.2k
  return (!rcvd_eagain &&
109
0
          conn->scheme->protocol & (CURLPROTO_SCP | CURLPROTO_SFTP)) ||
110
71.2k
         Curl_conn_data_pending(data);
111
71.2k
}
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
99.4k
{
145
99.4k
  if(!data || !data->conn)
146
0
    return FALSE;
147
99.4k
  return CURL_CFCHAIN_IN_SHUTDOWN(&data->conn->chain);
148
99.4k
}
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
100k
{
163
100k
  CURLcode result;
164
165
100k
  DEBUGASSERT(blen > 0);
166
100k
  *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
100k
  if(!eos_reliable && !data->req.header && data->req.size != -1) {
170
809
    blen = curlx_sotouz_range(data->req.size - data->req.bytecount, 0, blen);
171
809
  }
172
99.4k
  else if(xfer_recv_shutdown_started(data)) {
173
    /* we already received everything. Do not try more. */
174
0
    blen = 0;
175
0
  }
176
177
100k
  if(blen) {
178
100k
    result = Curl_xfer_recv(data, buf, blen, pnread);
179
100k
    if(result)
180
79.8k
      return result;
181
100k
  }
182
183
20.3k
  if(*pnread == 0)
184
3.94k
    DEBUGF(infof(data, "sendrecv_dl: we are done"));
185
20.3k
  return CURLE_OK;
186
100k
}
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
84.7k
{
196
84.7k
  struct connectdata *conn = data->conn;
197
84.7k
  CURLcode result = CURLE_OK;
198
84.7k
  char *buf, *xfer_buf;
199
84.7k
  size_t blen, xfer_blen;
200
84.7k
  int maxloops = 10;
201
84.7k
  bool safe_eos = FALSE;
202
84.7k
  bool rcvd_eagain = FALSE;
203
84.7k
  bool is_eos = FALSE, rate_limited = FALSE;
204
205
84.7k
  result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
206
84.7k
  if(result)
207
14
    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
100k
  do {
212
100k
    size_t bytestoread;
213
214
100k
    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
100k
      safe_eos = Curl_conn_has_safe_eos(conn) &&
221
75.6k
                 (!data->state.use_range ||
222
556
                  (conn->scheme->protocol & PROTO_FAMILY_HTTP));
223
100k
    }
224
225
100k
    buf = xfer_buf;
226
100k
    bytestoread = xfer_blen;
227
228
100k
    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
100k
    rcvd_eagain = FALSE;
246
100k
    result = xfer_recv_resp(data, buf, bytestoread, safe_eos, &blen);
247
100k
    if(result) {
248
79.8k
      if(result != CURLE_AGAIN)
249
8.64k
        goto out; /* real error */
250
71.2k
      rcvd_eagain = TRUE;
251
71.2k
      result = CURLE_OK;
252
71.2k
      if(data->req.download_done && data->req.no_body &&
253
22
         !data->req.resp_trailer) {
254
22
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
255
22
                     "not waiting for EOS"));
256
22
        blen = 0;
257
        /* continue as if we received the EOS */
258
22
      }
259
71.2k
      else
260
71.2k
        break; /* get out of loop */
261
71.2k
    }
262
263
    /* We only get a 0-length receive at the end of the response */
264
20.3k
    is_eos = (blen == 0);
265
266
20.3k
    if(!blen) {
267
3.96k
      result = Curl_req_stop_send_recv(data);
268
3.96k
      if(result)
269
0
        goto out;
270
3.96k
      if(k->eos_written) /* already did write this to client, leave */
271
0
        break;
272
3.96k
    }
273
274
20.3k
    result = Curl_xfer_write_resp(data, buf, blen, is_eos);
275
20.3k
    if(result || data->req.done)
276
943
      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
19.4k
    if((!safe_eos && data->req.download_done) || is_eos) {
282
3.89k
      CURL_REQ_CLEAR_RECV(data);
283
3.89k
    }
284
    /* if we stopped receiving, leave the loop */
285
19.4k
    if(!CURL_REQ_WANT_RECV(data))
286
3.89k
      break;
287
288
19.4k
  } while(maxloops--);
289
290
75.1k
  if(!is_eos && !rate_limited && CURL_REQ_WANT_RECV(data) &&
291
71.2k
     (!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
31
    Curl_multi_mark_dirty(data);
295
31
    CURL_TRC_M(data, "sendrecv_dl() no EAGAIN/pending data, mark as dirty");
296
31
  }
297
298
75.1k
  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
84.7k
out:
308
84.7k
  Curl_multi_xfer_buf_release(data, xfer_buf);
309
84.7k
  if(result)
310
9.60k
    DEBUGF(infof(data, "sendrecv_dl() -> %d", (int)result));
311
84.7k
  return result;
312
75.1k
}
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
8
{
319
  /* We should not get here when the sending is already done. */
320
8
  DEBUGASSERT(!Curl_req_done_sending(data));
321
322
8
  if(!Curl_req_done_sending(data))
323
8
    return Curl_req_send_more(data);
324
0
  return CURLE_OK;
325
8
}
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
84.7k
{
333
84.7k
  struct SingleRequest *k = &data->req;
334
84.7k
  const struct curltime *pnow = NULL;
335
84.7k
  CURLcode result = CURLE_OK;
336
337
84.7k
  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
84.7k
  if(CURL_REQ_WANT_RECV(data)) {
345
84.7k
    result = sendrecv_dl(data, k);
346
84.7k
    if(result || data->req.done)
347
9.60k
      goto out;
348
84.7k
  }
349
350
  /* If we still have writing to do, we check if we have a writable socket. */
351
75.1k
  if(Curl_req_want_send(data)) {
352
8
    result = sendrecv_ul(data);
353
8
    if(result)
354
0
      goto out;
355
8
  }
356
357
75.1k
  pnow = Curl_pgrs_now(data);
358
75.1k
  if(CURL_REQ_WANT_IO(data)) {
359
71.2k
    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
71.2k
  }
377
3.89k
  else {
378
    /*
379
     * The transfer has been performed. Make some general checks before
380
     * returning.
381
     */
382
3.89k
    if(!data->req.no_body && (k->size != -1) &&
383
675
       (k->bytecount != k->size) && !k->newurl) {
384
90
      failf(data, "transfer closed with %" FMT_OFF_T
385
90
            " bytes remaining to read", k->size - k->bytecount);
386
90
      result = CURLE_PARTIAL_FILE;
387
90
      goto out;
388
90
    }
389
3.89k
  }
390
391
  /* If there is nothing more to send/recv, the request is done */
392
75.0k
  if(!CURL_REQ_WANT_IO(data))
393
3.80k
    data->req.done = TRUE;
394
395
75.0k
  result = Curl_pgrsCheckX(data, pnow);
396
397
84.7k
out:
398
84.7k
  if(result)
399
9.69k
    DEBUGF(infof(data, "Curl_sendrecv() -> %d", (int)result));
400
84.7k
  return result;
401
75.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
14.0k
{
407
14.0k
  data->state.fread_func = data->set.fread_func_set;
408
14.0k
  data->state.in = data->set.in_set;
409
14.0k
  data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
410
14.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
13.8k
{
419
13.8k
  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
13.8k
  data->state.retrycount = 0;
428
429
13.8k
  if(!CURL_EASY_STR(data, STRING_SET_URL) && !data->set.uh) {
430
    /* we cannot do anything without URL */
431
6
    failf(data, "No URL set");
432
6
    return CURLE_URL_MALFORMAT;
433
6
  }
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
13.7k
  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
13.7k
  Curl_bufref_set(&data->state.url, CURL_EASY_STR(data, STRING_SET_URL),
453
13.7k
                  0, NULL);
454
455
13.7k
  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
13.7k
  data->state.prefer_ascii = data->set.prefer_ascii;
462
13.7k
#ifdef CURL_LIST_ONLY_PROTOCOL
463
13.7k
  data->state.list_only = data->set.list_only;
464
13.7k
#endif
465
13.7k
  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
13.7k
  Curl_peer_unlink(&data->state.initial_origin);
470
13.7k
  Curl_peer_unlink(&data->state.origin);
471
13.7k
  data->state.requests = 0;
472
13.7k
  data->state.still_connecting = FALSE;
473
13.7k
  data->state.followlocation = 0; /* reset the location-follow counter */
474
13.7k
  data->state.this_is_a_follow = FALSE; /* reset this */
475
13.7k
  data->state.http_ignorecustom = FALSE; /* use custom HTTP method */
476
13.7k
  data->state.errorbuf = FALSE; /* no error has occurred */
477
13.7k
#ifndef CURL_DISABLE_HTTP
478
13.7k
  Curl_http_neg_init(data, &data->state.http_neg);
479
13.7k
#endif
480
13.7k
  data->state.authproblem = FALSE;
481
13.7k
  data->state.authhost.want = data->set.httpauth;
482
13.7k
  data->state.authproxy.want = data->set.proxyauth;
483
13.7k
  curlx_safefree(data->info.wouldredirect);
484
13.7k
  curlx_safefree(data->info.effective_url);
485
13.7k
  Curl_data_priority_clear_state(data);
486
13.7k
  if(data->set.http_auto_referer)
487
0
    Curl_bufref_free(&data->state.referer);
488
13.7k
  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
13.7k
  else
492
13.7k
    Curl_bufref_free(&data->state.referer);
493
494
13.7k
  if(data->state.httpreq == HTTPREQ_PUT)
495
0
    data->state.infilesize = data->set.filesize;
496
13.7k
  else if((data->state.httpreq != HTTPREQ_GET) &&
497
102
          (data->state.httpreq != HTTPREQ_HEAD)) {
498
0
    data->state.infilesize = data->set.postfieldsize;
499
0
    if(data->set.postfields && (data->state.infilesize == -1))
500
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
501
0
  }
502
13.7k
  else
503
13.7k
    data->state.infilesize = 0;
504
505
  /* If there is a list of cookie files to read, do it now! */
506
13.7k
  result = Curl_cookie_loadfiles(data,
507
13.7k
                                 data->set.cookiesession ?
508
13.7k
                                 COOKIE_NOSESSION : 0);
509
13.7k
  if(!result)
510
13.7k
    Curl_cookie_run(data); /* activate */
511
512
  /* If there is a list of host pairs to deal with */
513
13.7k
  if(!result && data->set.a.resolve && !data->state.resolve_loaded)
514
0
    result = Curl_loadhostpairs(data);
515
516
13.7k
#ifndef CURL_DISABLE_HSTS
517
13.7k
  if(!result)
518
    /* If there is a list of hsts files to read */
519
13.7k
    result = Curl_hsts_loadfiles(data, data->set.a.hsts_files);
520
13.7k
#endif
521
522
13.7k
  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
13.7k
    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
13.7k
    Curl_initinfo(data); /* reset session-specific information "variables" */
537
13.7k
    Curl_pgrsResetTransferSizes(data);
538
13.7k
    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
13.7k
    Curl_rlimit_start(&data->progress.dl.rlimit, Curl_pgrs_now(data), -1);
542
13.7k
    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
13.7k
    data->state.authhost.picked &= data->state.authhost.want;
548
13.7k
    data->state.authproxy.picked &= data->state.authproxy.want;
549
550
13.7k
#ifndef CURL_DISABLE_FTP
551
13.7k
    data->state.wildcardmatch = data->set.wildcard_enabled;
552
13.7k
#ifdef DEBUGBUILD
553
13.7k
  if(getenv("CURL_DBG_FTP_WILDCARD"))
554
0
    data->state.wildcardmatch = TRUE;
555
13.7k
#endif
556
13.7k
    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
13.7k
#endif
573
13.7k
    result = Curl_hsts_loadcb(data, data->hsts);
574
13.7k
  }
575
576
13.7k
  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
13.7k
  data->req.rewind_read = FALSE;
582
13.7k
  Curl_client_reset(data);
583
13.7k
  Curl_headers_cleanup(data);
584
13.7k
  return result;
585
13.7k
}
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
4.20k
{
592
4.20k
  struct connectdata *conn = data->conn;
593
4.20k
  bool retry = FALSE;
594
4.20k
  *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
4.20k
  if(data->state.upload &&
600
0
     !(conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_RTSP)))
601
0
    return CURLE_OK;
602
603
4.20k
  if(data->set.never_retry)
604
0
    return CURLE_OK;
605
606
4.20k
  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
4.20k
#endif
613
4.20k
    )
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
4.20k
  else if(data->req.refused_stream &&
623
46
          (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
4.20k
  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
4.20k
  return CURLE_OK;
654
4.20k
}
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
13.4k
{
663
13.4k
  struct SingleRequest *k = &data->req;
664
13.4k
  struct connectdata *conn = data->conn;
665
666
13.4k
  DEBUGASSERT(conn);
667
  /* if request wants to send, switching off the send direction is wrong */
668
13.4k
  DEBUGASSERT(want_send || !Curl_req_want_send(data));
669
670
13.4k
  conn->bits.want_send = want_send;
671
13.4k
  conn->bits.want_recv = want_recv;
672
673
  /* without receiving, there should be not recv_size */
674
13.4k
  DEBUGASSERT(want_recv || (recv_size == -1));
675
13.4k
  k->size = recv_size;
676
13.4k
  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
13.4k
  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
13.4k
  if(conn->scheme->run->write_resp_hd || !data->req.no_body) {
686
13.4k
    if(want_recv)
687
13.4k
      CURL_REQ_SET_RECV(data);
688
13.4k
    if(want_send)
689
13.4k
      CURL_REQ_SET_SEND(data);
690
13.4k
  }
691
13.4k
  CURL_TRC_M(data, "xfer_setup: want_recv=%d, want_send=%d",
692
13.4k
             conn->bits.want_recv, conn->bits.want_send);
693
13.4k
}
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
13.4k
{
703
13.4k
  xfer_setup(data, TRUE, TRUE, recv_size);
704
13.4k
}
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
20.5k
{
721
20.5k
  CURLcode result = CURLE_OK;
722
723
20.5k
  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
20.5k
    result = data->conn->scheme->run->write_resp(data, buf, blen, is_eos);
727
20.5k
  }
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
20.5k
  if(!result && is_eos) {
740
    /* If we wrote the EOS, we are definitely done */
741
3.78k
    data->req.eos_written = TRUE;
742
3.78k
    data->req.download_done = TRUE;
743
3.78k
  }
744
20.5k
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
745
20.5k
                 blen, is_eos, (int)result);
746
20.5k
  return result;
747
20.5k
}
748
749
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
750
210
{
751
210
  return Curl_cwriter_is_paused(data);
752
210
}
753
754
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
755
                                 const char *hd0, size_t hdlen, bool is_eos)
756
190k
{
757
190k
  if(data->conn->scheme->run->write_resp_hd) {
758
190k
    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
190k
    return data->conn->scheme->run->write_resp_hd(data, hd0, hdlen, is_eos);
762
190k
  }
763
  /* No special handling by protocol handler, write as response bytes */
764
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
765
190k
}
766
767
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
768
13.8k
{
769
13.8k
  (void)premature;
770
13.8k
  return Curl_cw_out_done(data);
771
13.8k
}
772
773
bool Curl_xfer_needs_flush(struct Curl_easy *data)
774
76.9k
{
775
76.9k
  return Curl_cfchain_needs_flush(data, &data->conn->chain);
776
76.9k
}
777
778
CURLcode Curl_xfer_flush(struct Curl_easy *data)
779
8
{
780
8
  return Curl_cfchain_flush(data, &data->conn->chain);
781
8
}
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
13.4k
{
787
13.4k
  CURLcode result;
788
789
13.4k
  DEBUGASSERT(data);
790
13.4k
  DEBUGASSERT(data->conn);
791
792
13.4k
  result = Curl_conn_send(data, buf, blen, eos, pnwritten);
793
13.4k
  if(!result && *pnwritten)
794
13.4k
    data->info.request_size += *pnwritten;
795
796
13.4k
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
797
13.4k
               blen, eos, (int)result, *pnwritten));
798
13.4k
  return result;
799
13.4k
}
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
100k
{
822
100k
  DEBUGASSERT(data);
823
100k
  DEBUGASSERT(data->conn);
824
100k
  DEBUGASSERT(data->set.buffer_size > 0);
825
826
100k
  if(curlx_uitouz(data->set.buffer_size) < blen)
827
0
    blen = curlx_uitouz(data->set.buffer_size);
828
100k
  return Curl_conn_recv(data, buf, blen, pnrcvd);
829
100k
}
830
831
bool Curl_xfer_is_blocked(struct Curl_easy *data)
832
84.7k
{
833
84.7k
  bool want_send = CURL_REQ_WANT_SEND(data);
834
84.7k
  bool want_recv = CURL_REQ_WANT_RECV(data);
835
84.7k
  if(!want_send)
836
84.7k
    return want_recv && Curl_xfer_recv_is_paused(data);
837
0
  else if(!want_recv)
838
0
    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
84.7k
}
842
843
bool Curl_xfer_send_is_paused(struct Curl_easy *data)
844
0
{
845
0
  return Curl_rlimit_is_blocked(&data->progress.ul.rlimit);
846
0
}
847
848
bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
849
98.5k
{
850
98.5k
  return Curl_rlimit_is_blocked(&data->progress.dl.rlimit);
851
98.5k
}
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
133k
{
878
133k
#ifndef CURL_DISABLE_PROXY
879
133k
  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
133k
#endif
887
133k
  return (data->state.origin->scheme->flags & PROTOPT_SSL);
888
133k
}