Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/transfer.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifdef HAVE_NETINET_IN_H
27
#include <netinet/in.h>
28
#endif
29
#ifdef HAVE_NETDB_H
30
#include <netdb.h>
31
#endif
32
#ifdef HAVE_ARPA_INET_H
33
#include <arpa/inet.h>
34
#endif
35
#ifdef HAVE_NET_IF_H
36
#include <net/if.h>
37
#endif
38
#ifdef HAVE_SYS_IOCTL_H
39
#include <sys/ioctl.h>
40
#endif
41
#include <signal.h>
42
43
#ifdef HAVE_SYS_PARAM_H
44
#include <sys/param.h>
45
#endif
46
47
#ifdef HAVE_SYS_SELECT_H
48
#include <sys/select.h>
49
#elif defined(HAVE_UNISTD_H)
50
#include <unistd.h>
51
#endif
52
53
#ifndef HAVE_SOCKET
54
#error "We cannot compile without socket() support"
55
#endif
56
57
#include "urldata.h"
58
59
#include "cfilters.h"
60
#include "cw-out.h"
61
#include "transfer.h"
62
#include "sendf.h"
63
#include "curl_trc.h"
64
#include "progress.h"
65
#include "http.h"
66
#include "url.h"
67
#include "getinfo.h"
68
#include "multiif.h"
69
#include "connect.h"
70
#include "hsts.h"
71
#include "setopt.h"
72
#include "headers.h"
73
#include "bufref.h"
74
#include "rtsp.h"
75
76
#if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_SMTP) || \
77
  !defined(CURL_DISABLE_IMAP)
78
/*
79
 * checkheaders() checks the linked list of custom headers for a
80
 * particular header (prefix). Provide the prefix without colon!
81
 *
82
 * Returns a pointer to the first matching header or NULL if none matched.
83
 */
84
char *Curl_checkheaders(const struct Curl_easy *data,
85
                        const char *thisheader,
86
                        const size_t thislen)
87
0
{
88
0
  struct curl_slist *head;
89
0
  DEBUGASSERT(thislen);
90
0
  DEBUGASSERT(thisheader[thislen - 1] != ':');
91
92
0
  for(head = data->set.headers; head; head = head->next) {
93
0
    if(curl_strnequal(head->data, thisheader, thislen) &&
94
0
       Curl_headersep(head->data[thislen]))
95
0
      return head->data;
96
0
  }
97
98
0
  return NULL;
99
0
}
100
#endif
101
102
static int data_pending(struct Curl_easy *data, bool rcvd_eagain)
103
2.85k
{
104
2.85k
  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
2.85k
  return (!rcvd_eagain &&
109
0
          conn->scheme->protocol & (CURLPROTO_SCP | CURLPROTO_SFTP)) ||
110
2.85k
         Curl_conn_data_pending(data);
111
2.85k
}
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
3.57k
{
145
3.57k
  if(!data || !data->conn)
146
0
    return FALSE;
147
3.57k
  return CURL_CFCHAIN_IN_SHUTDOWN(&data->conn->chain);
148
3.57k
}
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
3.57k
{
163
3.57k
  CURLcode result;
164
165
3.57k
  DEBUGASSERT(blen > 0);
166
3.57k
  *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
3.57k
  if(!eos_reliable && !data->req.header && data->req.size != -1) {
170
0
    blen = curlx_sotouz_range(data->req.size - data->req.bytecount, 0, blen);
171
0
  }
172
3.57k
  else if(xfer_recv_shutdown_started(data)) {
173
    /* we already received everything. Do not try more. */
174
0
    blen = 0;
175
0
  }
176
177
3.57k
  if(blen) {
178
3.57k
    result = Curl_xfer_recv(data, buf, blen, pnread);
179
3.57k
    if(result)
180
2.85k
      return result;
181
3.57k
  }
182
183
720
  if(*pnread == 0)
184
0
    DEBUGF(infof(data, "sendrecv_dl: we are done"));
185
720
  return CURLE_OK;
186
3.57k
}
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
2.90k
{
196
2.90k
  struct connectdata *conn = data->conn;
197
2.90k
  CURLcode result = CURLE_OK;
198
2.90k
  char *buf, *xfer_buf;
199
2.90k
  size_t blen, xfer_blen;
200
2.90k
  int maxloops = 10;
201
2.90k
  bool safe_eos = FALSE;
202
2.90k
  bool rcvd_eagain = FALSE;
203
2.90k
  bool is_eos = FALSE, rate_limited = FALSE;
204
205
2.90k
  result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
206
2.90k
  if(result)
207
2
    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
3.57k
  do {
212
3.57k
    size_t bytestoread;
213
214
3.57k
    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
3.57k
      safe_eos = Curl_conn_has_safe_eos(conn) &&
221
0
                 (!data->state.use_range ||
222
0
                  (conn->scheme->protocol & PROTO_FAMILY_HTTP));
223
3.57k
    }
224
225
3.57k
    buf = xfer_buf;
226
3.57k
    bytestoread = xfer_blen;
227
228
3.57k
    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
3.57k
    rcvd_eagain = FALSE;
246
3.57k
    result = xfer_recv_resp(data, buf, bytestoread, safe_eos, &blen);
247
3.57k
    if(result) {
248
2.85k
      if(result != CURLE_AGAIN)
249
0
        goto out; /* real error */
250
2.85k
      rcvd_eagain = TRUE;
251
2.85k
      result = CURLE_OK;
252
2.85k
      if(data->req.download_done && data->req.no_body &&
253
0
         !data->req.resp_trailer) {
254
0
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
255
0
                     "not waiting for EOS"));
256
0
        blen = 0;
257
        /* continue as if we received the EOS */
258
0
      }
