Coverage Report

Created: 2025-07-11 06:33

/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
  int sockindex;
166
167
0
  if(!data || !data->conn)
168
0
    return CURLE_FAILED_INIT;
169
0
  if(data->conn->sockfd == CURL_SOCKET_BAD)
170
0
    return CURLE_FAILED_INIT;
171
0
  sockindex = (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]);
172
0
  return Curl_conn_shutdown(data, sockindex, done);
173
0
}
174
175
static bool xfer_recv_shutdown_started(struct Curl_easy *data)
176
0
{
177
0
  int sockindex;
178
179
0
  if(!data || !data->conn)
180
0
    return FALSE;
181
0
  if(data->conn->sockfd == CURL_SOCKET_BAD)
182
0
    return FALSE;
183
0
  sockindex = (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]);
184
0
  return Curl_shutdown_started(data, sockindex);
185
0
}
186
187
CURLcode Curl_xfer_send_shutdown(struct Curl_easy *data, bool *done)
188
0
{
189
0
  int sockindex;
190
191
0
  if(!data || !data->conn)
192
0
    return CURLE_FAILED_INIT;
193
0
  if(data->conn->writesockfd == CURL_SOCKET_BAD)
194
0
    return CURLE_FAILED_INIT;
195
0
  sockindex = (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]);
196
0
  return Curl_conn_shutdown(data, sockindex, done);
197
0
}
198
199
/**
200
 * Receive raw response data for the transfer.
201
 * @param data         the transfer
202
 * @param buf          buffer to keep response data received
203
 * @param blen         length of `buf`
204
 * @param eos_reliable if EOS detection in underlying connection is reliable
205
 * @param err error    code in case of -1 return
206
 * @return number of bytes read or -1 for error
207
 */
208
static ssize_t xfer_recv_resp(struct Curl_easy *data,
209
                              char *buf, size_t blen,
210
                              bool eos_reliable,
211
                              CURLcode *err)
212
0
{
213
0
  size_t nread;
214
215
0
  DEBUGASSERT(blen > 0);
216
  /* If we are reading BODY data and the connection does NOT handle EOF
217
   * and we know the size of the BODY data, limit the read amount */
218
0
  if(!eos_reliable && !data->req.header && data->req.size != -1) {
219
0
    curl_off_t totalleft = data->req.size - data->req.bytecount;
220
0
    if(totalleft <= 0)
221
0
      blen = 0;
222
0
    else if(totalleft < (curl_off_t)blen)
223
0
      blen = (size_t)totalleft;
224
0
  }
225
0
  else if(xfer_recv_shutdown_started(data)) {
226
    /* we already received everything. Do not try more. */
227
0
    blen = 0;
228
0
  }
229
230
0
  if(!blen) {
231
    /* want nothing more */
232
0
    *err = CURLE_OK;
233
0
    nread = 0;
234
0
  }
235
0
  else {
236
0
    *err = Curl_xfer_recv(data, buf, blen, &nread);
237
0
  }
238
239
0
  if(*err)
240
0
    return -1;
241
0
  if(nread == 0) {
242
0
    if(data->req.shutdown) {
243
0
      bool done;
244
0
      *err = xfer_recv_shutdown(data, &done);
245
0
      if(*err)
246
0
        return -1;
247
0
      if(!done) {
248
0
        *err = CURLE_AGAIN;
249
0
        return -1;
250
0
      }
251
0
    }
252
0
    DEBUGF(infof(data, "sendrecv_dl: we are done"));
253
0
  }
254
0
  return (ssize_t)nread;
255
0
}
256
257
/*
258
 * Go ahead and do a read if we have a readable socket or if
259
 * the stream was rewound (in which case we have data in a
260
 * buffer)
261
 */
262
static CURLcode sendrecv_dl(struct Curl_easy *data,
263
                            struct SingleRequest *k,
264
                            int *didwhat)
