Coverage Report

Created: 2026-04-12 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/transfer.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifdef HAVE_NETINET_IN_H
27
#include <netinet/in.h>
28
#endif
29
#ifdef HAVE_NETDB_H
30
#include <netdb.h>
31
#endif
32
#ifdef HAVE_ARPA_INET_H
33
#include <arpa/inet.h>
34
#endif
35
#ifdef HAVE_NET_IF_H
36
#include <net/if.h>
37
#endif
38
#ifdef HAVE_SYS_IOCTL_H
39
#include <sys/ioctl.h>
40
#endif
41
#include <signal.h>
42
43
#ifdef HAVE_SYS_PARAM_H
44
#include <sys/param.h>
45
#endif
46
47
#ifdef HAVE_SYS_SELECT_H
48
#include <sys/select.h>
49
#elif defined(HAVE_UNISTD_H)
50
#include <unistd.h>
51
#endif
52
53
#ifndef HAVE_SOCKET
54
#error "We cannot compile without socket() support!"
55
#endif
56
57
#include "urldata.h"
58
59
#include "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
 * 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
0
  data->state.requests = 0;
494
0
  data->state.followlocation = 0; /* reset the location-follow counter */
495
0
  data->state.this_is_a_follow = FALSE; /* reset this */
496
0
  data->state.http_ignorecustom = FALSE; /* use custom HTTP method */
497
0
  data->state.errorbuf = FALSE; /* no error has occurred */
498
0
#ifndef CURL_DISABLE_HTTP
499
0
  Curl_http_neg_init(data, &data->state.http_neg);
500
0
#endif
501
0
  data->state.authproblem = FALSE;
502
0
  data->state.authhost.want = data->set.httpauth;
503
0
  data->state.authproxy.want = data->set.proxyauth;
504
0
  curlx_safefree(data->info.wouldredirect);
505
0
  Curl_data_priority_clear_state(data);
506
507
0
  if(data->state.httpreq == HTTPREQ_PUT)
508
0
    data->state.infilesize = data->set.filesize;
509
0
  else if((data->state.httpreq != HTTPREQ_GET) &&
510
0
          (data->state.httpreq != HTTPREQ_HEAD)) {
511
0
    data->state.infilesize = data->set.postfieldsize;
512
0
    if(data->set.postfields && (data->state.infilesize == -1))
513
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
514
0
  }
515
0
  else
516
0
    data->state.infilesize = 0;
517
518
  /* If there is a list of cookie files to read, do it now! */
519
0
  result = Curl_cookie_loadfiles(data);
520
0
  if(!result)
521
0
    Curl_cookie_run(data); /* activate */
522
523
  /* If there is a list of host pairs to deal with */
524
0
  if(!result && data->state.resolve)
525
0
    result = Curl_loadhostpairs(data);
526
527
0
  if(!result)
528
    /* If there is a list of hsts files to read */
529
0
    result = Curl_hsts_loadfiles(data);
530
531
0
  if(!result) {
532
    /* Allow data->set.use_port to set which port to use. This needs to be
533
     * disabled for example when we follow Location: headers to URLs using
534
     * different ports! */
535
0
    data->state.allow_port = TRUE;
536
537
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(MSG_NOSIGNAL)
538
    /*************************************************************
539
     * Tell signal handler to ignore SIGPIPE
540
     *************************************************************/
541
    if(!data->set.no_signal)
542
      data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
543
#endif
544
545
0
    Curl_initinfo(data); /* reset session-specific information "variables" */
546
0
    Curl_pgrsResetTransferSizes(data);
547
0
    Curl_pgrsStartNow(data);
548
549
    /* In case the handle is reused and an authentication method was picked
550
       in the session we need to make sure we only use the one(s) we now
551
       consider to be fine */
552
0
    data->state.authhost.picked &= data->state.authhost.want;
553
0
    data->state.authproxy.picked &= data->state.authproxy.want;
554
555
0
#ifndef CURL_DISABLE_FTP
556
0
    data->state.wildcardmatch = data->set.wildcard_enabled;
557
0
    if(data->state.wildcardmatch) {
558
0
      struct WildcardData *wc;
559
0
      if(!data->wildcard) {
560
0
        data->wildcard = curlx_calloc(1, sizeof(struct WildcardData));
561
0
        if(!data->wildcard)
562
0
          return CURLE_OUT_OF_MEMORY;
563
0
      }
564
0
      wc = data->wildcard;
565
0
      if(wc->state < CURLWC_INIT) {
566
0
        if(wc->ftpwc)
567
0
          wc->dtor(wc->ftpwc);
568
0
        curlx_safefree(wc->pattern);
569
0
        curlx_safefree(wc->path);
570
0
        Curl_wildcard_init(wc); /* init wildcard structures */
571
0
      }
572
0
    }
573
0
#endif
574
0
    result = Curl_hsts_loadcb(data, data->hsts);
575
0
  }