259
2.85k
      else
260
2.85k
        break; /* get out of loop */
261
2.85k
    }
262
263
    /* We only get a 0-length receive at the end of the response */
264
720
    is_eos = (blen == 0);
265
266
720
    if(!blen) {
267
0
      result = Curl_req_stop_send_recv(data);
268
0
      if(result)
269
0
        goto out;
270
0
      if(k->eos_written) /* already did write this to client, leave */
271
0
        break;
272
0
    }
273
274
720
    result = Curl_xfer_write_resp(data, buf, blen, is_eos);
275
720
    if(result || data->req.done)
276
0
      goto out;
277
278
    /* if we are done, we stop receiving. On multiplexed connections,
279
     * we should read the EOS. Which may arrive as meta data after
280
     * the bytes. Not taking it in might lead to RST of streams. */
281
720
    if((!safe_eos && data->req.download_done) || is_eos) {
282
0
      CURL_REQ_CLEAR_RECV(data);
283
0
    }
284
    /* if we stopped receiving, leave the loop */
285
720
    if(!CURL_REQ_WANT_RECV(data))
286
26
      break;
287
288
720
  } while(maxloops--);
289
290
2.90k
  if(!is_eos && !rate_limited && CURL_REQ_WANT_RECV(data) &&
291
2.88k
     (!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
29
    Curl_multi_mark_dirty(data);
295
29
    CURL_TRC_M(data, "sendrecv_dl() no EAGAIN/pending data, mark as dirty");
296
29
  }
297
298
2.90k
  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
2.90k
out:
308
2.90k
  Curl_multi_xfer_buf_release(data, xfer_buf);
309
2.90k
  if(result)
310
2
    DEBUGF(infof(data, "sendrecv_dl() -> %d", (int)result));
311
2.90k
  return result;
312
2.90k
}
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
0
{
319
  /* We should not get here when the sending is already done. */
320
0
  DEBUGASSERT(!Curl_req_done_sending(data));
321
322
0
  if(!Curl_req_done_sending(data))
323
0
    return Curl_req_send_more(data);
324
0
  return CURLE_OK;
325
0
}
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
2.92k
{
333
2.92k
  struct SingleRequest *k = &data->req;
334
2.92k
  const struct curltime *pnow = NULL;
335
2.92k
  CURLcode result = CURLE_OK;
336
337
2.92k
  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
2.92k
  if(CURL_REQ_WANT_RECV(data)) {
345
2.90k
    result = sendrecv_dl(data, k);
346
2.90k
    if(result || data->req.done)
347
2
      goto out;
348
2.90k
  }
349
350
  /* If we still have writing to do, we check if we have a writable socket. */
351
2.91k
  if(Curl_req_want_send(data)) {
352
0
    result = sendrecv_ul(data);
353
0
    if(result)
354
0
      goto out;
355
0
  }
356
357
2.91k
  pnow = Curl_pgrs_now(data);
358
2.91k
  if(CURL_REQ_WANT_IO(data)) {
359
2.88k
    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
2.88k
  }
377
38
  else {
378
    /*
379
     * The transfer has been performed. Make some general checks before
380
     * returning.
381
     */
382
38
    if(!data->req.no_body && (k->size != -1) &&
383
0
       (k->bytecount != k->size) && !k->newurl) {
384
0
      failf(data, "transfer closed with %" FMT_OFF_T
385
0
            " bytes remaining to read", k->size - k->bytecount);
386
0
      result = CURLE_PARTIAL_FILE;
387
0
      goto out;
388
0
    }
389
38
  }
390
391
  /* If there is nothing more to send/recv, the request is done */
392
2.91k
  if(!CURL_REQ_WANT_IO(data))
393
38
    data->req.done = TRUE;
394
395
2.91k
  result = Curl_pgrsCheckX(data, pnow);
396
397
2.92k
out:
398
2.92k
  if(result)
399
2
    DEBUGF(infof(data, "Curl_sendrecv() -> %d", (int)result));
400
2.92k
  return result;
401
2.91k
}
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
4.03k
{
407
4.03k
  data->state.fread_func = data->set.fread_func_set;
408
4.03k
  data->state.in = data->set.in_set;
409
4.03k
  data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
410
4.03k
}
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
4.10k
{
419
4.10k
  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
4.10k
  data->state.retrycount = 0;
428
429
4.10k
  if(!CURL_EASY_STR(data, STRING_SET_URL) && !data->set.uh) {
430
    /* we cannot do anything without URL */
431
7
    failf(data, "No URL set");
432
7
    return CURLE_URL_MALFORMAT;
433
7
  }
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
4.09k
  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
4.09k
  Curl_bufref_set(&data->state.url, CURL_EASY_STR(data, STRING_SET_URL),
453
4.09k
                  0, NULL);
454
455
4.09k
  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
4.09k
  data->state.prefer_ascii = data->set.prefer_ascii;
462
4.09k
#ifdef CURL_LIST_ONLY_PROTOCOL
463
4.09k
  data->state.list_only = data->set.list_only;
464
4.09k
#endif
465
4.09k
  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
4.09k
  Curl_peer_unlink(&data->state.initial_origin);
470
4.09k
  Curl_peer_unlink(&data->state.origin);
471
4.09k
  data->state.requests = 0;
472
4.09k
  data->state.still_connecting = FALSE;
473
4.09k
  data->state.followlocation = 0; /* reset the location-follow counter */
474
4.09k
  data->state.this_is_a_follow = FALSE; /* reset this */
475
4.09k
  data->state.http_ignorecustom = FALSE; /* use custom HTTP method */
476
4.09k
  data->state.errorbuf = FALSE; /* no error has occurred */
477
4.09k
#ifndef CURL_DISABLE_HTTP
478
4.09k
  Curl_http_neg_init(data, &data->state.http_neg);
479
4.09k
#endif
480
4.09k
  data->state.authproblem = FALSE;
481
4.09k
  data->state.authhost.want = data->set.httpauth;
482
4.09k
  data->state.authproxy.want = data->set.proxyauth;
483
4.09k
  curlx_safefree(data->info.wouldredirect);
484
4.09k
  curlx_safefree(data->info.effective_url);
485
4.09k
  Curl_data_priority_clear_state(data);
486
4.09k
  if(data->set.http_auto_referer)
487
0
    Curl_bufref_free(&data->state.referer);
488
4.09k
  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
4.09k
  else
492
4.09k
    Curl_bufref_free(&data->state.referer);
493
494
4.09k
  if(data->state.httpreq == HTTPREQ_PUT)
495
0
    data->state.infilesize = data->set.filesize;
496
4.09k
  else if((data->state.httpreq != HTTPREQ_GET) &&
497
37
          (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
4.09k
  else
503
4.09k
    data->state.infilesize = 0;
504
505
  /* If there is a list of cookie files to read, do it now! */
506
4.09k
  result = Curl_cookie_loadfiles(data,
507
4.09k
                                 data->set.cookiesession ?
508
4.09k
                                 COOKIE_NOSESSION : 0);
509
4.09k
  if(!result)
510
4.09k
    Curl_cookie_run(data); /* activate */
511
512
  /* If there is a list of host pairs to deal with */
513
4.09k
  if(!result && data->set.a.resolve && !data->state.resolve_loaded)
514
0
    result = Curl_loadhostpairs(data);
515
516
4.09k
#ifndef CURL_DISABLE_HSTS
517
4.09k
  if(!result)
518
    /* If there is a list of hsts files to read */
519
4.09k
    result = Curl_hsts_loadfiles(data, data->set.a.hsts_files);
520
4.09k
#endif
521
522
4.09k
  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
4.03k
    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
4.03k
    Curl_initinfo(data); /* reset session-specific information "variables" */
537
4.03k
    Curl_pgrsResetTransferSizes(data);
538
4.03k
    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
4.03k
    Curl_rlimit_start(&data->progress.dl.rlimit, Curl_pgrs_now(data), -1);
542
4.03k
    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
4.03k
    data->state.authhost.picked &= data->state.authhost.want;
548
4.03k
    data->state.authproxy.picked &= data->state.authproxy.want;
549
550
4.03k
#ifndef CURL_DISABLE_FTP
551
4.03k
    data->state.wildcardmatch = data->set.wildcard_enabled;
552
4.03k
#ifdef DEBUGBUILD
553
4.03k
  if(getenv("CURL_DBG_FTP_WILDCARD"))
554
0
    data->state.wildcardmatch = TRUE;
555
4.03k
#endif
556
4.03k
    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
4.03k
#endif
573
4.03k
    result = Curl_hsts_loadcb(data, data->hsts);
574
4.03k
  }
575
576
4.09k
  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
4.09k
  data->req.rewind_read = FALSE;
582
4.09k
  Curl_client_reset(data);
583
4.09k
  Curl_headers_cleanup(data);
584
4.09k
  return result;
585
4.09k
}
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
38
{
592
38
  struct connectdata *conn = data->conn;
593
38
  bool retry = FALSE;
594
38
  *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
38
  if(data->state.upload &&
600
0
     !(conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_RTSP)))
601
0
    return CURLE_OK;
602
603
38
  if(data->set.never_retry)
604
0
    return CURLE_OK;
605
606
38
  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
38
#endif
613
38
    )
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
38
  else if(data->req.refused_stream &&
623
0
          (data->req.bytecount + data->req.headerbytecount == 0)) {
624
    /* This was sent on a refused stream, safe to rerun. A refused stream
625
       error can only happen on multiplexed protoccols (HTTP/2+3).
626
       While this *should* imply that the server never processed any
627
       of the request data, we only act on this when we have
628
       not received any response headers or content bytes. */
629
0
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
630
0
    retry = TRUE;
631
0
  }
