Coverage Report

Created: 2024-05-04 12:45

/proc/self/cwd/external/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
#include "strtoofft.h"
27
28
#ifdef HAVE_NETINET_IN_H
29
#include <netinet/in.h>
30
#endif
31
#ifdef HAVE_NETDB_H
32
#include <netdb.h>
33
#endif
34
#ifdef HAVE_ARPA_INET_H
35
#include <arpa/inet.h>
36
#endif
37
#ifdef HAVE_NET_IF_H
38
#include <net/if.h>
39
#endif
40
#ifdef HAVE_SYS_IOCTL_H
41
#include <sys/ioctl.h>
42
#endif
43
#include <signal.h>
44
45
#ifdef HAVE_SYS_PARAM_H
46
#include <sys/param.h>
47
#endif
48
49
#ifdef HAVE_SYS_SELECT_H
50
#include <sys/select.h>
51
#elif defined(HAVE_UNISTD_H)
52
#include <unistd.h>
53
#endif
54
55
#ifndef HAVE_SOCKET
56
#error "We can't compile without socket() support!"
57
#endif
58
59
#include "urldata.h"
60
#include <curl/curl.h>
61
#include "netrc.h"
62
63
#include "content_encoding.h"
64
#include "hostip.h"
65
#include "cfilters.h"
66
#include "transfer.h"
67
#include "sendf.h"
68
#include "speedcheck.h"
69
#include "progress.h"
70
#include "http.h"
71
#include "url.h"
72
#include "getinfo.h"
73
#include "vtls/vtls.h"
74
#include "vquic/vquic.h"
75
#include "select.h"
76
#include "multiif.h"
77
#include "connect.h"
78
#include "http2.h"
79
#include "mime.h"
80
#include "strcase.h"
81
#include "urlapi-int.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(strncasecompare(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
CURLcode Curl_get_upload_buffer(struct Curl_easy *data)
118
0
{
119
0
  if(!data->state.ulbuf) {
120
0
    data->state.ulbuf = malloc(data->set.upload_buffer_size);
121
0
    if(!data->state.ulbuf)
122
0
      return CURLE_OUT_OF_MEMORY;
123
0
  }
124
0
  return CURLE_OK;
125
0
}
126
127
#ifndef CURL_DISABLE_HTTP
128
/*
129
 * This function will be called to loop through the trailers buffer
130
 * until no more data is available for sending.
131
 */
132
static size_t trailers_read(char *buffer, size_t size, size_t nitems,
133
                            void *raw)
134
0
{
135
0
  struct Curl_easy *data = (struct Curl_easy *)raw;
136
0
  struct dynbuf *trailers_buf = &data->state.trailers_buf;
137
0
  size_t bytes_left = Curl_dyn_len(trailers_buf) -
138
0
    data->state.trailers_bytes_sent;
139
0
  size_t to_copy = (size*nitems < bytes_left) ? size*nitems : bytes_left;
140
0
  if(to_copy) {
141
0
    memcpy(buffer,
142
0
           Curl_dyn_ptr(trailers_buf) + data->state.trailers_bytes_sent,
143
0
           to_copy);
144
0
    data->state.trailers_bytes_sent += to_copy;
145
0
  }
146
0
  return to_copy;
147
0
}
148
149
static size_t trailers_left(void *raw)
150
0
{
151
0
  struct Curl_easy *data = (struct Curl_easy *)raw;
152
0
  struct dynbuf *trailers_buf = &data->state.trailers_buf;
153
0
  return Curl_dyn_len(trailers_buf) - data->state.trailers_bytes_sent;
154
0
}
155
#endif
156
157
/*
158
 * This function will call the read callback to fill our buffer with data
159
 * to upload.
160
 */
161
CURLcode Curl_fillreadbuffer(struct Curl_easy *data, size_t bytes,
162
                             size_t *nreadp)
163
0
{
164
0
  size_t buffersize = bytes;
165
0
  size_t nread;
166
167
0
  curl_read_callback readfunc = NULL;
168
0
  void *extra_data = NULL;
169
170
0
#ifndef CURL_DISABLE_HTTP
171
0
  if(data->state.trailers_state == TRAILERS_INITIALIZED) {
172
0
    struct curl_slist *trailers = NULL;
173
0
    CURLcode result;
174
0
    int trailers_ret_code;
175
176
    /* at this point we already verified that the callback exists
177
       so we compile and store the trailers buffer, then proceed */
178
0
    infof(data,
179
0
          "Moving trailers state machine from initialized to sending.");
180
0
    data->state.trailers_state = TRAILERS_SENDING;
181
0
    Curl_dyn_init(&data->state.trailers_buf, DYN_TRAILERS);
182
183
0
    data->state.trailers_bytes_sent = 0;
184
0
    Curl_set_in_callback(data, true);
185
0
    trailers_ret_code = data->set.trailer_callback(&trailers,
186
0
                                                   data->set.trailer_data);
187
0
    Curl_set_in_callback(data, false);
188
0
    if(trailers_ret_code == CURL_TRAILERFUNC_OK) {
189
0
      result = Curl_http_compile_trailers(trailers, &data->state.trailers_buf,
190
0
                                          data);
191
0
    }
192
0
    else {
193
0
      failf(data, "operation aborted by trailing headers callback");
194
0
      *nreadp = 0;
195
0
      result = CURLE_ABORTED_BY_CALLBACK;
196
0
    }
197
0
    if(result) {
198
0
      Curl_dyn_free(&data->state.trailers_buf);
199
0
      curl_slist_free_all(trailers);
200
0
      return result;
201
0
    }
202
0
    infof(data, "Successfully compiled trailers.");
203
0
    curl_slist_free_all(trailers);
204
0
  }
205
0
#endif
206
207
0
#ifndef CURL_DISABLE_HTTP
208
  /* if we are transmitting trailing data, we don't need to write
209
     a chunk size so we skip this */
210
0
  if(data->req.upload_chunky &&
211
0
     data->state.trailers_state == TRAILERS_NONE) {
212
    /* if chunked Transfer-Encoding */
213
0
    buffersize -= (8 + 2 + 2);   /* 32bit hex + CRLF + CRLF */
214
0
    data->req.upload_fromhere += (8 + 2); /* 32bit hex + CRLF */
215
0
  }
216
217
0
  if(data->state.trailers_state == TRAILERS_SENDING) {
218
    /* if we're here then that means that we already sent the last empty chunk
219
       but we didn't send a final CR LF, so we sent 0 CR LF. We then start
220
       pulling trailing data until we have no more at which point we
221
       simply return to the previous point in the state machine as if
222
       nothing happened.
223
       */
224
0
    readfunc = trailers_read;
225
0
    extra_data = (void *)data;
226
0
  }
227
0
  else
228
0
#endif
229
0
  {
230
0
    readfunc = data->state.fread_func;
231
0
    extra_data = data->state.in;
232
0
  }
233
234
0
  Curl_set_in_callback(data, true);
235
0
  nread = readfunc(data->req.upload_fromhere, 1,
236
0
                   buffersize, extra_data);
237
0
  Curl_set_in_callback(data, false);
238
239
0
  if(nread == CURL_READFUNC_ABORT) {
240
0
    failf(data, "operation aborted by callback");
241
0
    *nreadp = 0;
242
0
    return CURLE_ABORTED_BY_CALLBACK;
243
0
  }
244
0
  if(nread == CURL_READFUNC_PAUSE) {
245
0
    struct SingleRequest *k = &data->req;
246
247
0
    if(data->conn->handler->flags & PROTOPT_NONETWORK) {
248
      /* protocols that work without network cannot be paused. This is
249
         actually only FILE:// just now, and it can't pause since the transfer
250
         isn't done using the "normal" procedure. */
251
0
      failf(data, "Read callback asked for PAUSE when not supported");
252
0
      return CURLE_READ_ERROR;
253
0
    }
254
255
    /* CURL_READFUNC_PAUSE pauses read callbacks that feed socket writes */
256
0
    k->keepon |= KEEP_SEND_PAUSE; /* mark socket send as paused */
257
0
    if(data->req.upload_chunky) {
258
        /* Back out the preallocation done above */
259
0
      data->req.upload_fromhere -= (8 + 2);
260
0
    }
261
0
    *nreadp = 0;
262
263
0
    return CURLE_OK; /* nothing was read */
264
0
  }
265
0
  else if(nread > buffersize) {
266
    /* the read function returned a too large value */
267
0
    *nreadp = 0;
268
0
    failf(data, "read function returned funny value");
269
0
    return CURLE_READ_ERROR;
270
0
  }
271
272
0
#ifndef CURL_DISABLE_HTTP
273
0
  if(!data->req.forbidchunk && data->req.upload_chunky) {
274
    /* if chunked Transfer-Encoding
275
     *    build chunk:
276
     *
277
     *        <HEX SIZE> CRLF
278
     *        <DATA> CRLF
279
     */
280
    /* On non-ASCII platforms the <DATA> may or may not be
281
       translated based on state.prefer_ascii while the protocol
282
       portion must always be translated to the network encoding.
283
       To further complicate matters, line end conversion might be
284
       done later on, so we need to prevent CRLFs from becoming
285
       CRCRLFs if that's the case.  To do this we use bare LFs
286
       here, knowing they'll become CRLFs later on.
287
     */
288
289
0
    bool added_crlf = FALSE;
290
0
    int hexlen = 0;
291
0
    const char *endofline_native;
292
0
    const char *endofline_network;
293
294
0
    if(
295
0
#ifdef CURL_DO_LINEEND_CONV
296
0
       (data->state.prefer_ascii) ||
297
0
#endif
298
0
       (data->set.crlf)) {
299
      /* \n will become \r\n later on */
300
0
      endofline_native  = "\n";
301
0
      endofline_network = "\x0a";
302
0
    }
303
0
    else {
304
0
      endofline_native  = "\r\n";
305
0
      endofline_network = "\x0d\x0a";
306
0
    }
307
308
    /* if we're not handling trailing data, proceed as usual */
309
0
    if(data->state.trailers_state != TRAILERS_SENDING) {
310
0
      char hexbuffer[11] = "";
311
0
      hexlen = msnprintf(hexbuffer, sizeof(hexbuffer),
312
0
                         "%zx%s", nread, endofline_native);
313
314
      /* move buffer pointer */
315
0
      data->req.upload_fromhere -= hexlen;
316
0
      nread += hexlen;
317
318
      /* copy the prefix to the buffer, leaving out the NUL */
319
0
      memcpy(data->req.upload_fromhere, hexbuffer, hexlen);
320
321
      /* always append ASCII CRLF to the data unless
322
         we have a valid trailer callback */
323
0
      if((nread-hexlen) == 0 &&
324
0
          data->set.trailer_callback != NULL &&
325
0
          data->state.trailers_state == TRAILERS_NONE) {
326
0
        data->state.trailers_state = TRAILERS_INITIALIZED;
327
0
      }
328
0
      else {
329
0
        memcpy(data->req.upload_fromhere + nread,
330
0
               endofline_network,
331
0
               strlen(endofline_network));
332
0
        added_crlf = TRUE;
333
0
      }
334
0
    }
335
336
0
    if(data->state.trailers_state == TRAILERS_SENDING &&
337
0
       !trailers_left(data)) {
338
0
      Curl_dyn_free(&data->state.trailers_buf);
339
0
      data->state.trailers_state = TRAILERS_DONE;
340
0
      data->set.trailer_data = NULL;
341
0
      data->set.trailer_callback = NULL;
342
      /* mark the transfer as done */
343
0
      data->req.upload_done = TRUE;
344
0
      infof(data, "Signaling end of chunked upload after trailers.");
345
0
    }
346
0
    else
347
0
      if((nread - hexlen) == 0 &&
348
0
         data->state.trailers_state != TRAILERS_INITIALIZED) {
349
        /* mark this as done once this chunk is transferred */
350
0
        data->req.upload_done = TRUE;
351
0
        infof(data,
352
0
              "Signaling end of chunked upload via terminating chunk.");
353
0
      }
354
355
0
    if(added_crlf)
356
0
      nread += strlen(endofline_network); /* for the added end of line */
357
0
  }
358
0
#endif
359
360
0
  *nreadp = nread;
361
362
0
  return CURLE_OK;
363
0
}
364
365
static int data_pending(struct Curl_easy *data)
366
0
{
367
0
  struct connectdata *conn = data->conn;
368
369
0
  if(conn->handler->protocol&PROTO_FAMILY_FTP)
370
0
    return Curl_conn_data_pending(data, SECONDARYSOCKET);
371
372
  /* in the case of libssh2, we can never be really sure that we have emptied
373
     its internal buffers so we MUST always try until we get EAGAIN back */
374
0
  return conn->handler->protocol&(CURLPROTO_SCP|CURLPROTO_SFTP) ||
375
0
    Curl_conn_data_pending(data, FIRSTSOCKET);
376
0
}
377
378
/*
379
 * Check to see if CURLOPT_TIMECONDITION was met by comparing the time of the
380
 * remote document with the time provided by CURLOPT_TIMEVAL
381
 */
