Coverage Report

Created: 2026-07-14 07:09

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", (int)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", (int)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
 * Curl_pretransfer() is called immediately before a transfer starts, and only
443
 * once for one transfer no matter if it has redirects or do multi-pass
444
 * authentication etc.
445
 */
446
CURLcode Curl_pretransfer(struct Curl_easy *data)
447
0
{
448
0
  CURLcode result = CURLE_OK;
449
450
  /* Reset the retry count at the start of each request.
451
   * If the retry count is not reset, when the connection drops,
452
   * it will not enter the retry mechanism on CONN_MAX_RETRIES + 1 attempts
453
   * and will immediately throw
454
   * "Connection died, tried CONN_MAX_RETRIES times before giving up".
455
   * By resetting it here, we ensure each new request starts fresh. */
456
0
  data->state.retrycount = 0;
457
458
0
  if(!data->set.str[STRING_SET_URL] && !data->set.uh) {
459
    /* we cannot do anything without URL */
460
0
    failf(data, "No URL set");
461
0
    return CURLE_URL_MALFORMAT;
462
0
  }
463
464
  /* CURLOPT_CURLU overrides CURLOPT_URL and the contents of the CURLU handle
465
     is allowed to be changed by the user between transfers */
466
0
  if(data->set.uh) {
467
0
    CURLUcode uc;
468
0
    curlx_free(data->set.str[STRING_SET_URL]);
469
0
    uc = curl_url_get(data->set.uh,
470
0
                      CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
471
0
    if(uc) {
472
      /* clear the pointer to not point to freed memory anymore */
473
0
      Curl_bufref_set(&data->state.url, NULL, 0, NULL);
474
0
      failf(data, "No URL set");
475
0
      return CURLE_URL_MALFORMAT;
476
0
    }
477
0
  }
478
479
0
  Curl_bufref_set(&data->state.url, data->set.str[STRING_SET_URL], 0, NULL);
480
481
0
  if(data->set.postfields && data->set.set_resume_from) {
482
    /* we cannot */
483
0
    failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
484
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
485
0
  }
486
487
0
  data->state.prefer_ascii = data->set.prefer_ascii;
488
0
#ifdef CURL_LIST_ONLY_PROTOCOL
489
0
  data->state.list_only = data->set.list_only;
490
0
#endif
491
0
  data->state.httpreq = data->set.method;
492
493
  /* initial transfer request coming up, forget the initial origin
494
   * from a previous perform() on this handle. */
495
0
  Curl_peer_unlink(&data->state.initial_origin);
496
0
  Curl_peer_unlink(&data->state.origin);
497
0
  data->state.requests = 0;
498
0
  data->state.followlocation = 0; /* reset the location-follow counter */
499
0
  data->state.this_is_a_follow = FALSE; /* reset this */
500
0
  data->state.http_ignorecustom = FALSE; /* use custom HTTP method */
501
0
  data->state.errorbuf = FALSE; /* no error has occurred */
502
0
#ifndef CURL_DISABLE_HTTP
503
0
  Curl_http_neg_init(data, &data->state.http_neg);
504
0
#endif
505
0
  data->state.authproblem = FALSE;
506
0
  data->state.authhost.want = data->set.httpauth;
507
0
  data->state.authproxy.want = data->set.proxyauth;
508
0
  curlx_safefree(data->info.wouldredirect);
509
0
  Curl_data_priority_clear_state(data);
510
0
  if(data->set.http_auto_referer)
511
0
    Curl_bufref_free(&data->state.referer);
512
0
  if(data->set.str[STRING_SET_REFERER])
513
0
    Curl_bufref_set(&data->state.referer, data->set.str[STRING_SET_REFERER],
514
0
                    0, NULL);
515
0
  else
516
0
    Curl_bufref_free(&data->state.referer);
517
518
0
  if(data->state.httpreq == HTTPREQ_PUT)
519
0
    data->state.infilesize = data->set.filesize;
520
0
  else if((data->state.httpreq != HTTPREQ_GET) &&
521
0
          (data->state.httpreq != HTTPREQ_HEAD)) {
522
0
    data->state.infilesize = data->set.postfieldsize;
523
0
    if(data->set.postfields && (data->state.infilesize == -1))
524
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
525
0
  }
526
0
  else
527
0
    data->state.infilesize = 0;
528
529
  /* If there is a list of cookie files to read, do it now! */
530
0
  result = Curl_cookie_loadfiles(data);
531
0
  if(!result)
532
0
    Curl_cookie_run(data); /* activate */
533
534
  /* If there is a list of host pairs to deal with */
535
0
  if(!result && data->state.resolve)
536
0
    result = Curl_loadhostpairs(data);
537
538
0
  if(!result)
539
    /* If there is a list of hsts files to read */
540
0
    result = Curl_hsts_loadfiles(data);
541
542
0
  if(!result) {
543
    /* Allow data->set.use_port to set which port to use. This needs to be
544
     * disabled for example when we follow Location: headers to URLs using
545
     * different ports! */
546
0
    data->state.allow_port = TRUE;
547
548
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(MSG_NOSIGNAL)
549
    /*************************************************************
550
     * Tell signal handler to ignore SIGPIPE
551
     *************************************************************/
552
    if(!data->set.no_signal)
553
      data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
554
#endif
555
556
0
    Curl_initinfo(data); /* reset session-specific information "variables" */
557
0
    Curl_pgrsResetTransferSizes(data);
558
0
    Curl_pgrsStartNow(data);
559
560
    /* In case the handle is reused and an authentication method was picked
561
       in the session we need to make sure we only use the one(s) we now
562
       consider to be fine */
563
0
    data->state.authhost.picked &= data->state.authhost.want;
564
0
    data->state.authproxy.picked &= data->state.authproxy.want;
565
566
0
#ifndef CURL_DISABLE_FTP
567
0
    data->state.wildcardmatch = data->set.wildcard_enabled;
568
0
    if(data->state.wildcardmatch) {
569
0
      struct WildcardData *wc;
570
0
      if(!data->wildcard) {
571
0
        data->wildcard = curlx_calloc(1, sizeof(struct WildcardData));
572
0
        if(!data->wildcard)
573
0
          return CURLE_OUT_OF_MEMORY;
574
0
      }
575
0
      wc = data->wildcard;
576
0
      if(wc->state < CURLWC_INIT) {
577
0
        if(wc->ftpwc)
578
0
          wc->dtor(wc->ftpwc);
579
0
        curlx_safefree(wc->pattern);
580
0
        curlx_safefree(wc->path);
581
0
        Curl_wildcard_init(wc); /* init wildcard structures */
582
0
      }
583
0
    }
584
0
#endif
585
0
    result = Curl_hsts_loadcb(data, data->hsts);
586
0
  }
587
588
  /*
589
   * Set user-agent. Used for HTTP, but since we can attempt to tunnel
590
   * anything through an HTTP proxy we cannot limit this based on protocol.
591
   */
592
0
  if(!result && data->set.str[STRING_USERAGENT]) {
593
0
    curlx_free(data->state.aptr.uagent);
594
0
    data->state.aptr.uagent =
595
0
      curl_maprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
596
0
    if(!data->state.aptr.uagent)
597
0
      return CURLE_OUT_OF_MEMORY;
598
0
  }
599
600
0
  data->req.headerbytecount = 0;
601
0
  Curl_headers_cleanup(data);
602
0
  return result;
603
0
}
604
605
/* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
606
607
   NOTE: that the *url is curlx_malloc()ed. */
608
CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
609
0
{
610
0
  struct connectdata *conn = data->conn;
611
0
  bool retry = FALSE;
612
0
  *url = NULL;
613
614
  /* if we are talking upload, we cannot do the checks below, unless the
615
     protocol is HTTP as when uploading over HTTP we will still get a
616
     response */
617
0
  if(data->state.upload &&
618
0
     !(conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_RTSP)))
619
0
    return CURLE_OK;
620
621
0
  if(conn->bits.reuse &&
622
0
     (data->req.bytecount + data->req.headerbytecount == 0) &&
623
0
     ((!data->req.no_body && !data->req.done) ||
624
0
      (conn->scheme->protocol & PROTO_FAMILY_HTTP))
625
#ifndef CURL_DISABLE_RTSP
626
     && (data->set.rtspreq != RTSPREQ_RECEIVE)
627
#endif
628
0
    )
629
    /* We got no data, we attempted to reuse a connection. For HTTP this
630
       can be a retry so we try again regardless if we expected a body.
631
       For other protocols we only try again only if we expected a body.
632
633
       This might happen if the connection was left alive when we were
634
       done using it before, but that was closed when we wanted to read from
635
       it again. Bad luck. Retry the same request on a fresh connect! */
636
0
    retry = TRUE;
637
0
  else if(data->state.refused_stream &&
638
0
          (data->req.bytecount + data->req.headerbytecount == 0)) {
639
    /* This was sent on a refused stream, safe to rerun. A refused stream
640
       error can typically only happen on HTTP/2 level if the stream is safe
641
       to issue again, but the nghttp2 API can deliver the message to other
642
       streams as well, which is why this adds the check the data counters
643
       too. */
644
0
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
645
0
    data->state.refused_stream = FALSE; /* clear again */
646
0
    retry = TRUE;
647
0
  }
648
0
  if(retry) {
649
0
#define CONN_MAX_RETRIES 5
650
0
    if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
651
0
      failf(data, "Connection died, tried %d times before giving up",
652
0
            CONN_MAX_RETRIES);
653
0
      data->state.retrycount = 0;
654
0
      return CURLE_SEND_ERROR;
655
0
    }
656
0
    infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
657
0
          data->state.retrycount);