632
38
  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
38
  return CURLE_OK;
654
38
}
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
350
{
663
350
  struct SingleRequest *k = &data->req;
664
350
  struct connectdata *conn = data->conn;
665
666
350
  DEBUGASSERT(conn);
667
  /* if request wants to send, switching off the send direction is wrong */
668
350
  DEBUGASSERT(want_send || !Curl_req_want_send(data));
669
670
350
  conn->bits.want_send = want_send;
671
350
  conn->bits.want_recv = want_recv;
672
673
  /* without receiving, there should be not recv_size */
674
350
  DEBUGASSERT(want_recv || (recv_size == -1));
675
350
  k->size = recv_size;
676
350
  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
350
  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
350
  if(conn->scheme->run->write_resp_hd || !data->req.no_body) {
686
350
    if(want_recv)
687
350
      CURL_REQ_SET_RECV(data);
688
350
    if(want_send)
689
0
      CURL_REQ_SET_SEND(data);
690
350
  }
691
350
  CURL_TRC_M(data, "xfer_setup: want_recv=%d, want_send=%d",
692
350
             conn->bits.want_recv, conn->bits.want_send);
693
350
}
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
0
{
703
0
  xfer_setup(data, TRUE, TRUE, recv_size);
704
0
}
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
350
{
714
350
  xfer_setup(data, FALSE, TRUE, recv_size);
715
350
}
716
717
CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
718
                              const char *buf, size_t blen,