576
577
  /*
578
   * Set user-agent. Used for HTTP, but since we can attempt to tunnel
579
   * anything through an HTTP proxy we cannot limit this based on protocol.
580
   */
581
0
  if(!result && data->set.str[STRING_USERAGENT]) {
582
0
    curlx_free(data->state.aptr.uagent);
583
0
    data->state.aptr.uagent =
584
0
      curl_maprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
585
0
    if(!data->state.aptr.uagent)
586
0
      return CURLE_OUT_OF_MEMORY;
587
0
  }
588
589
0
  if(data->set.str[STRING_USERNAME] ||
590
0
     data->set.str[STRING_PASSWORD])
591
0
    data->state.creds_from = CREDS_OPTION;
592
0
  if(!result)
593
0
    result = Curl_setstropt(&data->state.aptr.user,
594
0
                            data->set.str[STRING_USERNAME]);
595
0
  if(!result)
596
0
    result = Curl_setstropt(&data->state.aptr.passwd,
597
0
                            data->set.str[STRING_PASSWORD]);
598
0
#ifndef CURL_DISABLE_PROXY
599
0
  if(!result)
600
0
    result = Curl_setstropt(&data->state.aptr.proxyuser,
601
0
                            data->set.str[STRING_PROXYUSERNAME]);
602
0
  if(!result)
603
0
    result = Curl_setstropt(&data->state.aptr.proxypasswd,
604
0
                            data->set.str[STRING_PROXYPASSWORD]);
605
0
#endif
606
607
0
  data->req.headerbytecount = 0;
608
0
  Curl_headers_cleanup(data);
609
0
  return result;
610
0
}
611
612
/* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
613
614
   NOTE: that the *url is curlx_malloc()ed. */
615
CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
616
0
{
617
0
  struct connectdata *conn = data->conn;
618
0
  bool retry = FALSE;
619
0
  *url = NULL;
620
621
  /* if we are talking upload, we cannot do the checks below, unless the
622
     protocol is HTTP as when uploading over HTTP we will still get a
623
     response */
624
0
  if(data->state.upload &&
625
0
     !(conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_RTSP)))
626
0
    return CURLE_OK;
627
628
0
  if(conn->bits.reuse &&
629
0
     (data->req.bytecount + data->req.headerbytecount == 0) &&
630
0
     ((!data->req.no_body && !data->req.done) ||
631
0
      (conn->scheme->protocol & PROTO_FAMILY_HTTP))
632
0
#ifndef CURL_DISABLE_RTSP
633
0
     && (data->set.rtspreq != RTSPREQ_RECEIVE)
634
0
#endif
635
0
    )
636
    /* We got no data, we attempted to reuse a connection. For HTTP this
637
       can be a retry so we try again regardless if we expected a body.
638
       For other protocols we only try again only if we expected a body.
639
640
       This might happen if the connection was left alive when we were
641
       done using it before, but that was closed when we wanted to read from
642
       it again. Bad luck. Retry the same request on a fresh connect! */
643
0
    retry = TRUE;