382
bool Curl_meets_timecondition(struct Curl_easy *data, time_t timeofdoc)
383
0
{
384
0
  if((timeofdoc == 0) || (data->set.timevalue == 0))
385
0
    return TRUE;
386
387
0
  switch(data->set.timecondition) {
388
0
  case CURL_TIMECOND_IFMODSINCE:
389
0
  default:
390
0
    if(timeofdoc <= data->set.timevalue) {
391
0
      infof(data,
392
0
            "The requested document is not new enough");
393
0
      data->info.timecond = TRUE;
394
0
      return FALSE;
395
0
    }
396
0
    break;
397
0
  case CURL_TIMECOND_IFUNMODSINCE:
398
0
    if(timeofdoc >= data->set.timevalue) {
399
0
      infof(data,
400
0
            "The requested document is not old enough");
401
0
      data->info.timecond = TRUE;
402
0
      return FALSE;
403
0
    }
404
0
    break;
405
0
  }
406
407
0
  return TRUE;
408
0
}
409
410
/*
411
 * Go ahead and do a read if we have a readable socket or if
412
 * the stream was rewound (in which case we have data in a
413
 * buffer)
414
 *
415
 * return '*comeback' TRUE if we didn't properly drain the socket so this
416
 * function should get called again without select() or similar in between!
417
 */
418
static CURLcode readwrite_data(struct Curl_easy *data,
419
                               struct connectdata *conn,
420
                               struct SingleRequest *k,
421
                               int *didwhat, bool *done,
422
                               bool *comeback)