719
                              bool is_eos)
720
720
{
721
720
  CURLcode result = CURLE_OK;
722
723
720
  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
720
    result = data->conn->scheme->run->write_resp(data, buf, blen, is_eos);
727
720
  }
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
720
  if(!result && is_eos) {
740
    /* If we wrote the EOS, we are definitely done */
741
0
    data->req.eos_written = TRUE;
742
0
    data->req.download_done = TRUE;
743
0
  }
744
720
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
745
720
                 blen, is_eos, (int)result);
746
720
  return result;
747
720
}
748
749
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
750
0
{
751
0
  return Curl_cwriter_is_paused(data);
752
0
}
753
754
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
755
                                 const char *hd0, size_t hdlen, bool is_eos)
756
0
{
757
0
  if(data->conn->scheme->run->write_resp_hd) {
758
0
    DEBUGASSERT(!hd0[hdlen]); /* null-terminated */
759
    /* protocol handlers offering this function take full responsibility
760
     * for writing all received download data to the client. */
761
0
    return data->conn->scheme->run->write_resp_hd(data, hd0, hdlen, is_eos);
762
0
  }
763
  /* No special handling by protocol handler, write as response bytes */
764
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
765
0
}
766
767
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
768
3.79k
{
769
3.79k
  (void)premature;
770
3.79k
  return Curl_cw_out_done(data);
771
3.79k
}
772
773
bool Curl_xfer_needs_flush(struct Curl_easy *data)
774
3.64k
{
775
3.64k
  return Curl_cfchain_needs_flush(data, &data->conn->chain);
776
3.64k
}
777
778
CURLcode Curl_xfer_flush(struct Curl_easy *data)
779
0
{
780
0
  return Curl_cfchain_flush(data, &data->conn->chain);
781
0
}
782
783
CURLcode Curl_xfer_try_send(struct Curl_easy *data,
784
                            const void *buf, size_t blen, bool eos,
785
                            size_t *pnwritten)