658
0
    *url = Curl_bufref_dup(&data->state.url);
659
0
    if(!*url)
660
0
      return CURLE_OUT_OF_MEMORY;
661
662
0
    connclose(conn, "retry"); /* close this connection */
663
0
    conn->bits.retry = TRUE; /* mark this as a connection we are about to
664
                                retry. Marking it this way should prevent i.e
665
                                HTTP transfers to return error because nothing
666
                                has been transferred! */
667
0
    Curl_creader_set_rewind(data, TRUE);
668
0
  }
669
0
  return CURLE_OK;
670
0
}
671
672
static void xfer_setup(
673
  struct Curl_easy *data,   /* transfer */
674
  int send_idx,             /* sockindex to send on or -1 */
675
  int recv_idx,             /* sockindex to receive on or -1 */
676
  curl_off_t recv_size      /* how much to receive, -1 if unknown */
677
  )
678
0
{
679
0
  struct SingleRequest *k = &data->req;
680
0
  struct connectdata *conn = data->conn;
681
682
0
  DEBUGASSERT(conn);
683
  /* indexes are in range */
684
0
  DEBUGASSERT((send_idx <= 1) && (send_idx >= -1));
685
0
  DEBUGASSERT((recv_idx <= 1) && (recv_idx >= -1));
686
  /* if request wants to send, switching off the send direction is wrong */
687
0
  DEBUGASSERT((send_idx >= 0) || !Curl_req_want_send(data));
688
689
0
  conn->send_idx = send_idx;
690
0
  conn->recv_idx = recv_idx;
691
692
  /* without receiving, there should be not recv_size */
693
0
  DEBUGASSERT((conn->recv_idx >= 0) || (recv_size == -1));
694
0
  k->size = recv_size;
695
0
  k->header = !!conn->scheme->run->write_resp_hd;
696
  /* by default, we do not shutdown at the end of the transfer */
697
0
  k->shutdown = FALSE;
698
0
  k->shutdown_err_ignore = FALSE;
699
700
  /* The code sequence below is placed in this function because all necessary
701
     input is not always known in do_complete() as this function may be called
702
     after that */
703
0
  if(!k->header && (recv_size > 0))
704
0
    Curl_pgrsSetDownloadSize(data, recv_size);
705
706
  /* we want header and/or body, if neither then do not do this! */
707
0
  if(conn->scheme->run->write_resp_hd || !data->req.no_body) {
708
0
    if(conn->recv_idx != -1)
709
0
      CURL_REQ_SET_RECV(data);
710
0
    if(conn->send_idx != -1)
711
0
      CURL_REQ_SET_SEND(data);
712
0
  }
713
0
  CURL_TRC_M(data, "xfer_setup: recv_idx=%d, send_idx=%d",
714
0
             conn->recv_idx, conn->send_idx);
715
0
}
716
717
void Curl_xfer_setup_nop(struct Curl_easy *data)
718
0
{
719
0
  xfer_setup(data, -1, -1, -1);
720
0
}
721
722
void Curl_xfer_setup_sendrecv(struct Curl_easy *data,
723
                              int sockindex,
724
                              curl_off_t recv_size)