644
0
  else if(data->state.refused_stream &&
645
0
          (data->req.bytecount + data->req.headerbytecount == 0)) {
646
    /* This was sent on a refused stream, safe to rerun. A refused stream
647
       error can typically only happen on HTTP/2 level if the stream is safe
648
       to issue again, but the nghttp2 API can deliver the message to other
649
       streams as well, which is why this adds the check the data counters
650
       too. */
651
0
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
652
0
    data->state.refused_stream = FALSE; /* clear again */
653
0
    retry = TRUE;
654
0
  }
655
0
  if(retry) {
656
0
#define CONN_MAX_RETRIES 5
657
0
    if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
658
0
      failf(data, "Connection died, tried %d times before giving up",
659
0
            CONN_MAX_RETRIES);
660
0
      data->state.retrycount = 0;
661
0
      return CURLE_SEND_ERROR;
662
0
    }
663
0
    infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
664
0
          data->state.retrycount);
665
0
    *url = Curl_bufref_dup(&data->state.url);
666
0
    if(!*url)
667
0
      return CURLE_OUT_OF_MEMORY;
668
669
0
    connclose(conn, "retry"); /* close this connection */
670
0
    conn->bits.retry = TRUE; /* mark this as a connection we are about to
671
                                retry. Marking it this way should prevent i.e
672
                                HTTP transfers to return error because nothing
673
                                has been transferred! */
674
0
    Curl_creader_set_rewind(data, TRUE);
675
0
  }
676
0
  return CURLE_OK;
677
0
}
678
679
static void xfer_setup(
680
  struct Curl_easy *data,   /* transfer */
681
  int send_idx,             /* sockindex to send on or -1 */
682
  int recv_idx,             /* sockindex to receive on or -1 */
683
  curl_off_t recv_size      /* how much to receive, -1 if unknown */
684
  )
685
0
{
686
0
  struct SingleRequest *k = &data->req;
687
0
  struct connectdata *conn = data->conn;
688
689
0
  DEBUGASSERT(conn != NULL);
690
  /* indexes are in range */
691
0
  DEBUGASSERT((send_idx <= 1) && (send_idx >= -1));
692
0
  DEBUGASSERT((recv_idx <= 1) && (recv_idx >= -1));
693
  /* if request wants to send, switching off the send direction is wrong */
694
0
  DEBUGASSERT((send_idx >= 0) || !Curl_req_want_send(data));
695
696
0
  conn->send_idx = send_idx;
697
0
  conn->recv_idx = recv_idx;
698
699
  /* without receiving, there should be not recv_size */
700
0
  DEBUGASSERT((conn->recv_idx >= 0) || (recv_size == -1));
701
0
  k->size = recv_size;
702
0
  k->header = !!conn->scheme->run->write_resp_hd;
703
  /* by default, we do not shutdown at the end of the transfer */
704
0
  k->shutdown = FALSE;
705
0
  k->shutdown_err_ignore = FALSE;
706
707
  /* The code sequence below is placed in this function because all necessary
708
     input is not always known in do_complete() as this function may be called
709
     after that */
710
0
  if(!k->header && (recv_size > 0))
711
0
    Curl_pgrsSetDownloadSize(data, recv_size);
712
713
  /* we want header and/or body, if neither then do not do this! */
714
0
  if(conn->scheme->run->write_resp_hd || !data->req.no_body) {
715
0
    if(conn->recv_idx != -1)
716
0
      CURL_REQ_SET_RECV(data);
717
0
    if(conn->send_idx != -1)
718
0
      CURL_REQ_SET_SEND(data);
719
0
  }
720
0
  CURL_TRC_M(data, "xfer_setup: recv_idx=%d, send_idx=%d",
721
0
             conn->recv_idx, conn->send_idx);
722
0
}
723
724
void Curl_xfer_setup_nop(struct Curl_easy *data)
725
0
{
726
0
  xfer_setup(data, -1, -1, -1);
727
0
}
728
729
void Curl_xfer_setup_sendrecv(struct Curl_easy *data,
730
                              int sockindex,
731
                              curl_off_t recv_size)
