Coverage Report

Created: 2025-11-11 06:17

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
25
#include "curl_setup.h"
26
27
#ifdef HAVE_NETINET_IN_H
28
#include <netinet/in.h>
29
#endif
30
#ifdef HAVE_NETDB_H
31
#include <netdb.h>
32
#endif
33
#ifdef HAVE_ARPA_INET_H
34
#include <arpa/inet.h>
35
#endif
36
#ifdef HAVE_NET_IF_H
37
#include <net/if.h>
38
#endif
39
#ifdef HAVE_SYS_IOCTL_H
40
#include <sys/ioctl.h>
41
#endif
42
#ifndef UNDER_CE
43
#include <signal.h>
44
#endif
45
46
#ifdef HAVE_SYS_PARAM_H
47
#include <sys/param.h>
48
#endif
49
50
#ifdef HAVE_SYS_SELECT_H
51
#include <sys/select.h>
52
#elif defined(HAVE_UNISTD_H)
53
#include <unistd.h>
54
#endif
55
56
#ifndef HAVE_SOCKET
57
#error "We cannot compile without socket() support!"
58
#endif
59
60
#include "urldata.h"
61
#include <curl/curl.h>
62
#include "netrc.h"
63
64
#include "content_encoding.h"
65
#include "hostip.h"
66
#include "cfilters.h"
67
#include "cw-out.h"
68
#include "transfer.h"
69
#include "sendf.h"
70
#include "speedcheck.h"
71
#include "progress.h"
72
#include "http.h"
73
#include "url.h"
74
#include "getinfo.h"
75
#include "vtls/vtls.h"
76
#include "vquic/vquic.h"
77
#include "select.h"
78
#include "multiif.h"
79
#include "connect.h"
80
#include "http2.h"
81
#include "mime.h"
82
#include "hsts.h"
83
#include "setopt.h"
84
#include "headers.h"
85
86
/* The last 2 #include files should be in this order */
87
#include "curl_memory.h"
88
#include "memdebug.h"
89
90
#if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_SMTP) || \
91
    !defined(CURL_DISABLE_IMAP)
92
/*
93
 * checkheaders() checks the linked list of custom headers for a
94
 * particular header (prefix). Provide the prefix without colon!
95
 *
96
 * Returns a pointer to the first matching header or NULL if none matched.
97
 */
98
char *Curl_checkheaders(const struct Curl_easy *data,
99
                        const char *thisheader,
100
                        const size_t thislen)
101
0
{
102
0
  struct curl_slist *head;
103
0
  DEBUGASSERT(thislen);
104
0
  DEBUGASSERT(thisheader[thislen-1] != ':');
105
106
0
  for(head = data->set.headers; head; head = head->next) {
107
0
    if(curl_strnequal(head->data, thisheader, thislen) &&
108
0
       Curl_headersep(head->data[thislen]) )
109
0
      return head->data;
110
0
  }
111
112
0
  return NULL;
113
0
}
114
#endif
115
116
static int data_pending(struct Curl_easy *data, bool rcvd_eagain)
117
0
{
118
0
  struct connectdata *conn = data->conn;
119
120
0
  if(conn->handler->protocol&PROTO_FAMILY_FTP)
121
0
    return Curl_conn_data_pending(data, SECONDARYSOCKET);
122
123
  /* in the case of libssh2, we can never be really sure that we have emptied
124
     its internal buffers so we MUST always try until we get EAGAIN back */
125
0
  return (!rcvd_eagain &&
126
0
          conn->handler->protocol&(CURLPROTO_SCP|CURLPROTO_SFTP)) ||
127
0
         Curl_conn_data_pending(data, FIRSTSOCKET);
128
0
}
129
130
/*
131
 * Check to see if CURLOPT_TIMECONDITION was met by comparing the time of the
132
 * remote document with the time provided by CURLOPT_TIMEVAL
133
 */