265
0
{
266
0
  struct connectdata *conn = data->conn;
267
0
  CURLcode result = CURLE_OK;
268
0
  char *buf, *xfer_buf;
269
0
  size_t blen, xfer_blen;
270
0
  int maxloops = 10;
271
0
  curl_off_t total_received = 0;
272
0
  bool is_multiplex = FALSE;
273
0
  bool rcvd_eagain = FALSE;
274
275
0
  result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
276
0
  if(result)
277
0
    goto out;
278
279
  /* This is where we loop until we have read everything there is to
280
     read or we get a CURLE_AGAIN */
281
0
  do {
282
0
    bool is_eos = FALSE;
283
0
    size_t bytestoread;
284
0
    ssize_t nread;
285
286
0
    if(!is_multiplex) {
287
      /* Multiplexed connection have inherent handling of EOF and we do not
288
       * have to carefully restrict the amount we try to read.
289
       * Multiplexed changes only in one direction. */
290
0
      is_multiplex = Curl_conn_is_multiplex(conn, FIRSTSOCKET);
291
0
    }
292
293
0
    buf = xfer_buf;
294
0
    bytestoread = xfer_blen;
295
296
0
    if(bytestoread && data->set.max_recv_speed > 0) {
297
      /* In case of speed limit on receiving: if this loop already got
298
       * data, break out. If not, limit the amount of bytes to receive.
299
       * The overall, timed, speed limiting is done in multi.c */
300
0
      if(total_received)
301
0
        break;
302
0
      if(data->set.max_recv_speed < (curl_off_t)bytestoread)
303
0
        bytestoread = (size_t)data->set.max_recv_speed;
304
0
    }
305
306
0
    rcvd_eagain = FALSE;
307
0
    nread = xfer_recv_resp(data, buf, bytestoread, is_multiplex, &result);
308
0
    if(nread < 0) {
309
0
      if(CURLE_AGAIN != result)
310
0
        goto out; /* real error */
311
0
      rcvd_eagain = TRUE;
312
0
      result = CURLE_OK;
313
0
      if(data->req.download_done && data->req.no_body &&
314
0
         !data->req.resp_trailer) {
315
0
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
316
0
               "not waiting for EOS"));
317
0
        nread = 0;
318
        /* continue as if we received the EOS */
319
0
      }
320
0
      else
321
0
        break; /* get out of loop */
322
0
    }
323
324
    /* We only get a 0-length receive at the end of the response */
325
0
    blen = (size_t)nread;
326
0
    is_eos = (blen == 0);
327
0
    *didwhat |= KEEP_RECV;
328
329
0
    if(!blen) {
330
      /* if we receive 0 or less here, either the data transfer is done or the
331
         server closed the connection and we bail out from this! */
332
0
      if(is_multiplex)
333
0
        DEBUGF(infof(data, "nread == 0, stream closed, bailing"));
334
0
      else
335
0
        DEBUGF(infof(data, "nread <= 0, server closed connection, bailing"));
336
0
      result = Curl_req_stop_send_recv(data);
337
0
      if(result)
338
0
        goto out;
339
0
      if(k->eos_written) /* already did write this to client, leave */
340
0
        break;
341
0
    }
342
0
    total_received += blen;
343
344
0
    result = Curl_xfer_write_resp(data, buf, blen, is_eos);
345
0
    if(result || data->req.done)
346
0
      goto out;
347
348
    /* if we are done, we stop receiving. On multiplexed connections,
349
     * we should read the EOS. Which may arrive as meta data after
350
     * the bytes. Not taking it in might lead to RST of streams. */
351
0
    if((!is_multiplex && data->req.download_done) || is_eos) {
352
0
      data->req.keepon &= ~KEEP_RECV;
353
0
    }
354
    /* if we are PAUSEd or stopped receiving, leave the loop */
355
0
    if((k->keepon & KEEP_RECV_PAUSE) || !(k->keepon & KEEP_RECV))
356
0
      break;
357
358
0
  } while(maxloops--);
359
360
0
  if(!Curl_xfer_is_blocked(data) &&
361
0
     (!rcvd_eagain || data_pending(data, rcvd_eagain))) {
362
    /* Did not read until EAGAIN or there is still data pending
363
     * in buffers. Mark as read-again via simulated SELECT results. */
364
0
    Curl_multi_mark_dirty(data);
365
0
    CURL_TRC_M(data, "sendrecv_dl() no EAGAIN/pending data, mark as dirty");
366
0
  }
367
368
0
  if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) &&
369
0
     (conn->bits.close || is_multiplex)) {
370
    /* When we have read the entire thing and the close bit is set, the server
371
       may now close the connection. If there is now any kind of sending going
372
       on from our side, we need to stop that immediately. */
373
0
    infof(data, "we are done reading and this is set to close, stop send");
374
0
    Curl_req_abort_sending(data);
375
0
  }