423
0
{
424
0
  CURLcode result = CURLE_OK;
425
0
  ssize_t nread; /* number of bytes read */
426
0
  size_t excess = 0; /* excess bytes read */
427
0
  bool readmore = FALSE; /* used by RTP to signal for more data */
428
0
  int maxloops = 100;
429
0
  curl_off_t max_recv = data->set.max_recv_speed?
430
0
                        data->set.max_recv_speed : CURL_OFF_T_MAX;
431
0
  char *buf = data->state.buffer;
432
0
  bool data_eof_handled = FALSE;
433
0
  DEBUGASSERT(buf);
434
435
0
  *done = FALSE;
436
0
  *comeback = FALSE;
437
438
  /* This is where we loop until we have read everything there is to
439
     read or we get a CURLE_AGAIN */
440
0
  do {
441
0
    bool is_empty_data = FALSE;
442
0
    size_t buffersize = data->set.buffer_size;
443
0
    size_t bytestoread = buffersize;
444
    /* For HTTP/2 and HTTP/3, read data without caring about the content
445
       length. This is safe because body in HTTP/2 is always segmented
446
       thanks to its framing layer. Meanwhile, we have to call Curl_read
447
       to ensure that http2_handle_stream_close is called when we read all
448
       incoming bytes for a particular stream. */
449
0
    bool is_http3 = Curl_conn_is_http3(data, conn, FIRSTSOCKET);
450
0
    data_eof_handled = is_http3 || Curl_conn_is_http2(data, conn, FIRSTSOCKET);
451
452
0
    if(!data_eof_handled && k->size != -1 && !k->header) {
453
      /* make sure we don't read too much */
454
0
      curl_off_t totalleft = k->size - k->bytecount;
455
0
      if(totalleft < (curl_off_t)bytestoread)
456
0
        bytestoread = (size_t)totalleft;
457
0
    }
458
459
0
    if(bytestoread) {
460
      /* receive data from the network! */
461
0
      result = Curl_read(data, conn->sockfd, buf, bytestoread, &nread);
462
463
      /* read would've blocked */
464
0
      if(CURLE_AGAIN == result) {
465
0
        result = CURLE_OK;
466
0
        break; /* get out of loop */
467
0
      }
468
469
0
      if(result>0)
470
0
        goto out;
471
0
    }
472
0
    else {
473
      /* read nothing but since we wanted nothing we consider this an OK
474
         situation to proceed from */
475
0
      DEBUGF(infof(data, "readwrite_data: we're done"));
476
0
      nread = 0;
477
0
    }
478
479
0
    if(!k->bytecount) {
480
0
      Curl_pgrsTime(data, TIMER_STARTTRANSFER);
481
0
      if(k->exp100 > EXP100_SEND_DATA)
482
        /* set time stamp to compare with when waiting for the 100 */
483
0
        k->start100 = Curl_now();
484
0
    }
485
486
0
    *didwhat |= KEEP_RECV;
487
    /* indicates data of zero size, i.e. empty file */
488
0
    is_empty_data = ((nread == 0) && (k->bodywrites == 0)) ? TRUE : FALSE;
489
490
0
    if(0 < nread || is_empty_data) {
491
0
      buf[nread] = 0;
492
0
    }
493
0
    if(!nread) {
494
      /* if we receive 0 or less here, either the data transfer is done or the
495
         server closed the connection and we bail out from this! */
496
0
      if(data_eof_handled)
497
0
        DEBUGF(infof(data, "nread == 0, stream closed, bailing"));
498
0
      else
499
0
        DEBUGF(infof(data, "nread <= 0, server closed connection, bailing"));
500
0
      k->keepon = 0; /* stop sending as well */
501
0
      if(!is_empty_data)
502
0
        break;
503
0
    }
504
505
    /* Default buffer to use when we write the buffer, it may be changed
506
       in the flow below before the actual storing is done. */
507
0
    k->str = buf;
508
509
0
    if(conn->handler->readwrite) {
510
0
      result = conn->handler->readwrite(data, conn, &nread, &readmore);
511
0
      if(result)
512
0
        goto out;
513
0
      if(readmore)
514
0
        break;
515
0
    }
516
517
0
#ifndef CURL_DISABLE_HTTP
518
    /* Since this is a two-state thing, we check if we are parsing
519
       headers at the moment or not. */
520
0
    if(k->header) {
521
      /* we are in parse-the-header-mode */
522
0
      bool stop_reading = FALSE;
523
0
      result = Curl_http_readwrite_headers(data, conn, &nread, &stop_reading);
524
0
      if(result)
525
0
        goto out;
526
527
0
      if(conn->handler->readwrite &&
528
0
         (k->maxdownload <= 0 && nread > 0)) {
529
0
        result = conn->handler->readwrite(data, conn, &nread, &readmore);
530
0
        if(result)
531
0
          goto out;
532
0
        if(readmore)
533
0
          break;
534
0
      }
535
536
0
      if(stop_reading) {
537
        /* We've stopped dealing with input, get out of the do-while loop */
538
539
0
        if(nread > 0) {
540
0
          infof(data,
541
0
                "Excess found:"
542
0
                " excess = %zd"
543
0
                " url = %s (zero-length body)",
544
0
                nread, data->state.up.path);
545
0
        }
546
547
0
        break;
548
0
      }
549
0
    }
550
0
#endif /* CURL_DISABLE_HTTP */
551
552
553
    /* This is not an 'else if' since it may be a rest from the header
554
       parsing, where the beginning of the buffer is headers and the end
555
       is non-headers. */
556
0
    if(!k->header && (nread > 0 || is_empty_data)) {
557
558
0
      if(data->req.no_body) {
559
        /* data arrives although we want none, bail out */
560
0
        streamclose(conn, "ignoring body");
561
0
        *done = TRUE;
562
0
        result = CURLE_WEIRD_SERVER_REPLY;
563
0
        goto out;
564
0
      }
565
566
0
#ifndef CURL_DISABLE_HTTP
567
0
      if(0 == k->bodywrites && !is_empty_data) {
568
        /* These checks are only made the first time we are about to
569
           write a piece of the body */
570
0
        if(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)) {
571
          /* HTTP-only checks */
572
0
          result = Curl_http_firstwrite(data, conn, done);
573
0
          if(result || *done)
574
0
            goto out;
575
0
        }
576
0
      } /* this is the first time we write a body part */
577
0
#endif /* CURL_DISABLE_HTTP */
578
579
0
      k->bodywrites++;
580
581
      /* pass data to the debug function before it gets "dechunked" */
582
0
      if(data->set.verbose) {
583
0
        if(k->badheader) {
584
0
          Curl_debug(data, CURLINFO_DATA_IN,
585
0
                     Curl_dyn_ptr(&data->state.headerb),
586
0
                     Curl_dyn_len(&data->state.headerb));
587
0
          if(k->badheader == HEADER_PARTHEADER)
588
0
            Curl_debug(data, CURLINFO_DATA_IN,
589
0
                       k->str, (size_t)nread);
590
0
        }
591
0
        else
592
0
          Curl_debug(data, CURLINFO_DATA_IN,
593
0
                     k->str, (size_t)nread);
594
0
      }
595
596
0
#ifndef CURL_DISABLE_HTTP
597
0
      if(k->chunk) {
598
        /*
599
         * Here comes a chunked transfer flying and we need to decode this
600
         * properly.  While the name says read, this function both reads
601
         * and writes away the data. The returned 'nread' holds the number
602
         * of actual data it wrote to the client.
603
         */
604
0
        CURLcode extra;
605
0
        CHUNKcode res =
606
0
          Curl_httpchunk_read(data, k->str, nread, &nread, &extra);
607
608
0
        if(CHUNKE_OK < res) {
609
0
          if(CHUNKE_PASSTHRU_ERROR == res) {
610
0
            failf(data, "Failed reading the chunked-encoded stream");
611
0
            result = extra;
612
0
            goto out;
613
0
          }
614
0
          failf(data, "%s in chunked-encoding", Curl_chunked_strerror(res));
615
0
          result = CURLE_RECV_ERROR;
616
0
          goto out;
617
0
        }
618
0
        if(CHUNKE_STOP == res) {
619
          /* we're done reading chunks! */
620
0
          k->keepon &= ~KEEP_RECV; /* read no more */
621
622
          /* N number of bytes at the end of the str buffer that weren't
623
             written to the client. */
624
0
          if(conn->chunk.datasize) {
625
0
            infof(data, "Leftovers after chunking: % "
626
0
                  CURL_FORMAT_CURL_OFF_T "u bytes",
627
0
                  conn->chunk.datasize);
628
0
          }
629
0
        }
630
        /* If it returned OK, we just keep going */
631
0
      }
632
0
#endif   /* CURL_DISABLE_HTTP */
633
634
      /* Account for body content stored in the header buffer */
635
0
      if((k->badheader == HEADER_PARTHEADER) && !k->ignorebody) {
636
0
        size_t headlen = Curl_dyn_len(&data->state.headerb);
637
0
        DEBUGF(infof(data, "Increasing bytecount by %zu", headlen));
638
0
        k->bytecount += headlen;
639
0
      }
640
641
0
      if((-1 != k->maxdownload) &&
642
0
         (k->bytecount + nread >= k->maxdownload)) {
643
644
0
        excess = (size_t)(k->bytecount + nread - k->maxdownload);
645
0
        if(excess > 0 && !k->ignorebody) {
646
0
          infof(data,
647
0
                "Excess found in a read:"
648
0
                " excess = %zu"
649
0
                ", size = %" CURL_FORMAT_CURL_OFF_T
650
0
                ", maxdownload = %" CURL_FORMAT_CURL_OFF_T
651
0
                ", bytecount = %" CURL_FORMAT_CURL_OFF_T,
652
0
                excess, k->size, k->maxdownload, k->bytecount);
653
0
          connclose(conn, "excess found in a read");
654
0
        }
655
656
0
        nread = (ssize_t) (k->maxdownload - k->bytecount);
657
0
        if(nread < 0) /* this should be unusual */
658
0
          nread = 0;
659
660
        /* HTTP/3 over QUIC should keep reading until QUIC connection
661
           is closed.  In contrast to HTTP/2 which can stop reading
662
           from TCP connection, HTTP/3 over QUIC needs ACK from server
663
           to ensure stream closure.  It should keep reading. */
664
0
        if(!is_http3) {
665
0
          k->keepon &= ~KEEP_RECV; /* we're done reading */
666
0
        }
667
0
      }
668
669
0
      k->bytecount += nread;
670
0
      max_recv -= nread;
671
672
0
      result = Curl_pgrsSetDownloadCounter(data, k->bytecount);
673
0
      if(result)
674
0
        goto out;
675
676
0
      if(!k->chunk && (nread || k->badheader || is_empty_data)) {
677
        /* If this is chunky transfer, it was already written */
678
679
0
        if(k->badheader && !k->ignorebody) {
680
          /* we parsed a piece of data wrongly assuming it was a header
681
             and now we output it as body instead */
682
0
          size_t headlen = Curl_dyn_len(&data->state.headerb);
683
684
          /* Don't let excess data pollute body writes */
685
0
          if(k->maxdownload == -1 || (curl_off_t)headlen <= k->maxdownload)
686
0
            result = Curl_client_write(data, CLIENTWRITE_BODY,
687
0
                                       Curl_dyn_ptr(&data->state.headerb),
688
0
                                       headlen);
689
0
          else
690
0
            result = Curl_client_write(data, CLIENTWRITE_BODY,
691
0
                                       Curl_dyn_ptr(&data->state.headerb),
692
0
                                       (size_t)k->maxdownload);
693
694
0
          if(result)
695
0
            goto out;
696
0
        }
697
0
        if(k->badheader < HEADER_ALLBAD) {
698
          /* This switch handles various content encodings. If there's an
699
             error here, be sure to check over the almost identical code
700
             in http_chunks.c.
701
             Make sure that ALL_CONTENT_ENCODINGS contains all the
702
             encodings handled here. */
703
0
          if(!k->ignorebody && nread) {
704
#ifndef CURL_DISABLE_POP3
705
            if(conn->handler->protocol & PROTO_FAMILY_POP3)
706
              result = Curl_pop3_write(data, k->str, nread);
707
            else
708
#endif /* CURL_DISABLE_POP3 */
709
0
              result = Curl_client_write(data, CLIENTWRITE_BODY, k->str,
710
0
                                         nread);
711
0
          }
712
0
        }
713
0
        k->badheader = HEADER_NORMAL; /* taken care of now */
714
715
0
        if(result)
716
0
          goto out;
717
0
      }
718
719
0
    } /* if(!header and data to read) */
720
721
0
    if(conn->handler->readwrite && excess) {
722
      /* Parse the excess data */
723
0
      k->str += nread;
724
725
0
      if(&k->str[excess] > &buf[data->set.buffer_size]) {
726
        /* the excess amount was too excessive(!), make sure
727
           it doesn't read out of buffer */
728
0
        excess = &buf[data->set.buffer_size] - k->str;
729
0
      }
730
0
      nread = (ssize_t)excess;
731
732
0
      result = conn->handler->readwrite(data, conn, &nread, &readmore);
733
0
      if(result)
734
0
        goto out;
735
736
0
      if(readmore)
737
0
        k->keepon |= KEEP_RECV; /* we're not done reading */
738
0
      break;
739
0
    }
740
741
0
    if(is_empty_data) {
742
      /* if we received nothing, the server closed the connection and we
743
         are done */
744
0
      k->keepon &= ~KEEP_RECV;
745
0
    }
746
747
0
    if((k->keepon & KEEP_RECV_PAUSE) || !(k->keepon & KEEP_RECV)) {
748
      /* this is a paused or stopped transfer */
749
0
      break;
750
0
    }
751
752
0
  } while((max_recv > 0) && data_pending(data) && maxloops--);
753
754
0
  if(maxloops <= 0 || max_recv <= 0) {
755
    /* we mark it as read-again-please */
756
0
    data->state.dselect_bits = CURL_CSELECT_IN;
757
0
    *comeback = TRUE;
758
0
  }
759
760
0
  if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) &&
761
0
     (conn->bits.close || data_eof_handled)) {
762
    /* When we've read the entire thing and the close bit is set, the server
763
       may now close the connection. If there's now any kind of sending going
764
       on from our side, we need to stop that immediately. */
765
0
    infof(data, "we are done reading and this is set to close, stop send");
766
0
    k->keepon &= ~KEEP_SEND; /* no writing anymore either */
767
0
  }
768
769
0
out:
770
0
  if(result)
771
0
    DEBUGF(infof(data, "readwrite_data() -> %d", result));
772
0
  return result;
773
0
}
774
775
CURLcode Curl_done_sending(struct Curl_easy *data,
776
                           struct SingleRequest *k)
777
0
{
778
0
  k->keepon &= ~KEEP_SEND; /* we're done writing */
779
780
  /* These functions should be moved into the handler struct! */
781
0
  Curl_conn_ev_data_done_send(data);
782
783
0
  return CURLE_OK;
784
0
}
785
786
#if defined(WIN32) && defined(USE_WINSOCK)
787
#ifndef SIO_IDEAL_SEND_BACKLOG_QUERY
788
#define SIO_IDEAL_SEND_BACKLOG_QUERY 0x4004747B
789
#endif
790
791
static void win_update_buffer_size(curl_socket_t sockfd)
792
{
793
  int result;
794
  ULONG ideal;
795
  DWORD ideallen;
796
  result = WSAIoctl(sockfd, SIO_IDEAL_SEND_BACKLOG_QUERY, 0, 0,
797
                    &ideal, sizeof(ideal), &ideallen, 0, 0);
798
  if(result == 0) {
799
    setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF,
800
               (const char *)&ideal, sizeof(ideal));
801
  }