134
bool Curl_meets_timecondition(struct Curl_easy *data, time_t timeofdoc)
135
0
{
136
0
  if((timeofdoc == 0) || (data->set.timevalue == 0))
137
0
    return TRUE;
138
139
0
  switch(data->set.timecondition) {
140
0
  case CURL_TIMECOND_IFMODSINCE:
141
0
  default:
142
0
    if(timeofdoc <= data->set.timevalue) {
143
0
      infof(data,
144
0
            "The requested document is not new enough");
145
0
      data->info.timecond = TRUE;
146
0
      return FALSE;
147
0
    }
148
0
    break;
149
0
  case CURL_TIMECOND_IFUNMODSINCE:
150
0
    if(timeofdoc >= data->set.timevalue) {
151
0
      infof(data,
152
0
            "The requested document is not old enough");
153
0
      data->info.timecond = TRUE;
154
0
      return FALSE;
155
0
    }
156
0
    break;
157
0
  }
158
159
0
  return TRUE;
160
0
}
161
162
static CURLcode xfer_recv_shutdown(struct Curl_easy *data, bool *done)
163
0
{
164
0
  if(!data || !data->conn)
165
0
    return CURLE_FAILED_INIT;
166
0
  return Curl_conn_shutdown(data, data->conn->recv_idx, done);
167
0
}
168
169
static bool xfer_recv_shutdown_started(struct Curl_easy *data)
170
0
{
171
0
  if(!data || !data->conn)
172
0
    return FALSE;
173
0
  return Curl_shutdown_started(data, data->conn->recv_idx);
174
0
}
175
176
CURLcode Curl_xfer_send_shutdown(struct Curl_easy *data, bool *done)
177
0
{
178
0
  if(!data || !data->conn)
179
0
    return CURLE_FAILED_INIT;
180
0
  return Curl_conn_shutdown(data, data->conn->send_idx, done);
181
0
}
182
183
/**
184
 * Receive raw response data for the transfer.
185
 * @param data         the transfer
186
 * @param buf          buffer to keep response data received
187
 * @param blen         length of `buf`
188
 * @param eos_reliable if EOS detection in underlying connection is reliable
189
 * @param err error    code in case of -1 return
190
 * @return number of bytes read or -1 for error
191
 */
192
static ssize_t xfer_recv_resp(struct Curl_easy *data,
193
                              char *buf, size_t blen,
194
                              bool eos_reliable,
195
                              CURLcode *err)
196
0
{
197
0
  size_t nread;
198
199
0
  DEBUGASSERT(blen > 0);
200
  /* If we are reading BODY data and the connection does NOT handle EOF
201
   * and we know the size of the BODY data, limit the read amount */
202
0
  if(!eos_reliable && !data->req.header && data->req.size != -1) {
203
0
    curl_off_t totalleft = data->req.size - data->req.bytecount;
204
0
    if(totalleft <= 0)
205
0
      blen = 0;
206
0
    else if(totalleft < (curl_off_t)blen)
207
0
      blen = (size_t)totalleft;
208
0
  }
209
0
  else if(xfer_recv_shutdown_started(data)) {
210
    /* we already received everything. Do not try more. */
211
0
    blen = 0;
212
0
  }
213
214
0
  if(!blen) {
215
    /* want nothing more */
216
0
    *err = CURLE_OK;
217
0
    nread = 0;
218
0
  }
219
0
  else {
220
0
    *err = Curl_xfer_recv(data, buf, blen, &nread);
221
0
  }
222
223
0
  if(*err)
224
0
    return -1;
225
0
  if(nread == 0) {
226
0
    if(data->req.shutdown) {
227
0
      bool done;
228
0
      *err = xfer_recv_shutdown(data, &done);
229
0
      if(*err)
230
0
        return -1;
231
0
      if(!done) {
232
0
        *err = CURLE_AGAIN;
233
0
        return -1;
234
0
      }
235
0
    }
236
0
    DEBUGF(infof(data, "sendrecv_dl: we are done"));
237
0
  }
238
0
  return (ssize_t)nread;
239
0
}
240
241
/*
242
 * Go ahead and do a read if we have a readable socket or if
243
 * the stream was rewound (in which case we have data in a
244
 * buffer)
245
 */
246
static CURLcode sendrecv_dl(struct Curl_easy *data,
247
                            struct SingleRequest *k)
248
0
{
249
0
  struct connectdata *conn = data->conn;
250
0
  CURLcode result = CURLE_OK;
251
0
  char *buf, *xfer_buf;
252
0
  size_t blen, xfer_blen;
253
0
  int maxloops = 10;
254
0
  curl_off_t total_received = 0;
255
0
  bool is_multiplex = FALSE;
256
0
  bool rcvd_eagain = FALSE;
257
0
  bool is_eos = FALSE;
258
259
0
  result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
260
0
  if(result)
261
0
    goto out;
262
263
  /* This is where we loop until we have read everything there is to
264
     read or we get a CURLE_AGAIN */
265
0
  do {
266
0
    size_t bytestoread;
267
0
    ssize_t nread;
268
269
0
    if(!is_multiplex) {
270
      /* Multiplexed connection have inherent handling of EOF and we do not
271
       * have to carefully restrict the amount we try to read.
272
       * Multiplexed changes only in one direction. */
273
0
      is_multiplex = Curl_conn_is_multiplex(conn, FIRSTSOCKET);
274
0
    }
275
276
0
    buf = xfer_buf;
277
0
    bytestoread = xfer_blen;
278
279
0
    if(bytestoread && data->set.max_recv_speed > 0) {
280
      /* In case of speed limit on receiving: if this loop already got
281
       * a quarter of the quota, break out. We want to stutter a bit
282
       * to keep in the limit, but too small receives will just cost
283
       * cpu unnecessarily. */
284
0
      if(total_received && (total_received >= (data->set.max_recv_speed / 4)))
285
0
        break;
286
0
      if(data->set.max_recv_speed < (curl_off_t)bytestoread)
287
0
        bytestoread = (size_t)data->set.max_recv_speed;
288
0
    }
289
290
0
    rcvd_eagain = FALSE;
291
0
    nread = xfer_recv_resp(data, buf, bytestoread, is_multiplex, &result);
292
0
    if(nread < 0) {
293
0
      if(CURLE_AGAIN != result)
294
0
        goto out; /* real error */
295
0
      rcvd_eagain = TRUE;
296
0
      result = CURLE_OK;
297
0
      if(data->req.download_done && data->req.no_body &&
298
0
         !data->req.resp_trailer) {
299
0
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
300
0
               "not waiting for EOS"));
301
0
        nread = 0;
302
        /* continue as if we received the EOS */
303
0
      }
304
0
      else
305
0
        break; /* get out of loop */
306
0
    }