376
377
0
out:
378
0
  Curl_multi_xfer_buf_release(data, xfer_buf);
379
0
  if(result)
380
0
    DEBUGF(infof(data, "sendrecv_dl() -> %d", result));
381
0
  return result;
382
0
}
383
384
/*
385
 * Send data to upload to the server, when the socket is writable.
386
 */
387
static CURLcode sendrecv_ul(struct Curl_easy *data, int *didwhat)
388
0
{
389
  /* We should not get here when the sending is already done. It
390
   * probably means that someone set `data-req.keepon |= KEEP_SEND`
391
   * when it should not. */
392
0
  DEBUGASSERT(!Curl_req_done_sending(data));
393
394
0
  if(!Curl_req_done_sending(data)) {
395
0
    *didwhat |= KEEP_SEND;
396
0
    return Curl_req_send_more(data);
397
0
  }
398
0
  return CURLE_OK;
399
0
}
400
401
/*
402
 * Curl_sendrecv() is the low-level function to be called when data is to
403
 * be read and written to/from the connection.
404
 */
405
CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
406
0
{
407
0
  struct SingleRequest *k = &data->req;
408
0
  CURLcode result = CURLE_OK;
409
0
  int didwhat = 0;
410
411
0
  DEBUGASSERT(nowp);
412
0
  if(Curl_xfer_is_blocked(data)) {
413
0
    result = CURLE_OK;
414
0
    goto out;
415
0
  }
416
417
  /* We go ahead and do a read if we have a readable socket or if the stream
418
     was rewound (in which case we have data in a buffer) */
419
0
  if(k->keepon & KEEP_RECV) {
420
0
    result = sendrecv_dl(data, k, &didwhat);
421
0
    if(result || data->req.done)
422
0
      goto out;
423
0
  }
424
425
  /* If we still have writing to do, we check if we have a writable socket. */
426
0
  if(Curl_req_want_send(data) || (data->req.keepon & KEEP_SEND_TIMED)) {
427
0
    result = sendrecv_ul(data, &didwhat);
428
0
    if(result)
429
0
      goto out;
430
0
  }
431
432
0
  if(!didwhat) {
433
    /* Transfer wanted to send/recv, but nothing was possible. */
434
0
    result = Curl_conn_ev_data_idle(data);
435
0
    if(result)
436
0
      goto out;
437
0
  }
438
439
0
  if(Curl_pgrsUpdate(data))
440
0
    result = CURLE_ABORTED_BY_CALLBACK;
441
0
  else
442
0
    result = Curl_speedcheck(data, *nowp);
443
0
  if(result)
444
0
    goto out;
445
446
0
  if(k->keepon) {
447
0
    if(0 > Curl_timeleft(data, nowp, FALSE)) {
448
0
      if(k->size != -1) {
449
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
450
0
              " milliseconds with %" FMT_OFF_T " out of %"
451
0
              FMT_OFF_T " bytes received",
452
0
              curlx_timediff(*nowp, data->progress.t_startsingle),
453
0
              k->bytecount, k->size);
454
0
      }
455
0
      else {
456
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
457
0
              " milliseconds with %" FMT_OFF_T " bytes received",
458
0
              curlx_timediff(*nowp, data->progress.t_startsingle),
459
0
              k->bytecount);
460
0
      }
461
0
      result = CURLE_OPERATION_TIMEDOUT;
462
0
      goto out;
463
0
    }
464
0
  }
465
0
  else {
466
    /*
467
     * The transfer has been performed. Just make some general checks before
468
     * returning.
469
     */
470
0
    if(!(data->req.no_body) && (k->size != -1) &&
471
0
       (k->bytecount != k->size) && !k->newurl) {
472
0
      failf(data, "transfer closed with %" FMT_OFF_T
473
0
            " bytes remaining to read", k->size - k->bytecount);
474
0
      result = CURLE_PARTIAL_FILE;
475
0
      goto out;
476
0
    }
477
0
    if(Curl_pgrsUpdate(data)) {
478
0
      result = CURLE_ABORTED_BY_CALLBACK;
479
0
      goto out;
480
0
    }
481
0
  }
482
483
  /* If there is nothing more to send/recv, the request is done */
484
0
  if(0 == (k->keepon&(KEEP_RECVBITS|KEEP_SENDBITS)))