725
0
{
726
0
  xfer_setup(data, sockindex, sockindex, recv_size);
727
0
}
728
729
void Curl_xfer_setup_send(struct Curl_easy *data,
730
                          int sockindex)
731
0
{
732
0
  xfer_setup(data, sockindex, -1, -1);
733
0
}
734
735
void Curl_xfer_setup_recv(struct Curl_easy *data,
736
                          int sockindex,
737
                          curl_off_t recv_size)
738
0
{
739
0
  xfer_setup(data, -1, sockindex, recv_size);
740
0
}
741
742
void Curl_xfer_set_shutdown(struct Curl_easy *data,
743
                            bool shutdown,
744
                            bool ignore_errors)
745
0
{
746
  /* Shutdown should only be set when the transfer only sends or receives. */
747
0
  DEBUGASSERT(!shutdown ||
748
0
              (data->conn->send_idx < 0) || (data->conn->recv_idx < 0));
749
0
  data->req.shutdown = shutdown;
750
0
  data->req.shutdown_err_ignore = ignore_errors;
751
0
}
752
753
CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
754
                              const char *buf, size_t blen,
755
                              bool is_eos)
756
0
{
757
0
  CURLcode result = CURLE_OK;
758
759
0
  if(data->conn->scheme->run->write_resp) {
760
    /* protocol handlers offering this function take full responsibility
761
     * for writing all received download data to the client. */
762
0
    result = data->conn->scheme->run->write_resp(data, buf, blen, is_eos);
763
0
  }
764
0
  else {
765
    /* No special handling by protocol handler, write all received data
766
     * as BODY to the client. */
767
0
    if(blen || is_eos) {
768
0
      int cwtype = CLIENTWRITE_BODY;
769
0
      if(is_eos)
770
0
        cwtype |= CLIENTWRITE_EOS;
771
0
      result = Curl_client_write(data, cwtype, buf, blen);
772
0
    }
773
0
  }
774
775
0
  if(!result && is_eos) {
776
    /* If we wrote the EOS, we are definitely done */
777
0
    data->req.eos_written = TRUE;
778
0
    data->req.download_done = TRUE;
779
0
  }
780
0
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
781
0
                 blen, is_eos, (int)result);