307
308
    /* We only get a 0-length receive at the end of the response */
309
0
    blen = (size_t)nread;
310
0
    is_eos = (blen == 0);
311
312
0
    if(!blen && (conn->recv[FIRSTSOCKET] == Curl_cf_recv)) {
313
      /* if we receive 0 or less here and the protocol handler did not
314
         replace the connection's `recv` callback, either the data transfer
315
         is done or the server closed the connection and
316
         we bail out from this!
317
         With a `recv` replacement, we assume the protocol handler knows
318
         what it is doing and a 0-length receive is fine. For example,
319
         SFTP downloads of an empty file would show this. See #19165. */
320
0
      if(is_multiplex)
321
0
        DEBUGF(infof(data, "nread == 0, stream closed, bailing"));
322
0
      else
323
0
        DEBUGF(infof(data, "nread <= 0, server closed connection, bailing"));
324
0
      result = Curl_req_stop_send_recv(data);
325
0
      if(result)
326
0
        goto out;
327
0
      if(k->eos_written) /* already did write this to client, leave */
328
0
        break;
329
0
    }
330
0
    total_received += blen;
331
332
0
    result = Curl_xfer_write_resp(data, buf, blen, is_eos);
333
0
    if(result || data->req.done)
334
0
      goto out;
335
336
    /* if we are done, we stop receiving. On multiplexed connections,
337
     * we should read the EOS. Which may arrive as meta data after
338
     * the bytes. Not taking it in might lead to RST of streams. */
339
0
    if((!is_multiplex && data->req.download_done) || is_eos) {
340
0
      data->req.keepon &= ~KEEP_RECV;
341
0
    }
342
    /* if we are PAUSEd or stopped receiving, leave the loop */
343
0
    if((k->keepon & KEEP_RECV_PAUSE) || !(k->keepon & KEEP_RECV))
344
0
      break;
345
346
0
  } while(maxloops--);
347
348
0
  if(!is_eos && !Curl_xfer_is_blocked(data) &&
349
0
     (!rcvd_eagain || data_pending(data, rcvd_eagain))) {
350
    /* Did not read until EAGAIN/EOS or there is still data pending
351
     * in buffers. Mark as read-again via simulated SELECT results. */
352
0
    Curl_multi_mark_dirty(data);
353
0
    CURL_TRC_M(data, "sendrecv_dl() no EAGAIN/pending data, mark as dirty");
354
0
  }
355
356
0
  if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) &&
357
0
     (conn->bits.close || is_multiplex)) {
358
    /* When we have read the entire thing and the close bit is set, the server
359
       may now close the connection. If there is now any kind of sending going
360
       on from our side, we need to stop that immediately. */
361
0
    infof(data, "we are done reading and this is set to close, stop send");
362
0
    Curl_req_abort_sending(data);
363
0
  }
364
365
0
out:
366
0
  Curl_multi_xfer_buf_release(data, xfer_buf);
367
0
  if(result)
368
0
    DEBUGF(infof(data, "sendrecv_dl() -> %d", result));
369
0
  return result;
370
0
}
371
372
/*
373
 * Send data to upload to the server, when the socket is writable.
374
 */
375
static CURLcode sendrecv_ul(struct Curl_easy *data)
376
0
{
377
  /* We should not get here when the sending is already done. It
378
   * probably means that someone set `data-req.keepon |= KEEP_SEND`
379
   * when it should not. */
380
0
  DEBUGASSERT(!Curl_req_done_sending(data));
381
382
0
  if(!Curl_req_done_sending(data))
383
0
    return Curl_req_send_more(data);
384
0
  return CURLE_OK;
385
0
}
386
387
/*
388
 * Curl_sendrecv() is the low-level function to be called when data is to
389
 * be read and written to/from the connection.
390
 */