485
0
    data->req.done = TRUE;
486
487
0
out:
488
0
  if(result)
489
0
    DEBUGF(infof(data, "Curl_sendrecv() -> %d", result));
490
0
  return result;
491
0
}
492
493
/* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
494
   which means this gets called once for each subsequent redirect etc */
495
void Curl_init_CONNECT(struct Curl_easy *data)
496
0
{
497
0
  data->state.fread_func = data->set.fread_func_set;
498
0
  data->state.in = data->set.in_set;
499
0
  data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
500
0
}
501
502
/*
503
 * Curl_pretransfer() is called immediately before a transfer starts, and only
504
 * once for one transfer no matter if it has redirects or do multi-pass
505
 * authentication etc.
506
 */
507
CURLcode Curl_pretransfer(struct Curl_easy *data)
508
0
{
509
0
  CURLcode result = CURLE_OK;
510
511
0
  if(!data->set.str[STRING_SET_URL] && !data->set.uh) {
512
    /* we cannot do anything without URL */
513
0
    failf(data, "No URL set");
514
0
    return CURLE_URL_MALFORMAT;
515
0
  }
516
517
  /* CURLOPT_CURLU overrides CURLOPT_URL and the contents of the CURLU handle
518
     is allowed to be changed by the user between transfers */
519
0
  if(data->set.uh) {
520
0
    CURLUcode uc;
521
0
    free(data->set.str[STRING_SET_URL]);
522
0
    uc = curl_url_get(data->set.uh,
523
0
                      CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
524
0
    if(uc) {
525
0
      failf(data, "No URL set");
526
0
      return CURLE_URL_MALFORMAT;
527
0
    }
528
0
  }
529
530
  /* since the URL may have been redirected in a previous use of this handle */
531
0
  if(data->state.url_alloc) {
532
0
    Curl_safefree(data->state.url);
533
0
    data->state.url_alloc = FALSE;
534
0
  }
535
536
0
  data->state.url = data->set.str[STRING_SET_URL];
537
538
0
  if(data->set.postfields && data->set.set_resume_from) {
539
    /* we cannot */
540
0
    failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
541
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
542
0
  }
543
544
0
  data->state.prefer_ascii = data->set.prefer_ascii;
545
0
#ifdef CURL_LIST_ONLY_PROTOCOL
546
0
  data->state.list_only = data->set.list_only;
547
0
#endif
548
0
  data->state.httpreq = data->set.method;
549
550
0
  data->state.requests = 0;
551
0
  data->state.followlocation = 0; /* reset the location-follow counter */
552
0
  data->state.this_is_a_follow = FALSE; /* reset this */
553
0
  data->state.errorbuf = FALSE; /* no error has occurred */
554
0
#ifndef CURL_DISABLE_HTTP
555
0
  Curl_http_neg_init(data, &data->state.http_neg);
556
0
#endif
557
0
  data->state.authproblem = FALSE;
558
0
  data->state.authhost.want = data->set.httpauth;
559
0
  data->state.authproxy.want = data->set.proxyauth;
560
0
  Curl_safefree(data->info.wouldredirect);
561
0
  Curl_data_priority_clear_state(data);
562
563
0
  if(data->state.httpreq == HTTPREQ_PUT)
564
0
    data->state.infilesize = data->set.filesize;
565
0
  else if((data->state.httpreq != HTTPREQ_GET) &&
566
0
          (data->state.httpreq != HTTPREQ_HEAD)) {
567
0
    data->state.infilesize = data->set.postfieldsize;
568
0
    if(data->set.postfields && (data->state.infilesize == -1))
569
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
570
0
  }
571
0
  else
572
0
    data->state.infilesize = 0;
573
574
  /* If there is a list of cookie files to read, do it now! */
575
0
  Curl_cookie_loadfiles(data);
576
577
  /* If there is a list of host pairs to deal with */
578
0
  if(data->state.resolve)
579
0
    result = Curl_loadhostpairs(data);
580
581
  /* If there is a list of hsts files to read */
582
0
  Curl_hsts_loadfiles(data);
583
584
0
  if(!result) {
585
    /* Allow data->set.use_port to set which port to use. This needs to be
586
     * disabled for example when we follow Location: headers to URLs using
587
     * different ports! */
588
0
    data->state.allow_port = TRUE;
589
590
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
591
    /*************************************************************
592
     * Tell signal handler to ignore SIGPIPE
593
     *************************************************************/
594
    if(!data->set.no_signal)
595
      data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
596
#endif
597
598
0
    Curl_initinfo(data); /* reset session-specific information "variables" */
599
0
    Curl_pgrsResetTransferSizes(data);
600
0
    Curl_pgrsStartNow(data);
601
602
    /* In case the handle is reused and an authentication method was picked
603
       in the session we need to make sure we only use the one(s) we now
604
       consider to be fine */
605
0
    data->state.authhost.picked &= data->state.authhost.want;
606
0
    data->state.authproxy.picked &= data->state.authproxy.want;
607
608
0
#ifndef CURL_DISABLE_FTP
609
0
    data->state.wildcardmatch = data->set.wildcard_enabled;
610
0
    if(data->state.wildcardmatch) {
611
0
      struct WildcardData *wc;
612
0
      if(!data->wildcard) {
613
0
        data->wildcard = calloc(1, sizeof(struct WildcardData));
614
0
        if(!data->wildcard)
615
0
          return CURLE_OUT_OF_MEMORY;
616
0
      }
617
0
      wc = data->wildcard;
618
0
      if(wc->state < CURLWC_INIT) {
619
0
        if(wc->ftpwc)
620
0
          wc->dtor(wc->ftpwc);
621
0
        Curl_safefree(wc->pattern);
622
0
        Curl_safefree(wc->path);
623
0
        result = Curl_wildcard_init(wc); /* init wildcard structures */
624
0
        if(result)
625
0
          return CURLE_OUT_OF_MEMORY;
626
0
      }
627
0
    }
628
0
#endif
629
0
    result = Curl_hsts_loadcb(data, data->hsts);
630
0
  }
631
632
  /*
633
   * Set user-agent. Used for HTTP, but since we can attempt to tunnel
634
   * basically anything through an HTTP proxy we cannot limit this based on
635
   * protocol.
636
   */
637
0
  if(data->set.str[STRING_USERAGENT]) {
638
0
    free(data->state.aptr.uagent);
639
0
    data->state.aptr.uagent =
640
0
      aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
641
0
    if(!data->state.aptr.uagent)
642
0
      return CURLE_OUT_OF_MEMORY;
643
0
  }
644
645
0
  if(data->set.str[STRING_USERNAME] ||
646
0
     data->set.str[STRING_PASSWORD])
647
0
    data->state.creds_from = CREDS_OPTION;
648
0
  if(!result)
649
0
    result = Curl_setstropt(&data->state.aptr.user,
650
0
                            data->set.str[STRING_USERNAME]);
651
0
  if(!result)
652
0
    result = Curl_setstropt(&data->state.aptr.passwd,
653
0
                            data->set.str[STRING_PASSWORD]);
654
0
#ifndef CURL_DISABLE_PROXY
655
0
  if(!result)
656
0
    result = Curl_setstropt(&data->state.aptr.proxyuser,
657
0
                            data->set.str[STRING_PROXYUSERNAME]);
658
0
  if(!result)
659
0
    result = Curl_setstropt(&data->state.aptr.proxypasswd,
660
0
                            data->set.str[STRING_PROXYPASSWORD]);
661
0
#endif
662
663
0
  data->req.headerbytecount = 0;
664
0
  Curl_headers_cleanup(data);
665
0
  return result;
666
0
}
667
668
/* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
669
670
   NOTE: that the *url is malloc()ed. */
671
CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
672
0
{
673
0
  struct connectdata *conn = data->conn;
674
0
  bool retry = FALSE;
675
0
  *url = NULL;
676
677
  /* if we are talking upload, we cannot do the checks below, unless the
678
     protocol is HTTP as when uploading over HTTP we will still get a
679
     response */
680
0
  if(data->state.upload &&
681
0
     !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
682
0
    return CURLE_OK;
683
684
0
  if((data->req.bytecount + data->req.headerbytecount == 0) &&
685
0
     conn->bits.reuse &&
686
0
     (!data->req.no_body || (conn->handler->protocol & PROTO_FAMILY_HTTP))
687
0
#ifndef CURL_DISABLE_RTSP
688
0
     && (data->set.rtspreq != RTSPREQ_RECEIVE)
689
0
#endif
690
0
    )
691
    /* We got no data, we attempted to reuse a connection. For HTTP this
692
       can be a retry so we try again regardless if we expected a body.
693
       For other protocols we only try again only if we expected a body.
694
695
       This might happen if the connection was left alive when we were
696
       done using it before, but that was closed when we wanted to read from
697
       it again. Bad luck. Retry the same request on a fresh connect! */
698
0
    retry = TRUE;
699
0
  else if(data->state.refused_stream &&
700
0
          (data->req.bytecount + data->req.headerbytecount == 0) ) {
701
    /* This was sent on a refused stream, safe to rerun. A refused stream
702
       error can typically only happen on HTTP/2 level if the stream is safe
703
       to issue again, but the nghttp2 API can deliver the message to other
704
       streams as well, which is why this adds the check the data counters
705
       too. */
706
0
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
707
0
    data->state.refused_stream = FALSE; /* clear again */
708
0
    retry = TRUE;
709
0
  }
710
0
  if(retry) {
711
0
#define CONN_MAX_RETRIES 5
712
0
    if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
713
0
      failf(data, "Connection died, tried %d times before giving up",
714
0
            CONN_MAX_RETRIES);
715
0
      data->state.retrycount = 0;
716
0
      return CURLE_SEND_ERROR;
717
0
    }
718
0
    infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
719
0
          data->state.retrycount);
720
0
    *url = strdup(data->state.url);
721
0
    if(!*url)
722
0
      return CURLE_OUT_OF_MEMORY;
723
724
0
    connclose(conn, "retry"); /* close this connection */
725
0
    conn->bits.retry = TRUE; /* mark this as a connection we are about
726
                                to retry. Marking it this way should
727
                                prevent i.e HTTP transfers to return
728
                                error just because nothing has been
729
                                transferred! */
730
0
    Curl_creader_set_rewind(data, TRUE);
731
0
  }
732
0
  return CURLE_OK;
733
0
}
734
735
/*
736
 * xfer_setup() is called to setup basic properties for the transfer.
737
 */