782
0
  return result;
783
0
}
784
785
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
786
0
{
787
0
  return Curl_cwriter_is_paused(data);
788
0
}
789
790
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
791
                                 const char *hd0, size_t hdlen, bool is_eos)
792
0
{
793
0
  if(data->conn->scheme->run->write_resp_hd) {
794
0
    DEBUGASSERT(!hd0[hdlen]); /* null-terminated */
795
    /* protocol handlers offering this function take full responsibility
796
     * for writing all received download data to the client. */
797
0
    return data->conn->scheme->run->write_resp_hd(data, hd0, hdlen, is_eos);
798
0
  }
799
  /* No special handling by protocol handler, write as response bytes */
800
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
801
0
}
802
803
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
804
0
{
805
0
  (void)premature;
806
0
  return Curl_cw_out_done(data);
807
0
}
808
809
bool Curl_xfer_needs_flush(struct Curl_easy *data)
810
0
{
811
0
  return Curl_conn_needs_flush(data, data->conn->send_idx);
812
0
}
813
814
CURLcode Curl_xfer_flush(struct Curl_easy *data)
815
0
{
816
0
  return Curl_conn_flush(data, data->conn->send_idx);
817
0
}
818
819
CURLcode Curl_xfer_send(struct Curl_easy *data,
820
                        const void *buf, size_t blen, bool eos,
821
                        size_t *pnwritten)