802
}
803
#else
804
#define win_update_buffer_size(x)
805
#endif
806
807
#define curl_upload_refill_watermark(data) \
808
0
        ((ssize_t)((data)->set.upload_buffer_size >> 5))
809
810
/*
811
 * Send data to upload to the server, when the socket is writable.
812
 */
813
static CURLcode readwrite_upload(struct Curl_easy *data,
814
                                 struct connectdata *conn,
815
                                 int *didwhat)
816
0
{
817
0
  ssize_t i, si;
818
0
  ssize_t bytes_written;
819
0
  CURLcode result;
820
0
  ssize_t nread; /* number of bytes read */
821
0
  bool sending_http_headers = FALSE;
822
0
  struct SingleRequest *k = &data->req;
823
824
0
  *didwhat |= KEEP_SEND;
825
826
0
  do {
827
0
    curl_off_t nbody;
828
0
    ssize_t offset = 0;
829
830
0
    if(0 != k->upload_present &&
831
0
       k->upload_present < curl_upload_refill_watermark(data) &&
832
0
       !k->upload_chunky &&/*(variable sized chunked header; append not safe)*/
833
0
       !k->upload_done &&  /*!(k->upload_done once k->upload_present sent)*/
834
0
       !(k->writebytecount + k->upload_present - k->pendingheader ==
835
0
         data->state.infilesize)) {
836
0
      offset = k->upload_present;
837
0
    }
838
839
    /* only read more data if there's no upload data already
840
       present in the upload buffer, or if appending to upload buffer */
841
0
    if(0 == k->upload_present || offset) {
842
0
      result = Curl_get_upload_buffer(data);
843
0
      if(result)
844
0
        return result;
845
0
      if(offset && k->upload_fromhere != data->state.ulbuf)
846
0
        memmove(data->state.ulbuf, k->upload_fromhere, offset);
847
      /* init the "upload from here" pointer */
848
0
      k->upload_fromhere = data->state.ulbuf;
849
850
0
      if(!k->upload_done) {
851
        /* HTTP pollution, this should be written nicer to become more
852
           protocol agnostic. */
853
0
        size_t fillcount;
854
0
        struct HTTP *http = k->p.http;
855
856
0
        if((k->exp100 == EXP100_SENDING_REQUEST) &&
857
0
           (http->sending == HTTPSEND_BODY)) {
858
          /* If this call is to send body data, we must take some action:
859
             We have sent off the full HTTP 1.1 request, and we shall now
860
             go into the Expect: 100 state and await such a header */
861
0
          k->exp100 = EXP100_AWAITING_CONTINUE; /* wait for the header */
862
0
          k->keepon &= ~KEEP_SEND;         /* disable writing */
863
0
          k->start100 = Curl_now();       /* timeout count starts now */
864
0
          *didwhat &= ~KEEP_SEND;  /* we didn't write anything actually */
865
          /* set a timeout for the multi interface */
866
0
          Curl_expire(data, data->set.expect_100_timeout, EXPIRE_100_TIMEOUT);
867
0
          break;
868
0
        }
869
870
0
        if(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)) {
871
0
          if(http->sending == HTTPSEND_REQUEST)
872
            /* We're sending the HTTP request headers, not the data.
873
               Remember that so we don't change the line endings. */
874
0
            sending_http_headers = TRUE;
875
0
          else
876
0
            sending_http_headers = FALSE;
877
0
        }
878
879
0
        k->upload_fromhere += offset;
880
0
        result = Curl_fillreadbuffer(data, data->set.upload_buffer_size-offset,
881
0
                                     &fillcount);
882
0
        k->upload_fromhere -= offset;
883
0
        if(result)
884
0
          return result;
885
886
0
        nread = offset + fillcount;
887
0
      }
888
0
      else
889
0
        nread = 0; /* we're done uploading/reading */
890
891
0
      if(!nread && (k->keepon & KEEP_SEND_PAUSE)) {
892
        /* this is a paused transfer */
893
0
        break;
894
0
      }
895
0
      if(nread <= 0) {
896
0
        result = Curl_done_sending(data, k);
897
0
        if(result)
898
0
          return result;
899
0
        break;
900
0
      }
901
902
      /* store number of bytes available for upload */
903
0
      k->upload_present = nread;
904
905
      /* convert LF to CRLF if so asked */
906
0
      if((!sending_http_headers) && (
907
0
#ifdef CURL_DO_LINEEND_CONV
908
         /* always convert if we're FTPing in ASCII mode */
909
0
         (data->state.prefer_ascii) ||
910
0
#endif
911
0
         (data->set.crlf))) {
912
        /* Do we need to allocate a scratch buffer? */
913
0
        if(!data->state.scratch) {
914
0
          data->state.scratch = malloc(2 * data->set.upload_buffer_size);
915
0
          if(!data->state.scratch) {
916
0
            failf(data, "Failed to alloc scratch buffer");
917
918
0
            return CURLE_OUT_OF_MEMORY;
919
0
          }
920
0
        }
921
922
        /*
923
         * ASCII/EBCDIC Note: This is presumably a text (not binary)
924
         * transfer so the data should already be in ASCII.
925
         * That means the hex values for ASCII CR (0x0d) & LF (0x0a)
926
         * must be used instead of the escape sequences \r & \n.
927
         */
928
0
        if(offset)
929
0
          memcpy(data->state.scratch, k->upload_fromhere, offset);
930
0
        for(i = offset, si = offset; i < nread; i++, si++) {
931
0
          if(k->upload_fromhere[i] == 0x0a) {
932
0
            data->state.scratch[si++] = 0x0d;
933
0
            data->state.scratch[si] = 0x0a;
934
0
            if(!data->set.crlf) {
935
              /* we're here only because FTP is in ASCII mode...
936
                 bump infilesize for the LF we just added */
937
0
              if(data->state.infilesize != -1)
938
0
                data->state.infilesize++;
939
0
            }
940
0
          }
941
0
          else
942
0
            data->state.scratch[si] = k->upload_fromhere[i];
943
0
        }
944
945
0
        if(si != nread) {
946
          /* only perform the special operation if we really did replace
947
             anything */
948
0
          nread = si;
949
950
          /* upload from the new (replaced) buffer instead */
951
0
          k->upload_fromhere = data->state.scratch;
952
953
          /* set the new amount too */
954
0
          k->upload_present = nread;
955
0
        }
956
0
      }
957
958
#ifndef CURL_DISABLE_SMTP
959
      if(conn->handler->protocol & PROTO_FAMILY_SMTP) {
960
        result = Curl_smtp_escape_eob(data, nread, offset);
961
        if(result)
962
          return result;
963
      }
964
#endif /* CURL_DISABLE_SMTP */
965
0
    } /* if 0 == k->upload_present or appended to upload buffer */
966
0
    else {
967
      /* We have a partial buffer left from a previous "round". Use
968
         that instead of reading more data */
969
0
    }
970
971
    /* write to socket (send away data) */
972
0
    result = Curl_write(data,
973
0
                        conn->writesockfd,  /* socket to send to */
974
0
                        k->upload_fromhere, /* buffer pointer */
975
0
                        k->upload_present,  /* buffer size */
976
0
                        &bytes_written);    /* actually sent */
977
0
    if(result)
978
0
      return result;
979
980
#if defined(WIN32) && defined(USE_WINSOCK)
981
    {
982
      struct curltime n = Curl_now();
983
      if(Curl_timediff(n, k->last_sndbuf_update) > 1000) {
984
        win_update_buffer_size(conn->writesockfd);
985
        k->last_sndbuf_update = n;
986
      }
987
    }
988
#endif
989
990
0
    if(k->pendingheader) {
991
      /* parts of what was sent was header */
992
0
      curl_off_t n = CURLMIN(k->pendingheader, bytes_written);
993
      /* show the data before we change the pointer upload_fromhere */
994
0
      Curl_debug(data, CURLINFO_HEADER_OUT, k->upload_fromhere, (size_t)n);
995
0
      k->pendingheader -= n;
996
0
      nbody = bytes_written - n; /* size of the written body part */
997
0
    }
998
0
    else
999
0
      nbody = bytes_written;
1000
1001
0
    if(nbody) {
1002
      /* show the data before we change the pointer upload_fromhere */
1003
0
      Curl_debug(data, CURLINFO_DATA_OUT,
1004
0
                 &k->upload_fromhere[bytes_written - nbody],
1005
0
                 (size_t)nbody);
1006
1007
0
      k->writebytecount += nbody;
1008
0
      Curl_pgrsSetUploadCounter(data, k->writebytecount);
1009
0
    }
1010
1011
0
    if((!k->upload_chunky || k->forbidchunk) &&
1012
0
       (k->writebytecount == data->state.infilesize)) {
1013
      /* we have sent all data we were supposed to */
1014
0
      k->upload_done = TRUE;
1015
0
      infof(data, "We are completely uploaded and fine");
1016
0
    }
1017
1018
0
    if(k->upload_present != bytes_written) {
1019
      /* we only wrote a part of the buffer (if anything), deal with it! */
1020
1021
      /* store the amount of bytes left in the buffer to write */
1022
0
      k->upload_present -= bytes_written;
1023
1024
      /* advance the pointer where to find the buffer when the next send
1025
         is to happen */
1026
0
      k->upload_fromhere += bytes_written;
1027
0
    }
1028
0
    else {
1029
      /* we've uploaded that buffer now */
1030
0
      result = Curl_get_upload_buffer(data);
1031
0
      if(result)
1032
0
        return result;
1033
0
      k->upload_fromhere = data->state.ulbuf;
1034
0
      k->upload_present = 0; /* no more bytes left */
1035
1036
0
      if(k->upload_done) {
1037
0
        result = Curl_done_sending(data, k);
1038
0
        if(result)
1039
0
          return result;
1040
0
      }
1041
0
    }
1042
1043
1044
0
  } while(0); /* just to break out from! */
1045
1046
0
  return CURLE_OK;