738
static void xfer_setup(
739
  struct Curl_easy *data,   /* transfer */
740
  int sockindex,            /* socket index to read from or -1 */
741
  curl_off_t size,          /* -1 if unknown at this point */
742
  bool getheader,           /* TRUE if header parsing is wanted */
743
  int writesockindex,       /* socket index to write to, it may be the same we
744
                               read from. -1 disables */
745
  bool shutdown,            /* shutdown connection at transfer end. Only
746
                             * supported when sending OR receiving. */
747
  bool shutdown_err_ignore  /* errors during shutdown do not fail the
748
                             * transfer */
749
  )
750
0
{
751
0
  struct SingleRequest *k = &data->req;
752
0
  struct connectdata *conn = data->conn;
753
0
  bool want_send = Curl_req_want_send(data);
754
755
0
  DEBUGASSERT(conn != NULL);
756
0
  DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
757
0
  DEBUGASSERT((writesockindex <= 1) && (writesockindex >= -1));
758
0
  DEBUGASSERT(!shutdown || (sockindex == -1) || (writesockindex == -1));
759
760
0
  if(Curl_conn_is_multiplex(conn, FIRSTSOCKET) || want_send) {
761
    /* when multiplexing, the read/write sockets need to be the same! */
762
0
    conn->sockfd = sockindex == -1 ?
763
0
      ((writesockindex == -1 ? CURL_SOCKET_BAD : conn->sock[writesockindex])) :
764
0
      conn->sock[sockindex];
765
0
    conn->writesockfd = conn->sockfd;
766
0
    if(want_send)
767
      /* special and HTTP-specific */
768
0
      writesockindex = FIRSTSOCKET;
769
0
  }
770
0
  else {
771
0
    conn->sockfd = sockindex == -1 ?
772
0
      CURL_SOCKET_BAD : conn->sock[sockindex];
773
0
    conn->writesockfd = writesockindex == -1 ?
774
0
      CURL_SOCKET_BAD : conn->sock[writesockindex];
775
0
  }
776
777
0
  k->getheader = getheader;
778
0
  k->size = size;
779
0
  k->shutdown = shutdown;
780
0
  k->shutdown_err_ignore = shutdown_err_ignore;
781
782
  /* The code sequence below is placed in this function just because all
783
     necessary input is not always known in do_complete() as this function may
784
     be called after that */
785
786
0
  if(!k->getheader) {
787
0
    k->header = FALSE;
788
0
    if(size > 0)
789
0
      Curl_pgrsSetDownloadSize(data, size);
790
0
  }
791
  /* we want header and/or body, if neither then do not do this! */
792
0
  if(k->getheader || !data->req.no_body) {
793
794
0
    if(sockindex != -1)
795
0
      k->keepon |= KEEP_RECV;
796
797
0
    if(writesockindex != -1)
798
0
      k->keepon |= KEEP_SEND;
799
0
  } /* if(k->getheader || !data->req.no_body) */
800
801
0
}
802
803
void Curl_xfer_setup_nop(struct Curl_easy *data)
804
0
{
805
0
  xfer_setup(data, -1, -1, FALSE, -1, FALSE, FALSE);
806
0
}
807
808
void Curl_xfer_setup1(struct Curl_easy *data,
809
                      int send_recv,
810
                      curl_off_t recv_size,
811
                      bool getheader)