786
0
{
787
0
  CURLcode result;
788
789
0
  DEBUGASSERT(data);
790
0
  DEBUGASSERT(data->conn);
791
792
0
  result = Curl_conn_send(data, buf, blen, eos, pnwritten);
793
0
  if(!result && *pnwritten)
794
0
    data->info.request_size += *pnwritten;
795
796
0
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
797
0
               blen, eos, (int)result, *pnwritten));
798
0
  return result;
799
0
}
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
3.57k
{
822
3.57k
  DEBUGASSERT(data);
823
3.57k
  DEBUGASSERT(data->conn);
824
3.57k
  DEBUGASSERT(data->set.buffer_size > 0);
825
826
3.57k
  if(curlx_uitouz(data->set.buffer_size) < blen)
827
0
    blen = curlx_uitouz(data->set.buffer_size);
828
3.57k
  return Curl_conn_recv(data, buf, blen, pnrcvd);
829
3.57k
}
830
831
bool Curl_xfer_is_blocked(struct Curl_easy *data)
832
2.92k
{
833
2.92k
  bool want_send = CURL_REQ_WANT_SEND(data);
834
2.92k
  bool want_recv = CURL_REQ_WANT_RECV(data);
835
2.92k
  if(!want_send)
836
2.92k
    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
2.92k
}
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
9.97k
{
850
9.97k
  return Curl_rlimit_is_blocked(&data->progress.dl.rlimit);
851
9.97k
}
852
853
CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable)
854
0
{
855
0
  CURLcode result = CURLE_OK;
856
0
  Curl_rlimit_block(&data->progress.ul.rlimit, enable, Curl_pgrs_now(data));
857
0
  if(!enable && Curl_creader_is_paused(data))
858
0
    result = Curl_creader_unpause(data);
859
0
  Curl_pgrsSendPause(data, enable);
860
0
  return result;
861
0
}
862
863
CURLcode Curl_xfer_pause_recv(struct Curl_easy *data, bool enable, bool flush)
864
0
{
865
0
  CURLcode result = CURLE_OK;
866
0
  Curl_rlimit_block(&data->progress.dl.rlimit, enable, Curl_pgrs_now(data));
867
0
  if(enable)
868
0
    data->req.writer.paused = TRUE;
869
0
  else if(Curl_cwriter_is_paused(data))
870
0
    result = Curl_cwriter_unpause(data, flush);
871
0
  Curl_conn_ev_data_pause(data, enable);
872
0
  Curl_pgrsRecvPause(data, enable);
873
0
  return result;
874
0
}
875
876
bool Curl_xfer_is_secure(struct Curl_easy *data)
877
0
{
878
0
#ifndef CURL_DISABLE_PROXY
879
0
  if(data->conn && data->conn->bits.origin_is_proxy) {
880
    /* talking to a forward proxy, not secure. we do not use
881
     * a forward proxy for https: and other 's' URLs. Let's just check that
882
     * this did not fail somewhere. */
883
0
    DEBUGASSERT(!(data->state.origin->scheme->flags & PROTOPT_SSL));
884
0
    return FALSE;
885
0
  }
886
0
#endif
887
0
  return (data->state.origin->scheme->flags & PROTOPT_SSL);
888
0
}