1047
0
}
1048
1049
static int select_bits_paused(struct Curl_easy *data, int select_bits)
1050
0
{
1051
  /* See issue #11982: we really need to be careful not to progress
1052
   * a transfer direction when that direction is paused. Not all parts
1053
   * of our state machine are handling PAUSED transfers correctly. So, we
1054
   * do not want to go there.
1055
   * NOTE: we are only interested in PAUSE, not HOLD. */
1056
0
  return (((select_bits & CURL_CSELECT_IN) &&
1057
0
           (data->req.keepon & KEEP_RECV_PAUSE)) ||
1058
0
          ((select_bits & CURL_CSELECT_OUT) &&
1059
0
           (data->req.keepon & KEEP_SEND_PAUSE)));
1060
0
}
1061
1062
/*
1063
 * Curl_readwrite() is the low-level function to be called when data is to
1064
 * be read and written to/from the connection.
1065
 *
1066
 * return '*comeback' TRUE if we didn't properly drain the socket so this
1067
 * function should get called again without select() or similar in between!
1068
 */
1069
CURLcode Curl_readwrite(struct connectdata *conn,
1070
                        struct Curl_easy *data,
1071
                        bool *done,
1072
                        bool *comeback)
1073
0
{
1074
0
  struct SingleRequest *k = &data->req;
1075
0
  CURLcode result;
1076
0
  struct curltime now;
1077
0
  int didwhat = 0;
1078
0
  int select_bits;
1079
1080
0
  if(data->state.dselect_bits) {
1081
0
    if(select_bits_paused(data, data->state.dselect_bits)) {
1082
      /* leave the bits unchanged, so they'll tell us what to do when
1083
       * this transfer gets unpaused. */
1084
0
      DEBUGF(infof(data, "readwrite, dselect_bits, early return on PAUSED"));
1085
0
      result = CURLE_OK;
1086
0
      goto out;
1087
0
    }
1088
0
    select_bits = data->state.dselect_bits;
1089
0
    data->state.dselect_bits = 0;
1090
0
  }
1091
0
  else if(conn->cselect_bits) {
1092
    /* CAVEAT: adding `select_bits_paused()` check here makes test640 hang
1093
     * (among others). Which hints at strange state handling in FTP land... */
1094
0
    select_bits = conn->cselect_bits;
1095
0
    conn->cselect_bits = 0;
1096
0
  }
1097
0
  else {
1098
0
    curl_socket_t fd_read;
1099
0
    curl_socket_t fd_write;
1100
    /* only use the proper socket if the *_HOLD bit is not set simultaneously
1101
       as then we are in rate limiting state in that transfer direction */
1102
0
    if((k->keepon & KEEP_RECVBITS) == KEEP_RECV)
1103
0
      fd_read = conn->sockfd;
1104
0
    else
1105
0
      fd_read = CURL_SOCKET_BAD;
1106
1107
0
    if((k->keepon & KEEP_SENDBITS) == KEEP_SEND)
1108
0
      fd_write = conn->writesockfd;
1109
0
    else
1110
0
      fd_write = CURL_SOCKET_BAD;
1111
1112
0
    select_bits = Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write, 0);
1113
0
  }
1114
1115
0
  if(select_bits == CURL_CSELECT_ERR) {
1116
0
    failf(data, "select/poll returned error");
1117
0
    result = CURLE_SEND_ERROR;
1118
0
    goto out;
1119
0
  }
1120
1121
#ifdef USE_HYPER
1122
  if(conn->datastream) {
1123
    result = conn->datastream(data, conn, &didwhat, done, select_bits);
1124
    if(result || *done)
1125
      goto out;
1126
  }
1127
  else {
1128
#endif
1129
  /* We go ahead and do a read if we have a readable socket or if
1130
     the stream was rewound (in which case we have data in a
1131
     buffer) */
1132
0
  if((k->keepon & KEEP_RECV) && (select_bits & CURL_CSELECT_IN)) {
1133
0
    result = readwrite_data(data, conn, k, &didwhat, done, comeback);
1134
0
    if(result || *done)
1135
0
      goto out;
1136
0
  }
1137
1138
  /* If we still have writing to do, we check if we have a writable socket. */
1139
0
  if((k->keepon & KEEP_SEND) && (select_bits & CURL_CSELECT_OUT)) {
1140
    /* write */
1141
1142
0
    result = readwrite_upload(data, conn, &didwhat);
1143
0
    if(result)
1144
0
      goto out;
1145
0
  }
1146
#ifdef USE_HYPER
1147
  }
1148
#endif
1149
1150
0
  now = Curl_now();
1151
0
  if(!didwhat) {
1152
    /* no read no write, this is a timeout? */
1153
0
    if(k->exp100 == EXP100_AWAITING_CONTINUE) {
1154
      /* This should allow some time for the header to arrive, but only a
1155
         very short time as otherwise it'll be too much wasted time too
1156
         often. */
1157
1158
      /* Quoting RFC2616, section "8.2.3 Use of the 100 (Continue) Status":
1159
1160
         Therefore, when a client sends this header field to an origin server
1161
         (possibly via a proxy) from which it has never seen a 100 (Continue)
1162
         status, the client SHOULD NOT wait for an indefinite period before
1163
         sending the request body.
1164
1165
      */
1166
1167
0
      timediff_t ms = Curl_timediff(now, k->start100);
1168
0
      if(ms >= data->set.expect_100_timeout) {
1169
        /* we've waited long enough, continue anyway */
1170
0
        k->exp100 = EXP100_SEND_DATA;
1171
0
        k->keepon |= KEEP_SEND;
1172
0
        Curl_expire_done(data, EXPIRE_100_TIMEOUT);
1173
0
        infof(data, "Done waiting for 100-continue");
1174
0
      }
1175
0
    }
1176
1177
0
    result = Curl_conn_ev_data_idle(data);
1178
0
    if(result)
1179
0
      goto out;
1180
0
  }
1181
1182
0
  if(Curl_pgrsUpdate(data))
1183
0
    result = CURLE_ABORTED_BY_CALLBACK;
1184
0
  else
1185
0
    result = Curl_speedcheck(data, now);
1186
0
  if(result)
1187
0
    goto out;
1188
1189
0
  if(k->keepon) {
1190
0
    if(0 > Curl_timeleft(data, &now, FALSE)) {
1191
0
      if(k->size != -1) {
1192
0
        failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T
1193
0
              " milliseconds with %" CURL_FORMAT_CURL_OFF_T " out of %"
1194
0
              CURL_FORMAT_CURL_OFF_T " bytes received",
1195
0
              Curl_timediff(now, data->progress.t_startsingle),
1196
0
              k->bytecount, k->size);
1197
0
      }
1198
0
      else {
1199
0
        failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T
1200
0
              " milliseconds with %" CURL_FORMAT_CURL_OFF_T " bytes received",
1201
0
              Curl_timediff(now, data->progress.t_startsingle),
1202
0
              k->bytecount);
1203
0
      }
1204
0
      result = CURLE_OPERATION_TIMEDOUT;
1205
0
      goto out;
1206
0
    }
1207
0
  }
1208
0
  else {
1209
    /*
1210
     * The transfer has been performed. Just make some general checks before
1211
     * returning.
1212
     */
1213
1214
0
    if(!(data->req.no_body) && (k->size != -1) &&
1215
0
       (k->bytecount != k->size) &&
1216
0
#ifdef CURL_DO_LINEEND_CONV
1217
       /* Most FTP servers don't adjust their file SIZE response for CRLFs,
1218
          so we'll check to see if the discrepancy can be explained
1219
          by the number of CRLFs we've changed to LFs.
1220
       */
1221
0
       (k->bytecount != (k->size + data->state.crlf_conversions)) &&
1222
0
#endif /* CURL_DO_LINEEND_CONV */
1223
0
       !k->newurl) {
1224
0
      failf(data, "transfer closed with %" CURL_FORMAT_CURL_OFF_T
1225
0
            " bytes remaining to read", k->size - k->bytecount);
1226
0
      result = CURLE_PARTIAL_FILE;
1227
0
      goto out;
1228
0
    }
1229
0
    if(!(data->req.no_body) && k->chunk &&
1230
0
       (conn->chunk.state != CHUNK_STOP)) {
1231
      /*
1232
       * In chunked mode, return an error if the connection is closed prior to
1233
       * the empty (terminating) chunk is read.
1234
       *
1235
       * The condition above used to check for
1236
       * conn->proto.http->chunk.datasize != 0 which is true after reading
1237
       * *any* chunk, not just the empty chunk.
1238
       *
1239
       */
1240
0
      failf(data, "transfer closed with outstanding read data remaining");
1241
0
      result = CURLE_PARTIAL_FILE;
1242
0
      goto out;
1243
0
    }
1244
0
    if(Curl_pgrsUpdate(data)) {
1245
0
      result = CURLE_ABORTED_BY_CALLBACK;
1246
0
      goto out;
1247
0
    }
1248
0
  }
1249
1250
  /* Now update the "done" boolean we return */
1251
0
  *done = (0 == (k->keepon&(KEEP_RECVBITS|KEEP_SENDBITS))) ? TRUE : FALSE;
1252
0
out:
1253
0
  if(result)
1254
0
    DEBUGF(infof(data, "Curl_readwrite() -> %d", result));
1255
0
  return result;
1256
0
}
1257
1258
/*
1259
 * Curl_single_getsock() gets called by the multi interface code when the app
1260
 * has requested to get the sockets for the current connection. This function
1261
 * will then be called once for every connection that the multi interface
1262
 * keeps track of. This function will only be called for connections that are
1263
 * in the proper state to have this information available.
1264
 */
1265
int Curl_single_getsock(struct Curl_easy *data,
1266
                        struct connectdata *conn,
1267
                        curl_socket_t *sock)