812
0
{
813
0
  int recv_index = (send_recv & CURL_XFER_RECV) ? FIRSTSOCKET : -1;
814
0
  int send_index = (send_recv & CURL_XFER_SEND) ? FIRSTSOCKET : -1;
815
0
  DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
816
0
  xfer_setup(data, recv_index, recv_size, getheader, send_index, FALSE, FALSE);
817
0
}
818
819
void Curl_xfer_setup2(struct Curl_easy *data,
820
                      int send_recv,
821
                      curl_off_t recv_size,
822
                      bool shutdown,
823
                      bool shutdown_err_ignore)
824
0
{
825
0
  int recv_index = (send_recv & CURL_XFER_RECV) ? SECONDARYSOCKET : -1;
826
0
  int send_index = (send_recv & CURL_XFER_SEND) ? SECONDARYSOCKET : -1;
827
0
  DEBUGASSERT((recv_index >= 0) || (recv_size == -1));
828
0
  xfer_setup(data, recv_index, recv_size, FALSE, send_index,
829
0
             shutdown, shutdown_err_ignore);
830
0
}
831
832
CURLcode Curl_xfer_write_resp(struct Curl_easy *data,
833
                              const char *buf, size_t blen,
834
                              bool is_eos)
835
0
{
836
0
  CURLcode result = CURLE_OK;
837
838
0
  if(data->conn->handler->write_resp) {
839
    /* protocol handlers offering this function take full responsibility
840
     * for writing all received download data to the client. */
841
0
    result = data->conn->handler->write_resp(data, buf, blen, is_eos);
842
0
  }
843
0
  else {
844
    /* No special handling by protocol handler, write all received data
845
     * as BODY to the client. */
846
0
    if(blen || is_eos) {
847
0
      int cwtype = CLIENTWRITE_BODY;
848
0
      if(is_eos)
849
0
        cwtype |= CLIENTWRITE_EOS;
850
0
      result = Curl_client_write(data, cwtype, buf, blen);
851
0
    }
852
0
  }
853
854
0
  if(!result && is_eos) {
855
    /* If we wrote the EOS, we are definitely done */
856
0
    data->req.eos_written = TRUE;
857
0
    data->req.download_done = TRUE;
858
0
  }
859
0
  CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d",
860
0
                 blen, is_eos, result);