732
0
{
733
0
  xfer_setup(data, sockindex, sockindex, recv_size);
734
0
}
735
736
void Curl_xfer_setup_send(struct Curl_easy *data,
737
                          int sockindex)
738
0
{
739
0
  xfer_setup(data, sockindex, -1, -1);
740
0
}
741
742
void Curl_xfer_setup_recv(struct Curl_easy *data,
743
                          int sockindex,
744
                          curl_off_t recv_size)
745
0
{
746
0
  xfer_setup(data, -1, sockindex, recv_size);
747
0
}
748
749
void Curl_xfer_set_shutdown(struct Curl_easy *data,
750
                            bool shutdown,
751
                            bool ignore_errors)
752
0
{
753
  /* Shutdown should only be set when the transfer only sends or receives. */
754
0
  DEBUGASSERT(!shutdown ||
755
0
              (data->conn->send_idx < 0) || (data->conn->recv_idx < 0));
756
0
  data->req.shutdown = shutdown;
757
0
  data->req.shutdown_err_ignore = ignore_errors;
758
0
}
759
760
CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
761
                              const char *buf, size_t blen,
762
                              bool is_eos)
763
0
{
764
0
  CURLcode result = CURLE_OK;
765
766
0
  if(data->conn->scheme->run->write_resp) {
767
    /* protocol handlers offering this function take full responsibility
768
     * for writing all received download data to the client. */
769
0
    result = data->conn->scheme->run->write_resp(data, buf, blen, is_eos);
770
0
  }
771
0
  else {
772
    /* No special handling by protocol handler, write all received data
773
     * as BODY to the client. */
774
0
    if(blen || is_eos) {
775
0
      int cwtype = CLIENTWRITE_BODY;
776
0
      if(is_eos)
777
0
        cwtype |= CLIENTWRITE_EOS;
778
0
      result = Curl_client_write(data, cwtype, buf, blen);
779
0
    }
780
0
  }
781
782
0
  if(!result && is_eos) {
783
    /* If we wrote the EOS, we are definitely done */
784
0
    data->req.eos_written = TRUE;
785
0
    data->req.download_done = TRUE;
786
0
  }
787
0
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
788
0
                 blen, is_eos, result);
789
0
  return result;
790
0
}
791
792
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
793
0
{
794
0
  return Curl_cwriter_is_paused(data);
795
0
}
796
797
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
798
                                 const char *hd0, size_t hdlen, bool is_eos)
799
0
{
800
0
  if(data->conn->scheme->run->write_resp_hd) {
801
0
    DEBUGASSERT(!hd0[hdlen]); /* null-terminated */
802
    /* protocol handlers offering this function take full responsibility
803
     * for writing all received download data to the client. */
804
0
    return data->conn->scheme->run->write_resp_hd(data, hd0, hdlen, is_eos);
805
0
  }
806
  /* No special handling by protocol handler, write as response bytes */
807
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
808
0
}
809
810
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
811
0
{
812
0
  (void)premature;
813
0
  return Curl_cw_out_done(data);
814
0
}
815
816
bool Curl_xfer_needs_flush(struct Curl_easy *data)
817
0
{
818
0
  return Curl_conn_needs_flush(data, data->conn->send_idx);
819
0
}
820
821
CURLcode Curl_xfer_flush(struct Curl_easy *data)
822
0
{
823
0
  return Curl_conn_flush(data, data->conn->send_idx);
824
0
}
825
826
CURLcode Curl_xfer_send(struct Curl_easy *data,
827
                        const void *buf, size_t blen, bool eos,
828
                        size_t *pnwritten)
829
0
{
830
0
  CURLcode result;
831
832
0
  DEBUGASSERT(data);
833
0
  DEBUGASSERT(data->conn);
834
835
0
  result = Curl_conn_send(data, data->conn->send_idx,
836
0
                          buf, blen, eos, pnwritten);
837
0
  if(result == CURLE_AGAIN) {
838
0
    result = CURLE_OK;
839
0
    *pnwritten = 0;
840
0
  }
841
0
  else if(!result && *pnwritten)
842
0
    data->info.request_size += *pnwritten;
843
844
0
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
845
0
               blen, eos, result, *pnwritten));