1268
0
{
1269
0
  int bitmap = GETSOCK_BLANK;
1270
0
  unsigned sockindex = 0;
1271
1272
0
  if(conn->handler->perform_getsock)
1273
0
    return conn->handler->perform_getsock(data, conn, sock);
1274
1275
  /* don't include HOLD and PAUSE connections */
1276
0
  if((data->req.keepon & KEEP_RECVBITS) == KEEP_RECV) {
1277
1278
0
    DEBUGASSERT(conn->sockfd != CURL_SOCKET_BAD);
1279
1280
0
    bitmap |= GETSOCK_READSOCK(sockindex);
1281
0
    sock[sockindex] = conn->sockfd;
1282
0
  }
1283
1284
  /* don't include HOLD and PAUSE connections */
1285
0
  if((data->req.keepon & KEEP_SENDBITS) == KEEP_SEND) {
1286
0
    if((conn->sockfd != conn->writesockfd) ||
1287
0
       bitmap == GETSOCK_BLANK) {
1288
      /* only if they are not the same socket and we have a readable
1289
         one, we increase index */
1290
0
      if(bitmap != GETSOCK_BLANK)
1291
0
        sockindex++; /* increase index if we need two entries */
1292
1293
0
      DEBUGASSERT(conn->writesockfd != CURL_SOCKET_BAD);
1294
1295
0
      sock[sockindex] = conn->writesockfd;
1296
0
    }
1297
1298
0
    bitmap |= GETSOCK_WRITESOCK(sockindex);
1299
0
  }
1300
1301
0
  return bitmap;
1302
0
}
1303
1304
/* Curl_init_CONNECT() gets called each time the handle switches to CONNECT
1305
   which means this gets called once for each subsequent redirect etc */
1306
void Curl_init_CONNECT(struct Curl_easy *data)
1307
0
{
1308
0
  data->state.fread_func = data->set.fread_func_set;
1309
0
  data->state.in = data->set.in_set;
1310
0
  data->state.upload = (data->state.httpreq == HTTPREQ_PUT);
1311
0
}
1312
1313
/*
1314
 * Curl_pretransfer() is called immediately before a transfer starts, and only
1315
 * once for one transfer no matter if it has redirects or do multi-pass
1316
 * authentication etc.
1317
 */
1318
CURLcode Curl_pretransfer(struct Curl_easy *data)
1319
0
{
1320
0
  CURLcode result;
1321
1322
0
  if(!data->state.url && !data->set.uh) {
1323
    /* we can't do anything without URL */
1324
0
    failf(data, "No URL set");
1325
0
    return CURLE_URL_MALFORMAT;
1326
0
  }
1327
1328
  /* since the URL may have been redirected in a previous use of this handle */
1329
0
  if(data->state.url_alloc) {
1330
    /* the already set URL is allocated, free it first! */
1331
0
    Curl_safefree(data->state.url);
1332
0
    data->state.url_alloc = FALSE;
1333
0
  }
1334
1335
0
  if(!data->state.url && data->set.uh) {
1336
0
    CURLUcode uc;
1337
0
    free(data->set.str[STRING_SET_URL]);
1338
0
    uc = curl_url_get(data->set.uh,
1339
0
                      CURLUPART_URL, &data->set.str[STRING_SET_URL], 0);
1340
0
    if(uc) {
1341
0
      failf(data, "No URL set");
1342
0
      return CURLE_URL_MALFORMAT;
1343
0
    }
1344
0
  }
1345
1346
0
  if(data->set.postfields && data->set.set_resume_from) {
1347
    /* we can't */
1348
0
    failf(data, "cannot mix POSTFIELDS with RESUME_FROM");
1349
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1350
0
  }
1351
1352
0
  data->state.prefer_ascii = data->set.prefer_ascii;
1353
#ifdef CURL_LIST_ONLY_PROTOCOL
1354
  data->state.list_only = data->set.list_only;
1355
#endif
1356
0
  data->state.httpreq = data->set.method;
1357
0
  data->state.url = data->set.str[STRING_SET_URL];
1358
1359
  /* Init the SSL session ID cache here. We do it here since we want to do it
1360
     after the *_setopt() calls (that could specify the size of the cache) but
1361
     before any transfer takes place. */
1362
0
  result = Curl_ssl_initsessions(data, data->set.general_ssl.max_ssl_sessions);
1363
0
  if(result)
1364
0
    return result;
1365
1366
0
  data->state.requests = 0;
1367
0
  data->state.followlocation = 0; /* reset the location-follow counter */
1368
0
  data->state.this_is_a_follow = FALSE; /* reset this */
1369
0
  data->state.errorbuf = FALSE; /* no error has occurred */
1370
0
  data->state.httpwant = data->set.httpwant;
1371
0
  data->state.httpversion = 0;
1372
0
  data->state.authproblem = FALSE;
1373
0
  data->state.authhost.want = data->set.httpauth;
1374
0
  data->state.authproxy.want = data->set.proxyauth;
1375
0
  Curl_safefree(data->info.wouldredirect);
1376
0
  Curl_data_priority_clear_state(data);
1377
1378
0
  if(data->state.httpreq == HTTPREQ_PUT)
1379
0
    data->state.infilesize = data->set.filesize;
1380
0
  else if((data->state.httpreq != HTTPREQ_GET) &&
1381
0
          (data->state.httpreq != HTTPREQ_HEAD)) {
1382
0
    data->state.infilesize = data->set.postfieldsize;
1383
0
    if(data->set.postfields && (data->state.infilesize == -1))
1384
0
      data->state.infilesize = (curl_off_t)strlen(data->set.postfields);
1385
0
  }
1386
0
  else
1387
0
    data->state.infilesize = 0;
1388
1389
  /* If there is a list of cookie files to read, do it now! */
1390
0
  Curl_cookie_loadfiles(data);
1391
1392
  /* If there is a list of host pairs to deal with */
1393
0
  if(data->state.resolve)
1394
0
    result = Curl_loadhostpairs(data);
1395
1396
  /* If there is a list of hsts files to read */
1397
0
  Curl_hsts_loadfiles(data);
1398
1399
0
  if(!result) {
1400
    /* Allow data->set.use_port to set which port to use. This needs to be
1401
     * disabled for example when we follow Location: headers to URLs using
1402
     * different ports! */
1403
0
    data->state.allow_port = TRUE;
1404
1405
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
1406
    /*************************************************************
1407
     * Tell signal handler to ignore SIGPIPE
1408
     *************************************************************/
1409
    if(!data->set.no_signal)
1410
      data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
1411
#endif
1412
1413
0
    Curl_initinfo(data); /* reset session-specific information "variables" */
1414
0
    Curl_pgrsResetTransferSizes(data);
1415
0
    Curl_pgrsStartNow(data);
1416
1417
    /* In case the handle is reused and an authentication method was picked
1418
       in the session we need to make sure we only use the one(s) we now
1419
       consider to be fine */
1420
0
    data->state.authhost.picked &= data->state.authhost.want;
1421
0
    data->state.authproxy.picked &= data->state.authproxy.want;
1422
1423
#ifndef CURL_DISABLE_FTP
1424
    data->state.wildcardmatch = data->set.wildcard_enabled;
1425
    if(data->state.wildcardmatch) {
1426
      struct WildcardData *wc;
1427
      if(!data->wildcard) {
1428
        data->wildcard = calloc(1, sizeof(struct WildcardData));
1429
        if(!data->wildcard)
1430
          return CURLE_OUT_OF_MEMORY;
1431
      }
1432
      wc = data->wildcard;
1433
      if((wc->state < CURLWC_INIT) ||
1434
         (wc->state >= CURLWC_CLEAN)) {
1435
        if(wc->ftpwc)
1436
          wc->dtor(wc->ftpwc);
1437
        Curl_safefree(wc->pattern);
1438
        Curl_safefree(wc->path);
1439
        result = Curl_wildcard_init(wc); /* init wildcard structures */
1440
        if(result)
1441
          return CURLE_OUT_OF_MEMORY;
1442
      }
1443
    }
1444
#endif
1445
0
    result = Curl_hsts_loadcb(data, data->hsts);
1446
0
  }
1447
1448
  /*
1449
   * Set user-agent. Used for HTTP, but since we can attempt to tunnel
1450
   * basically anything through an HTTP proxy we can't limit this based on
1451
   * protocol.
1452
   */
1453
0
  if(data->set.str[STRING_USERAGENT]) {
1454
0
    Curl_safefree(data->state.aptr.uagent);
1455
0
    data->state.aptr.uagent =
1456
0
      aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
1457
0
    if(!data->state.aptr.uagent)
1458
0
      return CURLE_OUT_OF_MEMORY;
1459
0
  }
1460
1461
0
  if(!result)
1462
0
    result = Curl_setstropt(&data->state.aptr.user,
1463
0
                            data->set.str[STRING_USERNAME]);
1464
0
  if(!result)
1465
0
    result = Curl_setstropt(&data->state.aptr.passwd,
1466
0
                            data->set.str[STRING_PASSWORD]);
1467
0
  if(!result)
1468
0
    result = Curl_setstropt(&data->state.aptr.proxyuser,
1469
0
                            data->set.str[STRING_PROXYUSERNAME]);
1470
0
  if(!result)
1471
0
    result = Curl_setstropt(&data->state.aptr.proxypasswd,
1472
0
                            data->set.str[STRING_PROXYPASSWORD]);
1473
1474
0
  data->req.headerbytecount = 0;
1475
0
  Curl_headers_cleanup(data);
1476
0
  return result;
1477
0
}
1478
1479
/*
1480
 * Curl_posttransfer() is called immediately after a transfer ends
1481
 */
1482
CURLcode Curl_posttransfer(struct Curl_easy *data)
1483
0
{
1484
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
1485
  /* restore the signal handler for SIGPIPE before we get back */
1486
  if(!data->set.no_signal)
1487
    signal(SIGPIPE, data->state.prev_signal);
1488
#else
1489
0
  (void)data; /* unused parameter */
1490
0
#endif
1491
1492
0
  return CURLE_OK;
1493
0
}
1494
1495
/*
1496
 * Curl_follow() handles the URL redirect magic. Pass in the 'newurl' string
1497
 * as given by the remote server and set up the new URL to request.
1498
 *
1499
 * This function DOES NOT FREE the given url.
1500
 */
1501
CURLcode Curl_follow(struct Curl_easy *data,
1502
                     char *newurl,    /* the Location: string */
1503
                     followtype type) /* see transfer.h */