391
CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
392
0
{
393
0
  struct SingleRequest *k = &data->req;
394
0
  CURLcode result = CURLE_OK;
395
396
0
  DEBUGASSERT(nowp);
397
0
  if(Curl_xfer_is_blocked(data)) {
398
0
    result = CURLE_OK;
399
0
    goto out;
400
0
  }
401
402
  /* We go ahead and do a read if we have a readable socket or if the stream
403
     was rewound (in which case we have data in a buffer) */
404
0
  if(k->keepon & KEEP_RECV) {
405
0
    result = sendrecv_dl(data, k);
406
0
    if(result || data->req.done)
407
0
      goto out;
408
0
  }
409
410
  /* If we still have writing to do, we check if we have a writable socket. */
411
0
  if(Curl_req_want_send(data) || (data->req.keepon & KEEP_SEND_TIMED)) {
412
0
    result = sendrecv_ul(data);
413
0
    if(result)
414
0
      goto out;
415
0
  }
416
417
0
  if(Curl_pgrsUpdate(data))
418
0
    result = CURLE_ABORTED_BY_CALLBACK;
419
0
  else
420
0
    result = Curl_speedcheck(data, *nowp);
421
0
  if(result)
422
0
    goto out;
423
424
0
  if(k->keepon) {
425
0
    if(0 > Curl_timeleft(data, nowp, FALSE)) {
426
0
      if(k->size != -1) {
427
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
428
0
              " milliseconds with %" FMT_OFF_T " out of %"
429
0
              FMT_OFF_T " bytes received",
430
0
              curlx_timediff(*nowp, data->progress.t_startsingle),
431
0
              k->bytecount, k->size);
432
0
      }
433
0
      else {
434
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
435
0
              " milliseconds with %" FMT_OFF_T " bytes received",
436
0
              curlx_timediff(*nowp, data->progress.t_startsingle),
437
0
              k->bytecount);
438
0
      }
439
0
      result = CURLE_OPERATION_TIMEDOUT;
440
0
      goto out;
441
0
    }
442
0
  }
443
0
  else {
444
    /*
445
     * The transfer has been performed. Just make some general checks before
446
     * returning.
447
     */
448
0
    if(!(data->req.no_body) && (k->size != -1) &&
449
0
       (k->bytecount != k->size) && !k->newurl) {
450
0
      failf(data, "transfer closed with %" FMT_OFF_T
451
0
            " bytes remaining to read", k->size - k->bytecount);
452
0
      result = CURLE_PARTIAL_FILE;
453
0
      goto out;
454
0
    }
455
0
    if(Curl_pgrsUpdate(data)) {
456
0
      result = CURLE_ABORTED_BY_CALLBACK;
457
0
      goto out;
458
0
    }
459
0
  }
460
461
  /* If there is nothing more to send/recv, the request is done */
462
0
  if((k->keepon & (KEEP_RECVBITS|KEEP_SENDBITS)) == 0)
463
0
    data->req.done = TRUE;
464
465
0
out:
466
0
  if(result)
467
0
    DEBUGF(infof(data, "Curl_sendrecv() -> %d", result));
468
0
  return result;
469
0
}
470
471
/* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
472
   which means this gets called once for each subsequent redirect etc */
473
void Curl_init_CONNECT(struct Curl_easy *data)
474
0
{
475
0
  data->state.fread_func = data->set.fread_func_set;
476
0
  data->state.in = data->set.in_set;
477
0
  data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
478
0
}
479
480
/*
481
 * Curl_pretransfer() is called immediately before a transfer starts, and only
482
 * once for one transfer no matter if it has redirects or do multi-pass
483
 * authentication etc.
484
 */