822
0
{
823
0
  CURLcode result;
824
825
0
  DEBUGASSERT(data);
826
0
  DEBUGASSERT(data->conn);
827
828
0
  result = Curl_conn_send(data, data->conn->send_idx,
829
0
                          buf, blen, eos, pnwritten);
830
0
  if(result == CURLE_AGAIN) {
831
0
    result = CURLE_OK;
832
0
    *pnwritten = 0;
833
0
  }
834
0
  else if(!result && *pnwritten)
835
0
    data->info.request_size += *pnwritten;
836
837
0
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
838
0
               blen, eos, (int)result, *pnwritten));
839
0
  return result;
840
0
}
841
842
CURLcode Curl_xfer_recv(struct Curl_easy *data,
843
                        char *buf, size_t blen,
844
                        size_t *pnrcvd)
845
0
{
846
0
  DEBUGASSERT(data);
847
0
  DEBUGASSERT(data->conn);
848
0
  DEBUGASSERT(data->set.buffer_size > 0);
849
850
0
  if(curlx_uitouz(data->set.buffer_size) < blen)
851
0
    blen = curlx_uitouz(data->set.buffer_size);
852
0
  return Curl_conn_recv(data, data->conn->recv_idx, buf, blen, pnrcvd);
853
0
}
854
855
CURLcode Curl_xfer_send_close(struct Curl_easy *data)
856
0
{
857
0
  Curl_conn_ev_data_done_send(data);
858
0
  return CURLE_OK;
859
0
}
860
861
bool Curl_xfer_is_blocked(struct Curl_easy *data)
862
0
{
863
0
  bool want_send = CURL_REQ_WANT_SEND(data);
864
0
  bool want_recv = CURL_REQ_WANT_RECV(data);
865
0
  if(!want_send)
866
0
    return want_recv && Curl_xfer_recv_is_paused(data);
867
0
  else if(!want_recv)
868
0
    return want_send && Curl_xfer_send_is_paused(data);
869
0
  else
870
0
    return Curl_xfer_recv_is_paused(data) && Curl_xfer_send_is_paused(data);
871
0
}
872
873
bool Curl_xfer_send_is_paused(struct Curl_easy *data)
874
0
{
875
0
  return Curl_rlimit_is_blocked(&data->progress.ul.rlimit);
876
0
}
877
878
bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
879
0
{
880
0
  return Curl_rlimit_is_blocked(&data->progress.dl.rlimit);
881
0
}
882
883
CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable)
884
0
{
885
0
  CURLcode result = CURLE_OK;
886
0
  Curl_rlimit_block(&data->progress.ul.rlimit, enable, Curl_pgrs_now(data));
887
0
  if(!enable && Curl_creader_is_paused(data))
888
0
    result = Curl_creader_unpause(data);
889
0
  Curl_pgrsSendPause(data, enable);
890
0
  return result;
891
0
}
892
893
CURLcode Curl_xfer_pause_recv(struct Curl_easy *data, bool enable)
894
0
{
895
0
  CURLcode result = CURLE_OK;
896
0
  Curl_rlimit_block(&data->progress.dl.rlimit, enable, Curl_pgrs_now(data));
897
0
  if(!enable && Curl_cwriter_is_paused(data))
898
0
    result = Curl_cwriter_unpause(data);
899
0
  Curl_conn_ev_data_pause(data, enable);
900
0
  Curl_pgrsRecvPause(data, enable);
901
0
  return result;
902
0
}
903
904
bool Curl_xfer_is_secure(struct Curl_easy *data)
905
0
{
906
0
#ifndef CURL_DISABLE_PROXY
907
0
  if(data->conn && data->conn->bits.origin_is_proxy) {
908
    /* talking to a forward proxy, not secure. we do not use
909
     * a forward proxy for https: and other 's' URLs. Let's just check that
910
     * this did not fail somewhere. */
911
0
    DEBUGASSERT(!(data->state.origin->scheme->flags & PROTOPT_SSL));
912
0
    return FALSE;
913
0
  }
914
0
#endif
915
0
  return (data->state.origin->scheme->flags & PROTOPT_SSL);
916
0
}