1504
0
{
1505
#ifdef CURL_DISABLE_HTTP
1506
  (void)data;
1507
  (void)newurl;
1508
  (void)type;
1509
  /* Location: following will not happen when HTTP is disabled */
1510
  return CURLE_TOO_MANY_REDIRECTS;
1511
#else
1512
1513
  /* Location: redirect */
1514
0
  bool disallowport = FALSE;
1515
0
  bool reachedmax = FALSE;
1516
0
  CURLUcode uc;
1517
1518
0
  DEBUGASSERT(type != FOLLOW_NONE);
1519
1520
0
  if(type != FOLLOW_FAKE)
1521
0
    data->state.requests++; /* count all real follows */
1522
0
  if(type == FOLLOW_REDIR) {
1523
0
    if((data->set.maxredirs != -1) &&
1524
0
       (data->state.followlocation >= data->set.maxredirs)) {
1525
0
      reachedmax = TRUE;
1526
0
      type = FOLLOW_FAKE; /* switch to fake to store the would-be-redirected
1527
                             to URL */
1528
0
    }
1529
0
    else {
1530
0
      data->state.followlocation++; /* count redirect-followings, including
1531
                                       auth reloads */
1532
1533
0
      if(data->set.http_auto_referer) {
1534
0
        CURLU *u;
1535
0
        char *referer = NULL;
1536
1537
        /* We are asked to automatically set the previous URL as the referer
1538
           when we get the next URL. We pick the ->url field, which may or may
1539
           not be 100% correct */
1540
1541
0
        if(data->state.referer_alloc) {
1542
0
          Curl_safefree(data->state.referer);
1543
0
          data->state.referer_alloc = FALSE;
1544
0
        }
1545
1546
        /* Make a copy of the URL without credentials and fragment */
1547
0
        u = curl_url();
1548
0
        if(!u)
1549
0
          return CURLE_OUT_OF_MEMORY;
1550
1551
0
        uc = curl_url_set(u, CURLUPART_URL, data->state.url, 0);
1552
0
        if(!uc)
1553
0
          uc = curl_url_set(u, CURLUPART_FRAGMENT, NULL, 0);
1554
0
        if(!uc)
1555
0
          uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
1556
0
        if(!uc)
1557
0
          uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
1558
0
        if(!uc)
1559
0
          uc = curl_url_get(u, CURLUPART_URL, &referer, 0);
1560
1561
0
        curl_url_cleanup(u);
1562
1563
0
        if(uc || !referer)
1564
0
          return CURLE_OUT_OF_MEMORY;
1565
1566
0
        data->state.referer = referer;
1567
0
        data->state.referer_alloc = TRUE; /* yes, free this later */
1568
0
      }
1569
0
    }
1570
0
  }
1571
1572
0
  if((type != FOLLOW_RETRY) &&
1573
0
     (data->req.httpcode != 401) && (data->req.httpcode != 407) &&
1574
0
     Curl_is_absolute_url(newurl, NULL, 0, FALSE)) {
1575
    /* If this is not redirect due to a 401 or 407 response and an absolute
1576
       URL: don't allow a custom port number */
1577
0
    disallowport = TRUE;
1578
0
  }
1579
1580
0
  DEBUGASSERT(data->state.uh);
1581
0
  uc = curl_url_set(data->state.uh, CURLUPART_URL, newurl,
1582
0
                    (type == FOLLOW_FAKE) ? CURLU_NON_SUPPORT_SCHEME :
1583
0
                    ((type == FOLLOW_REDIR) ? CURLU_URLENCODE : 0) |
1584
0
                    CURLU_ALLOW_SPACE |
1585
0
                    (data->set.path_as_is ? CURLU_PATH_AS_IS : 0));
1586
0
  if(uc) {
1587
0
    if(type != FOLLOW_FAKE) {
1588
0
      failf(data, "The redirect target URL could not be parsed: %s",
1589
0
            curl_url_strerror(uc));
1590
0
      return Curl_uc_to_curlcode(uc);
1591
0
    }
1592
1593
    /* the URL could not be parsed for some reason, but since this is FAKE
1594
       mode, just duplicate the field as-is */
1595
0
    newurl = strdup(newurl);
1596
0
    if(!newurl)
1597
0
      return CURLE_OUT_OF_MEMORY;
1598
0
  }
1599
0
  else {
1600
0
    uc = curl_url_get(data->state.uh, CURLUPART_URL, &newurl, 0);
1601
0
    if(uc)
1602
0
      return Curl_uc_to_curlcode(uc);
1603
1604
    /* Clear auth if this redirects to a different port number or protocol,
1605
       unless permitted */
1606
0
    if(!data->set.allow_auth_to_other_hosts && (type != FOLLOW_FAKE)) {
1607
0
      char *portnum;
1608
0
      int port;
1609
0
      bool clear = FALSE;
1610
1611
0
      if(data->set.use_port && data->state.allow_port)
1612
        /* a custom port is used */
1613
0
        port = (int)data->set.use_port;
1614
0
      else {
1615
0
        uc = curl_url_get(data->state.uh, CURLUPART_PORT, &portnum,
1616
0
                          CURLU_DEFAULT_PORT);
1617
0
        if(uc) {
1618
0
          free(newurl);
1619
0
          return Curl_uc_to_curlcode(uc);
1620
0
        }
1621
0
        port = atoi(portnum);
1622
0
        free(portnum);
1623
0
      }
1624
0
      if(port != data->info.conn_remote_port) {
1625
0
        infof(data, "Clear auth, redirects to port from %u to %u",
1626
0
              data->info.conn_remote_port, port);
1627
0
        clear = TRUE;
1628
0
      }
1629
0
      else {
1630
0
        char *scheme;
1631
0
        const struct Curl_handler *p;
1632
0
        uc = curl_url_get(data->state.uh, CURLUPART_SCHEME, &scheme, 0);
1633
0
        if(uc) {
1634
0
          free(newurl);
1635
0
          return Curl_uc_to_curlcode(uc);
1636
0
        }
1637
1638
0
        p = Curl_builtin_scheme(scheme, CURL_ZERO_TERMINATED);
1639
0
        if(p && (p->protocol != data->info.conn_protocol)) {
1640
0
          infof(data, "Clear auth, redirects scheme from %s to %s",
1641
0
                data->info.conn_scheme, scheme);
1642
0
          clear = TRUE;
1643
0
        }
1644
0
        free(scheme);
1645
0
      }
1646
0
      if(clear) {
1647
0
        Curl_safefree(data->state.aptr.user);
1648
0
        Curl_safefree(data->state.aptr.passwd);
1649
0
      }
1650
0
    }
1651
0
  }
1652
1653
0
  if(type == FOLLOW_FAKE) {
1654
    /* we're only figuring out the new url if we would've followed locations
1655
       but now we're done so we can get out! */
1656
0
    data->info.wouldredirect = newurl;
1657
1658
0
    if(reachedmax) {
1659
0
      failf(data, "Maximum (%ld) redirects followed", data->set.maxredirs);
1660
0
      return CURLE_TOO_MANY_REDIRECTS;
1661
0
    }
1662
0
    return CURLE_OK;
1663
0
  }
1664
1665
0
  if(disallowport)
1666
0
    data->state.allow_port = FALSE;
1667
1668
0
  if(data->state.url_alloc)
1669
0
    Curl_safefree(data->state.url);
1670
1671
0
  data->state.url = newurl;
1672
0
  data->state.url_alloc = TRUE;
1673
1674
0
  infof(data, "Issue another request to this URL: '%s'", data->state.url);
1675
1676
  /*
1677
   * We get here when the HTTP code is 300-399 (and 401). We need to perform
1678
   * differently based on exactly what return code there was.
1679
   *
1680
   * News from 7.10.6: we can also get here on a 401 or 407, in case we act on
1681
   * an HTTP (proxy-) authentication scheme other than Basic.
1682
   */
1683
0
  switch(data->info.httpcode) {
1684
    /* 401 - Act on a WWW-Authenticate, we keep on moving and do the
1685
       Authorization: XXXX header in the HTTP request code snippet */
1686
    /* 407 - Act on a Proxy-Authenticate, we keep on moving and do the
1687
       Proxy-Authorization: XXXX header in the HTTP request code snippet */
1688
    /* 300 - Multiple Choices */
1689
    /* 306 - Not used */
1690
    /* 307 - Temporary Redirect */
1691
0
  default:  /* for all above (and the unknown ones) */
1692
    /* Some codes are explicitly mentioned since I've checked RFC2616 and they
1693
     * seem to be OK to POST to.
1694
     */
1695
0
    break;
1696
0
  case 301: /* Moved Permanently */
1697
    /* (quote from RFC7231, section 6.4.2)
1698
     *
1699
     * Note: For historical reasons, a user agent MAY change the request
1700
     * method from POST to GET for the subsequent request.  If this
1701
     * behavior is undesired, the 307 (Temporary Redirect) status code
1702
     * can be used instead.
1703
     *
1704
     * ----
1705
     *
1706
     * Many webservers expect this, so these servers often answers to a POST
1707
     * request with an error page. To be sure that libcurl gets the page that
1708
     * most user agents would get, libcurl has to force GET.
1709
     *
1710
     * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
1711
     * can be overridden with CURLOPT_POSTREDIR.
1712
     */
1713
0
    if((data->state.httpreq == HTTPREQ_POST
1714
0
        || data->state.httpreq == HTTPREQ_POST_FORM
1715
0
        || data->state.httpreq == HTTPREQ_POST_MIME)
1716
0
       && !(data->set.keep_post & CURL_REDIR_POST_301)) {
1717
0
      infof(data, "Switch from POST to GET");
1718
0
      data->state.httpreq = HTTPREQ_GET;
1719
0
    }
1720
0
    break;
1721
0
  case 302: /* Found */
1722
    /* (quote from RFC7231, section 6.4.3)
1723
     *
1724
     * Note: For historical reasons, a user agent MAY change the request
1725
     * method from POST to GET for the subsequent request.  If this
1726
     * behavior is undesired, the 307 (Temporary Redirect) status code
1727
     * can be used instead.
1728
     *
1729
     * ----
1730
     *
1731
     * Many webservers expect this, so these servers often answers to a POST
1732
     * request with an error page. To be sure that libcurl gets the page that
1733
     * most user agents would get, libcurl has to force GET.
1734
     *
1735
     * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
1736
     * can be overridden with CURLOPT_POSTREDIR.
1737
     */
1738
0
    if((data->state.httpreq == HTTPREQ_POST
1739
0
        || data->state.httpreq == HTTPREQ_POST_FORM
1740
0
        || data->state.httpreq == HTTPREQ_POST_MIME)
1741
0
       && !(data->set.keep_post & CURL_REDIR_POST_302)) {
1742
0
      infof(data, "Switch from POST to GET");
1743
0
      data->state.httpreq = HTTPREQ_GET;
1744
0
    }
1745
0
    break;
1746
1747
0
  case 303: /* See Other */
1748
    /* 'See Other' location is not the resource but a substitute for the
1749
     * resource. In this case we switch the method to GET/HEAD, unless the
1750
     * method is POST and the user specified to keep it as POST.
1751
     * https://github.com/curl/curl/issues/5237#issuecomment-614641049
1752
     */
1753
0
    if(data->state.httpreq != HTTPREQ_GET &&
1754
0
       ((data->state.httpreq != HTTPREQ_POST &&
1755
0
         data->state.httpreq != HTTPREQ_POST_FORM &&
1756
0
         data->state.httpreq != HTTPREQ_POST_MIME) ||
1757
0
        !(data->set.keep_post & CURL_REDIR_POST_303))) {
1758
0
      data->state.httpreq = HTTPREQ_GET;
1759
0
      infof(data, "Switch to %s",
1760
0
            data->req.no_body?"HEAD":"GET");
1761
0
    }
1762
0
    break;
1763
0
  case 304: /* Not Modified */
1764
    /* 304 means we did a conditional request and it was "Not modified".
1765
     * We shouldn't get any Location: header in this response!
1766
     */
1767
0
    break;
1768
0
  case 305: /* Use Proxy */
1769
    /* (quote from RFC2616, section 10.3.6):
1770
     * "The requested resource MUST be accessed through the proxy given
1771
     * by the Location field. The Location field gives the URI of the
1772
     * proxy.  The recipient is expected to repeat this single request
1773
     * via the proxy. 305 responses MUST only be generated by origin
1774
     * servers."
1775
     */
1776
0
    break;
1777
0
  }
1778
0
  Curl_pgrsTime(data, TIMER_REDIRECT);
1779
0
  Curl_pgrsResetTransferSizes(data);
1780
1781
0
  return CURLE_OK;
1782
0
#endif /* CURL_DISABLE_HTTP */
1783
0
}
1784
1785
/* Returns CURLE_OK *and* sets '*url' if a request retry is wanted.
1786
1787
   NOTE: that the *url is malloc()ed. */