485
CURLcode Curl_pretransfer(struct Curl_easy *data)
486
0
{
487
0
  CURLcode result = CURLE_OK;
488
489
  /* Reset the retry count at the start of each request.
490
   * If the retry count is not reset, when the connection drops,
491
   * it will not enter the retry mechanism on CONN_MAX_RETRIES + 1 attempts
492
   * and will immediately throw
493
   * "Connection died, tried CONN_MAX_RETRIES times before giving up".
494
   * By resetting it here, we ensure each new request starts fresh. */
495
0
  data->state.retrycount = 0;
496
497
0
  if(!data->set.str[STRING_SET_URL] && !data->set.uh) {
498
    /* we cannot do anything without URL */
499
0
    failf(data, "No URL set");
500
0
    return CURLE_URL_MALFORMAT;
501
0
  }
502
503
  /* CURLOPT_CURLU overrides CURLOPT_URL and the contents of the CURLU handle
504
     is allowed to be changed by the user between transfers */
505
0
  if(data->set.uh) {
506
0
    CURLUcode uc;
507
0
    free(data->set.str[STRING_SET_URL]);
508
0
    uc = curl_url_get(data->set.uh,
509
0
                      CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
510
0
    if(uc) {
511
0
      failf(data, "No URL set");
512
0
      return CURLE_URL_MALFORMAT;
513
0
    }
514
0
  }
515
516
  /* since the URL may have been redirected in a previous use of this handle */
517
0
  if(data->state.url_alloc) {
518
0
    Curl_safefree(data->state.url);
519
0
    data->state.url_alloc = FALSE;
520
0
  }
521
522
0
  data->state.url = data->set.str[STRING_SET_URL];
523
524
0
  if(data->set.postfields && data->set.set_resume_from) {
525
    /* we cannot */
526
0
    failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
527
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
528
0
  }
529
530
0
  data->state.prefer_ascii = data->set.prefer_ascii;
531
0
#ifdef CURL_LIST_ONLY_PROTOCOL
532
0
  data->state.list_only = data->set.list_only;
533
0
#endif
534
0
  data->state.httpreq = data->set.method;
535
536
0
  data->state.requests = 0;
537
0
  data->state.followlocation = 0; /* reset the location-follow counter */
538
0
  data->state.this_is_a_follow = FALSE; /* reset this */
539
0
  data->state.errorbuf = FALSE; /* no error has occurred */
540
0
#ifndef CURL_DISABLE_HTTP
541
0
  Curl_http_neg_init(data, &data->state.http_neg);
542
0
#endif
543
0
  data->state.authproblem = FALSE;
544
0
  data->state.authhost.want = data->set.httpauth;
545
0
  data->state.authproxy.want = data->set.proxyauth;
546
0
  Curl_safefree(data->info.wouldredirect);
547
0
  Curl_data_priority_clear_state(data);
548
549
0
  if(data->state.httpreq == HTTPREQ_PUT)
550
0
    data->state.infilesize = data->set.filesize;
551
0
  else if((data->state.httpreq != HTTPREQ_GET) &&
552
0
          (data->state.httpreq != HTTPREQ_HEAD)) {
553
0
    data->state.infilesize = data->set.postfieldsize;
554
0
    if(data->set.postfields && (data->state.infilesize == -1))
555
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
556
0
  }
557
0
  else
558
0
    data->state.infilesize = 0;
559
560
  /* If there is a list of cookie files to read, do it now! */
561
0
  Curl_cookie_loadfiles(data);
562
563
  /* If there is a list of host pairs to deal with */
564
0
  if(data->state.resolve)
565
0
    result = Curl_loadhostpairs(data);
566
567
  /* If there is a list of hsts files to read */
568
0
  Curl_hsts_loadfiles(data);
569
570
0
  if(!result) {
571
    /* Allow data->set.use_port to set which port to use. This needs to be
572
     * disabled for example when we follow Location: headers to URLs using
573
     * different ports! */
574
0
    data->state.allow_port = TRUE;
575
576
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
577
    /*************************************************************
578
     * Tell signal handler to ignore SIGPIPE
579
     *************************************************************/
580
    if(!data->set.no_signal)
581
      data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
582
#endif
583
584
0
    Curl_initinfo(data); /* reset session-specific information "variables" */
585
0
    Curl_pgrsResetTransferSizes(data);
586
0
    Curl_pgrsStartNow(data);
587
588
    /* In case the handle is reused and an authentication method was picked
589
       in the session we need to make sure we only use the one(s) we now
590
       consider to be fine */
591
0
    data->state.authhost.picked &= data->state.authhost.want;
592
0
    data->state.authproxy.picked &= data->state.authproxy.want;
593
594
0
#ifndef CURL_DISABLE_FTP
595
0
    data->state.wildcardmatch = data->set.wildcard_enabled;
596
0
    if(data->state.wildcardmatch) {
597
0
      struct WildcardData *wc;
598
0
      if(!data->wildcard) {
599
0
        data->wildcard = calloc(1, sizeof(struct WildcardData));
600
0
        if(!data->wildcard)
601
0
          return CURLE_OUT_OF_MEMORY;
602
0
      }
603
0
      wc = data->wildcard;
604
0
      if(wc->state < CURLWC_INIT) {
605
0
        if(wc->ftpwc)
606
0
          wc->dtor(wc->ftpwc);
607
0
        Curl_safefree(wc->pattern);
608
0
        Curl_safefree(wc->path);
609
0
        Curl_wildcard_init(wc); /* init wildcard structures */
610
0
      }
611
0
    }
612
0
#endif
613
0
    result = Curl_hsts_loadcb(data, data->hsts);
614
0
  }
615
616
  /*
617
   * Set user-agent. Used for HTTP, but since we can attempt to tunnel
618
   * basically anything through an HTTP proxy we cannot limit this based on
619
   * protocol.
620
   */
621
0
  if(data->set.str[STRING_USERAGENT]) {
622
0
    free(data->state.aptr.uagent);
623
0
    data->state.aptr.uagent =
624
0
      curl_maprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
625
0
    if(!data->state.aptr.uagent)
626
0
      return CURLE_OUT_OF_MEMORY;
627
0
  }
628
629
0
  if(data->set.str[STRING_USERNAME] ||
630
0
     data->set.str[STRING_PASSWORD])
631
0
    data->state.creds_from = CREDS_OPTION;
632
0
  if(!result)
633
0
    result = Curl_setstropt(&data->state.aptr.user,
634
0
                            data->set.str[STRING_USERNAME]);
635
0
  if(!result)
636
0
    result = Curl_setstropt(&data->state.aptr.passwd,
637
0
                            data->set.str[STRING_PASSWORD]);
638
0
#ifndef CURL_DISABLE_PROXY
639
0
  if(!result)
640
0
    result = Curl_setstropt(&data->state.aptr.proxyuser,
641
0
                            data->set.str[STRING_PROXYUSERNAME]);
642
0
  if(!result)
643
0
    result = Curl_setstropt(&data->state.aptr.proxypasswd,
644
0
                            data->set.str[STRING_PROXYPASSWORD]);
645
0
#endif
646
647
0
  data->req.headerbytecount = 0;
648
0
  Curl_headers_cleanup(data);
649
0
  return result;
650
0
}
651
652
/* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
653
654
   NOTE: that the *url is malloc()ed. */
655
CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
656
0
{
657
0
  struct connectdata *conn = data->conn;
658
0
  bool retry = FALSE;
659
0
  *url = NULL;
660
661
  /* if we are talking upload, we cannot do the checks below, unless the
662
     protocol is HTTP as when uploading over HTTP we will still get a
663
     response */
664
0
  if(data->state.upload &&
665
0
     !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
666
0
    return CURLE_OK;
667
668
0
  if(conn->bits.reuse &&
669
0
     (data->req.bytecount + data->req.headerbytecount == 0) &&
670
0
     ((!data->req.no_body && !data->req.done) ||
671
0
      (conn->handler->protocol & PROTO_FAMILY_HTTP))
672
0
#ifndef CURL_DISABLE_RTSP
673
0
     && (data->set.rtspreq != RTSPREQ_RECEIVE)
674
0
#endif
675
0
    )
676
    /* We got no data, we attempted to reuse a connection. For HTTP this
677
       can be a retry so we try again regardless if we expected a body.
678
       For other protocols we only try again only if we expected a body.
679
680
       This might happen if the connection was left alive when we were
681
       done using it before, but that was closed when we wanted to read from
682
       it again. Bad luck. Retry the same request on a fresh connect! */
683
0
    retry = TRUE;
684
0
  else if(data->state.refused_stream &&
685
0
          (data->req.bytecount + data->req.headerbytecount == 0) ) {
686
    /* This was sent on a refused stream, safe to rerun. A refused stream
687
       error can typically only happen on HTTP/2 level if the stream is safe
688
       to issue again, but the nghttp2 API can deliver the message to other
689
       streams as well, which is why this adds the check the data counters
690
       too. */
691
0
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
692
0
    data->state.refused_stream = FALSE; /* clear again */
693
0
    retry = TRUE;
694
0
  }
695
0
  if(retry) {
696
0
#define CONN_MAX_RETRIES 5
697
0
    if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
698
0
      failf(data, "Connection died, tried %d times before giving up",
699
0
            CONN_MAX_RETRIES);
700
0
      data->state.retrycount = 0;
701
0
      return CURLE_SEND_ERROR;
702
0
    }
703
0
    infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
704
0
          data->state.retrycount);
705
0
    *url = strdup(data->state.url);
706
0
    if(!*url)
707
0
      return CURLE_OUT_OF_MEMORY;
708
709
0
    connclose(conn, "retry"); /* close this connection */
710
0
    conn->bits.retry = TRUE; /* mark this as a connection we are about
711
                                to retry. Marking it this way should
712
                                prevent i.e HTTP transfers to return
713
                                error just because nothing has been
714
                                transferred! */
715
0
    Curl_creader_set_rewind(data, TRUE);
716
0
  }
717
0
  return CURLE_OK;
718
0
}
719
720
static void xfer_setup(
721
  struct Curl_easy *data,   /* transfer */
722
  int send_idx,             /* sockindex to send on or -1 */
723
  int recv_idx,             /* sockindex to receive on or -1 */
724
  curl_off_t recv_size      /* how much to receive, -1 if unknown */
725
  )