861
0
  return result;
862
0
}
863
864
bool Curl_xfer_write_is_paused(struct Curl_easy *data)
865
0
{
866
0
  return Curl_cwriter_is_paused(data);
867
0
}
868
869
CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data,
870
                                 const char *hd0, size_t hdlen, bool is_eos)
871
0
{
872
0
  if(data->conn->handler->write_resp_hd) {
873
    /* protocol handlers offering this function take full responsibility
874
     * for writing all received download data to the client. */
875
0
    return data->conn->handler->write_resp_hd(data, hd0, hdlen, is_eos);
876
0
  }
877
  /* No special handling by protocol handler, write as response bytes */
878
0
  return Curl_xfer_write_resp(data, hd0, hdlen, is_eos);
879
0
}
880
881
CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature)
882
0
{
883
0
  (void)premature;
884
0
  return Curl_cw_out_done(data);
885
0
}
886
887
bool Curl_xfer_needs_flush(struct Curl_easy *data)
888
0
{
889
0
  int sockindex;
890
0
  sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
891
0
               (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
892
0
  return Curl_conn_needs_flush(data, sockindex);
893
0
}
894
895
CURLcode Curl_xfer_flush(struct Curl_easy *data)
896
0
{
897
0
  int sockindex;
898
0
  sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
899
0
               (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
900
0
  return Curl_conn_flush(data, sockindex);
901
0
}
902
903
CURLcode Curl_xfer_send(struct Curl_easy *data,
904
                        const void *buf, size_t blen, bool eos,
905
                        size_t *pnwritten)
906
0
{
907
0
  CURLcode result;
908
0
  int sockindex;
909
910
0
  DEBUGASSERT(data);
911
0
  DEBUGASSERT(data->conn);
912
913
0
  sockindex = ((data->conn->writesockfd != CURL_SOCKET_BAD) &&
914
0
               (data->conn->writesockfd == data->conn->sock[SECONDARYSOCKET]));
915
0
  result = Curl_conn_send(data, sockindex, buf, blen, eos, pnwritten);
916
0
  if(result == CURLE_AGAIN) {
917
0
    result = CURLE_OK;
918
0
    *pnwritten = 0;
919
0
  }
920
0
  else if(!result && *pnwritten)
921
0
    data->info.request_size += *pnwritten;
922
923
0
  DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu",
924
0
               blen, eos, result, *pnwritten));
925
0
  return result;
926
0
}
927
928
CURLcode Curl_xfer_recv(struct Curl_easy *data,
929
                        char *buf, size_t blen,
930
                        size_t *pnrcvd)