1788
CURLcode Curl_retry_request(struct Curl_easy *data, char **url)
1789
0
{
1790
0
  struct connectdata *conn = data->conn;
1791
0
  bool retry = FALSE;
1792
0
  *url = NULL;
1793
1794
  /* if we're talking upload, we can't do the checks below, unless the protocol
1795
     is HTTP as when uploading over HTTP we will still get a response */
1796
0
  if(data->state.upload &&
1797
0
     !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP)))
1798
0
    return CURLE_OK;
1799
1800
0
  if((data->req.bytecount + data->req.headerbytecount == 0) &&
1801
0
     conn->bits.reuse &&
1802
0
     (!data->req.no_body || (conn->handler->protocol & PROTO_FAMILY_HTTP))
1803
0
#ifndef CURL_DISABLE_RTSP
1804
0
     && (data->set.rtspreq != RTSPREQ_RECEIVE)
1805
0
#endif
1806
0
    )
1807
    /* We got no data, we attempted to reuse a connection. For HTTP this
1808
       can be a retry so we try again regardless if we expected a body.
1809
       For other protocols we only try again only if we expected a body.
1810
1811
       This might happen if the connection was left alive when we were
1812
       done using it before, but that was closed when we wanted to read from
1813
       it again. Bad luck. Retry the same request on a fresh connect! */
1814
0
    retry = TRUE;
1815
0
  else if(data->state.refused_stream &&
1816
0
          (data->req.bytecount + data->req.headerbytecount == 0) ) {
1817
    /* This was sent on a refused stream, safe to rerun. A refused stream
1818
       error can typically only happen on HTTP/2 level if the stream is safe
1819
       to issue again, but the nghttp2 API can deliver the message to other
1820
       streams as well, which is why this adds the check the data counters
1821
       too. */
1822
0
    infof(data, "REFUSED_STREAM, retrying a fresh connect");
1823
0
    data->state.refused_stream = FALSE; /* clear again */
1824
0
    retry = TRUE;
1825
0
  }
1826
0
  if(retry) {
1827
0
#define CONN_MAX_RETRIES 5
1828
0
    if(data->state.retrycount++ >= CONN_MAX_RETRIES) {
1829
0
      failf(data, "Connection died, tried %d times before giving up",
1830
0
            CONN_MAX_RETRIES);
1831
0
      data->state.retrycount = 0;
1832
0
      return CURLE_SEND_ERROR;
1833
0
    }
1834
0
    infof(data, "Connection died, retrying a fresh connect (retry count: %d)",
1835
0
          data->state.retrycount);
1836
0
    *url = strdup(data->state.url);
1837
0
    if(!*url)
1838
0
      return CURLE_OUT_OF_MEMORY;
1839
1840
0
    connclose(conn, "retry"); /* close this connection */
1841
0
    conn->bits.retry = TRUE; /* mark this as a connection we're about
1842
                                to retry. Marking it this way should
1843
                                prevent i.e HTTP transfers to return
1844
                                error just because nothing has been
1845
                                transferred! */
1846
1847
1848
0
    if((conn->handler->protocol&PROTO_FAMILY_HTTP) &&
1849
0
       data->req.writebytecount) {
1850
0
      data->state.rewindbeforesend = TRUE;
1851
0
      infof(data, "state.rewindbeforesend = TRUE");
1852
0
    }
1853
0
  }
1854
0
  return CURLE_OK;
1855
0
}
1856
1857
/*
1858
 * Curl_setup_transfer() is called to setup some basic properties for the
1859
 * upcoming transfer.
1860
 */
1861
void
1862
Curl_setup_transfer(
1863
  struct Curl_easy *data,   /* transfer */
1864
  int sockindex,            /* socket index to read from or -1 */
1865
  curl_off_t size,          /* -1 if unknown at this point */
1866
  bool getheader,           /* TRUE if header parsing is wanted */
1867
  int writesockindex        /* socket index to write to, it may very well be
1868
                               the same we read from. -1 disables */
1869
  )
1870
0
{
1871
0
  struct SingleRequest *k = &data->req;
1872
0
  struct connectdata *conn = data->conn;
1873
0
  struct HTTP *http = data->req.p.http;
1874
0
  bool httpsending;
1875
1876
0
  DEBUGASSERT(conn != NULL);
1877
0
  DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
1878
1879
0
  httpsending = ((conn->handler->protocol&PROTO_FAMILY_HTTP) &&
1880
0
                 (http->sending == HTTPSEND_REQUEST));
1881
1882
0
  if(conn->bits.multiplex || conn->httpversion >= 20 || httpsending) {
1883
    /* when multiplexing, the read/write sockets need to be the same! */
1884
0
    conn->sockfd = sockindex == -1 ?
1885
0
      ((writesockindex == -1 ? CURL_SOCKET_BAD : conn->sock[writesockindex])) :
1886
0
      conn->sock[sockindex];
1887
0
    conn->writesockfd = conn->sockfd;
1888
0
    if(httpsending)
1889
      /* special and very HTTP-specific */
1890
0
      writesockindex = FIRSTSOCKET;
1891
0
  }
1892
0
  else {
1893
0
    conn->sockfd = sockindex == -1 ?
1894
0
      CURL_SOCKET_BAD : conn->sock[sockindex];
1895
0
    conn->writesockfd = writesockindex == -1 ?
1896
0
      CURL_SOCKET_BAD:conn->sock[writesockindex];
1897
0
  }
1898
0
  k->getheader = getheader;
1899
1900
0
  k->size = size;
1901
1902
  /* The code sequence below is placed in this function just because all
1903
     necessary input is not always known in do_complete() as this function may
1904
     be called after that */
1905
1906
0
  if(!k->getheader) {
1907
0
    k->header = FALSE;
1908
0
    if(size > 0)
1909
0
      Curl_pgrsSetDownloadSize(data, size);
1910
0
  }
1911
  /* we want header and/or body, if neither then don't do this! */
1912
0
  if(k->getheader || !data->req.no_body) {
1913
1914
0
    if(sockindex != -1)
1915
0
      k->keepon |= KEEP_RECV;
1916
1917
0
    if(writesockindex != -1) {
1918
      /* HTTP 1.1 magic:
1919
1920
         Even if we require a 100-return code before uploading data, we might
1921
         need to write data before that since the REQUEST may not have been
1922
         finished sent off just yet.
1923
1924
         Thus, we must check if the request has been sent before we set the
1925
         state info where we wait for the 100-return code
1926
      */
1927
0
      if((data->state.expect100header) &&
1928
0
         (conn->handler->protocol&PROTO_FAMILY_HTTP) &&
1929
0
         (http->sending == HTTPSEND_BODY)) {
1930
        /* wait with write until we either got 100-continue or a timeout */
1931
0
        k->exp100 = EXP100_AWAITING_CONTINUE;
1932
0
        k->start100 = Curl_now();
1933
1934
        /* Set a timeout for the multi interface. Add the inaccuracy margin so
1935
           that we don't fire slightly too early and get denied to run. */
1936
0
        Curl_expire(data, data->set.expect_100_timeout, EXPIRE_100_TIMEOUT);
1937
0
      }
1938
0
      else {
1939
0
        if(data->state.expect100header)
1940
          /* when we've sent off the rest of the headers, we must await a
1941
             100-continue but first finish sending the request */
1942
0
          k->exp100 = EXP100_SENDING_REQUEST;
1943
1944
        /* enable the write bit when we're not waiting for continue */
1945
0
        k->keepon |= KEEP_SEND;
1946
0
      }
1947
0
    } /* if(writesockindex != -1) */
1948
0
  } /* if(k->getheader || !data->req.no_body) */
1949
1950
0
}