726
0
{
727
0
  struct SingleRequest *k = &data->req;
728
0
  struct connectdata *conn = data->conn;
729
730
0
  DEBUGASSERT(conn != NULL);
731
  /* indexes are in range */
732
0
  DEBUGASSERT((send_idx <= 1) && (send_idx >= -1));
733
0
  DEBUGASSERT((recv_idx <= 1) && (recv_idx >= -1));
734
  /* if request wants to send, switching off the send direction is wrong */
735
0
  DEBUGASSERT((send_idx >= 0) || !Curl_req_want_send(data));
736
737
0
  conn->send_idx = send_idx;
738
0
  conn->recv_idx = recv_idx;
739
740
  /* without receiving, there should be not recv_size */
741
0
  DEBUGASSERT((conn->recv_idx >= 0) || (recv_size == -1));
742
0
  k->size = recv_size;
743
0
  k->header = !!conn->handler->write_resp_hd;
744
  /* by default, we do not shutdown at the end of the transfer */
745
0
  k->shutdown = FALSE;
746
0
  k->shutdown_err_ignore = FALSE;
747
748
  /* The code sequence below is placed in this function just because all
749
     necessary input is not always known in do_complete() as this function may
750
     be called after that */
751
0
  if(!k->header && (recv_size > 0))
752
0
    Curl_pgrsSetDownloadSize(data, recv_size);
753
754
  /* we want header and/or body, if neither then do not do this! */
755
0
  if(conn->handler->write_resp_hd || !data->req.no_body) {
756
757
0
    if(conn->recv_idx != -1)
758
0
      k->keepon |= KEEP_RECV;
759
760
0
    if(conn->send_idx != -1)
761
0
      k->keepon |= KEEP_SEND;
762
0
  }
763
764
0
  CURL_TRC_M(data, "xfer_setup: recv_idx=%d, send_idx=%d",
765
0
             conn->recv_idx, conn->send_idx);
766
0
}
767
768
void Curl_xfer_setup_nop(struct Curl_easy *data)
769
0
{
770
0
  xfer_setup(data, -1, -1, -1);
771
0
}
772
773
void Curl_xfer_setup_sendrecv(struct Curl_easy *data,
774
                              int sockindex,
775
                              curl_off_t recv_size)