931
0
{
932
0
  int sockindex;
933
934
0
  DEBUGASSERT(data);
935
0
  DEBUGASSERT(data->conn);
936
0
  DEBUGASSERT(data->set.buffer_size > 0);
937
938
0
  sockindex = ((data->conn->sockfd != CURL_SOCKET_BAD) &&
939
0
               (data->conn->sockfd == data->conn->sock[SECONDARYSOCKET]));
940
0
  if((size_t)data->set.buffer_size < blen)
941
0
    blen = (size_t)data->set.buffer_size;
942
0
  return Curl_conn_recv(data, sockindex, buf, blen, pnrcvd);
943
0
}
944
945
CURLcode Curl_xfer_send_close(struct Curl_easy *data)
946
0
{
947
0
  Curl_conn_ev_data_done_send(data);
948
0
  return CURLE_OK;
949
0
}
950
951
bool Curl_xfer_is_blocked(struct Curl_easy *data)
952
0
{
953
0
  bool want_send = ((data)->req.keepon & KEEP_SEND);
954
0
  bool want_recv = ((data)->req.keepon & KEEP_RECV);
955
0
  if(!want_send)
956
0
    return want_recv && Curl_xfer_recv_is_paused(data);
957
0
  else if(!want_recv)
958
0
    return want_send && Curl_xfer_send_is_paused(data);
959
0
  else
960
0
    return Curl_xfer_recv_is_paused(data) && Curl_xfer_send_is_paused(data);
961
0
}
962
963
bool Curl_xfer_send_is_paused(struct Curl_easy *data)
964
0
{
965
0
  return (data->req.keepon & KEEP_SEND_PAUSE);
966
0
}
967
968
bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
969
0
{
970
0
  return (data->req.keepon & KEEP_RECV_PAUSE);
971
0
}
972
973
CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable)
974
0
{
975
0
  CURLcode result = CURLE_OK;
976
0
  if(enable) {
977
0
    data->req.keepon |= KEEP_SEND_PAUSE;
978
0
  }
979
0
  else {
980
0
    data->req.keepon &= ~KEEP_SEND_PAUSE;
981
0
    if(Curl_creader_is_paused(data))
982
0
      result = Curl_creader_unpause(data);
983
0
  }
984
0
  return result;
985
0
}
986
987
CURLcode Curl_xfer_pause_recv(struct Curl_easy *data, bool enable)
988
0
{
989
0
  CURLcode result = CURLE_OK;
990
0
  if(enable) {
991
0
    data->req.keepon |= KEEP_RECV_PAUSE;
992
0
  }
993
0
  else {
994
0
    data->req.keepon &= ~KEEP_RECV_PAUSE;
995
0
    if(Curl_cwriter_is_paused(data))
996
0
      result = Curl_cwriter_unpause(data);
997
0
  }
998
0
  Curl_conn_ev_data_pause(data, enable);
999
0
  return result;
1000
0
}