846
0
  return result;
847
0
}
848
849
CURLcode Curl_xfer_recv(struct Curl_easy *data,
850
                        char *buf, size_t blen,
851
                        size_t *pnrcvd)
852
0
{
853
0
  DEBUGASSERT(data);
854
0
  DEBUGASSERT(data->conn);
855
0
  DEBUGASSERT(data->set.buffer_size > 0);
856
857
0
  if(curlx_uitouz(data->set.buffer_size) < blen)
858
0
    blen = curlx_uitouz(data->set.buffer_size);
859
0
  return Curl_conn_recv(data, data->conn->recv_idx, buf, blen, pnrcvd);
860
0
}
861
862
CURLcode Curl_xfer_send_close(struct Curl_easy *data)
863
0
{
864
0
  Curl_conn_ev_data_done_send(data);
865
0
  return CURLE_OK;
866
0
}
867
868
bool Curl_xfer_is_blocked(struct Curl_easy *data)
869
0
{
870
0
  bool want_send = CURL_REQ_WANT_SEND(data);
871
0
  bool want_recv = CURL_REQ_WANT_RECV(data);
872
0
  if(!want_send)
873
0
    return want_recv && Curl_xfer_recv_is_paused(data);
874
0
  else if(!want_recv)
875
0
    return want_send && Curl_xfer_send_is_paused(data);
876
0
  else
877
0
    return Curl_xfer_recv_is_paused(data) && Curl_xfer_send_is_paused(data);
878
0
}
879
880
bool Curl_xfer_send_is_paused(struct Curl_easy *data)
881
0
{
882
0
  return Curl_rlimit_is_blocked(&data->progress.ul.rlimit);
883
0
}
884
885
bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
886
0
{
887
0
  return Curl_rlimit_is_blocked(&data->progress.dl.rlimit);
888
0
}
889
890
CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable)
891
0
{
892
0
  CURLcode result = CURLE_OK;
893
0
  Curl_rlimit_block(&data->progress.ul.rlimit, enable, Curl_pgrs_now(data));
894
0
  if(!enable && Curl_creader_is_paused(data))
895
0
    result = Curl_creader_unpause(data);
896
0
  Curl_pgrsSendPause(data, enable);
897
0
  return result;
898
0
}
899
900
CURLcode Curl_xfer_pause_recv(struct Curl_easy *data, bool enable)
901
0
{
902
0
  CURLcode result = CURLE_OK;
903
0
  Curl_rlimit_block(&data->progress.dl.rlimit, enable, Curl_pgrs_now(data));
904
0
  if(!enable && Curl_cwriter_is_paused(data))
905
0
    result = Curl_cwriter_unpause(data);
906
0
  Curl_conn_ev_data_pause(data, enable);
907
0
  Curl_pgrsRecvPause(data, enable);
908
0
  return result;
909
0
}
910
911
bool Curl_xfer_is_secure(struct Curl_easy *data)
912
0
{
913
0
  const struct Curl_scheme *scheme = NULL;
914
915
0
  if(data->conn) {
916
0
    scheme = data->conn->scheme;
917
    /* if we are connected, but not use SSL, the transfer is not secure.
918
     * This covers an insecure http:// proxy that is not tunneling.
919
     * We enforce tunneling for such cases, but better be sure here. */
920
0
    if(Curl_conn_is_connected(data->conn, FIRSTSOCKET) &&
921
0
       !Curl_conn_is_ssl(data->conn, FIRSTSOCKET))
922
0
      return FALSE;
923
0
  }
924
0
  else if(data->info.conn_scheme) { /* was connected once */
925
0
    scheme = Curl_get_scheme(data->info.conn_scheme);
926
0
  }
927
0
  else { /* never connected (yet?) */
928
0
    DEBUGASSERT(0); /* not implemented, would need to parse URL */
929
0
  }
930
0
  return scheme ? (scheme->flags & PROTOPT_SSL) : FALSE;
931
0
}