776
0
{
777
0
  xfer_setup(data, sockindex, sockindex, recv_size);
778
0
}
779
780
void Curl_xfer_setup_send(struct Curl_easy *data,
781
                          int sockindex)
782
0
{
783
0
  xfer_setup(data, sockindex, -1, -1);
784
0
}
785
786
void Curl_xfer_setup_recv(struct Curl_easy *data,
787
                          int sockindex,
788
                          curl_off_t recv_size)
789
0
{
790
0
  xfer_setup(data, -1, sockindex, recv_size);
791
0
}
792
793
void Curl_xfer_set_shutdown(struct Curl_easy *data,
794
                            bool shutdown,
795
                            bool ignore_errors)
796
0
{
797
  /* Shutdown should only be set when the transfer only sends or receives. */
798
0
  DEBUGASSERT(!shutdown ||
799
0
              (data->conn->send_idx < 0) || (data->conn->recv_idx < 0));
800
0
  data->req.shutdown = shutdown;
801
0
  data->req.shutdown_err_ignore = ignore_errors;
802
0
}
803
804
CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
805
                              const char *buf, size_t blen,
806
                              bool is_eos)
807
0
{
808
0
  CURLcode result = CURLE_OK;
809
810
0
  if(data->conn->handler->write_resp) {
811
    /* protocol handlers offering this function take full responsibility
812
     * for writing all received download data to the client. */
813
0
    result = data->conn->handler->write_resp(data, buf, blen, is_eos);
814
0
  }
815
0
  else {
816
    /* No special handling by protocol handler, write all received data
817
     * as BODY to the client. */
818
0
    if(blen || is_eos) {
819
0
      int cwtype = CLIENTWRITE_BODY;
820
0
      if(is_eos)
821
0
        cwtype |= CLIENTWRITE_EOS;
822
0
      result = Curl_client_write(data, cwtype, buf, blen);
823
0
    }
824
0
  }
825
826
0
  if(!result && is_eos) {
827
    /* If we wrote the EOS, we are definitely done */
828
0
    data->req.eos_written = TRUE;
829
0
    data->req.download_done = TRUE;
830
0
  }
831
0
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
832
0
                 blen, is_eos, result);
833
0
  return result;
834
0
}
835
836
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
837
0
{
838
0
  return Curl_cwriter_is_paused(data);
839
0
}
840
841
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
842
                                 const char *hd0, size_t hdlen, bool is_eos)
