Coverage Report

Created: 2025-08-26 07:08

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