Coverage Report

Created: 2026-06-15 07:03

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