843
0
{
844
0
  if(data->conn->handler->write_resp_hd) {
845
    /* protocol handlers offering this function take full responsibility
846
     * for writing all received download data to the client. */
847
0
    return data->conn->handler->write_resp_hd(data, hd0, hdlen, is_eos);
848
0
  }
849
  /* No special handling by protocol handler, write as response bytes */
850
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
851
0
}
852
853
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
854
0
{
855
0
  (void)premature;
856
0
  return Curl_cw_out_done(data);
857
0
}
858
859
bool Curl_xfer_needs_flush(struct Curl_easy *data)
860
0
{
861
0
  return Curl_conn_needs_flush(data, data->conn->send_idx);
862
0
}
863
864
CURLcode Curl_xfer_flush(struct Curl_easy *data)
865
0
{
866
0
  return Curl_conn_flush(data, data->conn->send_idx);
867
0
}
868
869
CURLcode Curl_xfer_send(struct Curl_easy *data,
870
                        const void *buf, size_t blen, bool eos,
871
                        size_t *pnwritten)
872
0
{
873
0
  CURLcode result;
874
875
0
  DEBUGASSERT(data);
876
0
  DEBUGASSERT(data->conn);
877
878
0
  result = Curl_conn_send(data, data->conn->send_idx,
879
0
                          buf, blen, eos, pnwritten);
880
0
  if(result == CURLE_AGAIN) {
881
0
    result = CURLE_OK;
882
0
    *pnwritten = 0;
883
0
  }
884
0
  else if(!result && *pnwritten)
885
0
    data->info.request_size += *pnwritten;
886
887
0
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
888
0
               blen, eos, result, *pnwritten));
889
0
  return result;
890
0
}
891
892
CURLcode Curl_xfer_recv(struct Curl_easy *data,
893
                        char *buf, size_t blen,
894
                        size_t *pnrcvd)
895
0
{
896
0
  DEBUGASSERT(data);
897
0
  DEBUGASSERT(data->conn);
898
0
  DEBUGASSERT(data->set.buffer_size > 0);
899
900
0
  if((size_t)data->set.buffer_size < blen)
901
0
    blen = (size_t)data->set.buffer_size;
902
0
  return Curl_conn_recv(data, data->conn->recv_idx, buf, blen, pnrcvd);
903
0
}
904
905
CURLcode Curl_xfer_send_close(struct Curl_easy *data)
906
0
{
907
0
  Curl_conn_ev_data_done_send(data);
908
0
  return CURLE_OK;
909
0
}
910
911
bool Curl_xfer_is_blocked(struct Curl_easy *data)
912
0
{
913
0
  bool want_send = ((data)->req.keepon & KEEP_SEND);
914
0
  bool want_recv = ((data)->req.keepon & KEEP_RECV);
915
0
  if(!want_send)
916
0
    return want_recv && Curl_xfer_recv_is_paused(data);
917
0
  else if(!want_recv)
918
0
    return want_send && Curl_xfer_send_is_paused(data);
919
0
  else
920
0
    return Curl_xfer_recv_is_paused(data) && Curl_xfer_send_is_paused(data);
921
0
}
922
923
bool Curl_xfer_send_is_paused(struct Curl_easy *data)
924
0
{
925
0
  return (data->req.keepon & KEEP_SEND_PAUSE);
926
0
}
927
928
bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
929
0
{
930
0
  return (data->req.keepon & KEEP_RECV_PAUSE);
931
0
}
932
933
CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable)
934
0
{
935
0
  CURLcode result = CURLE_OK;
936
0
  if(enable) {
937
0
    data->req.keepon |= KEEP_SEND_PAUSE;
938
0
  }
939
0
  else {
940
0
    data->req.keepon &= ~KEEP_SEND_PAUSE;
941
0
    if(Curl_creader_is_paused(data))
942
0
      result = Curl_creader_unpause(data);
943
0
  }
944
0
  return result;
945
0
}
946
947
CURLcode Curl_xfer_pause_recv(struct Curl_easy *data, bool enable)
948
0
{
949
0
  CURLcode result = CURLE_OK;
950
0
  if(enable) {
951
0
    data->req.keepon |= KEEP_RECV_PAUSE;
952
0
  }
953
0
  else {
954
0
    data->req.keepon &= ~KEEP_RECV_PAUSE;
955
0
    if(Curl_cwriter_is_paused(data))
956
0
      result = Curl_cwriter_unpause(data);
957
0
  }
958
0
  Curl_conn_ev_data_pause(data, enable);
959
0
  return result;
960
0
}
961
962
bool Curl_xfer_is_too_fast(struct Curl_easy *data)
963
0
{
964
0
  struct Curl_llist_node *e = Curl_llist_head(&data->state.timeoutlist);
965
0
  while(e) {
966
0
    struct time_node *n = Curl_node_elem(e);
967
0
    e = Curl_node_next(e);
968
0
    if(n->eid == EXPIRE_TOOFAST)
969
0
      return TRUE;
970
0
  }
971
0
  return FALSE;
972
0
}