Coverage Report

Created: 2026-12-01 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/http.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
#include "urldata.h"
26
27
#ifndef CURL_DISABLE_HTTP
28
29
#ifdef HAVE_NETINET_IN_H
30
#include <netinet/in.h>
31
#endif
32
33
#ifdef HAVE_NETDB_H
34
#include <netdb.h>
35
#endif
36
#ifdef HAVE_ARPA_INET_H
37
#include <arpa/inet.h>
38
#endif
39
#ifdef HAVE_NET_IF_H
40
#include <net/if.h>
41
#endif
42
#ifdef HAVE_SYS_IOCTL_H
43
#include <sys/ioctl.h>
44
#endif
45
46
#ifdef HAVE_SYS_PARAM_H
47
#include <sys/param.h>
48
#endif
49
50
#include "transfer.h"
51
#include "sendf.h"
52
#include "curl_trc.h"
53
#include "formdata.h"
54
#include "mime.h"
55
#include "progress.h"
56
#include "curlx/base64.h"
57
#include "cookie.h"
58
#include "vauth/vauth.h"
59
#include "vquic/vquic.h"
60
#include "http_digest.h"
61
#include "http_negotiate.h"
62
#include "http_aws_sigv4.h"
63
#include "http_httpsig.h"
64
#include "url.h"
65
#include "urlapi-int.h"
66
#include "curl_share.h"
67
#include "dynhds.h"
68
#include "http.h"
69
#include "headers.h"
70
#include "select.h"
71
#include "parsedate.h" /* for the week day and month names */
72
#include "multiif.h"
73
#include "strcase.h"
74
#include "content_encoding.h"
75
#include "http_proxy.h"
76
#include "http2.h"
77
#include "cfilters.h"
78
#include "connect.h"
79
#include "curlx/strdup.h"
80
#include "altsvc.h"
81
#include "hsts.h"
82
#include "rtsp.h"
83
#include "ws.h"
84
#include "bufref.h"
85
#include "curlx/strparse.h"
86
87
void Curl_http_neg_init(struct Curl_easy *data, struct http_negotiation *neg)
88
85.2k
{
89
85.2k
  memset(neg, 0, sizeof(*neg));
90
85.2k
  neg->accept_09 = data->set.http09_allowed;
91
85.2k
  switch(data->set.httpwant) {
92
424
  case CURL_HTTP_VERSION_1_0:
93
424
    neg->wanted = neg->allowed = (CURL_HTTP_V1x);
94
424
    neg->only_10 = TRUE;
95
424
    break;
96
104
  case CURL_HTTP_VERSION_1_1:
97
104
    neg->wanted = neg->allowed = (CURL_HTTP_V1x);
98
104
    break;
99
643
  case CURL_HTTP_VERSION_2_0:
100
643
    neg->wanted = neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x);
101
643
    neg->h2_upgrade = TRUE;
102
643
    break;
103
18
  case CURL_HTTP_VERSION_2TLS:
104
18
    neg->wanted = neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x);
105
18
    break;
106
38.3k
  case CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE:
107
38.3k
    neg->wanted = neg->allowed = (CURL_HTTP_V2x);
108
38.3k
    data->state.http_neg.h2_prior_knowledge = TRUE;
109
38.3k
    break;
110
0
  case CURL_HTTP_VERSION_3:
111
0
    neg->wanted = (CURL_HTTP_V1x | CURL_HTTP_V2x | CURL_HTTP_V3x);
112
0
    neg->allowed = neg->wanted;
113
0
    break;
114
0
  case CURL_HTTP_VERSION_3ONLY:
115
0
    neg->wanted = neg->allowed = (CURL_HTTP_V3x);
116
0
    break;
117
45.6k
  case CURL_HTTP_VERSION_NONE:
118
45.6k
  default:
119
45.6k
    neg->wanted = (CURL_HTTP_V1x | CURL_HTTP_V2x);
120
45.6k
    neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x | CURL_HTTP_V3x);
121
45.6k
    break;
122
85.2k
  }
123
85.2k
}
124
125
#ifndef CURL_DISABLE_PROXY
126
/*
127
 * checkProxyHeaders() checks the linked list of custom proxy headers
128
 * if proxy headers are not available, then it will lookup into http header
129
 * link list
130
 *
131
 * It takes a connectdata struct as input to see if this is a proxy request or
132
 * not, as it then might check a different header list. Provide the header
133
 * prefix without colon!
134
 */
135
char *Curl_checkProxyheaders(struct Curl_easy *data,
136
                             const struct connectdata *conn,
137
                             const char *thisheader,
138
                             const size_t thislen)
139
0
{
140
0
  struct curl_slist *head;
141
142
0
  for(head = (conn->http_proxy.peer && data->set.sep_headers) ?
143
0
        data->set.proxyheaders : data->set.headers;
144
0
      head; head = head->next) {
145
0
    if(curl_strnequal(head->data, thisheader, thislen) &&
146
0
       Curl_headersep(head->data[thislen]))
147
0
      return head->data;
148
0
  }
149
150
0
  return NULL;
151
0
}
152
#endif
153
154
/* If the header has a value, this function returns TRUE and the value is in
155
   'outp' with blanks trimmed off. */
156
static bool header_has_value(const char **headerp, struct Curl_str *outp)
157
3.58k
{
158
3.58k
  bool value = !curlx_str_cspn(headerp, outp, ";:") &&
159
3.58k
    (!curlx_str_single(headerp, ':') || !curlx_str_single(headerp, ';'));
160
161
3.58k
  if(value) {
162
3.58k
    curlx_str_cspn(headerp, outp, "\r\n");
163
3.58k
    curlx_str_trimblanks(outp);
164
3.58k
  }
165
3.58k
  return value;
166
3.58k
}
167
168
static bool http_header_is_empty(const char *header)
169
1.76k
{
170
1.76k
  struct Curl_str out;
171
172
1.76k
  if(header_has_value(&header, &out)) {
173
1.76k
    return curlx_strlen(&out) == 0;
174
1.76k
  }
175
0
  return TRUE; /* invalid header format, treat as empty */
176
1.76k
}
177
178
/*
179
 * Strip off leading and trailing whitespace from the value in the given HTTP
180
 * header line and return a strdup-ed copy in 'valp' - returns an empty
181
 * string if the header value consists entirely of whitespace.
182
 *
183
 * If the header is provided as "name;", ending with a semicolon, it returns a
184
 * blank string.
185
 */
186
static CURLcode copy_custom_value(const char *header, char **valp)
187
1.82k
{
188
1.82k
  struct Curl_str out = { 0 };
189
190
  /* find the end of the header name */
191
1.82k
  if(header_has_value(&header, &out)) {
192
1.82k
    *valp = curlx_memdup0(curlx_str(&out), curlx_strlen(&out));
193
1.82k
    if(*valp)
194
1.81k
      return CURLE_OK;
195
9
    return CURLE_OUT_OF_MEMORY;
196
1.82k
  }
197
  /* bad input */
198
0
  *valp = NULL;
199
0
  return CURLE_BAD_FUNCTION_ARGUMENT;
200
1.82k
}
201
202
/*
203
 * Strip off leading and trailing whitespace from the value in the given HTTP
204
 * header line and return a strdup-ed copy in 'valp' - returns an empty
205
 * string if the header value consists entirely of whitespace.
206
 *
207
 * This function MUST be used after the header has already been confirmed to
208
 * lead with "word:".
209
 *
210
 * @unittest: 1626
211
 */
212
char *Curl_copy_header_value(const char *header)
213
11.7k
{
214
11.7k
  struct Curl_str out;
215
216
  /* find the end of the header name */
217
11.7k
  if(!curlx_str_until(&header, &out, MAX_HTTP_RESP_HEADER_SIZE, ':') &&
218
11.7k
     !curlx_str_single(&header, ':')) {
219
11.7k
    curlx_str_untilnl(&header, &out, MAX_HTTP_RESP_HEADER_SIZE);
220
11.7k
    curlx_str_trimblanks(&out);
221
11.7k
    return curlx_memdup0(curlx_str(&out), curlx_strlen(&out));
222
11.7k
  }
223
  /* bad input, should never happen */
224
0
  DEBUGASSERT(0);
225
0
  return NULL;
226
0
}
227
228
#ifndef CURL_DISABLE_HTTP_AUTH
229
230
#ifndef CURL_DISABLE_BASIC_AUTH
231
/*
232
 * http_output_basic() sets up an Authorization: header (or the proxy version)
233
 * for HTTP Basic authentication.
234
 *
235
 * Returns CURLcode.
236
 */
237
static CURLcode http_output_basic(struct Curl_easy *data,
238
                                  struct Curl_creds *creds,
239
                                  const char *hd_name,
240
                                  bool proxy)
241
913
{
242
913
  size_t size = 0;
243
913
  char *authorization = NULL;
244
913
  char **p_hd;
245
913
  CURLcode result;
246
913
  char *out;
247
248
  /* credentials are unique per transfer for HTTP, do not use the ones for the
249
     connection */
250
913
  if(proxy) {
251
0
#ifndef CURL_DISABLE_PROXY
252
0
    p_hd = &data->req.hd_proxy_auth;
253
#else
254
    return CURLE_NOT_BUILT_IN;
255
#endif
256
0
  }
257
913
  else {
258
913
    p_hd = &data->req.hd_auth;
259
913
  }
260
261
913
  if(!creds) {
262
0
    DEBUGASSERT(0);
263
0
    return CURLE_FAILED_INIT;
264
0
  }
265
266
913
  out = curl_maprintf("%s:%s", creds->user, creds->passwd);
267
913
  if(!out)
268
8
    return CURLE_OUT_OF_MEMORY;
269
270
905
  result = curlx_base64_encode((uint8_t *)out, strlen(out),
271
905
                               &authorization, &size);
272
905
  if(result)
273
6
    goto fail;
274
275
899
  if(!authorization) {
276
0
    result = CURLE_REMOTE_ACCESS_DENIED;
277
0
    goto fail;
278
0
  }
279
280
899
  curlx_free(*p_hd);
281
899
  *p_hd = curl_maprintf("%s: Basic %s\r\n", hd_name, authorization);
282
899
  curlx_free(authorization);
283
899
  if(!*p_hd) {
284
10
    result = CURLE_OUT_OF_MEMORY;
285
10
    goto fail;
286
10
  }
287
288
905
fail:
289
905
  curlx_free(out);
290
905
  return result;
291
899
}
292
293
#endif
294
295
#ifndef CURL_DISABLE_BEARER_AUTH
296
/* Sets up an Authorization: header for HTTP Bearer authentication. */
297
static CURLcode http_output_bearer(struct Curl_easy *data)
298
34
{
299
34
  curlx_free(data->req.hd_auth);
300
34
  if(!Curl_creds_has_oauth_bearer(data->state.creds)) {
301
0
    DEBUGASSERT(0); /* we should not have come here */
302
0
    return CURLE_FAILED_INIT;
303
0
  }
304
34
  data->req.hd_auth =
305
34
    curl_maprintf("Authorization: Bearer %s\r\n",
306
34
                  Curl_creds_oauth_bearer(data->state.creds));
307
308
34
  if(!data->req.hd_auth)
309
0
    return CURLE_OUT_OF_MEMORY;
310
34
  return CURLE_OK;
311
34
}
312
#endif
313
314
#endif
315
316
/* pickoneauth() selects the most favorable authentication method from the
317
 * ones available and the ones we want.
318
 *
319
 * return TRUE if one was picked
320
 */
321
static bool pickoneauth(struct auth *pick, unsigned long mask,
322
                        struct Curl_creds *creds)
323
294
{
324
294
  bool have_user_pass = Curl_creds_has_user_or_pass(creds);
325
294
  bool picked;
326
  /* only deal with authentication we want */
327
294
  unsigned long avail = pick->avail & pick->want & mask;
328
294
  picked = TRUE;
329
330
  /* The order of these checks is highly relevant, as this will be the order
331
     of preference in case of the existence of multiple accepted types. */
332
294
  if(avail & CURLAUTH_NEGOTIATE)  /* available on empty creds */
333
0
    pick->picked = CURLAUTH_NEGOTIATE;
334
294
#ifndef CURL_DISABLE_BEARER_AUTH
335
294
  else if((avail & CURLAUTH_BEARER) && Curl_creds_has_oauth_bearer(creds))
336
0
    pick->picked = CURLAUTH_BEARER;
337
294
#endif
338
294
#ifndef CURL_DISABLE_DIGEST_AUTH
339
294
  else if((avail & CURLAUTH_DIGEST) && have_user_pass)
340
0
    pick->picked = CURLAUTH_DIGEST;
341
294
#endif
342
294
#ifndef CURL_DISABLE_BASIC_AUTH
343
294
  else if((avail & CURLAUTH_BASIC) && have_user_pass)
344
0
    pick->picked = CURLAUTH_BASIC;
345
294
#endif
346
294
#ifndef CURL_DISABLE_AWS
347
294
  else if(avail & CURLAUTH_AWS_SIGV4)
348
0
    pick->picked = CURLAUTH_AWS_SIGV4;
349
294
#endif
350
294
#ifndef CURL_DISABLE_HTTPSIG
351
294
  else if(avail & CURLAUTH_HTTPSIG)
352
0
    pick->picked = CURLAUTH_HTTPSIG;
353
294
#endif
354
294
  else {
355
294
    pick->picked = CURLAUTH_PICKNONE; /* we select to use nothing */
356
294
    picked = FALSE;
357
294
  }
358
294
  pick->avail = CURLAUTH_NONE; /* clear it here */
359
360
294
  return picked;
361
294
}
362
363
/*
364
 * http_perhapsrewind()
365
 *
366
 * The current request needs to be done again - maybe due to a follow
367
 * or authentication negotiation. Check if:
368
 * 1) a rewind of the data sent to the server is necessary
369
 * 2) the current transfer should continue or be stopped early
370
 */
371
static CURLcode http_perhapsrewind(struct Curl_easy *data,
372
                                   struct connectdata *conn)
373
641
{
374
641
  curl_off_t bytessent = data->req.writebytecount;
375
641
  curl_off_t expectsend = Curl_creader_total_length(data);
376
641
  curl_off_t upload_remain = (expectsend >= 0) ? (expectsend - bytessent) : -1;
377
641
  bool little_upload_remains = (upload_remain >= 0 && upload_remain < 2000);
378
641
  bool needs_rewind = Curl_creader_needs_rewind(data);
379
  /* By default, we would like to abort the transfer when little or unknown
380
   * amount remains. This may be overridden by authentications further
381
   * below! */
382
641
  bool abort_upload = (!data->req.upload_done && !little_upload_remains);
383
641
  VERBOSE(const char *ongoing_auth = NULL);
384
385
  /* We need a rewind before uploading client read data again. The
386
   * checks below influence of the upload is to be continued
387
   * or aborted early.
388
   * This depends on how much remains to be sent and in what state
389
   * the authentication is. Some auth schemes such as Negotiate do not work
390
   * for a new connection. */
391
641
  if(needs_rewind) {
392
223
    infof(data, "Need to rewind upload for next request");
393
223
    Curl_creader_set_rewind(data, TRUE);
394
223
  }
395
396
641
  if(conn->bits.close)
397
    /* If we already decided to close this connection, we cannot veto. */
398
21
    return CURLE_OK;
399
400
620
  if(abort_upload) {
401
    /* We would like to abort the upload - but should we? */
402
#ifdef USE_SPNEGO
403
    /* There is still data left to send */
404
    if((data->state.authproxy.picked == CURLAUTH_NEGOTIATE) ||
405
       (data->state.authhost.picked == CURLAUTH_NEGOTIATE)) {
406
      VERBOSE(ongoing_auth = "NEGOTIATE");
407
      if((conn->http_negotiate_state != CURL_NEGO_AUTH_NONE) ||
408
         (conn->proxy_negotiate_state != CURL_NEGO_AUTH_NONE)) {
409
        /* The NEGOTIATE-negotiation has started, keep on sending.
410
         * Need to do further work on same connection */
411
        abort_upload = FALSE;
412
      }
413
    }
414
#endif
415
115
  }
416
417
620
  if(abort_upload) {
418
115
    if(upload_remain >= 0)
419
76
      infof(data, "%s%sclose instead of sending %" FMT_OFF_T " more bytes",
420
115
            ongoing_auth ? ongoing_auth : "",
421
115
            ongoing_auth ? " send, " : "",
422
115
            upload_remain);
423
39
    else
424
39
      infof(data, "%s%sclose instead of sending unknown amount "
425
115
            "of more bytes",
426
115
            ongoing_auth ? ongoing_auth : "",
427
115
            ongoing_auth ? " send, " : "");
428
    /* We decided to abort the ongoing transfer */
429
115
    streamclose(conn);
430
115
    data->req.size = 0; /* do not download any more than 0 bytes */
431
115
    data->req.http_bodyless = TRUE;
432
115
  }
433
620
  return CURLE_OK;
434
641
}
435
436
/**
437
 * http_should_fail() determines whether an HTTP response code has gotten us
438
 * into an error state or not.
439
 *
440
 * @retval FALSE communications should continue
441
 *
442
 * @retval TRUE communications should not continue
443
 */
444
static bool http_should_fail(struct Curl_easy *data, int httpcode)
445
12.6k
{
446
12.6k
  DEBUGASSERT(data);
447
12.6k
  DEBUGASSERT(data->conn);
448
449
  /*
450
   * If we have not been asked to fail on error,
451
   * do not fail.
452
   */
453
12.6k
  if(!data->set.http_fail_on_error)
454
12.4k
    return FALSE;
455
456
  /*
457
   * Any code < 400 is never terminal.
458
   */
459
219
  if(httpcode < 400)
460
47
    return FALSE;
461
462
  /*
463
   * A 416 response to a resume request is presumably because the file is
464
   * already completely downloaded and thus not actually a fail.
465
   */
466
172
  if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
467
70
     httpcode == 416)
468
28
    return FALSE;
469
470
  /*
471
   * Any code >= 400 that is not 401 or 407 is always
472
   * a terminal error
473
   */
474
144
  if((httpcode != 401) && (httpcode != 407))
475
82
    return TRUE;
476
477
  /*
478
   * All we have left to deal with is 401 and 407
479
   */
480
62
  DEBUGASSERT((httpcode == 401) || (httpcode == 407));
481
482
  /*
483
   * Examine the current authentication state to see if this is an error. The
484
   * idea is for this function to get called after processing all the headers
485
   * in a response message. If we have been asked to authenticate at
486
   * a particular stage, and we have done it, we are OK. If we are already
487
   * completely authenticated, it is not OK to get another 401 or 407.
488
   *
489
   * It is possible for authentication to go stale such that the client needs
490
   * to reauthenticate. Once that info is available, use it here.
491
   */
492
493
  /*
494
   * Either we are not authenticating, or we are supposed to be authenticating
495
   * something else. This is an error.
496
   */
497
62
  if((httpcode == 401) && !data->state.creds)
498
13
    return TRUE;
499
49
#ifndef CURL_DISABLE_PROXY
500
49
  if((httpcode == 407) && !data->conn->http_proxy.creds)
501
13
    return TRUE;
502
36
#endif
503
504
36
  return (bool)data->state.authproblem;
505
49
}
506
507
/*
508
 * Curl_http_auth_act() gets called when all HTTP headers have been received
509
 * and it checks what authentication methods that are available and decides
510
 * which one (if any) to use. It will set 'newurl' if an auth method was
511
 * picked.
512
 */
513
CURLcode Curl_http_auth_act(struct Curl_easy *data)
514
6.30k
{
515
6.30k
  struct connectdata *conn = data->conn;
516
6.30k
  bool pickhost = FALSE;
517
6.30k
  bool pickproxy = FALSE;
518
6.30k
  CURLcode result = CURLE_OK;
519
6.30k
  unsigned long authmask = ~0UL;
520
521
6.30k
  if(!Curl_creds_has_oauth_bearer(data->state.creds))
522
6.16k
    authmask &= (unsigned long)~CURLAUTH_BEARER;
523
524
6.30k
  if(100 <= data->req.httpcode && data->req.httpcode <= 199)
525
    /* this is a transient response code, ignore */
526
0
    return CURLE_OK;
527
528
6.30k
  if(data->state.authproblem)
529
29
    return data->set.http_fail_on_error ? CURLE_HTTP_RETURNED_ERROR : CURLE_OK;
530
531
6.27k
  if(data->state.creds &&
532
536
     ((data->req.httpcode == 401) ||
533
511
      (data->req.authneg && data->req.httpcode < 300))) {
534
294
    pickhost = pickoneauth(&data->state.authhost, authmask, data->state.creds);
535
294
    if(!pickhost)
536
294
      data->state.authproblem = TRUE;
537
0
    else
538
0
      data->info.httpauthpicked = data->state.authhost.picked;
539
294
  }
540
6.27k
#ifndef CURL_DISABLE_PROXY
541
6.27k
  if(conn->http_proxy.creds &&
542
0
     ((data->req.httpcode == 407) ||
543
0
      (data->req.authneg && data->req.httpcode < 300))) {
544
0
    pickproxy = pickoneauth(&data->state.authproxy,
545
0
                            authmask & ~CURLAUTH_BEARER,
546
0
                            conn->http_proxy.creds);
547
0
    if(!pickproxy)
548
0
      data->state.authproblem = TRUE;
549
0
    else
550
0
      data->info.proxyauthpicked = data->state.authproxy.picked;
551
0
  }
552
6.27k
#endif
553
554
6.27k
  if(pickhost || pickproxy) {
555
0
    result = http_perhapsrewind(data, conn);
556
0
    if(result)
557
0
      return result;
558
559
    /* In case this is GSS auth, the newurl field is already allocated so
560
       we must make sure to free it before allocating a new one. As figured
561
       out in bug #2284386 */
562
0
    curlx_free(data->req.newurl);
563
    /* clone URL */
564
0
    data->req.newurl = Curl_bufref_dup(&data->state.url);
565
0
    if(!data->req.newurl)
566
0
      return CURLE_OUT_OF_MEMORY;
567
0
  }
568
6.27k
  else if((data->req.httpcode < 300) &&
569
4.32k
          !data->state.authhost.done &&
570
295
          data->req.authneg &&
571
          /* no (known) authentication available,
572
             authentication is not "done" yet and
573
             no authentication seems to be required and
574
             we did not try HEAD or GET */
575
269
          (data->state.httpreq != HTTPREQ_GET) &&
576
269
          (data->state.httpreq != HTTPREQ_HEAD)) {
577
    /* clone URL */
578
269
    data->req.newurl = Curl_bufref_dup(&data->state.url);
579
269
    if(!data->req.newurl)
580
0
      return CURLE_OUT_OF_MEMORY;
581
269
    data->state.authhost.done = TRUE;
582
269
  }
583
6.27k
  if(http_should_fail(data, data->req.httpcode)) {
584
18
    failf(data, "The requested URL returned error: %d",
585
18
          data->req.httpcode);
586
18
    result = CURLE_HTTP_RETURNED_ERROR;
587
18
  }
588
589
6.27k
  return result;
590
6.27k
}
591
592
#ifndef CURL_DISABLE_HTTP_AUTH
593
/*
594
 * Output the correct authentication header depending on the auth type
595
 * and whether or not it is to a proxy.
596
 */
597
static CURLcode output_auth_headers(struct Curl_easy *data,
598
                                    struct connectdata *conn,
599
                                    struct auth *authstatus,
600
                                    const char *method,
601
                                    const char *path,
602
                                    bool proxy,
603
                                    struct Curl_creds *creds,
604
                                    const char *hd_name,
605
                                    size_t hd_len,
606
                                    const char **pauth_used)
607
7.19k
{
608
7.19k
  CURLcode result = CURLE_OK;
609
610
7.19k
  *pauth_used = NULL;
611
7.19k
  switch(authstatus->picked) {
612
613
0
#ifndef CURL_DISABLE_AWS
614
2.96k
  case CURLAUTH_AWS_SIGV4:
615
2.96k
    if(!proxy) {
616
      /* this method is never for proxy */
617
2.96k
      *pauth_used = "AWS_SIGV4";
618
2.96k
      result = Curl_output_aws_sigv4(data);
619
2.96k
    }
620
2.96k
    break;
621
0
#endif
622
623
0
#ifndef CURL_DISABLE_HTTPSIG
624
1.60k
  case CURLAUTH_HTTPSIG:
625
1.60k
    if(!proxy) {
626
      /* HTTPSIG uses its own configured key material rather than
627
         data->state.creds. Do not let unrelated credentials from a
628
         redirected URL bypass the cross-host auth boundary. */
629
1.60k
      if(Curl_auth_allowed_to_host(data)) {
630
1.60k
        *pauth_used = "HTTPSIG";
631
1.60k
        result = Curl_output_httpsig(data);
632
1.60k
      }
633
0
      else
634
0
        authstatus->done = TRUE;
635
1.60k
    }
636
1.60k
    break;
637
0
#endif
638
639
#ifdef USE_SPNEGO
640
  case CURLAUTH_NEGOTIATE:
641
    if(
642
#ifndef CURL_DISABLE_PROXY
643
      (proxy && !Curl_checkProxyheaders(data, conn, hd_name, hd_len)) ||
644
#endif
645
      (!proxy && !Curl_checkheaders(data, hd_name, hd_len) &&
646
       Curl_auth_allowed_to_host(data))) {
647
      *pauth_used = "Negotiate";
648
      result = Curl_output_negotiate(data, conn, hd_name, proxy);
649
    }
650
    else
651
      authstatus->done = TRUE;
652
    break;
653
#endif
654
655
0
#ifndef CURL_DISABLE_DIGEST_AUTH
656
661
  case CURLAUTH_DIGEST:
657
661
    *pauth_used = "Digest";
658
661
    result = Curl_output_digest(data, creds, hd_name, proxy,
659
661
                                (const unsigned char *)method,
660
661
                                (const unsigned char *)path);
661
661
    break;
662
0
#endif
663
664
0
#ifndef CURL_DISABLE_BASIC_AUTH
665
1.56k
  case CURLAUTH_BASIC:
666
1.56k
    if(creds && Curl_creds_has_user_or_pass(creds) && (
667
913
#ifndef CURL_DISABLE_PROXY
668
913
       (proxy && !Curl_checkProxyheaders(data, conn, hd_name, hd_len)) ||
669
913
#endif
670
913
       (!proxy && !Curl_checkheaders(data, hd_name, hd_len)))) {
671
913
      *pauth_used = "Basic";
672
913
      result = http_output_basic(data, creds, hd_name, proxy);
673
913
      if(result)
674
24
        return result;
675
913
    }
676
    /* NOTE: this function should set 'done' TRUE, as the other auth
677
       functions work that way */
678
1.54k
    authstatus->done = TRUE;
679
1.54k
    break;
680
0
#endif
681
682
0
#ifndef CURL_DISABLE_BEARER_AUTH
683
71
  case CURLAUTH_BEARER:
684
71
    if(!proxy && Curl_creds_has_oauth_bearer(creds) &&
685
34
       !Curl_checkheaders(data, hd_name, hd_len)) {
686
34
      *pauth_used = "Bearer";
687
34
      result = http_output_bearer(data);
688
34
      if(result)
689
0
        return result;
690
34
    }
691
    /* NOTE: this function should set 'done' TRUE, as the other auth
692
       functions work that way */
693
71
    authstatus->done = TRUE;
694
71
    break;
695
0
#endif
696
697
331
  default:
698
    /* a pick we do not support? */
699
331
    (void)conn;
700
#ifdef CURL_DISABLE_DIGEST_AUTH
701
    (void)method;
702
    (void)path;
703
#endif
704
331
    break;
705
7.19k
  } /* end of switch */
706
707
7.17k
  return result;
708
7.19k
}
709
710
CURLcode Curl_http_output_auth(struct Curl_easy *data,
711
                               struct connectdata *conn,
712
                               Curl_HttpReq httpreq,
713
                               const char *method,
714
                               const char *path,
715
                               const char *query,
716
                               bool is_connect)
717
62.8k
{
718
62.8k
  CURLcode result = CURLE_OK;
719
62.8k
  struct auth *authhost;
720
62.8k
  struct auth *authproxy;
721
62.8k
  const char *path_and_query = path;
722
62.8k
  char *tmp_str = NULL;
723
62.8k
  const char *auth_used;
724
725
62.8k
  DEBUGASSERT(data);
726
62.8k
  authhost = &data->state.authhost;
727
62.8k
  authproxy = &data->state.authproxy;
728
729
62.8k
  if(
730
62.8k
#ifndef CURL_DISABLE_PROXY
731
62.8k
    (!conn->http_proxy.peer || !conn->http_proxy.creds) &&
732
62.8k
#endif
733
#ifdef USE_SPNEGO
734
    !(authhost->want & CURLAUTH_NEGOTIATE) &&
735
    !(authproxy->want & CURLAUTH_NEGOTIATE) &&
736
#endif
737
62.8k
#ifndef CURL_DISABLE_HTTPSIG
738
62.8k
    !(authhost->want & CURLAUTH_HTTPSIG) &&
739
61.0k
#endif
740
61.0k
    !data->state.creds) {
741
    /* no authentication with no user or password */
742
55.6k
    authhost->done = TRUE;
743
55.6k
    authproxy->done = TRUE;
744
55.6k
    result = CURLE_OK;
745
55.6k
    goto out;
746
55.6k
  }
747
748
7.21k
  if(query) {
749
1.23k
    tmp_str = curl_maprintf("%s?%s", path, query);
750
1.23k
    if(!tmp_str) {
751
5
      result = CURLE_OUT_OF_MEMORY;
752
5
      goto out;
753
5
    }
754
1.23k
    path_and_query = tmp_str;
755
1.23k
  }
756
757
7.20k
  if(authhost->want && !authhost->picked)
758
    /* The app has selected one or more methods, but none has been picked
759
       so far by a server round-trip. Then we set the picked one to the
760
       want one, and if this is one single bit it will be used instantly. */
761
5.09k
    authhost->picked = authhost->want;
762
763
7.20k
  if(authproxy->want && !authproxy->picked)
764
    /* The app has selected one or more methods, but none has been picked so
765
       far by a proxy round-trip. Then we set the picked one to the want one,
766
       and if this is one single bit it will be used instantly. */
767
5.09k
    authproxy->picked = authproxy->want;
768
769
7.20k
#ifndef CURL_DISABLE_PROXY
770
  /* Send proxy authentication header only with a forwarding proxy
771
   * or when doing a CONNECT* request. */
772
7.20k
  if(conn->bits.origin_is_proxy || is_connect) {
773
0
    result = output_auth_headers(data, conn, authproxy,
774
0
                                 method, path_and_query,
775
0
                                 TRUE, conn->http_proxy.creds,
776
0
                                 STRCONST("Proxy-Authorization"),
777
0
                                 &auth_used);
778
0
    if(result)
779
0
      goto out;
780
0
    data->info.proxyauthpicked = auth_used ? authproxy->picked : 0;
781
0
    authproxy->multipass = auth_used ? !authproxy->done : FALSE;
782
0
    if(auth_used)
783
0
      infof(data, "Proxy auth using %s with user '%s'", auth_used,
784
0
            conn->http_proxy.creds ? conn->http_proxy.creds->user : "");
785
0
  }
786
7.20k
  else
787
7.20k
    authproxy->done = TRUE;
788
#else
789
  /* no proxy, pretend we are done authenticating with it */
790
  (void)is_connect;
791
  authproxy->done = TRUE;
792
#endif /* CURL_DISABLE_PROXY */
793
794
  /* Either we have credentials for the origin we talk to or
795
     performing authentication is allowed here */
796
7.20k
  if(!is_connect &&
797
7.20k
     (data->state.creds || Curl_auth_allowed_to_host(data))) {
798
7.19k
    result = output_auth_headers(data, conn, authhost,
799
7.19k
                                 method, path_and_query,
800
7.19k
                                 FALSE, data->state.creds,
801
7.19k
                                 STRCONST("Authorization"),
802
7.19k
                                 &auth_used);
803
7.19k
    if(result)
804
1.66k
      goto out;
805
5.53k
    data->info.httpauthpicked = auth_used ? authhost->picked : 0;
806
5.53k
    authhost->multipass = auth_used ? !authhost->done : FALSE;
807
5.53k
    if(auth_used)
808
4.51k
      infof(data, "Server auth using %s with user '%s'", auth_used,
809
5.53k
            data->state.creds ? data->state.creds->user : "");
810
5.53k
  }
811
10
  else
812
10
    authhost->done = TRUE;
813
814
5.54k
  if(((authhost->multipass && !authhost->done) ||
815
4.88k
      (authproxy->multipass && !authproxy->done)) &&
816
667
     (httpreq != HTTPREQ_GET) &&
817
488
     (httpreq != HTTPREQ_HEAD)) {
818
    /* Auth is required and we are not authenticated yet. Make a PUT or POST
819
       with content-length zero as a "probe". */
820
453
    data->req.authneg = TRUE;
821
453
  }
822
5.09k
  else
823
5.09k
    data->req.authneg = FALSE;
824
825
62.8k
out:
826
62.8k
  curlx_free(tmp_str);
827
62.8k
  return result;
828
5.54k
}
829
830
#else /* !CURL_DISABLE_HTTP_AUTH */
831
/* when disabled */
832
CURLcode Curl_http_output_auth(struct Curl_easy *data,
833
                               struct connectdata *conn,
834
                               Curl_HttpReq httpreq,
835
                               const char *method,
836
                               const char *path,
837
                               const char *query,
838
                               bool is_connect)
839
{
840
  (void)data;
841
  (void)conn;
842
  (void)httpreq;
843
  (void)method;
844
  (void)path;
845
  (void)query;
846
  (void)is_connect;
847
  return CURLE_OK;
848
}
849
#endif /* !CURL_DISABLE_HTTP_AUTH, else */
850
851
#if defined(USE_SPNEGO) || \
852
  !defined(CURL_DISABLE_DIGEST_AUTH) || \
853
  !defined(CURL_DISABLE_BASIC_AUTH) || \
854
  !defined(CURL_DISABLE_BEARER_AUTH)
855
static bool authcmp(const char *auth, const char *line)
856
0
{
857
  /* the auth string must not have an alnum following */
858
0
  size_t n = strlen(auth);
859
0
  return curl_strnequal(auth, line, n) && !ISALNUM(line[n]);
860
0
}
861
#endif
862
863
#ifdef USE_SPNEGO
864
static CURLcode auth_spnego(struct Curl_easy *data,
865
                            bool proxy,
866
                            const char *auth,
867
                            struct auth *authp,
868
                            uint32_t *availp)
869
{
870
  if((authp->avail & CURLAUTH_NEGOTIATE) || Curl_auth_is_spnego_supported()) {
871
    *availp |= CURLAUTH_NEGOTIATE;
872
    authp->avail |= CURLAUTH_NEGOTIATE;
873
874
    if(authp->picked == CURLAUTH_NEGOTIATE) {
875
      struct connectdata *conn = data->conn;
876
      CURLcode result = Curl_input_negotiate(data, conn, proxy, auth);
877
      uint8_t *negstate = proxy ? &conn->proxy_negotiate_state :
878
        &conn->http_negotiate_state;
879
      if(!result) {
880
        curlx_free(data->req.newurl);
881
        data->req.newurl = Curl_bufref_dup(&data->state.url);
882
        if(!data->req.newurl)
883
          return CURLE_OUT_OF_MEMORY;
884
        data->state.authproblem = FALSE;
885
        /* we received a GSS auth token and we dealt with it fine */
886
        *negstate = CURL_NEGO_AUTH_RECV;
887
      }
888
      else
889
        data->state.authproblem = TRUE;
890
    }
891
  }
892
  return CURLE_OK;
893
}
894
#endif
895
896
#ifndef CURL_DISABLE_DIGEST_AUTH
897
static CURLcode auth_digest(struct Curl_easy *data,
898
                            bool proxy,
899
                            const char *auth,
900
                            struct auth *authp,
901
                            uint32_t *availp)
902
0
{
903
0
  if(authp->avail & CURLAUTH_DIGEST) {
904
0
    *availp |= CURLAUTH_DIGEST;
905
0
    infof(data, "Ignoring duplicate digest auth header.");
906
0
  }
907
0
  else if(Curl_auth_is_digest_supported()) {
908
    /* We call this function on input Digest headers even if Digest
909
     * authentication is not activated yet, as we need to store the
910
     * incoming data from this header in case we are going to use
911
     * Digest */
912
0
    CURLcode result = Curl_input_digest(data, proxy, auth);
913
0
    if(result == CURLE_OUT_OF_MEMORY)
914
0
      return result;
915
0
    if(result == CURLE_NOT_BUILT_IN) {
916
0
      infof(data, "Ignoring unusable Digest algorithm.");
917
0
      return CURLE_OK;
918
0
    }
919
0
    *availp |= CURLAUTH_DIGEST;
920
0
    authp->avail |= CURLAUTH_DIGEST;
921
0
    if(result) {
922
0
      infof(data, "Digest authentication problem, ignoring.");
923
0
      data->state.authproblem = TRUE;
924
0
    }
925
0
  }
926
0
  return CURLE_OK;
927
0
}
928
#endif
929
930
#ifndef CURL_DISABLE_BASIC_AUTH
931
static CURLcode auth_basic(struct Curl_easy *data,
932
                           struct auth *authp,
933
                           uint32_t *availp)
934
0
{
935
0
  *availp |= CURLAUTH_BASIC;
936
0
  authp->avail |= CURLAUTH_BASIC;
937
0
  if(authp->picked == CURLAUTH_BASIC) {
938
    /* We asked for Basic authentication but got a 40X back anyway, which
939
       means our name+password is not valid. */
940
0
    authp->avail = CURLAUTH_NONE;
941
0
    infof(data, "Basic authentication problem, ignoring.");
942
0
    data->state.authproblem = TRUE;
943
0
  }
944
0
  return CURLE_OK;
945
0
}
946
#endif
947
948
#ifndef CURL_DISABLE_BEARER_AUTH
949
static CURLcode auth_bearer(struct Curl_easy *data,
950
                            struct auth *authp,
951
                            uint32_t *availp)
952
0
{
953
0
  *availp |= CURLAUTH_BEARER;
954
0
  authp->avail |= CURLAUTH_BEARER;
955
0
  if(authp->picked == CURLAUTH_BEARER) {
956
    /* We asked for Bearer authentication but got a 40X back anyway, which
957
       means our token is not valid. */
958
0
    authp->avail = CURLAUTH_NONE;
959
0
    infof(data, "Bearer authentication problem, ignoring.");
960
0
    data->state.authproblem = TRUE;
961
0
  }
962
0
  return CURLE_OK;
963
0
}
964
#endif
965
966
/*
967
 * Curl_http_input_auth() deals with Proxy-Authenticate: and WWW-Authenticate:
968
 * headers. They are dealt with both in the transfer.c main loop and in the
969
 * proxy CONNECT loop.
970
 *
971
 * The 'auth' line ends with a NUL byte without CR or LF present.
972
 */
973
CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
974
                              const char *auth) /* the first non-space */
975
0
{
976
  /*
977
   * This resource requires authentication
978
   */
979
0
#if defined(USE_SPNEGO) ||                      \
980
0
  !defined(CURL_DISABLE_DIGEST_AUTH) ||         \
981
0
  !defined(CURL_DISABLE_BASIC_AUTH) ||          \
982
0
  !defined(CURL_DISABLE_BEARER_AUTH)
983
984
0
  uint32_t *availp;
985
0
  struct auth *authp;
986
0
  CURLcode result = CURLE_OK;
987
0
  DEBUGASSERT(auth);
988
0
  DEBUGASSERT(data);
989
990
0
  if(proxy) {
991
0
    availp = &data->info.proxyauthavail;
992
0
    authp = &data->state.authproxy;
993
0
  }
994
0
  else {
995
0
    availp = &data->info.httpauthavail;
996
0
    authp = &data->state.authhost;
997
0
  }
998
999
  /*
1000
   * Here we check if we want the specific single authentication (using ==) and
1001
   * if we do, we initiate usage of it.
1002
   *
1003
   * If the provided authentication is wanted as one out of several accepted
1004
   * types (using &), we OR this authentication type to the authavail
1005
   * variable.
1006
   *
1007
   * Note:
1008
   *
1009
   * ->picked is first set to the 'want' value (one or more bits) before the
1010
   * request is sent, and then it is again set _after_ all response 401/407
1011
   * headers have been received but then only to a single preferred method
1012
   * (bit).
1013
   */
1014
1015
0
  while(*auth) {
1016
#ifdef USE_SPNEGO
1017
    if(authcmp("Negotiate", auth))
1018
      result = auth_spnego(data, proxy, auth, authp, availp);
1019
#endif
1020
0
#ifndef CURL_DISABLE_DIGEST_AUTH
1021
0
    if(!result && authcmp("Digest", auth))
1022
0
      result = auth_digest(data, proxy, auth, authp, availp);
1023
0
#endif
1024
0
#ifndef CURL_DISABLE_BASIC_AUTH
1025
0
    if(!result && authcmp("Basic", auth))
1026
0
      result = auth_basic(data, authp, availp);
1027
0
#endif
1028
0
#ifndef CURL_DISABLE_BEARER_AUTH
1029
0
    if(authcmp("Bearer", auth))
1030
0
      result = auth_bearer(data, authp, availp);
1031
0
#endif
1032
1033
0
    if(result)
1034
0
      break;
1035
1036
    /* there may be multiple methods on one line, so keep reading */
1037
0
    auth = strchr(auth, ',');
1038
0
    if(auth) /* if we are on a comma, skip it */
1039
0
      auth++;
1040
0
    else
1041
0
      break;
1042
0
    curlx_str_passblanks(&auth);
1043
0
  }
1044
0
  return result;
1045
#else
1046
  (void)data;
1047
  (void)proxy;
1048
  (void)auth;
1049
  /* nothing to do when disabled */
1050
  return CURLE_OK;
1051
#endif
1052
0
}
1053
1054
static void http_switch_to_get(struct Curl_easy *data, int code)
1055
0
{
1056
0
  const char *req = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
1057
1058
0
  if((req || data->state.httpreq != HTTPREQ_GET) &&
1059
0
     (data->set.http_follow_mode == CURLFOLLOW_OBEYCODE)) {
1060
0
    NOVERBOSE((void)code);
1061
0
    infof(data, "Switch to GET because of %d response", code);
1062
0
    data->state.http_ignorecustom = TRUE;
1063
0
  }
1064
0
  else if(req && (data->set.http_follow_mode != CURLFOLLOW_FIRSTONLY))
1065
0
    infof(data, "Stick to %s instead of GET", req);
1066
1067
0
  data->state.httpreq = HTTPREQ_GET;
1068
0
  Curl_creader_set_rewind(data, FALSE);
1069
0
}
1070
1071
#define HTTPREQ_IS_POST(data)                    \
1072
0
  ((data)->state.httpreq == HTTPREQ_POST ||      \
1073
0
   (data)->state.httpreq == HTTPREQ_POST_FORM || \
1074
0
   (data)->state.httpreq == HTTPREQ_POST_MIME)
1075
1076
static CURLcode set_auto_referer(struct Curl_easy *data)
1077
22
{
1078
22
  CURLU *u;
1079
22
  char *referer = NULL;
1080
22
  CURLUcode uc;
1081
1082
  /* We are asked to automatically set the previous URL as the referer
1083
     when we get the next URL. We pick the ->url field, which may or may
1084
     not be 100% correct */
1085
22
  Curl_bufref_free(&data->state.referer);
1086
1087
  /* Make a copy of the URL without credentials and fragment */
1088
22
  u = curl_url();
1089
22
  if(!u)
1090
0
    return CURLE_OUT_OF_MEMORY;
1091
1092
22
  uc = curl_url_set(u, CURLUPART_URL,
1093
22
                    Curl_bufref_ptr(&data->state.url), 0);
1094
22
  if(!uc)
1095
22
    uc = curl_url_set(u, CURLUPART_FRAGMENT, NULL, 0);
1096
22
  if(!uc)
1097
22
    uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
1098
22
  if(!uc)
1099
22
    uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
1100
22
  if(!uc)
1101
22
    uc = curl_url_get(u, CURLUPART_URL, &referer, 0);
1102
1103
22
  curl_url_cleanup(u);
1104
1105
22
  if(uc || !referer)
1106
0
    return CURLE_OUT_OF_MEMORY;
1107
1108
22
  Curl_bufref_set(&data->state.referer, referer, 0, curl_free);
1109
22
  return CURLE_OK;
1110
22
}
1111
1112
static CURLcode follow_parse_url(struct Curl_easy *data, const char *newurl,
1113
                                 followtype type, char **follow_urlp,
1114
                                 CURLUcode *ucp)
1115
10.5k
{
1116
10.5k
  CURLUcode uc;
1117
1118
10.5k
  DEBUGASSERT(data->state.uh);
1119
10.5k
  uc = curl_url_set(data->state.uh, CURLUPART_URL, newurl, (unsigned int)
1120
10.5k
                    ((type == FOLLOW_FAKE) ? CURLU_NON_SUPPORT_SCHEME :
1121
10.5k
                     ((type == FOLLOW_REDIR) ? CURLU_URLENCODE : 0) |
1122
10.3k
                     CURLU_ALLOW_SPACE |
1123
10.3k
                     (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)));
1124
10.5k
  *ucp = uc;
1125
10.5k
  if(uc) {
1126
90
    if((uc == CURLUE_OUT_OF_MEMORY) || (type != FOLLOW_FAKE)) {
1127
46
      failf(data, "The redirect target URL could not be parsed: %s",
1128
46
            curl_url_strerror(uc));
1129
46
      return Curl_uc_to_curlcode(uc);
1130
46
    }
1131
1132
    /* the URL could not be parsed for some reason, but since this is FAKE
1133
       mode, duplicate the field as-is */
1134
44
    *follow_urlp = curlx_strdup(newurl);
1135
44
    if(!*follow_urlp)
1136
0
      return CURLE_OUT_OF_MEMORY;
1137
44
  }
1138
10.4k
  else {
1139
10.4k
    CURLU *u = curl_url();
1140
10.4k
    if(!u)
1141
8
      return CURLE_OUT_OF_MEMORY;
1142
10.4k
    uc = curl_url_set(u, CURLUPART_URL,
1143
10.4k
                      Curl_bufref_ptr(&data->state.url),
1144
10.4k
                      CURLU_URLENCODE | CURLU_ALLOW_SPACE);
1145
10.4k
    if(!uc)
1146
10.4k
      uc = curl_url_get(data->state.uh, CURLUPART_URL, follow_urlp, 0);
1147
10.4k
    if(uc) {
1148
28
      curl_url_cleanup(u);
1149
28
      return Curl_uc_to_curlcode(uc);
1150
28
    }
1151
1152
10.4k
#ifndef CURL_DISABLE_DIGEST_AUTH
1153
10.4k
    {
1154
10.4k
      bool same_origin = Curl_url_same_origin(u, data->state.uh);
1155
10.4k
      curl_url_cleanup(u);
1156
10.4k
      if(!same_origin)
1157
509
        Curl_auth_digest_cleanup(&data->state.digest);
1158
10.4k
    }
1159
#else
1160
    curl_url_cleanup(u);
1161
#endif
1162
10.4k
  }
1163
10.4k
  return CURLE_OK;
1164
10.5k
}
1165
1166
static CURLcode follow_fake(struct Curl_easy *data, char *follow_url,
1167
                            CURLUcode uc, bool reachedmax)
1168
263
{
1169
  /* we are only figuring out the new URL if we would have followed locations
1170
     but now we are done so we can get out! */
1171
263
  if(!uc) {
1172
219
    CURLU *u = curl_url();
1173
219
    char *nocred = NULL;
1174
1175
219
    if(!u) {
1176
1
      curlx_free(follow_url);
1177
1
      return CURLE_OUT_OF_MEMORY;
1178
1
    }
1179
218
    if(!curl_url_set(u, CURLUPART_URL, follow_url,
1180
218
                     CURLU_NON_SUPPORT_SCHEME |
1181
218
                     (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)) &&
1182
213
       !curl_url_set(u, CURLUPART_USER, NULL, 0) &&
1183
213
       !curl_url_set(u, CURLUPART_PASSWORD, NULL, 0))
1184
213
      (void)curl_url_get(u, CURLUPART_URL, &nocred, CURLU_GET_EMPTY);
1185
218
    curl_url_cleanup(u);
1186
218
    if(nocred) {
1187
213
      curlx_free(follow_url);
1188
213
      follow_url = nocred;
1189
213
    }
1190
218
  }
1191
262
  data->info.wouldredirect = follow_url;
1192
1193
262
  if(reachedmax) {
1194
10
    failf(data, "Maximum (%d) redirects followed", data->set.maxredirs);
1195
10
    return CURLE_TOO_MANY_REDIRECTS;
1196
10
  }
1197
252
  return CURLE_OK;
1198
262
}
1199
1200
static bool follow_switch_get(struct Curl_easy *data)
1201
10.2k
{
1202
  /*
1203
   * We get here when the HTTP code is 300-399 and 401 or 407. We need to
1204
   * perform differently based on exactly what return code there was.
1205
   */
1206
10.2k
  switch(data->info.httpcode) {
1207
0
  case 301: /* Moved Permanently */
1208
    /* (quote from RFC7231, section 6.4.2)
1209
     *
1210
     * Note: For historical reasons, a user agent MAY change the request
1211
     * method from POST to GET for the subsequent request. If this
1212
     * behavior is undesired, the 307 (Temporary Redirect) status code
1213
     * can be used instead.
1214
     *
1215
     * ----
1216
     *
1217
     * Many webservers expect this, so these servers often answers to a POST
1218
     * request with an error page. To be sure that libcurl gets the page that
1219
     * most user agents would get, libcurl has to force GET.
1220
     *
1221
     * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
1222
     * can be overridden with CURLOPT_POSTREDIR.
1223
     */
1224
0
    if(HTTPREQ_IS_POST(data) && !data->set.post301) {
1225
0
      http_switch_to_get(data, 301);
1226
0
      return TRUE;
1227
0
    }
1228
0
    break;
1229
0
  case 302: /* Found */
1230
    /* (quote from RFC7231, section 6.4.3)
1231
     *
1232
     * Note: For historical reasons, a user agent MAY change the request
1233
     * method from POST to GET for the subsequent request. If this
1234
     * behavior is undesired, the 307 (Temporary Redirect) status code
1235
     * can be used instead.
1236
     *
1237
     * ----
1238
     *
1239
     * Many webservers expect this, so these servers often answers to a POST
1240
     * request with an error page. To be sure that libcurl gets the page that
1241
     * most user agents would get, libcurl has to force GET.
1242
     *
1243
     * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
1244
     * can be overridden with CURLOPT_POSTREDIR.
1245
     */
1246
0
    if(HTTPREQ_IS_POST(data) && !data->set.post302) {
1247
0
      http_switch_to_get(data, 302);
1248
0
      return TRUE;
1249
0
    }
1250
0
    break;
1251
0
  case 303: /* See Other */
1252
    /* 'See Other' location is not the resource but a substitute for the
1253
     * resource. In this case we switch the method to GET/HEAD, unless the
1254
     * method is POST and the user specified to keep it as POST.
1255
     */
1256
0
    if(!HTTPREQ_IS_POST(data) || !data->set.post303) {
1257
0
      http_switch_to_get(data, 303);
1258
0
      return TRUE;
1259
0
    }
1260
0
    break;
1261
1262
    /* 304 means we did a conditional request and it was "Not modified".
1263
     * We should not get any Location: header in this response!
1264
     */
1265
1266
    /* 305 (quote from RFC2616, section 10.3.6):
1267
     * "The requested resource MUST be accessed through the proxy given
1268
     * by the Location field. The Location field gives the URI of the
1269
     * proxy. The recipient is expected to repeat this single request
1270
     * via the proxy. 305 responses MUST only be generated by origin
1271
     * servers."
1272
     */
1273
10.2k
  default:
1274
10.2k
    break;
1275
10.2k
  }
1276
10.2k
  return FALSE;
1277
10.2k
}
1278
1279
CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
1280
                          followtype type)
1281
10.5k
{
1282
10.5k
  bool disallowport = FALSE;
1283
10.5k
  bool reachedmax = FALSE;
1284
10.5k
  char *follow_url = NULL;
1285
10.5k
  CURLUcode uc;
1286
10.5k
  CURLcode result;
1287
10.5k
  CURLcode rewind_result;
1288
10.5k
  bool switch_to_get;
1289
1290
10.5k
  DEBUGASSERT(type != FOLLOW_NONE);
1291
1292
10.5k
  if(type != FOLLOW_FAKE)
1293
10.3k
    data->state.requests++; /* count all real follows */
1294
10.5k
  if(type == FOLLOW_REDIR) {
1295
644
    if((data->set.maxredirs != -1) &&
1296
634
       (data->state.followlocation >= data->set.maxredirs)) {
1297
10
      reachedmax = TRUE;
1298
10
      type = FOLLOW_FAKE; /* switch to fake to store the would-be-redirected
1299
                             to URL */
1300
10
    }
1301
634
    else {
1302
634
      data->state.followlocation++; /* count redirect-followings, including
1303
                                       auth reloads */
1304
1305
634
      if(data->set.http_auto_referer) {
1306
22
        result = set_auto_referer(data);
1307
22
        if(result)
1308
0
          return result;
1309
22
      }
1310
634
    }
1311
644
  }
1312
1313
10.5k
  if((type != FOLLOW_RETRY) &&
1314
903
     (data->req.httpcode != 401) && (data->req.httpcode != 407) &&
1315
903
     Curl_is_absolute_url(newurl, NULL, 0, FALSE)) {
1316
    /* If this is not redirect due to a 401 or 407 response and an absolute
1317
       URL: do not allow a custom port number */
1318
383
    disallowport = TRUE;
1319
383
  }
1320
1321
10.5k
  result = follow_parse_url(data, newurl, type, &follow_url, &uc);
1322
10.5k
  if(result)
1323
82
    return result;
1324
1325
10.4k
  DEBUGASSERT(follow_url);
1326
1327
10.4k
  if(type == FOLLOW_FAKE)
1328
263
    return follow_fake(data, follow_url, uc, reachedmax);
1329
1330
10.2k
  if(disallowport)
1331
241
    data->state.allow_port = FALSE;
1332
1333
10.2k
  Curl_bufref_set(&data->state.url, follow_url, 0, curl_free);
1334
10.2k
  rewind_result = Curl_req_soft_reset(&data->req, data);
1335
10.2k
  infof(data, "Issue another request to this URL: '%s'", follow_url);
1336
10.2k
  if((data->set.http_follow_mode == CURLFOLLOW_FIRSTONLY) &&
1337
655
     !data->state.http_ignorecustom &&
1338
629
     CURL_EASY_STR(data, STRING_CUSTOMREQUEST)) {
1339
50
    data->state.http_ignorecustom = TRUE;
1340
50
    infof(data, "Drop custom request method for next request");
1341
50
  }
1342
1343
10.2k
  switch_to_get = follow_switch_get(data);
1344
1345
  /* When rewind of upload data failed and we are not switching to GET,
1346
   * we need to fail the follow, as we cannot send the data again. */
1347
10.2k
  if(rewind_result && !switch_to_get)
1348
189
    return rewind_result;
1349
1350
10.0k
  Curl_pgrsTime(data, TIMER_REDIRECT);
1351
10.0k
  Curl_pgrsResetTransferSizes(data);
1352
1353
10.0k
  return CURLE_OK;
1354
10.2k
}
1355
1356
/*
1357
 * Curl_compareheader()
1358
 *
1359
 * Returns TRUE if 'headerline' contains the 'header' with given 'content'
1360
 * (within a comma-separated list of tokens). Pass 'header' WITH the colon.
1361
 *
1362
 * @unittest: 1625
1363
 */
1364
bool Curl_compareheader(const char *headerline, /* line to check */
1365
                        const char *header, /* header keyword _with_ colon */
1366
                        const size_t hlen, /* len of the keyword in bytes */
1367
                        const char *content, /* content string to find */
1368
                        const size_t clen) /* len of the content in bytes */
1369
831
{
1370
  /* RFC2616, section 4.2 says: "Each header field consists of a name followed
1371
   * by a colon (":") and the field value. Field names are case-insensitive.
1372
   * The field value MAY be preceded by any amount of LWS, though a single SP
1373
   * is preferred." */
1374
1375
831
  const char *p;
1376
831
  struct Curl_str val;
1377
831
  DEBUGASSERT(hlen);
1378
831
  DEBUGASSERT(clen);
1379
831
  DEBUGASSERT(header);
1380
831
  DEBUGASSERT(content);
1381
1382
831
  if(!curl_strnequal(headerline, header, hlen))
1383
88
    return FALSE; /* does not start with header */
1384
1385
  /* pass the header */
1386
743
  p = &headerline[hlen];
1387
1388
743
  if(curlx_str_cspn(&p, &val, "\r\n"))
1389
66
    return FALSE;
1390
677
  curlx_str_trimblanks(&val);
1391
1392
  /* find the content string in the rest of the line */
1393
677
  if(curlx_strlen(&val) >= clen) {
1394
582
    size_t len;
1395
582
    p = curlx_str(&val);
1396
986
    for(len = curlx_strlen(&val); len >= clen;) {
1397
880
      struct Curl_str next;
1398
880
      const char *o = p;
1399
      /* after a match there must be a comma, space, newline or NUL byte */
1400
880
      if(curl_strnequal(p, content, clen) &&
1401
125
         ((p[clen] == ',') || ISBLANK(p[clen]) || ISNEWLINE(p[clen]) ||
1402
40
          !p[clen]))
1403
85
        return TRUE; /* match! */
1404
      /* advance to the next comma */
1405
795
      if(curlx_str_until(&p, &next, len, ',') ||
1406
583
         curlx_str_single(&p, ','))
1407
320
        break; /* no comma, get out */
1408
1409
      /* if there are more dummy commas, move over them as well */
1410
475
      do
1411
701
        curlx_str_passblanks(&p);
1412
701
      while(!curlx_str_single(&p, ','));
1413
      /* trailing blanks may move the parsing point past the value end,
1414
         then there is nothing left to match */
1415
475
      if((size_t)(p - o) > len)
1416
71
        break;
1417
404
      len -= (p - o);
1418
404
    }
1419
582
  }
1420
592
  return FALSE; /* no match */
1421
677
}
1422
1423
struct cr_exp100_ctx {
1424
  struct Curl_creader super;
1425
  struct curltime start; /* time started waiting */
1426
  enum expect100 state;
1427
};
1428
1429
/* Expect: 100-continue client reader, blocking uploads */
1430
1431
static void http_exp100_continue(struct Curl_easy *data,
1432
                                 struct Curl_creader *reader)
1433
301
{
1434
301
  struct cr_exp100_ctx *ctx = reader->ctx;
1435
301
  if(ctx->state > EXP100_SEND_DATA) {
1436
235
    ctx->state = EXP100_SEND_DATA;
1437
235
    Curl_expire_clear(data, EXPIRE_100_TIMEOUT);
1438
235
  }
1439
301
}
1440
1441
static CURLcode cr_exp100_read(struct Curl_easy *data,
1442
                               struct Curl_creader *reader,
1443
                               char *buf, size_t blen,
1444
                               size_t *nread, bool *eos)
1445
7.38k
{
1446
7.38k
  struct cr_exp100_ctx *ctx = reader->ctx;
1447
7.38k
  timediff_t ms;
1448
1449
7.38k
  switch(ctx->state) {
1450
1.32k
  case EXP100_SENDING_REQUEST:
1451
1.32k
    if(!Curl_req_sendbuf_empty(data)) {
1452
      /* The initial request data has not been fully sent yet. Do
1453
       * not start the timer yet. */
1454
958
      DEBUGF(infof(data, "cr_exp100_read, request not full sent yet"));
1455
958
      *nread = 0;
1456
958
      *eos = FALSE;
1457
958
      return CURLE_OK;
1458
958
    }
1459
    /* We are now waiting for a reply from the server or
1460
     * a timeout on our side IFF the request has been fully sent. */
1461
370
    DEBUGF(infof(data, "cr_exp100_read, start AWAITING_CONTINUE, "
1462
370
                 "timeout %dms", data->set.expect_100_timeout));
1463
370
    ctx->state = EXP100_AWAITING_CONTINUE;
1464
370
    ctx->start = *Curl_pgrs_now(data);
1465
370
    Curl_expire_set(data, EXPIRE_100_TIMEOUT,
1466
370
                    data->set.expect_100_timeout, &ctx->start);
1467
370
    *nread = 0;
1468
370
    *eos = FALSE;
1469
370
    return CURLE_OK;
1470
0
  case EXP100_FAILED:
1471
0
    DEBUGF(infof(data, "cr_exp100_read, expectation failed, error"));
1472
0
    *nread = 0;
1473
0
    *eos = FALSE;
1474
0
    return CURLE_READ_ERROR;
1475
1.98k
  case EXP100_AWAITING_CONTINUE:
1476
1.98k
    ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &ctx->start);
1477
1.98k
    if(ms < data->set.expect_100_timeout) {
1478
1.80k
      DEBUGF(infof(data, "cr_exp100_read, AWAITING_CONTINUE, not expired"));
1479
1.80k
      *nread = 0;
1480
1.80k
      *eos = FALSE;
1481
1.80k
      return CURLE_OK;
1482
1.80k
    }
1483
    /* we have waited long enough, continue anyway */
1484
174
    http_exp100_continue(data, reader);
1485
174
    infof(data, "Done waiting for 100-continue");
1486
174
    FALLTHROUGH();
1487
4.25k
  default:
1488
4.25k
    DEBUGF(infof(data, "cr_exp100_read, pass through"));
1489
4.25k
    return Curl_creader_read(data, reader->next, buf, blen, nread, eos);
1490
7.38k
  }
1491
7.38k
}
1492
1493
static void cr_exp100_done(struct Curl_easy *data,
1494
                           struct Curl_creader *reader, int premature)
1495
703
{
1496
703
  struct cr_exp100_ctx *ctx = reader->ctx;
1497
703
  ctx->state = premature ? EXP100_FAILED : EXP100_SEND_DATA;
1498
703
  Curl_expire_clear(data, EXPIRE_100_TIMEOUT);
1499
703
}
1500
1501
static const struct Curl_crtype cr_exp100 = {
1502
  "cr-exp100",
1503
  Curl_creader_def_init,
1504
  cr_exp100_read,
1505
  Curl_creader_def_close,
1506
  Curl_creader_def_needs_rewind,
1507
  Curl_creader_def_total_length,
1508
  Curl_creader_def_resume_from,
1509
  Curl_creader_def_cntrl,
1510
  Curl_creader_def_is_paused,
1511
  cr_exp100_done,
1512
  sizeof(struct cr_exp100_ctx)
1513
};
1514
1515
static CURLcode http_exp100_add_reader(struct Curl_easy *data)
1516
974
{
1517
974
  struct Curl_creader *reader = NULL;
1518
974
  CURLcode result;
1519
1520
974
  result = Curl_creader_create(&reader, data, &cr_exp100, CURL_CR_PROTOCOL);
1521
974
  if(!result)
1522
967
    result = Curl_creader_add(data, reader);
1523
974
  if(!result) {
1524
967
    struct cr_exp100_ctx *ctx = reader->ctx;
1525
967
    ctx->state = EXP100_SENDING_REQUEST;
1526
967
  }
1527
1528
974
  if(result && reader)
1529
0
    Curl_creader_free(data, reader);
1530
974
  return result;
1531
974
}
1532
1533
static void http_exp100_got100(struct Curl_easy *data)
1534
165
{
1535
165
  struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1536
165
  if(r)
1537
90
    http_exp100_continue(data, r);
1538
165
}
1539
1540
static bool http_exp100_is_waiting(struct Curl_easy *data)
1541
251k
{
1542
251k
  struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1543
251k
  if(r) {
1544
3.36k
    struct cr_exp100_ctx *ctx = r->ctx;
1545
3.36k
    return ctx->state == EXP100_AWAITING_CONTINUE;
1546
3.36k
  }
1547
248k
  return FALSE;
1548
251k
}
1549
1550
static void http_exp100_send_anyway(struct Curl_easy *data)
1551
57
{
1552
57
  struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1553
57
  if(r)
1554
37
    http_exp100_continue(data, r);
1555
57
}
1556
1557
static bool http_exp100_is_selected(struct Curl_easy *data)
1558
0
{
1559
0
  struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1560
0
  return !!r;
1561
0
}
1562
1563
/* this returns the socket to wait for in the DO and DOING state for the multi
1564
   interface and then we are always _sending_ a request and thus we wait for
1565
   the single socket to become writable only */
1566
CURLcode Curl_http_doing_pollset(struct Curl_easy *data,
1567
                                 struct easy_pollset *ps)
1568
0
{
1569
  /* write mode */
1570
0
  return Curl_pollset_add_out(data, ps, data->conn->chain.sock);
1571
0
}
1572
1573
CURLcode Curl_http_perform_pollset(struct Curl_easy *data,
1574
                                   struct easy_pollset *ps)
1575
397k
{
1576
397k
  struct connectdata *conn = data->conn;
1577
397k
  CURLcode result = CURLE_OK;
1578
1579
397k
  if(CURL_REQ_WANT_RECV(data)) {
1580
396k
    result = Curl_pollset_add_in(data, ps, conn->chain.sock);
1581
396k
  }
1582
1583
  /* on a "Expect: 100-continue" timed wait, do not poll for outgoing */
1584
397k
  if(!result && Curl_req_want_send(data) && !http_exp100_is_waiting(data)) {
1585
250k
    result = Curl_pollset_add_out(data, ps, conn->chain.sock);
1586
250k
  }
1587
397k
  return result;
1588
397k
}
1589
1590
static CURLcode http_write_header(struct Curl_easy *data,
1591
                                  const char *hd, size_t hdlen)
1592
9.08k
{
1593
9.08k
  CURLcode result;
1594
9.08k
  int writetype;
1595
1596
  /* now, only output this if the header AND body are requested:
1597
   */
1598
9.08k
  Curl_debug(data, CURLINFO_HEADER_IN, hd, hdlen);
1599
1600
9.08k
  writetype = CLIENTWRITE_HEADER |
1601
9.08k
    ((data->req.httpcode / 100 == 1) ? CLIENTWRITE_1XX : 0);
1602
1603
9.08k
  result = Curl_client_write(data, writetype, hd, hdlen);
1604
9.08k
  if(result)
1605
1.69k
    return result;
1606
1607
7.38k
  result = Curl_bump_headersize(data, hdlen, FALSE);
1608
7.38k
  if(result)
1609
0
    return result;
1610
1611
7.38k
  data->req.deductheadercount = (100 <= data->req.httpcode &&
1612
7.26k
                                 199 >= data->req.httpcode) ?
1613
6.63k
    data->req.headerbytecount : 0;
1614
7.38k
  return result;
1615
7.38k
}
1616
1617
/*
1618
 * Curl_http_done() gets called after a single HTTP request has been
1619
 * performed.
1620
 */
1621
1622
CURLcode Curl_http_done(struct Curl_easy *data,
1623
                        CURLcode status, bool premature)
1624
63.0k
{
1625
63.0k
  struct connectdata *conn = data->conn;
1626
1627
  /* Clear multipass flag. If authentication is not done yet, then it will get
1628
   * a chance to be set back to true when we output the next auth header */
1629
63.0k
  data->state.authhost.multipass = FALSE;
1630
63.0k
  data->state.authproxy.multipass = FALSE;
1631
1632
63.0k
  if(curlx_dyn_len(&data->state.headerb)) {
1633
1.92k
    (void)http_write_header(data, curlx_dyn_ptr(&data->state.headerb),
1634
1.92k
                            curlx_dyn_len(&data->state.headerb));
1635
1.92k
  }
1636
63.0k
  curlx_dyn_reset(&data->state.headerb);
1637
1638
63.0k
  if(status)
1639
27.2k
    return status;
1640
1641
35.7k
  if(!premature && /* this check is pointless when DONE is called before the
1642
                      entire operation is complete */
1643
26.7k
     !conn->bits.retry &&
1644
17.3k
     !data->set.connect_only &&
1645
17.1k
     (data->req.bytecount +
1646
17.1k
      data->req.headerbytecount -
1647
17.1k
      data->req.deductheadercount) <= 0) {
1648
    /* If this connection is not closed to be retried, AND nothing was
1649
       read from the HTTP server (that counts), this cannot be right so we
1650
       return an error here */
1651
12.5k
    failf(data, "Empty reply from server");
1652
    /* Mark it as closed to avoid the "left intact" message */
1653
12.5k
    streamclose(conn);
1654
12.5k
    return CURLE_GOT_NOTHING;
1655
12.5k
  }
1656
1657
23.1k
  return CURLE_OK;
1658
35.7k
}
1659
1660
/* Determine if we may use HTTP 1.1 for this request. */
1661
static bool http_may_use_1_1(const struct Curl_easy *data)
1662
21.9k
{
1663
21.9k
  const struct connectdata *conn = data->conn;
1664
  /* We have seen a previous response for *this* transfer with 1.0,
1665
   * on another connection or the same one. */
1666
21.9k
  if(data->state.http_neg.rcvd_min == 10)
1667
6
    return FALSE;
1668
  /* We have seen a previous response on *this* connection with 1.0. */
1669
21.9k
  if(conn && conn->httpversion_seen == 10)
1670
0
    return FALSE;
1671
  /* We want 1.0 and have seen no previous response on *this* connection
1672
     with a higher version (maybe no response at all yet). */
1673
21.9k
  if(data->state.http_neg.only_10 &&
1674
221
     (!conn || conn->httpversion_seen <= 10))
1675
221
    return FALSE;
1676
  /* We are not restricted to use 1.0 only. */
1677
21.7k
  return !data->state.http_neg.only_10;
1678
21.9k
}
1679
1680
static unsigned char http_request_version(struct Curl_easy *data)
1681
60.5k
{
1682
60.5k
  unsigned char v = Curl_conn_http_version(data, data->conn);
1683
60.5k
  if(!v) {
1684
    /* No specific HTTP connection filter installed. */
1685
21.9k
    v = http_may_use_1_1(data) ? 11 : 10;
1686
21.9k
  }
1687
60.5k
  return v;
1688
60.5k
}
1689
1690
static const char *get_http_string(int httpversion)
1691
60.4k
{
1692
60.4k
  switch(httpversion) {
1693
0
  case 30:
1694
0
    return "3";
1695
38.5k
  case 20:
1696
38.5k
    return "2";
1697
21.6k
  case 11:
1698
21.6k
    return "1.1";
1699
227
  default:
1700
227
    return "1.0";
1701
60.4k
  }
1702
60.4k
}
1703
1704
struct http_custom_ctx {
1705
  struct Curl_easy *data;
1706
  struct dynbuf *req;
1707
  int httpversion;
1708
};
1709
1710
static CURLcode http_custom_treat(void *user_data,
1711
                                  struct Curl_str *name,
1712
                                  struct Curl_str *value,
1713
                                  const char *orig_data)
1714
21.8k
{
1715
21.8k
  struct http_custom_ctx *x = user_data;
1716
21.8k
  CURLcode result;
1717
1718
21.8k
  if(x->data->state.http_host &&
1719
     /* a Host: header was sent already, do not pass on any custom
1720
        Host: header as that will produce *two* in the same
1721
        request! */
1722
21.2k
     curlx_str_casecompare2(name, STRCONST("Host")))
1723
912
    goto out;
1724
20.9k
  else if(x->data->state.httpreq == HTTPREQ_POST_FORM &&
1725
          /* this header (extended by formdata.c) is sent later */
1726
0
          curlx_str_casecompare2(name, STRCONST("Content-Type")))
1727
0
    goto out;
1728
20.9k
  else if(x->data->state.httpreq == HTTPREQ_POST_MIME &&
1729
          /* this header is sent later */
1730
6.57k
          curlx_str_casecompare2(name, STRCONST("Content-Type")))
1731
0
    goto out;
1732
20.9k
  else if(x->data->req.authneg &&
1733
          /* while doing auth neg, do not allow the custom length since
1734
             we will force length zero then */
1735
221
          curlx_str_casecompare2(name, STRCONST("Content-Length")))
1736
18
    goto out;
1737
20.9k
  else if(curlx_str_casecompare2(name, STRCONST("Connection")))
1738
    /* Connection headers are handled specially */
1739
221
    goto out;
1740
20.7k
  else if((x->httpversion >= 20) &&
1741
19.7k
          curlx_str_casecompare2(name, STRCONST("Transfer-Encoding")))
1742
    /* HTTP/2 does not support chunked requests */
1743
220
    goto out;
1744
20.5k
  else if((curlx_str_casecompare2(name, STRCONST("Authorization")) ||
1745
20.5k
           curlx_str_casecompare2(name, STRCONST("Cookie"))) &&
1746
          /* be careful of sending this potentially sensitive header to
1747
             other hosts */
1748
988
          !Curl_auth_allowed_to_host(x->data))
1749
34
    goto out;
1750
1751
20.4k
  if(!curlx_strlen(value)) {
1752
2.73k
    result = curlx_dyn_addn(x->req, curlx_str(name), curlx_strlen(name));
1753
2.73k
    if(!result)
1754
2.73k
      result = curlx_dyn_addn(x->req, STRCONST(":\r\n"));
1755
2.73k
  }
1756
17.7k
  else
1757
17.7k
    result = curlx_dyn_addf(x->req, "%s\r\n", orig_data);
1758
1759
20.4k
  if(result)
1760
9
    return result;
1761
1762
21.8k
out:
1763
21.8k
  return CURLE_AGAIN;
1764
20.4k
}
1765
1766
CURLcode Curl_add_custom_headers(struct Curl_easy *data,
1767
                                 int httpversion,
1768
                                 struct dynbuf *req)
1769
60.3k
{
1770
60.3k
  struct curl_slist *h[2];
1771
60.3k
  struct http_custom_ctx custom_ctx;
1772
60.3k
  int numlists = 0;
1773
60.3k
  int i;
1774
1775
60.3k
  h[numlists++] = data->set.headers;
1776
60.3k
#ifndef CURL_DISABLE_PROXY
1777
60.3k
  if(data->conn->bits.origin_is_proxy &&
1778
0
     data->set.sep_headers)
1779
0
      h[numlists++] = data->set.proxyheaders;
1780
60.3k
#endif
1781
1782
  /* loop through one or two lists */
1783
60.3k
  custom_ctx.data = data;
1784
60.3k
  custom_ctx.req = req;
1785
60.3k
  custom_ctx.httpversion = httpversion;
1786
120k
  for(i = 0; i < numlists; i++) {
1787
60.3k
    CURLcode result =
1788
60.3k
      Curl_dynhds_add_slist(NULL, h[i], http_custom_treat, &custom_ctx);
1789
60.3k
    if(result)
1790
9
      return result;
1791
60.3k
  }
1792
1793
60.3k
  return CURLE_OK;
1794
60.3k
}
1795
1796
#ifndef CURL_DISABLE_PARSEDATE
1797
CURLcode Curl_add_timecondition(struct Curl_easy *data,
1798
                                struct dynbuf *req)
1799
60.3k
{
1800
60.3k
  const struct tm *tm;
1801
60.3k
  struct tm keeptime;
1802
60.3k
  CURLcode result;
1803
60.3k
  char datestr[80];
1804
60.3k
  const char *condp;
1805
60.3k
  size_t len;
1806
1807
60.3k
  if(data->set.timecondition == CURL_TIMECOND_NONE)
1808
    /* no condition was asked for */
1809
60.0k
    return CURLE_OK;
1810
1811
321
  result = curlx_gmtime(data->set.timevalue, &keeptime);
1812
321
  if(result) {
1813
37
    failf(data, "Invalid TIMEVALUE");
1814
37
    return result;
1815
37
  }
1816
284
  tm = &keeptime;
1817
1818
284
  switch(data->set.timecondition) {
1819
0
  default:
1820
0
    DEBUGF(infof(data, "invalid time condition"));
1821
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1822
1823
112
  case CURL_TIMECOND_IFMODSINCE:
1824
112
    condp = "If-Modified-Since";
1825
112
    len = 17;
1826
112
    break;
1827
110
  case CURL_TIMECOND_IFUNMODSINCE:
1828
110
    condp = "If-Unmodified-Since";
1829
110
    len = 19;
1830
110
    break;
1831
62
  case CURL_TIMECOND_LASTMOD:
1832
62
    condp = "Last-Modified";
1833
62
    len = 13;
1834
62
    break;
1835
284
  }
1836
1837
284
  if(Curl_checkheaders(data, condp, len)) {
1838
    /* A custom header was specified; it will be sent instead. */
1839
0
    return CURLE_OK;
1840
0
  }
1841
1842
  /* The If-Modified-Since header family should have their times set in
1843
   * GMT as RFC2616 defines: "All HTTP date/time stamps MUST be
1844
   * represented in Greenwich Mean Time (GMT), without exception. For the
1845
   * purposes of HTTP, GMT is exactly equal to UTC (Coordinated Universal
1846
   * Time)." (see page 20 of RFC2616).
1847
   */
1848
1849
  /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
1850
284
  curl_msnprintf(datestr, sizeof(datestr),
1851
284
                 "%s: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
1852
284
                 condp,
1853
284
                 Curl_wkday[tm->tm_wday ? tm->tm_wday - 1 : 6],
1854
284
                 tm->tm_mday,
1855
284
                 Curl_month[tm->tm_mon],
1856
284
                 tm->tm_year + 1900,
1857
284
                 tm->tm_hour,
1858
284
                 tm->tm_min,
1859
284
                 tm->tm_sec);
1860
1861
284
  result = curlx_dyn_add(req, datestr);
1862
284
  return result;
1863
284
}
1864
#else
1865
/* disabled */
1866
CURLcode Curl_add_timecondition(struct Curl_easy *data,
1867
                                struct dynbuf *req)
1868
{
1869
  (void)data;
1870
  (void)req;
1871
  return CURLE_OK;
1872
}
1873
#endif
1874
1875
void Curl_http_method(struct Curl_easy *data,
1876
                      const char **method, Curl_HttpReq *reqp)
1877
66.6k
{
1878
66.6k
  Curl_HttpReq httpreq = (Curl_HttpReq)data->state.httpreq;
1879
66.6k
  const char *request;
1880
66.6k
#ifndef CURL_DISABLE_WEBSOCKETS
1881
66.6k
  if(data->conn->scheme->protocol & (CURLPROTO_WS | CURLPROTO_WSS))
1882
0
    httpreq = HTTPREQ_GET;
1883
66.6k
  else
1884
66.6k
#endif
1885
66.6k
  if((data->conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_FTP)) &&
1886
66.6k
     data->state.upload)
1887
2.02k
    httpreq = HTTPREQ_PUT;
1888
1889
  /* Now set the 'request' pointer to the proper request string */
1890
66.6k
  if(!data->state.http_ignorecustom &&
1891
66.6k
     CURL_EASY_STR(data, STRING_CUSTOMREQUEST)) {
1892
695
    request = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
1893
695
  }
1894
65.9k
  else {
1895
65.9k
    if(data->req.no_body)
1896
571
      request = "HEAD";
1897
65.3k
    else {
1898
65.3k
      DEBUGASSERT((httpreq >= HTTPREQ_GET) && (httpreq <= HTTPREQ_HEAD));
1899
65.3k
      switch(httpreq) {
1900
1.57k
      case HTTPREQ_POST:
1901
1.57k
      case HTTPREQ_POST_FORM:
1902
17.4k
      case HTTPREQ_POST_MIME:
1903
17.4k
        request = "POST";
1904
17.4k
        break;
1905
2.01k
      case HTTPREQ_PUT:
1906
2.01k
        request = "PUT";
1907
2.01k
        break;
1908
0
      default: /* this should never happen */
1909
45.8k
      case HTTPREQ_GET:
1910
45.8k
        request = "GET";
1911
45.8k
        break;
1912
0
      case HTTPREQ_HEAD:
1913
0
        request = "HEAD";
1914
0
        break;
1915
65.3k
      }
1916
65.3k
    }
1917
65.9k
  }
1918
66.6k
  *method = request;
1919
66.6k
  *reqp = httpreq;
1920
66.6k
}
1921
1922
static CURLcode http_set_aptr_host(struct Curl_easy *data)
1923
62.9k
{
1924
62.9k
  struct connectdata *conn = data->conn;
1925
62.9k
  const char *ptr = NULL;
1926
1927
62.9k
  curlx_safefree(data->state.http_host);
1928
62.9k
#ifndef CURL_DISABLE_COOKIES
1929
62.9k
  curlx_safefree(data->req.cookiehost);
1930
62.9k
#endif
1931
1932
62.9k
  if(Curl_peer_equal(data->state.initial_origin, data->state.origin))
1933
62.7k
    ptr = Curl_checkheaders(data, STRCONST("Host"));
1934
1935
62.9k
  if(ptr) {
1936
1.63k
#ifndef CURL_DISABLE_COOKIES
1937
    /* If we have a given custom Host: header, we extract the hostname in
1938
       order to possibly use it for cookie reasons later on. We only allow the
1939
       custom Host: header if this is NOT a redirect, as setting Host: in the
1940
       redirected request is being out on thin ice. Except if the hostname
1941
       is the same as the first one! */
1942
1.63k
    char *cookiehost;
1943
1.63k
    CURLcode result = copy_custom_value(ptr, &cookiehost);
1944
1.63k
    if(result)
1945
4
      return result;
1946
1.63k
    if(!*cookiehost)
1947
      /* ignore empty data */
1948
439
      curlx_free(cookiehost);
1949
1.19k
    else {
1950
      /* If the host begins with '[', we start searching for the port after
1951
         the bracket has been closed */
1952
1.19k
      if(*cookiehost == '[') {
1953
519
        char *closingbracket;
1954
        /* since the 'cookiehost' is an allocated memory area that will be
1955
           freed later we cannot increment the pointer */
1956
519
        memmove(cookiehost, cookiehost + 1, strlen(cookiehost) - 1);
1957
519
        closingbracket = strchr(cookiehost, ']');
1958
519
        if(closingbracket)
1959
52
          *closingbracket = 0;
1960
519
      }
1961
674
      else {
1962
674
        int startsearch = 0;
1963
674
        char *colon = strchr(cookiehost + startsearch, ':');
1964
674
        if(colon)
1965
213
          *colon = 0; /* The host must not include an embedded port number */
1966
674
      }
1967
1.19k
      data->req.cookiehost = cookiehost;
1968
1.19k
    }
1969
1.63k
#endif
1970
1971
1.63k
    if(!curl_strequal("Host:", ptr)) {
1972
1.31k
      data->state.http_host = curl_maprintf("Host:%s", &ptr[5]);
1973
1.31k
      if(!data->state.http_host)
1974
4
        return CURLE_OUT_OF_MEMORY;
1975
1.31k
    }
1976
1.63k
  }
1977
61.2k
  else {
1978
    /* This is the  HTTP Host: header, so we want
1979
     * - for IPv6 origins: "[ipv6-address]" where the IPv6 address is
1980
     *  found in origin->hostname, stripped of zoneid/scopeid.
1981
     * - the (IDN converted) origin->hostname (DNS name or IPv4) otherwise.
1982
     * Note: zoneid/scopeid  only applies to local routing and has no
1983
     * meaning on the remote HTTP server (eg. would confuse it). */
1984
61.2k
    bool ipv6 = (bool)data->state.origin->ipv6;
1985
61.2k
    struct dynbuf tmp;
1986
61.2k
    size_t hlen;
1987
61.2k
    CURLcode result;
1988
1989
61.2k
    curlx_dyn_init(&tmp, DYN_HTTP_REQUEST);
1990
61.2k
    result = curlx_dyn_addn(&tmp, STRCONST("Host: "));
1991
61.2k
    if(!result && ipv6)
1992
0
      result = curlx_dyn_addn(&tmp, STRCONST("["));
1993
61.2k
    if(!result)
1994
61.2k
      result = curlx_dyn_add(&tmp, data->state.origin->hostname);
1995
61.2k
    if(!result && ipv6)
1996
0
      result = curlx_dyn_addn(&tmp, STRCONST("]"));
1997
61.2k
    if(!result &&
1998
61.2k
       ((data->state.origin->port != data->state.origin->scheme->defport) ||
1999
61.1k
       (data->state.origin->scheme->family != conn->scheme->family))) {
2000
72
      result = curlx_dyn_addf(&tmp, ":%u", data->state.origin->port);
2001
72
    }
2002
2003
61.2k
    data->state.http_host = result ? NULL : curlx_dyn_take(&tmp, &hlen);
2004
61.2k
    curlx_dyn_free(&tmp);
2005
61.2k
    return result;
2006
61.2k
  }
2007
1.62k
  return CURLE_OK;
2008
62.9k
}
2009
2010
/*
2011
 * Append the request-target to the HTTP request
2012
 */
2013
static CURLcode http_target(struct Curl_easy *data,
2014
                            struct dynbuf *r)
2015
60.4k
{
2016
60.4k
  CURLcode result = CURLE_OK;
2017
60.4k
  const char *path = data->state.up.path;
2018
60.4k
  const char *query = data->state.up.query;
2019
60.4k
#ifndef CURL_DISABLE_PROXY
2020
60.4k
  struct connectdata *conn = data->conn;
2021
60.4k
#endif
2022
2023
60.4k
  if(CURL_EASY_STR(data, STRING_TARGET)) {
2024
4.13k
    path = CURL_EASY_STR(data, STRING_TARGET);
2025
4.13k
    query = NULL;
2026
4.13k
  }
2027
2028
60.4k
#ifndef CURL_DISABLE_PROXY
2029
60.4k
  if(conn->bits.origin_is_proxy) {
2030
    /* Using a proxy but does not tunnel through it */
2031
2032
    /* The path sent to the proxy is in fact the entire URL, but if the remote
2033
       host is a IDN-name, we must make sure that the request we produce only
2034
       uses the decoded hostname! */
2035
2036
    /* and no fragment part */
2037
0
    CURLUcode uc;
2038
0
    char *url;
2039
0
    CURLU *h = curl_url_dup(data->state.uh);
2040
0
    if(!h)
2041
0
      return CURLE_OUT_OF_MEMORY;
2042
2043
0
    if(!data->state.origin->ipv6 &&
2044
0
       (data->state.origin->user_hostname != data->state.origin->hostname)) {
2045
0
      uc = curl_url_set(h, CURLUPART_HOST, data->state.origin->hostname, 0);
2046
0
      if(uc) {
2047
0
        curl_url_cleanup(h);
2048
0
        return CURLE_OUT_OF_MEMORY;
2049
0
      }
2050
0
    }
2051
0
    uc = curl_url_set(h, CURLUPART_FRAGMENT, NULL, 0);
2052
0
    if(uc) {
2053
0
      curl_url_cleanup(h);
2054
0
      return CURLE_OUT_OF_MEMORY;
2055
0
    }
2056
2057
0
    if(data->state.origin->scheme == &Curl_scheme_http) {
2058
      /* when getting HTTP, we do not want the userinfo the URL */
2059
0
      uc = curl_url_set(h, CURLUPART_USER, NULL, 0);
2060
0
      if(uc) {
2061
0
        curl_url_cleanup(h);
2062
0
        return CURLE_OUT_OF_MEMORY;
2063
0
      }
2064
0
      uc = curl_url_set(h, CURLUPART_PASSWORD, NULL, 0);
2065
0
      if(uc) {
2066
0
        curl_url_cleanup(h);
2067
0
        return CURLE_OUT_OF_MEMORY;
2068
0
      }
2069
0
    }
2070
0
    else if(data->state.creds && (data->state.creds->source != CREDS_URL)) {
2071
        /* credentials not from the URL need to be set */
2072
0
      uc = curl_url_set(h, CURLUPART_USER,
2073
0
                        data->state.creds->user, CURLU_URLENCODE);
2074
0
      if(!uc)
2075
0
        uc = curl_url_set(h, CURLUPART_PASSWORD,
2076
0
                          data->state.creds->passwd, CURLU_URLENCODE);
2077
0
      if(uc) {
2078
0
        curl_url_cleanup(h);
2079
0
        return Curl_uc_to_curlcode(uc);
2080
0
      }
2081
0
    }
2082
2083
    /* Extract the URL to use in the request. */
2084
0
    uc = curl_url_get(h, CURLUPART_URL, &url, CURLU_NO_DEFAULT_PORT);
2085
0
    if(uc) {
2086
0
      curl_url_cleanup(h);
2087
0
      return CURLE_OUT_OF_MEMORY;
2088
0
    }
2089
2090
0
    curl_url_cleanup(h);
2091
2092
    /* target or URL */
2093
0
    result = curlx_dyn_add(r, CURL_EASY_STR(data, STRING_TARGET) ?
2094
0
      CURL_EASY_STR(data, STRING_TARGET) : url);
2095
0
    curlx_free(url);
2096
0
    if(result)
2097
0
      return result;
2098
2099
0
    if((data->state.origin->scheme == &Curl_scheme_ftp) &&
2100
0
       data->set.proxy_transfer_mode) {
2101
      /* when doing ftp, append ;type=<a|i> if not present */
2102
0
      size_t len = strlen(path);
2103
0
      bool type_present = FALSE;
2104
0
      if((len >= 7) && !memcmp(&path[len - 7], ";type=", 6)) {
2105
0
        switch(Curl_raw_toupper(path[len - 1])) {
2106
0
        case 'A':
2107
0
        case 'D':
2108
0
        case 'I':
2109
0
          type_present = TRUE;
2110
0
          break;
2111
0
        }
2112
0
      }
2113
0
      if(!type_present) {
2114
0
        result = curlx_dyn_addf(r, ";type=%c",
2115
0
                                data->state.prefer_ascii ? 'a' : 'i');
2116
0
        if(result)
2117
0
          return result;
2118
0
      }
2119
0
    }
2120
0
  }
2121
2122
60.4k
  else
2123
60.4k
#endif
2124
60.4k
  {
2125
60.4k
    result = curlx_dyn_add(r, path);
2126
60.4k
    if(result)
2127
7
      return result;
2128
60.4k
    if(query)
2129
2.88k
      result = curlx_dyn_addf(r, "?%s", query);
2130
60.4k
  }
2131
2132
60.4k
  return result;
2133
60.4k
}
2134
2135
#if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API)
2136
static CURLcode set_post_reader(struct Curl_easy *data, Curl_HttpReq httpreq)
2137
15.0k
{
2138
15.0k
  CURLcode result;
2139
2140
15.0k
  switch(httpreq) {
2141
0
#ifndef CURL_DISABLE_MIME
2142
15.0k
  case HTTPREQ_POST_MIME:
2143
15.0k
    data->state.mimepost = data->set.a.mimepostp;
2144
15.0k
    break;
2145
0
#endif
2146
0
#ifndef CURL_DISABLE_FORM_API
2147
0
  case HTTPREQ_POST_FORM:
2148
    /* Convert the form structure into a mime structure, then keep
2149
       the conversion */
2150
0
    if(!data->state.formp) {
2151
0
      data->state.formp = curlx_calloc(1, sizeof(curl_mimepart));
2152
0
      if(!data->state.formp)
2153
0
        return CURLE_OUT_OF_MEMORY;
2154
0
      Curl_mime_cleanpart(data->state.formp);
2155
0
      result = Curl_getformdata(data, data->state.formp, data->set.httppost,
2156
0
                                data->state.fread_func);
2157
0
      if(result) {
2158
0
        curlx_safefree(data->state.formp);
2159
0
        return result;
2160
0
      }
2161
0
      data->state.mimepost = data->state.formp;
2162
0
    }
2163
0
    break;
2164
0
#endif
2165
0
  default:
2166
0
    data->state.mimepost = NULL;
2167
0
    break;
2168
15.0k
  }
2169
2170
15.0k
  switch(httpreq) {
2171
0
  case HTTPREQ_POST_FORM:
2172
15.0k
  case HTTPREQ_POST_MIME:
2173
    /* This is form posting using mime data. */
2174
15.0k
#ifndef CURL_DISABLE_MIME
2175
15.0k
    if(data->state.mimepost) {
2176
15.0k
      const char *cthdr = Curl_checkheaders(data, STRCONST("Content-Type"));
2177
2178
      /* Read and seek body only. */
2179
15.0k
      data->state.mimepost->flags |= MIME_BODY_ONLY;
2180
2181
      /* Prepare the mime structure headers & set content type. */
2182
2183
15.0k
      if(cthdr)
2184
0
        for(cthdr += 13; *cthdr == ' '; cthdr++)
2185
0
          ;
2186
15.0k
      else if(data->state.mimepost->kind == MIMEKIND_MULTIPART)
2187
15.0k
        cthdr = "multipart/form-data";
2188
2189
15.0k
      curl_mime_headers(data->state.mimepost, data->set.headers, 0);
2190
15.0k
      result = Curl_mime_prepare_headers(data, data->state.mimepost, cthdr,
2191
15.0k
                                         NULL, MIMESTRATEGY_FORM);
2192
15.0k
      curl_mime_headers(data->state.mimepost, NULL, 0);
2193
15.0k
      if(result)
2194
141
        return result;
2195
14.9k
      result = Curl_creader_set_mime(data, data->state.mimepost);
2196
14.9k
      if(result)
2197
220
        return result;
2198
14.9k
    }
2199
0
    else
2200
0
#endif
2201
0
    {
2202
0
      result = Curl_creader_set_null(data);
2203
0
    }
2204
14.7k
    data->state.infilesize = Curl_creader_total_length(data);
2205
14.7k
    return result;
2206
2207
0
  default:
2208
0
    return Curl_creader_set_null(data);
2209
15.0k
  }
2210
  /* never reached */
2211
15.0k
}
2212
#endif
2213
2214
static CURLcode set_reader(struct Curl_easy *data, Curl_HttpReq httpreq)
2215
61.2k
{
2216
61.2k
  CURLcode result = CURLE_OK;
2217
61.2k
  curl_off_t postsize = data->state.infilesize;
2218
2219
61.2k
  DEBUGASSERT(data->conn);
2220
2221
61.2k
  if(data->req.authneg) {
2222
453
    return Curl_creader_set_null(data);
2223
453
  }
2224
2225
60.7k
  switch(httpreq) {
2226
1.98k
  case HTTPREQ_PUT: /* Let's PUT the data to the server! */
2227
1.98k
    return postsize ? Curl_creader_set_fread(data, postsize) :
2228
1.98k
      Curl_creader_set_null(data);
2229
2230
0
#if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API)
2231
0
  case HTTPREQ_POST_FORM:
2232
15.0k
  case HTTPREQ_POST_MIME:
2233
15.0k
    return set_post_reader(data, httpreq);
2234
0
#endif
2235
2236
1.49k
  case HTTPREQ_POST:
2237
    /* this is the simple POST, using x-www-form-urlencoded style */
2238
    /* the size of the post body */
2239
1.49k
    if(!postsize) {
2240
125
      result = Curl_creader_set_null(data);
2241
125
    }
2242
1.36k
    else if(data->set.postfields) {
2243
463
      size_t plen = curlx_sotouz_range(postsize, 0, SIZE_MAX);
2244
463
      if(plen == SIZE_MAX)
2245
0
        return CURLE_OUT_OF_MEMORY;
2246
463
      else if(plen)
2247
463
        result = Curl_creader_set_buf(data, data->set.postfields, plen);
2248
0
      else
2249
0
        result = Curl_creader_set_null(data);
2250
463
    }
2251
904
    else {
2252
      /* we read the bytes from the callback. In case "chunked" encoding
2253
       * is forced by the application, we disregard `postsize`. This is
2254
       * a backward compatibility decision to earlier versions where
2255
       * chunking disregarded this. See issue #13229. */
2256
904
      bool chunked = FALSE;
2257
904
      char *ptr = Curl_checkheaders(data, STRCONST("Transfer-Encoding"));
2258
904
      if(ptr) {
2259
        /* Some kind of TE is requested, check if 'chunked' is chosen */
2260
212
        chunked = Curl_compareheader(ptr, STRCONST("Transfer-Encoding:"),
2261
212
                                     STRCONST("chunked"));
2262
212
      }
2263
904
      result = Curl_creader_set_fread(data, chunked ? -1 : postsize);
2264
904
    }
2265
1.49k
    return result;
2266
2267
42.1k
  default:
2268
    /* HTTP GET/HEAD download, has no body, needs no Content-Length */
2269
42.1k
    data->state.infilesize = 0;
2270
42.1k
    return Curl_creader_set_null(data);
2271
60.7k
  }
2272
  /* not reached */
2273
60.7k
}
2274
2275
static CURLcode http_resume(struct Curl_easy *data, Curl_HttpReq httpreq)
2276
60.8k
{
2277
60.8k
  if((HTTPREQ_POST == httpreq || HTTPREQ_POST_FORM == httpreq ||
2278
59.3k
      HTTPREQ_POST_MIME == httpreq || HTTPREQ_PUT == httpreq) &&
2279
18.6k
     data->state.resume_from) {
2280
299
    failf(data, "HTTP upload cannot be resumed");
2281
299
    return CURLE_BAD_FUNCTION_ARGUMENT;
2282
299
  }
2283
60.5k
  return CURLE_OK;
2284
60.8k
}
2285
2286
static CURLcode http_req_set_TE(struct Curl_easy *data,
2287
                                struct dynbuf *req,
2288
                                int httpversion)
2289
60.4k
{
2290
60.4k
  CURLcode result = CURLE_OK;
2291
60.4k
  const char *ptr;
2292
2293
60.4k
  ptr = Curl_checkheaders(data, STRCONST("Transfer-Encoding"));
2294
60.4k
  if(ptr) {
2295
    /* Some kind of TE is requested, check if 'chunked' is chosen */
2296
365
    data->req.upload_chunky =
2297
365
      Curl_compareheader(ptr,
2298
365
                         STRCONST("Transfer-Encoding:"), STRCONST("chunked"));
2299
365
    if(data->req.upload_chunky && (httpversion >= 20)) {
2300
0
      infof(data, "suppressing chunked transfer encoding on connection "
2301
0
            "using HTTP version 2 or higher");
2302
0
      data->req.upload_chunky = FALSE;
2303
0
    }
2304
365
  }
2305
60.0k
  else {
2306
60.0k
    curl_off_t req_clen = Curl_creader_total_length(data);
2307
2308
60.0k
    if(req_clen < 0) {
2309
      /* indeterminate request content length */
2310
4.60k
      if(httpversion > 10) {
2311
        /* On HTTP/1.1, enable chunked, on HTTP/2 and later we do not
2312
         * need it */
2313
4.57k
        data->req.upload_chunky = (httpversion < 20);
2314
4.57k
      }
2315
29
      else {
2316
29
        failf(data, "Chunky upload is not supported by HTTP 1.0");
2317
29
        return CURLE_UPLOAD_FAILED;
2318
29
      }
2319
4.60k
    }
2320
55.4k
    else {
2321
      /* else, no chunky upload */
2322
55.4k
      data->req.upload_chunky = FALSE;
2323
55.4k
    }
2324
2325
60.0k
    if(data->req.upload_chunky)
2326
1.14k
      result = curlx_dyn_add(req, "Transfer-Encoding: chunked\r\n");
2327
60.0k
  }
2328
60.4k
  return result;
2329
60.4k
}
2330
2331
static CURLcode addexpect(struct Curl_easy *data, struct dynbuf *r,
2332
                          int httpversion, bool *announced_exp100)
2333
18.2k
{
2334
18.2k
  CURLcode result;
2335
18.2k
  char *ptr;
2336
2337
18.2k
  *announced_exp100 = FALSE;
2338
  /* Avoid Expect: 100-continue if Upgrade: is used */
2339
18.2k
  if(data->req.upgr101 != UPGR101_NONE)
2340
231
    return CURLE_OK;
2341
2342
  /* For really small puts we do not use Expect: headers at all, and for
2343
     the somewhat bigger ones we allow the app to disable it. Make
2344
     sure that the expect100header is always set to the preferred value
2345
     here. */
2346
17.9k
  ptr = Curl_checkheaders(data, STRCONST("Expect"));
2347
17.9k
  if(ptr) {
2348
0
    *announced_exp100 =
2349
0
      Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
2350
0
  }
2351
17.9k
  else if(!data->state.disableexpect && (httpversion == 11)) {
2352
    /* if not doing HTTP 1.0 or version 2, or disabled explicitly, we add an
2353
       Expect: 100-continue to the headers which actually speeds up post
2354
       operations (as there is one packet coming back from the web server) */
2355
5.16k
    curl_off_t client_len = Curl_creader_client_length(data);
2356
5.16k
    if(client_len > EXPECT_100_THRESHOLD || client_len < 0) {
2357
977
      result = curlx_dyn_addn(r, STRCONST("Expect: 100-continue\r\n"));
2358
977
      if(result)
2359
3
        return result;
2360
974
      *announced_exp100 = TRUE;
2361
974
    }
2362
5.16k
  }
2363
17.9k
  return CURLE_OK;
2364
17.9k
}
2365
2366
static CURLcode http_add_content_hds(struct Curl_easy *data,
2367
                                     struct dynbuf *r,
2368
                                     int httpversion,
2369
                                     Curl_HttpReq httpreq)
2370
60.3k
{
2371
60.3k
  CURLcode result = CURLE_OK;
2372
60.3k
  curl_off_t req_clen;
2373
60.3k
  bool announced_exp100 = FALSE;
2374
2375
60.3k
  DEBUGASSERT(data->conn);
2376
60.3k
  if(data->req.upload_chunky) {
2377
1.12k
    result = Curl_httpchunk_add_reader(data);
2378
1.12k
    if(result)
2379
7
      return result;
2380
1.12k
  }
2381
2382
  /* Get the request body length that has been set up */
2383
60.3k
  req_clen = Curl_creader_total_length(data);
2384
60.3k
  switch(httpreq) {
2385
1.94k
  case HTTPREQ_PUT:
2386
3.42k
  case HTTPREQ_POST:
2387
3.42k
#if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API)
2388
3.42k
  case HTTPREQ_POST_FORM:
2389
18.2k
  case HTTPREQ_POST_MIME:
2390
18.2k
#endif
2391
    /* We only set Content-Length and allow a custom Content-Length if
2392
       we do not upload data chunked, as RFC2616 forbids us to set both
2393
       kinds of headers (Transfer-Encoding: chunked and Content-Length).
2394
       We do not override a custom "Content-Length" header, but during
2395
       authentication negotiation that header is suppressed.
2396
     */
2397
18.2k
    if(req_clen >= 0 && !data->req.upload_chunky &&
2398
13.4k
       (data->req.authneg ||
2399
13.4k
        !Curl_checkheaders(data, STRCONST("Content-Length")))) {
2400
      /* we allow replacing this header if not during auth negotiation,
2401
         although it is not wise to actually set your own */
2402
13.4k
      result = curlx_dyn_addf(r, "Content-Length: %" FMT_OFF_T "\r\n",
2403
13.4k
                              req_clen);
2404
13.4k
    }
2405
18.2k
    if(result)
2406
19
      goto out;
2407
2408
18.2k
#ifndef CURL_DISABLE_MIME
2409
    /* Output mime-generated headers. */
2410
18.2k
    if(data->state.mimepost &&
2411
14.3k
       ((httpreq == HTTPREQ_POST_FORM) || (httpreq == HTTPREQ_POST_MIME))) {
2412
14.3k
      struct curl_slist *hdr;
2413
2414
28.7k
      for(hdr = data->state.mimepost->curlheaders; hdr; hdr = hdr->next) {
2415
14.3k
        result = curlx_dyn_addf(r, "%s\r\n", hdr->data);
2416
14.3k
        if(result)
2417
23
          goto out;
2418
14.3k
      }
2419
14.3k
    }
2420
18.2k
#endif
2421
18.2k
    if(httpreq == HTTPREQ_POST &&
2422
1.47k
       !Curl_checkheaders(data, STRCONST("Content-Type"))) {
2423
1.47k
      result = curlx_dyn_addn(r, STRCONST("Content-Type: application/"
2424
1.47k
                                          "x-www-form-urlencoded\r\n"));
2425
1.47k
      if(result)
2426
7
        goto out;
2427
1.47k
    }
2428
18.2k
    result = addexpect(data, r, httpversion, &announced_exp100);
2429
18.2k
    if(result)
2430
3
      goto out;
2431
18.2k
    break;
2432
42.0k
  default:
2433
42.0k
    break;
2434
60.3k
  }
2435
2436
60.2k
  Curl_pgrsSetUploadSize(data, req_clen);
2437
60.2k
  if(announced_exp100)
2438
974
    result = http_exp100_add_reader(data);
2439
2440
60.3k
out:
2441
60.3k
  return result;
2442
60.2k
}
2443
2444
#ifndef CURL_DISABLE_COOKIES
2445
2446
static CURLcode http_cookies(struct Curl_easy *data,
2447
                             struct dynbuf *r)
2448
60.3k
{
2449
60.3k
  CURLcode result = CURLE_OK;
2450
60.3k
  const char *addcookies = NULL;
2451
60.3k
  bool linecap = FALSE;
2452
60.3k
  if(CURL_EASY_STR(data, STRING_COOKIE) &&
2453
441
     !Curl_checkheaders(data, STRCONST("Cookie")) &&
2454
407
     Curl_auth_allowed_to_host(data))
2455
397
    addcookies = CURL_EASY_STR(data, STRING_COOKIE);
2456
2457
60.3k
  if(data->cookies || addcookies) {
2458
60.3k
    struct Curl_llist list;
2459
60.3k
    int count = 0;
2460
2461
60.3k
    if(data->cookies && data->state.cookie_engine) {
2462
60.3k
      bool okay;
2463
60.3k
      const char *host = data->req.cookiehost ?
2464
59.2k
        data->req.cookiehost : data->state.origin->hostname;
2465
60.3k
      Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
2466
60.3k
      result = Curl_cookie_getlist(data, &okay, host, &list);
2467
60.3k
      if(!result && okay) {
2468
382
        struct Curl_llist_node *n;
2469
382
        size_t clen = 8; /* hold the size of the generated Cookie: header */
2470
2471
        /* loop through all cookies that matched */
2472
622
        for(n = Curl_llist_head(&list); n; n = Curl_node_next(n)) {
2473
240
          struct Cookie *co = Curl_node_elem(n);
2474
240
          if(co->value) {
2475
240
            size_t add;
2476
240
            if(!count) {
2477
141
              result = curlx_dyn_addn(r, STRCONST("Cookie: "));
2478
141
              if(result)
2479
0
                break;
2480
141
            }
2481
240
            add = strlen(co->name) + strlen(co->value) + 1;
2482
240
            if(clen + add >= MAX_COOKIE_HEADER_LEN) {
2483
0
              infof(data, "Restricted outgoing cookies due to header size, "
2484
0
                    "'%s' not sent", co->name);
2485
0
              linecap = TRUE;
2486
0
              break;
2487
0
            }
2488
240
            result = curlx_dyn_addf(r, "%s%s=%s", count ? "; " : "",
2489
240
                                    co->name, co->value);
2490
240
            if(result)
2491
0
              break;
2492
240
            clen += add + (count ? 2 : 0);
2493
240
            count++;
2494
240
          }
2495
240
        }
2496
382
        Curl_llist_destroy(&list, NULL);
2497
382
      }
2498
60.3k
      Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
2499
60.3k
    }
2500
60.3k
    if(addcookies && !result && !linecap) {
2501
397
      if(!count)
2502
379
        result = curlx_dyn_addn(r, STRCONST("Cookie: "));
2503
397
      if(!result) {
2504
392
        result = curlx_dyn_addf(r, "%s%s", count ? "; " : "", addcookies);
2505
392
        count++;
2506
392
      }
2507
397
    }
2508
60.3k
    if(count && !result)
2509
510
      result = curlx_dyn_addn(r, STRCONST("\r\n"));
2510
2511
60.3k
    if(result)
2512
10
      return result;
2513
60.3k
  }
2514
60.3k
  return result;
2515
60.3k
}
2516
#else
2517
#define http_cookies(a, b) CURLE_OK
2518
#endif
2519
2520
static CURLcode http_range(struct Curl_easy *data,
2521
                           Curl_HttpReq httpreq)
2522
60.5k
{
2523
60.5k
  if(data->state.use_range) {
2524
    /*
2525
     * A range is selected. We use different headers whether we are downloading
2526
     * or uploading and we always let customized headers override our internal
2527
     * ones if any such are specified.
2528
     */
2529
1.03k
    if(((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD)) &&
2530
822
       !Curl_checkheaders(data, STRCONST("Range"))) {
2531
      /* if a line like this was already allocated, free the previous one */
2532
822
      curlx_free(data->state.rangeline);
2533
822
      data->state.rangeline = curl_maprintf("Range: bytes=%s\r\n",
2534
822
                                                 data->state.range);
2535
822
      if(!data->state.rangeline)
2536
5
        return CURLE_OUT_OF_MEMORY;
2537
822
    }
2538
209
    else if((httpreq == HTTPREQ_POST || httpreq == HTTPREQ_PUT) &&
2539
128
            !Curl_checkheaders(data, STRCONST("Content-Range"))) {
2540
128
      curl_off_t req_clen = Curl_creader_total_length(data);
2541
      /* if a line like this was already allocated, free the previous one */
2542
128
      curlx_free(data->state.rangeline);
2543
2544
128
      if(data->set.set_resume_from < 0) {
2545
        /* Upload resume was asked for, but we do not know the size of the
2546
           remote part so we tell the server (and act accordingly) that we
2547
           upload the whole file (again) */
2548
0
        data->state.rangeline =
2549
0
          curl_maprintf("Content-Range: bytes 0-%" FMT_OFF_T "/"
2550
0
                        "%" FMT_OFF_T "\r\n", req_clen - 1, req_clen);
2551
0
      }
2552
128
      else if(data->state.resume_from) {
2553
        /* This is because "resume" was selected */
2554
        /* Not sure if we want to send this header during authentication
2555
         * negotiation, but test1084 checks for it. In which case we have a
2556
         * "null" client reader installed that gives an unexpected length. */
2557
0
        curl_off_t total_len = data->req.authneg ?
2558
0
                               data->state.infilesize :
2559
0
                               (data->state.resume_from + req_clen);
2560
0
        data->state.rangeline =
2561
0
          curl_maprintf("Content-Range: bytes %s%" FMT_OFF_T "/"
2562
0
                        "%" FMT_OFF_T "\r\n",
2563
0
                        data->state.range, total_len - 1, total_len);
2564
0
      }
2565
128
      else {
2566
        /* Range was selected and then we pass the incoming range and append
2567
           total size */
2568
128
        data->state.rangeline =
2569
128
          curl_maprintf("Content-Range: bytes %s/%" FMT_OFF_T "\r\n",
2570
128
                        data->state.range, req_clen);
2571
128
      }
2572
128
      if(!data->state.rangeline)
2573
4
        return CURLE_OUT_OF_MEMORY;
2574
128
    }
2575
1.03k
  }
2576
60.5k
  return CURLE_OK;
2577
60.5k
}
2578
2579
static CURLcode http_firstwrite(struct Curl_easy *data)
2580
6.28k
{
2581
6.28k
  struct connectdata *conn = data->conn;
2582
6.28k
  struct SingleRequest *k = &data->req;
2583
2584
6.28k
  if(data->req.newurl) {
2585
870
    if(conn->bits.close) {
2586
      /* Abort after the headers if "follow Location" is set
2587
         and we are set to close anyway. */
2588
60
      CURL_REQ_CLEAR_RECV(data);
2589
60
      k->done = TRUE;
2590
60
      return CURLE_OK;
2591
60
    }
2592
    /* We have a new URL to load, but since we want to be able to reuse this
2593
       connection properly, we read the full response in "ignore more" */
2594
810
    k->ignorebody = TRUE;
2595
810
    infof(data, "Ignoring the response-body");
2596
810
  }
2597
6.22k
  if(data->state.resume_from && !k->content_range &&
2598
199
     (data->state.httpreq == HTTPREQ_GET) &&
2599
176
     !k->ignorebody) {
2600
2601
149
    if(k->size == data->state.resume_from) {
2602
      /* The resume point is at the end of file, consider this fine even if it
2603
         does not allow resume from here. */
2604
21
      infof(data, "The entire document is already downloaded");
2605
21
      streamclose(conn);
2606
      /* Abort download */
2607
21
      CURL_REQ_CLEAR_RECV(data);
2608
21
      k->done = TRUE;
2609
21
      return CURLE_OK;
2610
21
    }
2611
2612
    /* we wanted to resume a download, although the server does not seem to
2613
     * support this and we did this with a GET (if it was not a GET we did a
2614
     * POST or PUT resume) */
2615
128
    failf(data, "HTTP server does not seem to support "
2616
128
          "byte ranges. Cannot resume.");
2617
128
    return CURLE_RANGE_ERROR;
2618
149
  }
2619
2620
6.07k
  if(data->set.timecondition && !data->state.range &&
2621
     /* A time condition has been set AND no ranges have been requested. This
2622
        seems to be what chapter 13.3.4 of RFC 2616 defines to be the correct
2623
        action for an HTTP/1.1 client */
2624
25
     !Curl_meets_timecondition(data, k->timeofdoc)) {
2625
0
    k->done = TRUE;
2626
    /* We are simulating an HTTP 304 from server so we return
2627
       what should have been returned from the server */
2628
0
    data->info.httpcode = 304;
2629
0
    infof(data, "Simulate an HTTP 304 response");
2630
    /* we abort the transfer before it is completed == we ruin the
2631
       reuse ability. Close the connection */
2632
0
    streamclose(conn);
2633
0
    return CURLE_OK;
2634
0
  } /* we have a time condition */
2635
2636
6.07k
  return CURLE_OK;
2637
6.07k
}
2638
2639
static CURLcode http_add_connection_hd(struct Curl_easy *data,
2640
                                       struct dynbuf *req)
2641
60.2k
{
2642
60.2k
  struct curl_slist *head;
2643
60.2k
  const char *sep = "Connection: ";
2644
60.2k
  CURLcode result = CURLE_OK;
2645
60.2k
  size_t rlen = curlx_dyn_len(req);
2646
60.2k
  bool skip;
2647
2648
  /* Add the 1st custom "Connection: " header, if there is one */
2649
99.6k
  for(head = data->set.headers; head; head = head->next) {
2650
39.5k
    if(curl_strnequal(head->data, "Connection", 10) &&
2651
680
       Curl_headersep(head->data[10]) &&
2652
613
       !http_header_is_empty(head->data)) {
2653
191
      char *value;
2654
191
      result = copy_custom_value(head->data, &value);
2655
191
      if(result)
2656
5
        return result;
2657
186
      result = curlx_dyn_addf(req, "%s%s", sep, value);
2658
186
      sep = ", ";
2659
186
      curlx_free(value);
2660
186
      break; /* leave, having added 1st one */
2661
191
    }
2662
39.5k
  }
2663
2664
  /* add our internal Connection: header values, if we have any */
2665
60.2k
  if(!result && data->state.http_hd_te) {
2666
269
    result = curlx_dyn_addf(req, "%s%s", sep, "TE");
2667
269
    sep = ", ";
2668
269
  }
2669
60.2k
  if(!result && data->state.http_hd_upgrade) {
2670
417
    result = curlx_dyn_addf(req, "%s%s", sep, "Upgrade");
2671
417
    sep = ", ";
2672
417
  }
2673
60.2k
  if(!result && data->state.http_hd_h2_settings) {
2674
417
    result = curlx_dyn_addf(req, "%s%s", sep, "HTTP2-Settings");
2675
417
  }
2676
60.2k
  if(!result && (rlen < curlx_dyn_len(req)))
2677
871
    result = curlx_dyn_addn(req, STRCONST("\r\n"));
2678
60.2k
  if(result)
2679
1
    return result;
2680
2681
  /* Add all user-defined Connection: headers after the first */
2682
60.2k
  skip = TRUE;
2683
100k
  for(head = data->set.headers; head; head = head->next) {
2684
40.1k
    if(curl_strnequal(head->data, "Connection", 10) &&
2685
1.21k
       Curl_headersep(head->data[10]) &&
2686
1.14k
       !http_header_is_empty(head->data)) {
2687
690
      if(skip) {
2688
186
        skip = FALSE;
2689
186
        continue;
2690
186
      }
2691
504
      result = curlx_dyn_addf(req, "%s\r\n", head->data);
2692
504
      if(result)
2693
0
        return result;
2694
504
    }
2695
40.1k
  }
2696
2697
60.2k
  return CURLE_OK;
2698
60.2k
}
2699
2700
/* Header identifier in order we send them by default */
2701
typedef enum {
2702
  H1_HD_REQUEST,
2703
  H1_HD_HOST,
2704
#ifndef CURL_DISABLE_PROXY
2705
  H1_HD_PROXY_AUTH,
2706
#endif
2707
  H1_HD_AUTH,
2708
  H1_HD_RANGE,
2709
  H1_HD_USER_AGENT,
2710
  H1_HD_ACCEPT,
2711
  H1_HD_TE,
2712
  H1_HD_ACCEPT_ENCODING,
2713
  H1_HD_REFERER,
2714
#ifndef CURL_DISABLE_PROXY
2715
  H1_HD_PROXY_CONNECTION,
2716
#endif
2717
  H1_HD_TRANSFER_ENCODING,
2718
#ifndef CURL_DISABLE_ALTSVC
2719
  H1_HD_ALT_USED,
2720
#endif
2721
  H1_HD_UPGRADE,
2722
  H1_HD_COOKIES,
2723
  H1_HD_CONDITIONALS,
2724
  H1_HD_CUSTOM,
2725
  H1_HD_CONTENT,
2726
  H1_HD_CONNECTION,
2727
  H1_HD_LAST  /* the last, empty header line */
2728
} http_hd_t;
2729
2730
static CURLcode http_add_hd(struct Curl_easy *data,
2731
                            struct dynbuf *req,
2732
                            http_hd_t id,
2733
                            unsigned char httpversion,
2734
                            const char *method,
2735
                            bool is_connect,
2736
                            Curl_HttpReq httpreq)
2737
1.20M
{
2738
1.20M
  struct connectdata *conn = data->conn;
2739
1.20M
  CURLcode result = CURLE_OK;
2740
2741
1.20M
  switch(id) {
2742
60.5k
  case H1_HD_REQUEST:
2743
    /* add the main request stuff */
2744
    /* GET/HEAD/POST/PUT */
2745
60.5k
    result = curlx_dyn_addf(req, "%s ", method);
2746
60.5k
    if(!result)
2747
60.4k
      result = http_target(data, req);
2748
60.5k
    if(!result)
2749
60.4k
      result = curlx_dyn_addf(req, " HTTP/%s\r\n",
2750
60.4k
                              get_http_string(httpversion));
2751
60.5k
    break;
2752
2753
60.4k
  case H1_HD_HOST:
2754
60.4k
    if(data->state.http_host) {
2755
60.1k
      result = curlx_dyn_add(req, data->state.http_host);
2756
60.1k
      if(!result)
2757
60.1k
        result = curlx_dyn_addn(req, STRCONST("\r\n"));
2758
60.1k
    }
2759
60.4k
    break;
2760
2761
0
#ifndef CURL_DISABLE_PROXY
2762
60.4k
  case H1_HD_PROXY_AUTH:
2763
60.4k
    if(data->req.hd_proxy_auth && (conn->bits.origin_is_proxy || is_connect))
2764
0
      result = curlx_dyn_add(req, data->req.hd_proxy_auth);
2765
60.4k
    break;
2766
0
#endif
2767
2768
60.4k
  case H1_HD_AUTH:
2769
60.4k
    if(data->req.hd_auth && !is_connect)
2770
3.77k
      result = curlx_dyn_add(req, data->req.hd_auth);
2771
60.4k
    break;
2772
2773
60.4k
  case H1_HD_RANGE:
2774
60.4k
    if(data->state.use_range && data->state.rangeline)
2775
935
      result = curlx_dyn_add(req, data->state.rangeline);
2776
60.4k
    break;
2777
2778
60.4k
  case H1_HD_USER_AGENT: {
2779
60.4k
    const char *ua = CURL_EASY_STR(data, STRING_USERAGENT);
2780
60.4k
    if(ua && *ua && !Curl_checkheaders(data, STRCONST("User-Agent")))
2781
196
      result = curlx_dyn_addf(req, "User-Agent: %s\r\n", ua);
2782
60.4k
    break;
2783
0
  }
2784
2785
60.4k
  case H1_HD_ACCEPT:
2786
60.4k
    if(!Curl_checkheaders(data, STRCONST("Accept")))
2787
60.4k
      result = curlx_dyn_add(req, "Accept: */*\r\n");
2788
60.4k
    break;
2789
2790
60.4k
  case H1_HD_TE:
2791
60.4k
#ifdef HAVE_LIBZ
2792
60.4k
    if(!Curl_checkheaders(data, STRCONST("TE")) &&
2793
59.8k
       data->set.http_transfer_encoding) {
2794
271
      data->state.http_hd_te = TRUE;
2795
271
      result = curlx_dyn_add(req, "TE: gzip\r\n");
2796
271
    }
2797
60.4k
#endif
2798
60.4k
    break;
2799
2800
60.4k
  case H1_HD_ACCEPT_ENCODING: {
2801
60.4k
    const char *enc = CURL_EASY_STR(data, STRING_ENCODING);
2802
60.4k
    if(enc && !Curl_checkheaders(data, STRCONST("Accept-Encoding")))
2803
4.78k
      result = curlx_dyn_addf(req, "Accept-Encoding: %s\r\n", enc);
2804
60.4k
    break;
2805
0
  }
2806
2807
60.4k
  case H1_HD_REFERER:
2808
60.4k
    if(Curl_bufref_ptr(&data->state.referer) &&
2809
211
       !Curl_checkheaders(data, STRCONST("Referer")))
2810
211
      result = curlx_dyn_addf(req, "Referer: %s\r\n",
2811
211
                              Curl_bufref_ptr(&data->state.referer));
2812
60.4k
    break;
2813
2814
0
#ifndef CURL_DISABLE_PROXY
2815
60.4k
  case H1_HD_PROXY_CONNECTION:
2816
60.4k
    if(conn->bits.origin_is_proxy &&
2817
0
       !Curl_checkheaders(data, STRCONST("Proxy-Connection")) &&
2818
0
       !Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection")))
2819
0
      result = curlx_dyn_add(req, "Proxy-Connection: Keep-Alive\r\n");
2820
60.4k
    break;
2821
0
#endif
2822
2823
60.4k
  case H1_HD_TRANSFER_ENCODING:
2824
60.4k
    result = http_req_set_TE(data, req, httpversion);
2825
60.4k
    break;
2826
2827
0
#ifndef CURL_DISABLE_ALTSVC
2828
60.4k
  case H1_HD_ALT_USED:
2829
60.4k
    if(conn->bits.altused && conn->via_peer &&
2830
0
       !Curl_checkheaders(data, STRCONST("Alt-Used")))
2831
0
      result = curlx_dyn_addf(req, "Alt-Used: %s:%u\r\n",
2832
0
                              conn->via_peer->hostname, conn->via_peer->port);
2833
60.4k
    break;
2834
0
#endif
2835
2836
60.4k
  case H1_HD_UPGRADE:
2837
60.4k
    if(!Curl_conn_is_ssl(conn) && (httpversion < 20) &&
2838
21.8k
       (data->state.http_neg.wanted & CURL_HTTP_V2x) &&
2839
21.6k
       data->state.http_neg.h2_upgrade) {
2840
      /* append HTTP2 upgrade magic stuff to the HTTP request if it is not done
2841
         over SSL */
2842
419
      result = Curl_http2_request_upgrade(req, data);
2843
419
    }
2844
60.4k
#ifndef CURL_DISABLE_WEBSOCKETS
2845
60.4k
    if(!result && conn->scheme->protocol & (CURLPROTO_WS | CURLPROTO_WSS))
2846
0
      result = Curl_ws_request(data, req);
2847
60.4k
#endif
2848
60.4k
    break;
2849
2850
60.3k
  case H1_HD_COOKIES:
2851
60.3k
    result = http_cookies(data, req);
2852
60.3k
    break;
2853
2854
60.3k
  case H1_HD_CONDITIONALS:
2855
60.3k
    result = Curl_add_timecondition(data, req);
2856
60.3k
    break;
2857
2858
60.3k
  case H1_HD_CUSTOM:
2859
60.3k
    result = Curl_add_custom_headers(data, httpversion, req);
2860
60.3k
    break;
2861
2862
60.3k
  case H1_HD_CONTENT:
2863
60.3k
    result = http_add_content_hds(data, req, httpversion, httpreq);
2864
60.3k
    break;
2865
2866
60.2k
  case H1_HD_CONNECTION: {
2867
60.2k
    result = http_add_connection_hd(data, req);
2868
60.2k
    break;
2869
0
  }
2870
2871
60.2k
  case H1_HD_LAST:
2872
60.2k
    result = curlx_dyn_addn(req, STRCONST("\r\n"));
2873
60.2k
    break;
2874
1.20M
  }
2875
1.20M
  return result;
2876
1.20M
}
2877
2878
/*
2879
 * Curl_http() gets called from the generic multi_do() function when an HTTP
2880
 * request is to be performed. This creates and sends a properly constructed
2881
 * HTTP request.
2882
 */
2883
CURLcode Curl_http(struct Curl_easy *data, bool *done)
2884
62.9k
{
2885
62.9k
  CURLcode result = CURLE_OK;
2886
62.9k
  Curl_HttpReq httpreq;
2887
62.9k
  const char *method;
2888
62.9k
  struct dynbuf req;
2889
62.9k
  unsigned char httpversion;
2890
62.9k
  size_t hd_id;
2891
2892
  /* Always consider the DO phase done after this function call, even if there
2893
     may be parts of the request that are not yet sent, since we can deal with
2894
     the rest of the request in the PERFORM phase. */
2895
62.9k
  *done = TRUE;
2896
  /* initialize a dynamic send-buffer */
2897
62.9k
  curlx_dyn_init(&req, DYN_HTTP_REQUEST);
2898
  /* make sure the header buffer is reset - if there are leftovers from a
2899
     previous transfer */
2900
62.9k
  curlx_dyn_reset(&data->state.headerb);
2901
62.9k
  data->state.maybe_folded = FALSE;
2902
62.9k
  data->state.leading_unfold = FALSE;
2903
2904
  /* Add collecting of headers written to client. For a new connection,
2905
   * we might have done that already, but reuse
2906
   * or multiplex needs it here as well. */
2907
62.9k
  result = Curl_headers_init(data);
2908
62.9k
  if(result)
2909
18
    goto out;
2910
2911
62.9k
  data->state.http_hd_te = FALSE;
2912
62.9k
  data->state.http_hd_upgrade = FALSE;
2913
62.9k
  data->state.http_hd_h2_settings = FALSE;
2914
2915
  /* what kind of request do we need to send? */
2916
62.9k
  Curl_http_method(data, &method, &httpreq);
2917
2918
  /* select host to send */
2919
62.9k
  result = http_set_aptr_host(data);
2920
  /* setup the authentication headers, how that method and host are known */
2921
62.9k
  if(!result)
2922
62.8k
    result = Curl_http_output_auth(data, data->conn, httpreq, method,
2923
62.8k
                                   data->state.up.path,
2924
62.8k
                                   data->state.up.query, FALSE);
2925
  /* Setup input reader, resume information and ranges */
2926
62.9k
  if(!result)
2927
61.2k
    result = set_reader(data, httpreq);
2928
62.9k
  if(!result)
2929
60.8k
    result = http_resume(data, httpreq);
2930
62.9k
  if(!result)
2931
60.5k
    result = http_range(data, httpreq);
2932
62.9k
  if(result)
2933
2.39k
    goto out;
2934
2935
60.5k
  httpversion = http_request_version(data);
2936
  /* Add request line and all headers to `req` */
2937
1.26M
  for(hd_id = 0; hd_id <= H1_HD_LAST; ++hd_id) {
2938
1.20M
    result = http_add_hd(data, &req, (http_hd_t)hd_id,
2939
1.20M
                         httpversion, method, FALSE, httpreq);
2940
1.20M
    if(result)
2941
248
      goto out;
2942
1.20M
  }
2943
2944
  /* setup variables for the upcoming transfer and send */
2945
60.2k
  Curl_xfer_setup_sendrecv(data, -1);
2946
60.2k
  result = Curl_req_send(data, &req, httpversion);
2947
2948
60.2k
  if((httpversion >= 20) && data->req.upload_chunky)
2949
    /* upload_chunky was set above to set up the request in a chunky fashion,
2950
       but is disabled here again to avoid that the chunked encoded version is
2951
       actually used when sending the request body over h2 */
2952
0
    data->req.upload_chunky = FALSE;
2953
2954
62.9k
out:
2955
62.9k
  if(result == CURLE_TOO_LARGE)
2956
49
    failf(data, "HTTP request too large");
2957
2958
62.9k
  curlx_dyn_free(&req);
2959
62.9k
  return result;
2960
60.2k
}
2961
2962
typedef enum {
2963
  STATUS_UNKNOWN, /* not enough data to tell yet */
2964
  STATUS_DONE, /* a status line was read */
2965
  STATUS_BAD /* not a status line */
2966
} statusline;
2967
2968
/* Check a string for a prefix. Check no more than 'len' bytes */
2969
static bool checkprefixmax(const char *prefix, size_t plen,
2970
                           const char *buffer, size_t blen)
2971
239k
{
2972
239k
  size_t ch = CURLMIN(plen, blen);
2973
239k
  return curl_strnequal(prefix, buffer, ch);
2974
239k
}
2975
2976
/*
2977
 * checkhttpprefix()
2978
 *
2979
 * Returns TRUE if member of the list matches prefix of string
2980
 */
2981
static statusline checkhttpprefix(struct Curl_easy *data,
2982
                                  const char *s, size_t len)
2983
239k
{
2984
239k
  statusline rc = STATUS_BAD;
2985
239k
  statusline onmatch = len >= 5 ? STATUS_DONE : STATUS_UNKNOWN;
2986
239k
  size_t i;
2987
2988
239k
  for(i = 0; i < Curl_frzn_strs_count(data->set.a.http200aliases); ++i) {
2989
0
    struct Curl_str *alias = Curl_frzn_strs_get(data->set.a.http200aliases, i);
2990
0
    if(alias && checkprefixmax(alias->str, alias->len, s, len)) {
2991
0
      rc = onmatch;
2992
0
      break;
2993
0
    }
2994
0
  }
2995
2996
239k
  if((rc != STATUS_DONE) && checkprefixmax(STRCONST("HTTP/"), s, len))
2997
238k
    rc = onmatch;
2998
2999
239k
  return rc;
3000
239k
}
3001
3002
#ifndef CURL_DISABLE_RTSP
3003
static statusline checkrtspprefix(struct Curl_easy *data,
3004
                                  const char *s, size_t len)
3005
0
{
3006
0
  statusline status = STATUS_BAD;
3007
0
  statusline onmatch = len >= 5 ? STATUS_DONE : STATUS_UNKNOWN;
3008
0
  (void)data;
3009
0
  if(checkprefixmax(STRCONST("RTSP/"), s, len))
3010
0
    status = onmatch;
3011
3012
0
  return status;
3013
0
}
3014
#endif /* CURL_DISABLE_RTSP */
3015
3016
static statusline checkprotoprefix(struct Curl_easy *data,
3017
                                   struct connectdata *conn,
3018
                                   const char *s, size_t len)
3019
238k
{
3020
238k
#ifndef CURL_DISABLE_RTSP
3021
238k
  if(conn->scheme->protocol & CURLPROTO_RTSP)
3022
0
    return checkrtspprefix(data, s, len);
3023
#else
3024
  (void)conn;
3025
#endif /* CURL_DISABLE_RTSP */
3026
3027
238k
  return checkhttpprefix(data, s, len);
3028
238k
}
3029
3030
/* HTTP header has field name `n` (a string constant) */
3031
#define HD_IS(hd, hdlen, n) \
3032
18.6k
  (((hdlen) >= (sizeof(n) - 1)) && curl_strnequal(n, hd, sizeof(n) - 1))
3033
3034
#define HD_VAL(hd, hdlen, n) \
3035
94.2k
  ((((hdlen) >= (sizeof(n) - 1)) && (hd) && \
3036
94.2k
    curl_strnequal(n, hd, sizeof(n) - 1)) ? ((hd) + (sizeof(n) - 1)) : NULL)
3037
3038
/* HTTP header has field name `n` (a string constant) and contains `v`
3039
 * (a string constant) in its value(s) */
3040
#define HD_IS_AND_SAYS(hd, hdlen, n, v) \
3041
3.93k
  (HD_IS(hd, hdlen, n) && \
3042
3.93k
   ((hdlen) > ((sizeof(n) - 1) + (sizeof(v) - 1))) && \
3043
3.93k
   Curl_compareheader(hd, STRCONST(n), STRCONST(v)))
3044
3045
/*
3046
 * http_header_a() parses a single response header starting with A.
3047
 */
3048
static CURLcode http_header_a(struct Curl_easy *data,
3049
                              const char *hd, size_t hdlen)
3050
2.56k
{
3051
2.56k
#ifndef CURL_DISABLE_ALTSVC
3052
2.56k
  const char *v;
3053
2.56k
  v = (data->asi &&
3054
2.56k
       (Curl_xfer_is_secure(data) ||
3055
2.56k
#ifdef DEBUGBUILD
3056
        /* allow debug builds to circumvent the HTTPS restriction */
3057
2.56k
        getenv("CURL_ALTSVC_HTTP")
3058
#else
3059
        0
3060
#endif
3061
2.56k
         )) ? HD_VAL(hd, hdlen, "Alt-Svc:") : NULL;
3062
2.56k
  if(v) {
3063
    /* the ALPN of the current request */
3064
0
    struct SingleRequest *k = &data->req;
3065
0
    enum alpnid id = (k->httpversion == 30) ? ALPN_h3 :
3066
0
      (k->httpversion == 20) ? ALPN_h2 : ALPN_h1;
3067
0
    return Curl_altsvc_parse(data, data->asi, v, data->state.origin, id);
3068
0
  }
3069
#else
3070
  (void)data;
3071
  (void)hd;
3072
  (void)hdlen;
3073
#endif
3074
2.56k
  return CURLE_OK;
3075
2.56k
}
3076
3077
/*
3078
 * http_header_c() parses a single response header starting with C.
3079
 */
3080
static CURLcode http_header_c(struct Curl_easy *data,
3081
                              const char *hd, size_t hdlen)
3082
36.4k
{
3083
36.4k
  struct connectdata *conn = data->conn;
3084
36.4k
  struct SingleRequest *k = &data->req;
3085
36.4k
  const char *v;
3086
3087
  /* Check for Content-Length: header lines to get size. Browsers insist we
3088
     should accept multiple Content-Length headers and that a comma separated
3089
     list also is fine and then we should accept them all as long as they are
3090
     the same value. Different values trigger error.
3091
   */
3092
36.4k
  v = (!k->http_bodyless && !data->set.ignorecl && !k->ignore_cl) ?
3093
36.4k
    HD_VAL(hd, hdlen, "Content-Length:") : NULL;
3094
36.4k
  if(v) {
3095
669
    do {
3096
669
      curl_off_t contentlength;
3097
669
      int offt = curlx_str_numblanks(&v, &contentlength);
3098
3099
669
      if(offt == STRE_OVERFLOW) {
3100
        /* out of range */
3101
12
        if(data->set.max_filesize) {
3102
12
          failf(data, "Maximum file size exceeded");
3103
12
          return CURLE_FILESIZE_EXCEEDED;
3104
12
        }
3105
0
        streamclose(conn);
3106
0
        infof(data, "Overflow Content-Length: value");
3107
0
        return CURLE_OK;
3108
12
      }
3109
657
      else {
3110
657
        if((offt == STRE_OK) &&
3111
619
           ((k->size == -1) || /* not set to something before */
3112
570
            (k->size == contentlength))) { /* or the same value */
3113
3114
570
          k->size = contentlength;
3115
570
          curlx_str_passblanks(&v);
3116
3117
          /* on a comma, loop and get the next instead */
3118
570
          if(!curlx_str_single(&v, ','))
3119
67
            continue;
3120
3121
503
          if(!curlx_str_newline(&v)) {
3122
492
            k->maxdownload = k->size;
3123
492
            return CURLE_OK;
3124
492
          }
3125
503
        }
3126
        /* negative, different value or rubbish - bad HTTP */
3127
98
        failf(data, "Invalid Content-Length: value");
3128
98
        return CURLE_WEIRD_SERVER_REPLY;
3129
657
      }
3130
669
    } while(1);
3131
602
  }
3132
35.8k
  v = (!k->http_bodyless && CURL_EASY_STR(data, STRING_ENCODING)) ?
3133
35.8k
    HD_VAL(hd, hdlen, "Content-Encoding:") : NULL;
3134
35.8k
  if(v) {
3135
    /*
3136
     * Process Content-Encoding. Look for the values: identity, gzip, deflate,
3137
     * compress, x-gzip and x-compress. x-gzip and x-compress are the same as
3138
     * gzip and compress. (Sec 3.5 RFC 2616). zlib cannot handle compress.
3139
     * Errors are handled further down when the response body is processed
3140
     */
3141
3.65k
    return Curl_build_unencoding_stack(data, v, FALSE);
3142
3.65k
  }
3143
  /* check for Content-Type: header lines to get the MIME-type */
3144
32.1k
  v = HD_VAL(hd, hdlen, "Content-Type:");
3145
32.1k
  if(v) {
3146
9.12k
    char *contenttype = Curl_copy_header_value(hd);
3147
9.12k
    if(!contenttype)
3148
1
      return CURLE_OUT_OF_MEMORY;
3149
9.12k
    if(!*contenttype)
3150
      /* ignore empty data */
3151
8.91k
      curlx_free(contenttype);
3152
207
    else {
3153
207
      curlx_free(data->info.contenttype);
3154
207
      data->info.contenttype = contenttype;
3155
207
    }
3156
9.12k
    return CURLE_OK;
3157
9.12k
  }
3158
23.0k
  if((k->httpversion < 20) &&
3159
2.60k
     HD_IS_AND_SAYS(hd, hdlen, "Connection:", "close")) {
3160
    /*
3161
     * [RFC 2616, section 8.1.2.1]
3162
     * "Connection: close" is HTTP/1.1 language and means that
3163
     * the connection will close when this request has been
3164
     * served.
3165
     */
3166
85
    connclose(conn);
3167
85
    return CURLE_OK;
3168
85
  }
3169
22.9k
  if((k->httpversion == 10) &&
3170
1.32k
     HD_IS_AND_SAYS(hd, hdlen, "Connection:", "keep-alive")) {
3171
    /*
3172
     * An HTTP/1.0 reply with the 'Connection: keep-alive' line
3173
     * tells us the connection will be kept alive for our
3174
     * pleasure. Default action for 1.0 is to close.
3175
     *
3176
     * [RFC2068, section 19.7.1] */
3177
0
    connkeep(conn);
3178
0
    infof(data, "HTTP/1.0 connection set to keep alive");
3179
0
    return CURLE_OK;
3180
0
  }
3181
22.9k
  v = !k->http_bodyless ? HD_VAL(hd, hdlen, "Content-Range:") : NULL;
3182
22.9k
  if(v) {
3183
    /* Content-Range: bytes [num]-
3184
       Content-Range: bytes: [num]-
3185
       Content-Range: [num]-
3186
       Content-Range: [asterisk]/[total]
3187
3188
       The second format was added since Sun's webserver
3189
       JavaWebServer/1.1.1 obviously sends the header this way!
3190
       The third added since some servers use that!
3191
       The fourth means the requested range was unsatisfied.
3192
     */
3193
3194
1.60k
    const char *ptr = v;
3195
3196
    /* Move forward until first digit or asterisk */
3197
22.4k
    while(*ptr && !ISDIGIT(*ptr) && *ptr != '*')
3198
20.8k
      ptr++;
3199
3200
    /* if it truly stopped on a digit */
3201
1.60k
    if(ISDIGIT(*ptr)) {
3202
973
      if(!curlx_str_number(&ptr, &k->offset, CURL_OFF_T_MAX) &&
3203
885
         (data->state.resume_from == k->offset))
3204
        /* we asked for a resume and we got it */
3205
230
        k->content_range = TRUE;
3206
973
    }
3207
636
    else if(*ptr == '*' && ptr[1] == '/' && k->httpcode == 416 &&
3208
0
            data->state.resume_from && data->state.httpreq == HTTPREQ_GET) {
3209
0
      curl_off_t total;
3210
0
      ptr += 2;
3211
0
      if(!curlx_str_number(&ptr, &total, CURL_OFF_T_MAX)) {
3212
0
        while(ISBLANK(*ptr))
3213
0
          ptr++;
3214
0
        if((*ptr == '\r' || *ptr == '\n' || !*ptr) &&
3215
0
           total != data->state.resume_from) {
3216
0
          failf(data, "Cannot resume: remote file size differs from offset");
3217
0
          return CURLE_RANGE_ERROR;
3218
0
        }
3219
0
      }
3220
0
    }
3221
636
    else if(k->httpcode < 300)
3222
368
      data->state.resume_from = 0; /* get everything */
3223
1.60k
  }
3224
22.9k
  return CURLE_OK;
3225
22.9k
}
3226
3227
/*
3228
 * http_header_l() parses a single response header starting with L.
3229
 */
3230
static CURLcode http_header_l(struct Curl_easy *data,
3231
                              const char *hd, size_t hdlen)
3232
5.91k
{
3233
5.91k
  struct connectdata *conn = data->conn;
3234
5.91k
  struct SingleRequest *k = &data->req;
3235
5.91k
  const char *v = (!k->http_bodyless &&
3236
1.42k
                   (data->set.timecondition || data->set.get_filetime)) ?
3237
5.91k
    HD_VAL(hd, hdlen, "Last-Modified:") : NULL;
3238
5.91k
  if(v) {
3239
555
    if(Curl_getdate_capped(v, &k->timeofdoc))
3240
555
      k->timeofdoc = 0;
3241
555
    if(data->set.get_filetime)
3242
299
      data->info.filetime = k->timeofdoc;
3243
555
    return CURLE_OK;
3244
555
  }
3245
5.35k
  if(HD_IS(hd, hdlen, "Location:") && (k->httpcode/100 >= 2)) {
3246
    /* this is the URL that the server advises us to use instead */
3247
2.63k
    char *location = Curl_copy_header_value(hd);
3248
2.63k
    if(!location)
3249
1
      return CURLE_OUT_OF_MEMORY;
3250
2.62k
    if(!*location ||
3251
1.56k
       (data->req.location && !strcmp(data->req.location, location))) {
3252
      /* ignore empty header, or exact repeat of a previous one */
3253
1.56k
      curlx_free(location);
3254
1.56k
      return CURLE_OK;
3255
1.56k
    }
3256
1.06k
    else {
3257
      /* has value and is not an exact repeat */
3258
1.06k
      if(data->req.location) {
3259
53
        failf(data, "Multiple Location headers");
3260
53
        curlx_free(location);
3261
53
        return CURLE_WEIRD_SERVER_REPLY;
3262
53
      }
3263
1.01k
      data->req.location = location;
3264
3265
1.01k
      if(data->set.http_follow_mode && (k->httpcode/100 == 3)) {
3266
641
        CURLcode result;
3267
641
        DEBUGASSERT(!data->req.newurl);
3268
641
        data->req.newurl = curlx_strdup(data->req.location); /* clone */
3269
641
        if(!data->req.newurl)
3270
0
          return CURLE_OUT_OF_MEMORY;
3271
3272
        /* some cases of POST and PUT etc needs to rewind the data
3273
           stream at this point */
3274
641
        result = http_perhapsrewind(data, conn);
3275
641
        if(result)
3276
0
          return result;
3277
3278
        /* mark the next request as a followed location: */
3279
641
        data->state.this_is_a_follow = TRUE;
3280
641
      }
3281
1.01k
    }
3282
2.62k
  }
3283
3.74k
  return CURLE_OK;
3284
5.35k
}
3285
3286
/*
3287
 * http_header_p() parses a single response header starting with P.
3288
 */
3289
static CURLcode http_header_p(struct Curl_easy *data,
3290
                              const char *hd, size_t hdlen)
3291
509
{
3292
509
  struct SingleRequest *k = &data->req;
3293
3294
509
#ifndef CURL_DISABLE_PROXY
3295
509
  const char *v = HD_VAL(hd, hdlen, "Proxy-Connection:");
3296
509
  if(v) {
3297
0
    struct connectdata *conn = data->conn;
3298
0
    if((k->httpversion == 10) && conn->http_proxy.peer &&
3299
0
       HD_IS_AND_SAYS(hd, hdlen, "Proxy-Connection:", "keep-alive")) {
3300
      /*
3301
       * When an HTTP/1.0 reply comes when using a proxy, the
3302
       * 'Proxy-Connection: keep-alive' line tells us the
3303
       * connection will be kept alive for our pleasure.
3304
       * Default action for 1.0 is to close.
3305
       */
3306
0
      connkeep(conn); /* do not close */
3307
0
      infof(data, "HTTP/1.0 proxy connection set to keep alive");
3308
0
    }
3309
0
    else if((k->httpversion == 11) && conn->http_proxy.peer &&
3310
0
            HD_IS_AND_SAYS(hd, hdlen, "Proxy-Connection:", "close")) {
3311
      /*
3312
       * We get an HTTP/1.1 response from a proxy and it says it will
3313
       * close down after this transfer.
3314
       */
3315
0
      connclose(conn);
3316
0
      infof(data, "HTTP/1.1 proxy connection set close");
3317
0
    }
3318
0
    return CURLE_OK;
3319
0
  }
3320
509
#endif
3321
509
  if((407 == k->httpcode) && HD_IS(hd, hdlen, "Proxy-authenticate:")) {
3322
0
    char *auth;
3323
0
    CURLcode result;
3324
0
#ifndef CURL_DISABLE_PROXY
3325
0
    if(!data->conn->bits.origin_is_proxy)
3326
      /* this response is not from a forward proxy, not for us */
3327
0
      return CURLE_OK;
3328
0
#endif
3329
0
    auth = Curl_copy_header_value(hd);
3330
0
    result = auth ? CURLE_OK : CURLE_OUT_OF_MEMORY;
3331
0
    if(!result) {
3332
0
      result = Curl_http_input_auth(data, TRUE, auth);
3333
0
      curlx_free(auth);
3334
0
    }
3335
0
    return result;
3336
0
  }
3337
#ifdef USE_SPNEGO
3338
  if(HD_IS(hd, hdlen, "Persistent-Auth:")) {
3339
    struct connectdata *conn = data->conn;
3340
    struct negotiatedata *negdata = Curl_auth_nego_get(conn, FALSE);
3341
    struct auth *authp = &data->state.authhost;
3342
    if(!negdata)
3343
      return CURLE_OUT_OF_MEMORY;
3344
    if(authp->picked == CURLAUTH_NEGOTIATE) {
3345
      char *persistentauth = Curl_copy_header_value(hd);
3346
      if(!persistentauth)
3347
        return CURLE_OUT_OF_MEMORY;
3348
      negdata->noauthpersist = !!checkprefix("false", persistentauth);
3349
      negdata->havenoauthpersist = TRUE;
3350
      infof(data, "Negotiate: noauthpersist -> %d, header part: %s",
3351
            negdata->noauthpersist, persistentauth);
3352
      curlx_free(persistentauth);
3353
    }
3354
  }
3355
#endif
3356
509
  return CURLE_OK;
3357
509
}
3358
3359
/*
3360
 * http_header_r() parses a single response header starting with R.
3361
 */
3362
static CURLcode http_header_r(struct Curl_easy *data,
3363
                              const char *hd, size_t hdlen)
3364
17.2k
{
3365
17.2k
  const char *v = HD_VAL(hd, hdlen, "Retry-After:");
3366
17.2k
  if(v) {
3367
    /* Retry-After = HTTP-date / delay-seconds */
3368
15.9k
    curl_off_t retry_after = 0; /* zero for unknown or "now" */
3369
15.9k
    time_t date = 0;
3370
15.9k
    curlx_str_passblanks(&v);
3371
3372
    /* try it as a date first, because a date can otherwise start with and
3373
       get treated as a number */
3374
15.9k
    if(!Curl_getdate_capped(v, &date)) {
3375
702
      time_t current = time(NULL);
3376
702
      if(date >= current)
3377
        /* convert date to number of seconds into the future */
3378
379
        retry_after = date - current;
3379
702
    }
3380
15.2k
    else
3381
      /* Try it as a decimal number, ignore errors */
3382
15.2k
      (void)curlx_str_number(&v, &retry_after, CURL_OFF_T_MAX);
3383
    /* limit to 6 hours max. this is not documented so that it can be changed
3384
       in the future if necessary. */
3385
15.9k
    if(retry_after > 21600)
3386
1.09k
      retry_after = 21600;
3387
15.9k
    data->info.retry_after = retry_after;
3388
15.9k
  }
3389
17.2k
  return CURLE_OK;
3390
17.2k
}
3391
3392
/*
3393
 * http_header_s() parses a single response header starting with S.
3394
 */
3395
static CURLcode http_header_s(struct Curl_easy *data,
3396
                              const char *hd, size_t hdlen)
3397
19.2k
{
3398
19.2k
#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_HSTS)
3399
19.2k
  const char *v;
3400
#else
3401
  (void)data;
3402
  (void)hd;
3403
  (void)hdlen;
3404
#endif
3405
3406
19.2k
#ifndef CURL_DISABLE_COOKIES
3407
  /* Ignore Set-Cookie on 1xx, same as Location. A 103 can otherwise
3408
     store a cookie the final response never sent. */
3409
19.2k
  v = (data->cookies && data->state.cookie_engine &&
3410
19.2k
       (data->req.httpcode/100 >= 2)) ?
3411
19.2k
    HD_VAL(hd, hdlen, "Set-Cookie:") : NULL;
3412
19.2k
  if(v) {
3413
    /* If there is a custom-set Host: name, use it here, or else use
3414
     * real peer hostname. */
3415
16.3k
    const char *host = data->req.cookiehost ?
3416
9.98k
      data->req.cookiehost : data->state.origin->hostname;
3417
16.3k
    const unsigned char secure_context =
3418
16.3k
      Curl_secure_context(data, host, strlen(host)) ? COOKIE_SECURE : 0;
3419
16.3k
    CURLcode result;
3420
16.3k
    Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
3421
16.3k
    result = Curl_cookie_add(data, data->cookies, v, host,
3422
16.3k
                             data->state.up.path,
3423
16.3k
                             COOKIE_HTTPHEADER | secure_context);
3424
16.3k
    Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
3425
16.3k
    return result;
3426
16.3k
  }
3427
2.92k
#endif
3428
2.92k
#ifndef CURL_DISABLE_HSTS
3429
  /* If enabled, the header is incoming and this is over HTTPS */
3430
2.92k
  v = (data->hsts &&
3431
2.69k
       (Curl_xfer_is_secure(data) ||
3432
2.69k
#ifdef DEBUGBUILD
3433
        /* allow debug builds to circumvent the HTTPS restriction */
3434
2.69k
        getenv("CURL_HSTS_HTTP")
3435
#else
3436
        0
3437
#endif
3438
2.69k
         )
3439
2.92k
    ) ? HD_VAL(hd, hdlen, "Strict-Transport-Security:") : NULL;
3440
2.92k
  if(v) {
3441
2.18k
    CURLcode result = Curl_hsts_parse(
3442
2.18k
      data->hsts, data->state.origin->hostname, v);
3443
2.18k
    if(result) {
3444
1.65k
      if(result == CURLE_OUT_OF_MEMORY)
3445
0
        return result;
3446
1.65k
      infof(data, "Illegal STS header skipped");
3447
1.65k
    }
3448
522
#ifdef DEBUGBUILD
3449
522
    else
3450
522
      infof(data, "Parsed STS header fine (%zu entries)",
3451
2.18k
            Curl_llist_count(&data->hsts->list));
3452
2.18k
#endif
3453
2.18k
  }
3454
2.92k
#endif
3455
3456
2.92k
  return CURLE_OK;
3457
2.92k
}
3458
3459
/*
3460
 * http_header_t() parses a single response header starting with T.
3461
 */
3462
static CURLcode http_header_t(struct Curl_easy *data,
3463
                              const char *hd, size_t hdlen)
3464
339
{
3465
339
  struct connectdata *conn = data->conn;
3466
339
  struct SingleRequest *k = &data->req;
3467
3468
  /* RFC 9112, ch. 6.1
3469
   * "Transfer-Encoding MAY be sent in a response to a HEAD request or
3470
   *  in a 304 (Not Modified) response (Section 15.4.5 of [HTTP]) to a
3471
   *  GET request, neither of which includes a message body, to indicate
3472
   *  that the origin server would have applied a transfer coding to the
3473
   *  message body if the request had been an unconditional GET."
3474
   *
3475
   * Read: in these cases the 'Transfer-Encoding' does not apply
3476
   * to any data following the response headers. Do not add any decoders.
3477
   */
3478
339
  const char *v = (!k->http_bodyless &&
3479
260
                   (data->state.httpreq != HTTPREQ_HEAD) &&
3480
223
                   (k->httpcode != 304)) ?
3481
339
    HD_VAL(hd, hdlen, "Transfer-Encoding:") : NULL;
3482
339
  if(v) {
3483
    /* One or more encodings. We check for chunked and/or a compression
3484
       algorithm. */
3485
132
    CURLcode result = Curl_build_unencoding_stack(data, v, TRUE);
3486
132
    if(result)
3487
55
      return result;
3488
77
    if(!k->chunk) {
3489
      /* if this is not chunked, only close can signal the end of this
3490
       * transfer as Content-Length is said not to be trusted for
3491
       * transfer-encoding! */
3492
77
      CURL_TRC_M(data, "HTTP/1.1 transfer-encoding without chunks");
3493
77
      connclose(conn);
3494
77
      k->ignore_cl = TRUE;
3495
77
    }
3496
77
    return CURLE_OK;
3497
132
  }
3498
207
  v = HD_VAL(hd, hdlen, "Trailer:");
3499
207
  if(v) {
3500
0
    data->req.resp_trailer = TRUE;
3501
0
    return CURLE_OK;
3502
0
  }
3503
207
  return CURLE_OK;
3504
207
}
3505
3506
/*
3507
 * http_header_w() parses a single response header starting with W.
3508
 */
3509
static CURLcode http_header_w(struct Curl_easy *data,
3510
                              const char *hd, size_t hdlen)
3511
468
{
3512
468
  struct SingleRequest *k = &data->req;
3513
468
  CURLcode result = CURLE_OK;
3514
3515
468
  if((401 == k->httpcode) && HD_IS(hd, hdlen, "WWW-Authenticate:")) {
3516
0
    char *auth = Curl_copy_header_value(hd);
3517
0
    if(!auth)
3518
0
      result = CURLE_OUT_OF_MEMORY;
3519
0
    else {
3520
0
      result = Curl_http_input_auth(data, FALSE, auth);
3521
0
      curlx_free(auth);
3522
0
    }
3523
0
  }
3524
468
  return result;
3525
468
}
3526
3527
/*
3528
 * http_header() parses a single response header.
3529
 */
3530
static CURLcode http_header(struct Curl_easy *data,
3531
                            const char *hd, size_t hdlen)
3532
95.5k
{
3533
95.5k
  CURLcode result = CURLE_OK;
3534
3535
95.5k
  switch(hd[0]) {
3536
2.54k
  case 'a':
3537
2.56k
  case 'A':
3538
2.56k
    result = http_header_a(data, hd, hdlen);
3539
2.56k
    break;
3540
30.1k
  case 'c':
3541
36.4k
  case 'C':
3542
36.4k
    result = http_header_c(data, hd, hdlen);
3543
36.4k
    break;
3544
5.87k
  case 'l':
3545
5.91k
  case 'L':
3546
5.91k
    result = http_header_l(data, hd, hdlen);
3547
5.91k
    break;
3548
488
  case 'p':
3549
509
  case 'P':
3550
509
    result = http_header_p(data, hd, hdlen);
3551
509
    break;
3552
17.2k
  case 'r':
3553
17.2k
  case 'R':
3554
17.2k
    result = http_header_r(data, hd, hdlen);
3555
17.2k
    break;
3556
19.2k
  case 's':
3557
19.2k
  case 'S':
3558
19.2k
    result = http_header_s(data, hd, hdlen);
3559
19.2k
    break;
3560
52
  case 't':
3561
339
  case 'T':
3562
339
    result = http_header_t(data, hd, hdlen);
3563
339
    break;
3564
332
  case 'w':
3565
468
  case 'W':
3566
468
    result = http_header_w(data, hd, hdlen);
3567
468
    break;
3568
95.5k
  }
3569
3570
95.5k
  if(!result) {
3571
95.3k
    struct connectdata *conn = data->conn;
3572
95.3k
    if(conn->scheme->protocol & CURLPROTO_RTSP)
3573
0
      result = Curl_rtsp_parseheader(data, hd);
3574
95.3k
  }
3575
95.5k
  return result;
3576
95.5k
}
3577
3578
/*
3579
 * Called after the first HTTP response line (the status line) has been
3580
 * received and parsed.
3581
 */
3582
static CURLcode http_statusline(struct Curl_easy *data,
3583
                                struct connectdata *conn)
3584
10.5k
{
3585
10.5k
  struct SingleRequest *k = &data->req;
3586
3587
10.5k
  switch(k->httpversion) {
3588
1.96k
  case 10:
3589
6.51k
  case 11:
3590
6.51k
#ifdef USE_HTTP2
3591
10.5k
  case 20:
3592
10.5k
#endif
3593
#ifdef USE_HTTP3
3594
  case 30:
3595
#endif
3596
    /* no major version switch mid-connection */
3597
10.5k
    if(k->httpversion_sent &&
3598
10.5k
       (k->httpversion / 10 != k->httpversion_sent / 10)) {
3599
10
      failf(data, "Version mismatch (from HTTP/%d to HTTP/%d)",
3600
10
            k->httpversion_sent / 10, k->httpversion / 10);
3601
10
      return CURLE_WEIRD_SERVER_REPLY;
3602
10
    }
3603
10.5k
    break;
3604
10.5k
  default:
3605
20
    failf(data, "Unsupported HTTP version (%d.%d) in response",
3606
20
          k->httpversion / 10, k->httpversion % 10);
3607
20
    return CURLE_UNSUPPORTED_PROTOCOL;
3608
10.5k
  }
3609
3610
10.5k
  data->info.httpcode = k->httpcode;
3611
10.5k
  data->info.httpversion = k->httpversion;
3612
10.5k
  conn->httpversion_seen = k->httpversion;
3613
3614
10.5k
  if(!data->state.http_neg.rcvd_min ||
3615
588
     data->state.http_neg.rcvd_min > k->httpversion)
3616
    /* store the lowest server version we encounter */
3617
9.95k
    data->state.http_neg.rcvd_min = k->httpversion;
3618
3619
  /*
3620
   * This code executes as part of processing the header. As a
3621
   * result, it is not totally clear how to interpret the
3622
   * response code yet as that depends on what other headers may
3623
   * be present. 401 and 407 may be errors, but may be OK
3624
   * depending on how authentication is working. Other codes
3625
   * are definitely errors, so give up here.
3626
   */
3627
10.5k
  if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
3628
298
     k->httpcode == 416) {
3629
    /* "Requested Range Not Satisfiable", proceed and pretend this is no
3630
       error */
3631
27
    k->ignorebody = TRUE; /* Avoid appending error msg to good data. */
3632
27
  }
3633
3634
10.5k
  if(k->httpversion == 10) {
3635
    /* Default action for HTTP/1.0 must be to close, unless
3636
       we get one of those fancy headers that tell us the
3637
       server keeps it open for us! */
3638
1.96k
    infof(data, "HTTP 1.0, assume close after body");
3639
1.96k
    connclose(conn);
3640
1.96k
  }
3641
3642
10.5k
  k->http_bodyless = k->httpcode >= 100 && k->httpcode < 200;
3643
10.5k
  switch(k->httpcode) {
3644
1.16k
  case 304:
3645
    /* (quote from RFC2616, section 10.3.5): The 304 response
3646
     * MUST NOT contain a message-body, and thus is always
3647
     * terminated by the first empty line after the header
3648
     * fields. */
3649
1.16k
    if(data->set.timecondition)
3650
13
      data->info.timecond = TRUE;
3651
1.16k
    FALLTHROUGH();
3652
3.40k
  case 204:
3653
    /* (quote from RFC2616, section 10.2.5): The server has
3654
     * fulfilled the request but does not need to return an
3655
     * entity-body ... The 204 response MUST NOT include a
3656
     * message-body, and thus is always terminated by the first
3657
     * empty line after the header fields. */
3658
3.40k
    k->size = 0;
3659
3.40k
    k->maxdownload = 0;
3660
3.40k
    k->http_bodyless = TRUE;
3661
3.40k
    break;
3662
7.12k
  default:
3663
7.12k
    break;
3664
10.5k
  }
3665
10.5k
  return CURLE_OK;
3666
10.5k
}
3667
3668
/* Content-Length must be ignored if any Transfer-Encoding is present in the
3669
   response. Refer to RFC 7230 section 3.3.3 and RFC2616 section 4.4. This is
3670
   figured out here after all headers have been received but before the final
3671
   call to the user's header callback, so that a valid content length can be
3672
   retrieved by the user in the final call. */
3673
static CURLcode http_size(struct Curl_easy *data)
3674
6.15k
{
3675
6.15k
  struct SingleRequest *k = &data->req;
3676
6.15k
  if(data->req.ignore_cl || k->chunk) {
3677
16
    k->size = k->maxdownload = -1;
3678
16
  }
3679
6.14k
  else if(k->size != -1) {
3680
1.77k
    if(data->set.max_filesize &&
3681
1.77k
       !k->ignorebody &&
3682
963
       (k->size > data->set.max_filesize)) {
3683
71
      failf(data, "Maximum file size exceeded");
3684
71
      return CURLE_FILESIZE_EXCEEDED;
3685
71
    }
3686
1.70k
    if(k->ignorebody)
3687
810
      infof(data, "setting size while ignoring");
3688
1.70k
    Curl_pgrsSetDownloadSize(data, k->size);
3689
1.70k
    k->maxdownload = k->size;
3690
1.70k
  }
3691
6.08k
  return CURLE_OK;
3692
6.15k
}
3693
3694
CURLcode Curl_verify_header(struct Curl_easy *data,
3695
                            const char *hd, size_t hdlen)
3696
95.7k
{
3697
95.7k
  struct SingleRequest *k = &data->req;
3698
95.7k
  const char *nul = memchr(hd, '\0', hdlen);
3699
95.7k
  const char *cr = memchr(hd, '\r', nul ? (size_t)(nul - hd) : hdlen);
3700
  /* CR may only precede the LF, nothing else */
3701
95.7k
  if(cr && ((size_t)(cr - hd) < hdlen - 2)) {
3702
18
    failf(data, "Carriage return found in header");
3703
18
    return CURLE_WEIRD_SERVER_REPLY;
3704
18
  }
3705
95.7k
  if(nul) {
3706
    /* this is bad, bail out */
3707
22
    failf(data, "NUL byte in header");
3708
22
    return CURLE_WEIRD_SERVER_REPLY;
3709
22
  }
3710
95.7k
  if(k->headerline < 2)
3711
    /* the first "header" is the status-line and it has no colon */
3712
10.5k
    return CURLE_OK;
3713
85.1k
  if(((hd[0] == ' ') || (hd[0] == '\t')) && k->headerline > 2)
3714
    /* line folding, cannot happen on line 2 */
3715
0
    ;
3716
85.1k
  else if(!memchr(hd, ':', hdlen)) {
3717
    /* this is bad, bail out */
3718
108
    failf(data, "Header without colon");
3719
108
    return CURLE_WEIRD_SERVER_REPLY;
3720
108
  }
3721
85.0k
  return CURLE_OK;
3722
85.1k
}
3723
3724
CURLcode Curl_bump_headersize(struct Curl_easy *data,
3725
                              size_t delta,
3726
                              bool connect_only)
3727
103k
{
3728
103k
  size_t bad = 0;
3729
103k
  unsigned int max = MAX_HTTP_RESP_HEADER_SIZE;
3730
103k
  if(delta < MAX_HTTP_RESP_HEADER_SIZE) {
3731
103k
    data->info.header_size += (unsigned int)delta;
3732
103k
    data->req.allheadercount += (unsigned int)delta;
3733
103k
    if(!connect_only)
3734
103k
      data->req.headerbytecount += (unsigned int)delta;
3735
103k
    if(data->req.allheadercount > max)
3736
0
      bad = data->req.allheadercount;
3737
103k
    else if(data->info.header_size > (max * 20)) {
3738
0
      bad = data->info.header_size;
3739
0
      max *= 20;
3740
0
    }
3741
103k
  }
3742
0
  else
3743
0
    bad = data->req.allheadercount + delta;
3744
103k
  if(bad) {
3745
0
    failf(data, "Too large response headers: %zu > %u", bad, max);
3746
0
    return CURLE_RECV_ERROR;
3747
0
  }
3748
103k
  return CURLE_OK;
3749
103k
}
3750
3751
/*
3752
 * Handle a 101 Switching Protocols response. Performs the actual protocol
3753
 * upgrade to HTTP/2 or WebSocket based on what was requested.
3754
 */
3755
static CURLcode http_on_101_upgrade(struct Curl_easy *data,
3756
                                    const char *buf, size_t blen,
3757
                                    size_t *pconsumed,
3758
                                    bool *conn_changed)
3759
132
{
3760
132
  struct connectdata *conn = data->conn;
3761
132
  struct SingleRequest *k = &data->req;
3762
3763
#if !defined(USE_NGHTTP2) && defined(CURL_DISABLE_WEBSOCKETS)
3764
  (void)buf;
3765
  (void)blen;
3766
  (void)pconsumed;
3767
#else
3768
132
  CURLcode result;
3769
132
  int upgr101_requested = k->upgr101;
3770
132
#endif
3771
3772
132
  if(k->httpversion_sent != 11) {
3773
    /* invalid for other HTTP versions */
3774
10
    failf(data, "server sent 101 response while not talking HTTP/1.1");
3775
10
    return CURLE_WEIRD_SERVER_REPLY;
3776
10
  }
3777
3778
  /* Whatever the success, upgrade was selected. */
3779
122
  k->upgr101 = UPGR101_RECEIVED;
3780
122
  conn->bits.upgrade_in_progress = FALSE;
3781
122
  *conn_changed = TRUE;
3782
3783
  /* To be fully compliant, we would check the "Upgrade:" response header to
3784
   * mention the protocol we requested. */
3785
122
#ifdef USE_NGHTTP2
3786
122
  if(upgr101_requested == UPGR101_H2) {
3787
    /* Switch to HTTP/2, where we will get more responses. blen bytes in buf
3788
     * are already h2 protocol bytes */
3789
109
    infof(data, "Received 101, Switching to HTTP/2");
3790
109
    result = Curl_http2_upgrade(data, buf, blen);
3791
109
    if(!result)
3792
91
      *pconsumed += blen;
3793
109
    return result;
3794
109
  }
3795
13
#endif
3796
13
#ifndef CURL_DISABLE_WEBSOCKETS
3797
13
  if(upgr101_requested == UPGR101_WS) {
3798
    /* Switch to WebSocket, where we now stream ws frames. blen bytes in buf
3799
     * are already ws protocol bytes */
3800
0
    infof(data, "Received 101, Switching to WebSocket");
3801
0
    result = Curl_ws_accept(data, buf, blen);
3802
0
    if(!result)
3803
0
      *pconsumed += blen; /* ws accept handled the data */
3804
0
    return result;
3805
0
  }
3806
13
#endif
3807
  /* We silently accept this as the final response. What are we switching to
3808
   * if we did not ask for an Upgrade? Maybe the application provided an
3809
   * `Upgrade: xxx` header? */
3810
13
  k->header = FALSE;
3811
13
  return CURLE_OK;
3812
13
}
3813
3814
/*
3815
 * Handle 1xx intermediate HTTP responses. Sets up state for more
3816
 * headers and processes 100-continue and 101 upgrade responses.
3817
 */
3818
static CURLcode http_on_1xx_response(struct Curl_easy *data,
3819
                                     const char *buf, size_t blen,
3820
                                     size_t *pconsumed,
3821
                                     bool *conn_changed)
3822
736
{
3823
736
  struct SingleRequest *k = &data->req;
3824
3825
  /* "A user agent MAY ignore unexpected 1xx status responses."
3826
   * By default, we expect to get more responses after this one. */
3827
736
  k->header = TRUE;
3828
736
  k->headerline = 0; /* restart the header line counter */
3829
3830
736
  switch(k->httpcode) {
3831
165
  case 100:
3832
    /* We have made an HTTP PUT or POST and this is 1.1-lingo that tells us
3833
     * that the server is OK with this and ready to receive the data. */
3834
165
    http_exp100_got100(data);
3835
165
    break;
3836
132
  case 101:
3837
132
    return http_on_101_upgrade(data, buf, blen, pconsumed, conn_changed);
3838
439
  default:
3839
    /* The server may send us other 1xx responses, like informative 103. This
3840
     * has no influence on request processing and we expect to receive a
3841
     * final response eventually. */
3842
439
    break;
3843
736
  }
3844
604
  return CURLE_OK;
3845
736
}
3846
3847
#ifdef USE_SPNEGO
3848
/*
3849
 * Check if SPNEGO authentication negotiation failed due to connection closure
3850
 * (typically on HTTP/1.0 servers).
3851
 */
3852
static void http_check_auth_closure(struct Curl_easy *data,
3853
                                    struct connectdata *conn)
3854
{
3855
  /* At this point we have some idea about the fate of the connection. If we
3856
     are closing the connection it may result auth failure. */
3857
  if(conn->bits.close &&
3858
    (((data->req.httpcode == 401) &&
3859
      (conn->http_negotiate_state == CURL_NEGO_AUTH_RECV)) ||
3860
     ((data->req.httpcode == 407) &&
3861
      (conn->proxy_negotiate_state == CURL_NEGO_AUTH_RECV)))) {
3862
    infof(data, "Connection closure while negotiating auth (HTTP 1.0?)");
3863
    data->state.authproblem = TRUE;
3864
  }
3865
  if((conn->http_negotiate_state == CURL_NEGO_AUTH_DONE) &&
3866
     (data->req.httpcode != 401)) {
3867
    conn->http_negotiate_state = CURL_NEGO_AUTH_SUCC;
3868
  }
3869
  if((conn->proxy_negotiate_state == CURL_NEGO_AUTH_DONE) &&
3870
     (data->req.httpcode != 407)) {
3871
    conn->proxy_negotiate_state = CURL_NEGO_AUTH_SUCC;
3872
  }
3873
}
3874
#else
3875
#define http_check_auth_closure(x, y) /* empty */
3876
#endif
3877
3878
/*
3879
 * Handle an error response (>= 300) received while still sending the
3880
 * request body. Deals with 417 Expectation Failed retries, keep-sending
3881
 * on error, and aborting the send.
3882
 */
3883
static CURLcode http_handle_send_error(struct Curl_easy *data)
3884
1.92k
{
3885
1.92k
  struct connectdata *conn = data->conn;
3886
1.92k
  struct SingleRequest *k = &data->req;
3887
1.92k
  CURLcode result = CURLE_OK;
3888
3889
1.92k
  if(!data->req.authneg && !conn->bits.close &&
3890
1.71k
     !Curl_creader_will_rewind(data)) {
3891
    /*
3892
     * General treatment of errors when about to send data.
3893
     * Including: "417 Expectation Failed", while waiting for
3894
     * 100-continue.
3895
     *
3896
     * The check for close above is done because if something
3897
     * else has already deemed the connection to get closed then
3898
     * something else should have considered the big picture and
3899
     * we avoid this check.
3900
     */
3901
3902
1.52k
    switch(data->state.httpreq) {
3903
11
    case HTTPREQ_PUT:
3904
35
    case HTTPREQ_POST:
3905
35
    case HTTPREQ_POST_FORM:
3906
266
    case HTTPREQ_POST_MIME:
3907
      /* We got an error response. If this happened before the
3908
       * whole request body has been sent we stop sending and
3909
       * mark the connection for closure after we have read the
3910
       * entire response. */
3911
266
      if(!Curl_req_done_sending(data)) {
3912
169
        if((k->httpcode == 417) && http_exp100_is_selected(data)) {
3913
          /* 417 Expectation Failed - try again without the
3914
             Expect header */
3915
0
          if(!k->writebytecount && http_exp100_is_waiting(data)) {
3916
0
            infof(data, "Got HTTP failure 417 while waiting for a 100");
3917
0
          }
3918
0
          else {
3919
0
            infof(data, "Got HTTP failure 417 while sending data");
3920
0
            streamclose(conn);
3921
0
            result = http_perhapsrewind(data, conn);
3922
0
            if(result)
3923
0
              return result;
3924
0
          }
3925
0
          data->state.disableexpect = TRUE;
3926
0
          Curl_req_abort_sending(data);
3927
0
          DEBUGASSERT(!data->req.newurl);
3928
0
          data->req.newurl = Curl_bufref_dup(&data->state.url);
3929
0
          if(!data->req.newurl)
3930
0
            return CURLE_OUT_OF_MEMORY;
3931
0
        }
3932
169
        else if(data->set.http_keep_sending_on_error) {
3933
57
          infof(data, "HTTP error before end of send, keep sending");
3934
57
          http_exp100_send_anyway(data);
3935
57
        }
3936
112
        else {
3937
112
          infof(data, "HTTP error before end of send, stop sending");
3938
112
          streamclose(conn);
3939
112
          result = Curl_req_abort_sending(data);
3940
112
          if(result)
3941
0
            return result;
3942
112
        }
3943
169
      }
3944
266
      break;
3945
3946
1.25k
    default: /* default label present to avoid compiler warnings */
3947
1.25k
      break;
3948
1.52k
    }
3949
1.52k
  }
3950
3951
1.92k
  if(Curl_creader_will_rewind(data) && !Curl_req_done_sending(data)) {
3952
    /* We rewind before next send, continue sending now */
3953
129
    infof(data, "Keep sending data to get tossed away");
3954
129
    CURL_REQ_SET_SEND(data);
3955
129
  }
3956
1.92k
  return result;
3957
1.92k
}
3958
3959
static CURLcode http_on_response(struct Curl_easy *data,
3960
                                 const char *last_hd, size_t last_hd_len,
3961
                                 const char *buf, size_t blen,
3962
                                 size_t *pconsumed)
3963
7.15k
{
3964
7.15k
  struct connectdata *conn = data->conn;
3965
7.15k
  CURLcode result = CURLE_OK;
3966
7.15k
  struct SingleRequest *k = &data->req;
3967
7.15k
  bool conn_changed = FALSE;
3968
3969
7.15k
  (void)buf; /* not used without HTTP2 enabled */
3970
7.15k
  *pconsumed = 0;
3971
3972
7.15k
  if(k->upgr101 == UPGR101_RECEIVED) {
3973
    /* supposedly upgraded to http2 now */
3974
11
    if(data->req.httpversion != 20)
3975
0
      infof(data, "Lying server, not serving HTTP/2");
3976
11
  }
3977
3978
7.15k
  if(k->httpcode < 200 && last_hd) {
3979
    /* Intermediate responses might trigger processing of more responses,
3980
     * write the last header to the client before proceeding. */
3981
748
    result = http_write_header(data, last_hd, last_hd_len);
3982
748
    last_hd = NULL; /* handled it */
3983
748
    if(result)
3984
0
      goto out;
3985
748
  }
3986
3987
7.15k
  if(k->httpcode < 100) {
3988
12
    failf(data, "Unsupported response code in HTTP response");
3989
12
    result = CURLE_UNSUPPORTED_PROTOCOL;
3990
12
    goto out;
3991
12
  }
3992
7.14k
  else if(k->httpcode < 200) {
3993
736
    result = http_on_1xx_response(data, buf, blen, pconsumed, &conn_changed);
3994
736
    goto out;
3995
736
  }
3996
3997
  /* k->httpcode >= 200, final response */
3998
6.41k
  k->header = FALSE;
3999
6.41k
  if(conn->bits.upgrade_in_progress) {
4000
    /* Asked for protocol upgrade, but it was not selected */
4001
39
    conn->bits.upgrade_in_progress = FALSE;
4002
39
    conn_changed = TRUE;
4003
39
  }
4004
4005
6.41k
  if((k->size == -1) && !k->chunk && !conn->bits.close &&
4006
3.40k
     (k->httpversion == 11) &&
4007
2.64k
     !(conn->scheme->protocol & CURLPROTO_RTSP) &&
4008
2.64k
     data->state.httpreq != HTTPREQ_HEAD) {
4009
    /* On HTTP 1.1, when connection is not to get closed, but no
4010
       Content-Length nor Transfer-Encoding chunked have been received,
4011
       according to RFC2616 section 4.4 point 5, we assume that the server
4012
       will close the connection to signal the end of the document. */
4013
2.61k
    infof(data, "no chunk, no close, no size. Assume close to signal end");
4014
2.61k
    streamclose(conn);
4015
2.61k
  }
4016
4017
6.41k
  http_check_auth_closure(data, conn);
4018
4019
6.41k
#ifndef CURL_DISABLE_WEBSOCKETS
4020
  /* All >=200 HTTP status codes are errors when wanting ws */
4021
6.41k
  if(data->req.upgr101 == UPGR101_WS) {
4022
0
    failf(data, "Refused WebSocket upgrade: %d", k->httpcode);
4023
0
    result = CURLE_HTTP_RETURNED_ERROR;
4024
0
    goto out;
4025
0
  }
4026
6.41k
#endif
4027
4028
  /* Check if this response means the transfer errored. */
4029
6.41k
  if(http_should_fail(data, data->req.httpcode)) {
4030
108
    failf(data, "The requested URL returned error: %d",
4031
108
          k->httpcode);
4032
108
    result = CURLE_HTTP_RETURNED_ERROR;
4033
108
    goto out;
4034
108
  }
4035
4036
  /* Curl_http_auth_act() checks what authentication methods that are
4037
   * available and decides which one (if any) to use. It will set 'newurl' if
4038
   * an auth method was picked. */
4039
6.30k
  result = Curl_http_auth_act(data);
4040
6.30k
  if(result)
4041
19
    goto out;
4042
4043
6.28k
  if(k->httpcode >= 300) {
4044
1.92k
    result = http_handle_send_error(data);
4045
1.92k
    if(result)
4046
0
      goto out;
4047
1.92k
  }
4048
4049
  /* final response without error, prepare to receive the body */
4050
6.28k
  result = http_firstwrite(data);
4051
6.28k
  if(result)
4052
128
    goto out;
4053
4054
  /* This is the last response that we get for the current request. Check on
4055
   * the body size and determine if the response is complete. */
4056
6.15k
  result = http_size(data);
4057
6.15k
  if(result)
4058
71
    goto out;
4059
4060
6.08k
#ifndef CURL_DISABLE_RTSP
4061
  /* An RTSP response to any method may carry a body, which is announced
4062
     with Content-Length (RFC 2326 section 12.14). Read it instead of
4063
     treating it as excess data, unless the application asked for no body
4064
     with CURLOPT_NOBODY. */
4065
6.08k
  if((conn->scheme->protocol & CURLPROTO_RTSP) && data->req.no_body &&
4066
0
     !data->set.opt_no_body && (k->size > 0))
4067
0
    data->req.no_body = FALSE;
4068
6.08k
#endif
4069
4070
  /* If we requested a "no body", this is a good time to get
4071
   * out and return home.
4072
   */
4073
6.08k
  if(data->req.no_body)
4074
140
    k->download_done = TRUE;
4075
4076
  /* If max download size is *zero* (nothing) we already have nothing and can
4077
     safely return ok now! For HTTP/2, we would like to call
4078
     http2_handle_stream_close to properly close a stream. In order to do
4079
     this, we keep reading until we close the stream. */
4080
6.08k
  if((k->maxdownload == 0) && (k->httpversion_sent < 20))
4081
559
    k->download_done = TRUE;
4082
4083
7.15k
out:
4084
7.15k
  if(last_hd)
4085
    /* if not written yet, write it now */
4086
6.41k
    result = Curl_1st_fatal(result,
4087
6.41k
                            http_write_header(data, last_hd, last_hd_len));
4088
7.15k
  if(conn_changed)
4089
    /* poke the multi handle to allow pending pipewait to retry */
4090
161
    Curl_multi_connchanged(data->multi);
4091
7.15k
  return result;
4092
6.08k
}
4093
4094
static CURLcode http_rw_hd(struct Curl_easy *data,
4095
                           const char *hd, size_t hdlen,
4096
                           const char *buf_remain, size_t blen,
4097
                           size_t *pconsumed)
4098
103k
{
4099
103k
  CURLcode result = CURLE_OK;
4100
103k
  struct SingleRequest *k = &data->req;
4101
103k
  int writetype;
4102
103k
  DEBUGASSERT(!hd[hdlen]); /* null-terminated */
4103
4104
103k
  *pconsumed = 0;
4105
103k
  if((0x0a == *hd) || (0x0d == *hd)) {
4106
    /* Empty header line means end of headers! */
4107
7.22k
    struct dynbuf last_header;
4108
7.22k
    size_t consumed;
4109
4110
7.22k
    if((hdlen != 1 || hd[0] != '\n') &&
4111
5.08k
       (hdlen != 2 || hd[0] != '\r' || hd[1] != '\n')) {
4112
61
      failf(data, "Invalid header termination");
4113
61
      return CURLE_WEIRD_SERVER_REPLY;
4114
61
    }
4115
4116
7.16k
    curlx_dyn_init(&last_header, hdlen + 1);
4117
7.16k
    result = curlx_dyn_addn(&last_header, hd, hdlen);
4118
7.16k
    if(result)
4119
7
      return result;
4120
4121
    /* analyze the response to find out what to do. */
4122
    /* Caveat: we clear anything in the header brigade, because a
4123
     * response might switch HTTP version which may call use recursively.
4124
     * Not nice, but that is currently the way of things. */
4125
7.15k
    curlx_dyn_reset(&data->state.headerb);
4126
7.15k
    result = http_on_response(data, curlx_dyn_ptr(&last_header),
4127
7.15k
                              curlx_dyn_len(&last_header),
4128
7.15k
                              buf_remain, blen, &consumed);
4129
7.15k
    *pconsumed += consumed;
4130
7.15k
    curlx_dyn_free(&last_header);
4131
7.15k
    return result;
4132
7.16k
  }
4133
4134
  /*
4135
   * Checks for special headers coming up.
4136
   */
4137
4138
96.1k
  writetype = CLIENTWRITE_HEADER;
4139
96.1k
  if(!k->headerline++) {
4140
    /* This is the first header, it MUST be the error code line
4141
       or else we consider this to be the body right away! */
4142
10.8k
    bool fine_statusline = FALSE;
4143
4144
10.8k
    k->httpversion = 0; /* Do not know yet */
4145
10.8k
    if(data->conn->scheme->protocol & PROTO_FAMILY_HTTP) {
4146
      /*
4147
       * https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2
4148
       *
4149
       * The response code is always a three-digit number in HTTP as the spec
4150
       * says. We allow any three-digit number here, but we cannot make
4151
       * guarantees on future behaviors since it is not within the protocol.
4152
       */
4153
10.8k
      const char *p = hd;
4154
4155
10.8k
      curlx_str_passblanks(&p);
4156
10.8k
      if(!strncmp(p, "HTTP/", 5)) {
4157
10.5k
        p += 5;
4158
10.5k
        switch(*p) {
4159
5.18k
        case '1':
4160
5.18k
          p++;
4161
5.18k
          if((p[0] == '.') && (p[1] == '0' || p[1] == '1')) {
4162
5.15k
            if(ISBLANK(p[2])) {
4163
5.13k
              k->httpversion = (unsigned char)(10 + (p[1] - '0'));
4164
5.13k
              p += 3;
4165
5.13k
              if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
4166
5.07k
                k->httpcode = ((p[0] - '0') * 100) + ((p[1] - '0') * 10) +
4167
5.07k
                  (p[2] - '0');
4168
                /* RFC 9112 requires a single space following the status code,
4169
                   but the browsers do not so let's not insist */
4170
5.07k
                fine_statusline = TRUE;
4171
5.07k
              }
4172
5.13k
            }
4173
5.15k
          }
4174
5.18k
          if(!fine_statusline) {
4175
107
            failf(data, "Unsupported HTTP/1 subversion in response");
4176
107
            return CURLE_UNSUPPORTED_PROTOCOL;
4177
107
          }
4178
5.07k
          break;
4179
5.07k
        case '2':
4180
5.38k
        case '3':
4181
5.38k
          if(!ISBLANK(p[1]))
4182
1.07k
            break;
4183
4.30k
          k->httpversion = (unsigned char)((*p - '0') * 10);
4184
4.30k
          p += 2;
4185
4.30k
          if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
4186
4.06k
            k->httpcode = ((p[0] - '0') * 100) + ((p[1] - '0') * 10) +
4187
4.06k
              (p[2] - '0');
4188
4.06k
            p += 3;
4189
4.06k
            if(!ISBLANK(*p))
4190
18
              break;
4191
4.04k
            fine_statusline = TRUE;
4192
4.04k
          }
4193
4.28k
          break;
4194
4.28k
        default: /* unsupported */
4195
18
          failf(data, "Unsupported HTTP version in response");
4196
18
          return CURLE_UNSUPPORTED_PROTOCOL;
4197
10.5k
        }
4198
10.5k
      }
4199
4200
10.7k
      if(!fine_statusline) {
4201
        /* If user has set option HTTP200ALIASES,
4202
           compare header line against list of aliases */
4203
1.64k
        statusline check = checkhttpprefix(data, hd, hdlen);
4204
1.64k
        if(check == STATUS_DONE) {
4205
1.43k
          fine_statusline = TRUE;
4206
1.43k
          k->httpcode = 200;
4207
1.43k
          k->httpversion = 10;
4208
1.43k
        }
4209
1.64k
      }
4210
10.7k
    }
4211
0
    else if(data->conn->scheme->protocol & CURLPROTO_RTSP) {
4212
0
      const char *p = hd;
4213
0
      struct Curl_str ver;
4214
0
      curl_off_t status;
4215
      /* we set the max string a little excessive to forgive some leading
4216
         spaces */
4217
0
      if(!curlx_str_until(&p, &ver, 32, ' ') &&
4218
0
         !curlx_str_single(&p, ' ') &&
4219
0
         !curlx_str_number(&p, &status, 999)) {
4220
0
        curlx_str_trimblanks(&ver);
4221
0
        if(curlx_str_cmp(&ver, "RTSP/1.0")) {
4222
0
          k->httpcode = (int)status;
4223
0
          fine_statusline = TRUE;
4224
0
          k->httpversion = 11; /* RTSP acts like HTTP 1.1 */
4225
0
        }
4226
0
      }
4227
0
      if(!fine_statusline)
4228
0
        return CURLE_WEIRD_SERVER_REPLY;
4229
0
    }
4230
4231
10.7k
    if(fine_statusline) {
4232
10.5k
      result = http_statusline(data, data->conn);
4233
10.5k
      if(result)
4234
30
        return result;
4235
10.5k
      writetype |= CLIENTWRITE_STATUS;
4236
10.5k
    }
4237
208
    else {
4238
208
      k->header = FALSE;   /* this is not a header line */
4239
208
      return CURLE_WEIRD_SERVER_REPLY;
4240
208
    }
4241
10.7k
  }
4242
4243
95.7k
  result = Curl_verify_header(data, hd, hdlen);
4244
95.7k
  if(result)
4245
148
    return result;
4246
4247
95.5k
  result = http_header(data, hd, hdlen);
4248
95.5k
  if(result)
4249
252
    return result;
4250
4251
  /*
4252
   * Taken in one (more) header. Write it to the client.
4253
   */
4254
95.3k
  Curl_debug(data, CURLINFO_HEADER_IN, hd, hdlen);
4255
4256
95.3k
  if(k->httpcode / 100 == 1)
4257
1.26k
    writetype |= CLIENTWRITE_1XX;
4258
95.3k
  result = Curl_client_write(data, writetype, hd, hdlen);
4259
95.3k
  if(result)
4260
31
    return result;
4261
4262
95.3k
  result = Curl_bump_headersize(data, hdlen, FALSE);
4263
95.3k
  if(result)
4264
0
    return result;
4265
4266
95.3k
  return CURLE_OK;
4267
95.3k
}
4268
4269
/* remove trailing CRLF then all trailing whitespace */
4270
void Curl_http_to_fold(struct dynbuf *bf)
4271
5.26k
{
4272
5.26k
  size_t len = curlx_dyn_len(bf);
4273
5.26k
  const char *hd = curlx_dyn_ptr(bf);
4274
5.26k
  if(len && (hd[len - 1] == '\n'))
4275
5.26k
    len--;
4276
5.26k
  if(len && (hd[len - 1] == '\r'))
4277
2.22k
    len--;
4278
7.34k
  while(len && ISBLANK(hd[len - 1])) /* strip off trailing whitespace */
4279
2.07k
    len--;
4280
5.26k
  curlx_dyn_setlen(bf, len);
4281
5.26k
}
4282
4283
static void unfold_header(struct Curl_easy *data)
4284
5.26k
{
4285
5.26k
  Curl_http_to_fold(&data->state.headerb);
4286
5.26k
  data->state.leading_unfold = TRUE;
4287
5.26k
}
4288
4289
/*
4290
 * Read any HTTP header lines from the server and pass them to the client app.
4291
 */
4292
static CURLcode http_parse_headers(struct Curl_easy *data,
4293
                                   const char *buf, size_t blen,
4294
                                   size_t *pconsumed)
4295
321k
{
4296
321k
  struct connectdata *conn = data->conn;
4297
321k
  CURLcode result = CURLE_OK;
4298
321k
  struct SingleRequest *k = &data->req;
4299
321k
  const char *end_ptr;
4300
321k
  bool leftover_body = FALSE;
4301
4302
  /* we have bytes for the next header, make sure it is not a folded header
4303
     before passing it on */
4304
321k
  if(data->state.maybe_folded && blen) {
4305
3.18k
    if(ISBLANK(buf[0])) {
4306
      /* folded, remove the trailing newlines and append the next header */
4307
2.54k
      unfold_header(data);
4308
2.54k
    }
4309
640
    else {
4310
      /* the header data we hold is a complete header, pass it on */
4311
640
      size_t ignore_this;
4312
640
      result = http_rw_hd(data, curlx_dyn_ptr(&data->state.headerb),
4313
640
                          curlx_dyn_len(&data->state.headerb),
4314
640
                          NULL, 0, &ignore_this);
4315
640
      curlx_dyn_reset(&data->state.headerb);
4316
640
      if(result)
4317
23
        return result;
4318
640
    }
4319
3.16k
    data->state.maybe_folded = FALSE;
4320
3.16k
  }
4321
4322
  /* header line within buffer loop */
4323
321k
  *pconsumed = 0;
4324
342k
  while(blen && k->header) {
4325
323k
    size_t consumed;
4326
323k
    size_t hlen;
4327
323k
    const char *hd;
4328
323k
    size_t unfold_len = 0;
4329
4330
323k
    if(data->state.leading_unfold) {
4331
      /* immediately after an unfold, keep only a single whitespace */
4332
37.9k
      while(blen && ISBLANK(buf[0])) {
4333
23.0k
        buf++;
4334
23.0k
        blen--;
4335
23.0k
        unfold_len++;
4336
23.0k
      }
4337
14.9k
      if(blen) {
4338
        /* insert a single space */
4339
5.24k
        result = curlx_dyn_addn(&data->state.headerb, " ", 1);
4340
5.24k
        if(result)
4341
0
          return result;
4342
5.24k
        data->state.leading_unfold = FALSE; /* done now */
4343
5.24k
      }
4344
14.9k
    }
4345
4346
323k
    end_ptr = memchr(buf, '\n', blen);
4347
323k
    if(!end_ptr) {
4348
      /* Not a complete header line within buffer, append the data to
4349
         the end of the headerbuff. */
4350
297k
      result = curlx_dyn_addn(&data->state.headerb, buf, blen);
4351
297k
      if(result)
4352
0
        return result;
4353
297k
      *pconsumed += blen + unfold_len;
4354
4355
297k
      if(!k->headerline) {
4356
        /* check if this looks like a protocol header */
4357
231k
        statusline st =
4358
231k
          checkprotoprefix(data, conn,
4359
231k
                           curlx_dyn_ptr(&data->state.headerb),
4360
231k
                           curlx_dyn_len(&data->state.headerb));
4361
4362
231k
        if(st == STATUS_BAD) {
4363
          /* this is not the beginning of a protocol first header line.
4364
           * Cannot be 0.9 if version was detected or connection was reused. */
4365
1.26k
          k->header = FALSE;
4366
1.26k
          streamclose(conn);
4367
1.26k
          if((k->httpversion >= 10) || conn->bits.reuse) {
4368
61
            failf(data, "Invalid status line");
4369
61
            return CURLE_WEIRD_SERVER_REPLY;
4370
61
          }
4371
1.20k
          if(!data->state.http_neg.accept_09) {
4372
999
            failf(data, "Received HTTP/0.9 when not allowed");
4373
999
            return CURLE_UNSUPPORTED_PROTOCOL;
4374
999
          }
4375
201
          leftover_body = TRUE;
4376
201
          goto out;
4377
1.20k
        }
4378
231k
      }
4379
296k
      goto out; /* read more and try again */
4380
297k
    }
4381
4382
    /* the size of the remaining header line */
4383
25.7k
    consumed = (end_ptr - buf) + 1;
4384
4385
25.7k
    result = curlx_dyn_addn(&data->state.headerb, buf, consumed);
4386
25.7k
    if(result)
4387
3
      return result;
4388
25.7k
    blen -= consumed;
4389
25.7k
    buf += consumed;
4390
25.7k
    *pconsumed += consumed + unfold_len;
4391
4392
    /****
4393
     * We now have a FULL header line in 'headerb'.
4394
     *****/
4395
4396
25.7k
    hlen = curlx_dyn_len(&data->state.headerb);
4397
25.7k
    hd = curlx_dyn_ptr(&data->state.headerb);
4398
4399
25.7k
    if(!k->headerline) {
4400
      /* the first read "header", the status line */
4401
6.84k
      statusline st = checkprotoprefix(data, conn, hd, hlen);
4402
6.84k
      if(st == STATUS_BAD) {
4403
180
        streamclose(conn);
4404
        /* this is not the beginning of a protocol first header line.
4405
         * Cannot be 0.9 if version was detected or connection was reused. */
4406
180
        if((k->httpversion >= 10) || conn->bits.reuse) {
4407
19
          failf(data, "Invalid status line");
4408
19
          return CURLE_WEIRD_SERVER_REPLY;
4409
19
        }
4410
161
        if(!data->state.http_neg.accept_09) {
4411
146
          failf(data, "Received HTTP/0.9 when not allowed");
4412
146
          return CURLE_UNSUPPORTED_PROTOCOL;
4413
146
        }
4414
15
        k->header = FALSE;
4415
15
        leftover_body = TRUE;
4416
15
        goto out;
4417
161
      }
4418
6.84k
    }
4419
18.9k
    else {
4420
18.9k
      if(hlen && !ISNEWLINE(hd[0])) {
4421
        /* this is NOT the header separator */
4422
4423
        /* if we have bytes for the next header, check for folding */
4424
13.4k
        if(blen && ISBLANK(buf[0])) {
4425
          /* remove the trailing CRLF and append the next header */
4426
2.72k
          unfold_header(data);
4427
2.72k
          continue;
4428
2.72k
        }
4429
10.7k
        else if(!blen) {
4430
          /* this might be a folded header so deal with it in next invoke */
4431
3.29k
          data->state.maybe_folded = TRUE;
4432
3.29k
          break;
4433
3.29k
        }
4434
13.4k
      }
4435
18.9k
    }
4436
4437
19.5k
    result = http_rw_hd(data, hd, hlen, buf, blen, &consumed);
4438
    /* We are done with this line. We reset because response
4439
     * processing might switch to HTTP/2 and that might call us
4440
     * directly again. */
4441
19.5k
    curlx_dyn_reset(&data->state.headerb);
4442
19.5k
    if(consumed) {
4443
43
      blen -= consumed;
4444
43
      buf += consumed;
4445
43
      *pconsumed += consumed;
4446
43
    }
4447
19.5k
    if(result)
4448
811
      return result;
4449
19.5k
  }
4450
4451
  /* We might have reached the end of the header part here, but
4452
     there might be a non-header part left in the end of the read
4453
     buffer. */
4454
319k
out:
4455
319k
  if(!k->header && !leftover_body) {
4456
4.41k
    curlx_dyn_free(&data->state.headerb);
4457
4.41k
  }
4458
319k
  return CURLE_OK;
4459
321k
}
4460
4461
CURLcode Curl_http_write_resp_hd(struct Curl_easy *data,
4462
                                 const char *hd, size_t hdlen,
4463
                                 bool is_eos)
4464
83.0k
{
4465
83.0k
  CURLcode result;
4466
83.0k
  size_t consumed;
4467
83.0k
  char tmp = 0;
4468
83.0k
  DEBUGASSERT(!hd[hdlen]); /* null-terminated */
4469
4470
83.0k
  result = http_rw_hd(data, hd, hdlen, &tmp, 0, &consumed);
4471
83.0k
  if(!result && is_eos) {
4472
0
    result = Curl_client_write(data, (CLIENTWRITE_BODY | CLIENTWRITE_EOS),
4473
0
                               &tmp, 0);
4474
0
  }
4475
83.0k
  return result;
4476
83.0k
}
4477
4478
/*
4479
 * HTTP protocol `write_resp` implementation. Parse headers
4480
 * when not done yet and otherwise return without consuming data.
4481
 */
4482
CURLcode Curl_http_write_resp_hds(struct Curl_easy *data,
4483
                                  const char *buf, size_t blen,
4484
                                  size_t *pconsumed)
4485
333k
{
4486
333k
  if(!data->req.header) {
4487
12.4k
    *pconsumed = 0;
4488
12.4k
    return CURLE_OK;
4489
12.4k
  }
4490
321k
  else {
4491
321k
    CURLcode result;
4492
4493
321k
    result = http_parse_headers(data, buf, blen, pconsumed);
4494
321k
    if(!result && !data->req.header) {
4495
4.62k
      if(!data->req.no_body && curlx_dyn_len(&data->state.headerb)) {
4496
        /* leftover from parsing something that turned out not
4497
         * to be a header, only happens if we allow for
4498
         * HTTP/0.9 like responses */
4499
186
        result = Curl_client_write(data, CLIENTWRITE_BODY,
4500
186
                                   curlx_dyn_ptr(&data->state.headerb),
4501
186
                                   curlx_dyn_len(&data->state.headerb));
4502
186
      }
4503
4.62k
      curlx_dyn_free(&data->state.headerb);
4504
4.62k
    }
4505
321k
    return result;
4506
321k
  }
4507
333k
}
4508
4509
CURLcode Curl_http_write_resp(struct Curl_easy *data,
4510
                              const char *buf, size_t blen,
4511
                              bool is_eos)
4512
333k
{
4513
333k
  CURLcode result;
4514
333k
  size_t consumed;
4515
333k
  int flags;
4516
4517
333k
  result = Curl_http_write_resp_hds(data, buf, blen, &consumed);
4518
333k
  if(result || data->req.done)
4519
2.13k
    goto out;
4520
4521
331k
  DEBUGASSERT(consumed <= blen);
4522
331k
  blen -= consumed;
4523
331k
  buf += consumed;
4524
  /* either all was consumed in header parsing, or we have data left
4525
   * and are done with headers, e.g. it is BODY data */
4526
331k
  DEBUGASSERT(!blen || !data->req.header);
4527
331k
  if(!data->req.header && (blen || is_eos)) {
4528
    /* BODY data after header been parsed, write and consume */
4529
15.8k
    flags = CLIENTWRITE_BODY;
4530
15.8k
    if(is_eos)
4531
3.47k
      flags |= CLIENTWRITE_EOS;
4532
15.8k
    result = Curl_client_write(data, flags, buf, blen);
4533
15.8k
  }
4534
333k
out:
4535
333k
  return result;
4536
331k
}
4537
4538
/* Decode HTTP status code string. */
4539
CURLcode Curl_http_decode_status(int32_t *pstatus, const char *s, size_t len)
4540
4.02k
{
4541
4.02k
  CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
4542
4.02k
  int32_t status = 0;
4543
4.02k
  int i;
4544
4545
4.02k
  if(len != 3)
4546
0
    goto out;
4547
4548
16.0k
  for(i = 0; i < 3; ++i) {
4549
12.0k
    char c = s[i];
4550
4551
12.0k
    if(c < '0' || c > '9')
4552
0
      goto out;
4553
4554
12.0k
    status *= 10;
4555
12.0k
    status += c - '0';
4556
12.0k
  }
4557
4.02k
  result = CURLE_OK;
4558
4.02k
out:
4559
4.02k
  *pstatus = result ? -1 : status;
4560
4.02k
  return result;
4561
4.02k
}
4562
4563
CURLcode Curl_http_req_make(struct httpreq **preq,
4564
                            const char *method, size_t m_len,
4565
                            const char *scheme, size_t s_len,
4566
                            const char *authority, size_t a_len,
4567
                            const char *path, size_t p_len)
4568
34.5k
{
4569
34.5k
  struct httpreq *req;
4570
34.5k
  CURLcode result = CURLE_OUT_OF_MEMORY;
4571
4572
34.5k
  DEBUGASSERT(method && m_len);
4573
4574
34.5k
  req = curlx_calloc(1, sizeof(*req) + m_len);
4575
34.5k
  if(!req)
4576
25
    goto out;
4577
#if defined(__GNUC__) && __GNUC__ >= 13
4578
#pragma GCC diagnostic push
4579
/* error: 'memcpy' offset [137, 142] from the object at 'req' is out of
4580
   the bounds of referenced subobject 'method' with type 'char[1]' at
4581
   offset 136 */
4582
#pragma GCC diagnostic ignored "-Warray-bounds"
4583
#endif
4584
34.5k
  memcpy(req->method, method, m_len);
4585
#if defined(__GNUC__) && __GNUC__ >= 13
4586
#pragma GCC diagnostic pop
4587
#endif
4588
34.5k
  if(scheme) {
4589
0
    req->scheme = curlx_memdup0(scheme, s_len);
4590
0
    if(!req->scheme)
4591
0
      goto out;
4592
0
  }
4593
34.5k
  if(authority) {
4594
86
    req->authority = curlx_memdup0(authority, a_len);
4595
86
    if(!req->authority)
4596
0
      goto out;
4597
86
  }
4598
34.5k
  if(path) {
4599
34.4k
    req->path = curlx_memdup0(path, p_len);
4600
34.4k
    if(!req->path)
4601
8
      goto out;
4602
34.4k
  }
4603
34.5k
  Curl_dynhds_init(&req->headers, 0, DYN_HTTP_REQUEST);
4604
34.5k
  Curl_dynhds_init(&req->trailers, 0, DYN_HTTP_REQUEST);
4605
34.5k
  result = CURLE_OK;
4606
4607
34.5k
out:
4608
34.5k
  if(result && req)
4609
8
    Curl_http_req_free(req);
4610
34.5k
  *preq = result ? NULL : req;
4611
34.5k
  return result;
4612
34.5k
}
4613
4614
static CURLcode req_assign_url_authority(struct httpreq *req, CURLU *url)
4615
1.79k
{
4616
1.79k
  char *host, *port;
4617
1.79k
  struct dynbuf buf;
4618
1.79k
  CURLUcode uc;
4619
1.79k
  CURLcode result = CURLE_URL_MALFORMAT;
4620
4621
1.79k
  host = port = NULL;
4622
1.79k
  curlx_dyn_init(&buf, DYN_HTTP_REQUEST);
4623
4624
1.79k
  uc = curl_url_get(url, CURLUPART_HOST, &host, 0);
4625
1.79k
  if(uc && uc != CURLUE_NO_HOST)
4626
2
    goto out;
4627
1.79k
  if(!host) {
4628
134
    req->authority = NULL;
4629
134
    result = CURLE_OK;
4630
134
    goto out;
4631
134
  }
4632
4633
1.65k
  uc = curl_url_get(url, CURLUPART_PORT, &port, CURLU_NO_DEFAULT_PORT);
4634
1.65k
  if(uc && uc != CURLUE_NO_PORT)
4635
0
    goto out;
4636
4637
1.65k
  result = curlx_dyn_add(&buf, host);
4638
1.65k
  if(result)
4639
3
    goto out;
4640
1.65k
  if(port) {
4641
215
    result = curlx_dyn_addf(&buf, ":%s", port);
4642
215
    if(result)
4643
0
      goto out;
4644
215
  }
4645
1.65k
  req->authority = curlx_dyn_ptr(&buf);
4646
1.79k
out:
4647
1.79k
  curlx_free(host);
4648
1.79k
  curlx_free(port);
4649
1.79k
  if(result)
4650
5
    curlx_dyn_free(&buf);
4651
1.79k
  return result;
4652
1.65k
}
4653
4654
static CURLcode req_assign_url_path(struct httpreq *req, CURLU *url)
4655
1.79k
{
4656
1.79k
  char *path, *query;
4657
1.79k
  struct dynbuf buf;
4658
1.79k
  CURLUcode uc;
4659
1.79k
  CURLcode result = CURLE_URL_MALFORMAT;
4660
4661
1.79k
  path = query = NULL;
4662
1.79k
  curlx_dyn_init(&buf, DYN_HTTP_REQUEST);
4663
4664
1.79k
  uc = curl_url_get(url, CURLUPART_PATH, &path, 0);
4665
1.79k
  if(uc)
4666
3
    goto out;
4667
1.78k
  uc = curl_url_get(url, CURLUPART_QUERY, &query, 0);
4668
1.78k
  if(uc && uc != CURLUE_NO_QUERY)
4669
1
    goto out;
4670
4671
1.78k
  if(!query) {
4672
1.70k
    req->path = path;
4673
1.70k
    path = NULL;
4674
1.70k
  }
4675
86
  else {
4676
86
    result = curlx_dyn_add(&buf, path);
4677
86
    if(!result)
4678
85
      result = curlx_dyn_addf(&buf, "?%s", query);
4679
86
    if(result)
4680
1
      goto out;
4681
85
    req->path = curlx_dyn_ptr(&buf);
4682
85
  }
4683
1.78k
  result = CURLE_OK;
4684
4685
1.79k
out:
4686
1.79k
  curlx_free(path);
4687
1.79k
  curlx_free(query);
4688
1.79k
  if(result)
4689
5
    curlx_dyn_free(&buf);
4690
1.79k
  return result;
4691
1.78k
}
4692
4693
CURLcode Curl_http_req_make2(struct httpreq **preq,
4694
                             const char *method, size_t m_len,
4695
                             CURLU *url, const char *scheme_default)
4696
1.80k
{
4697
1.80k
  struct httpreq *req;
4698
1.80k
  CURLcode result = CURLE_OUT_OF_MEMORY;
4699
1.80k
  CURLUcode uc;
4700
4701
1.80k
  DEBUGASSERT(method && m_len);
4702
4703
1.80k
  req = curlx_calloc(1, sizeof(*req) + m_len);
4704
1.80k
  if(!req)
4705
4
    goto out;
4706
1.79k
  memcpy(req->method, method, m_len);
4707
4708
1.79k
  uc = curl_url_get(url, CURLUPART_SCHEME, &req->scheme, 0);
4709
1.79k
  if(uc && uc != CURLUE_NO_SCHEME)
4710
1
    goto out;
4711
1.79k
  if(!req->scheme && scheme_default) {
4712
0
    req->scheme = curlx_strdup(scheme_default);
4713
0
    if(!req->scheme)
4714
0
      goto out;
4715
0
  }
4716
4717
1.79k
  result = req_assign_url_authority(req, url);
4718
1.79k
  if(result)
4719
5
    goto out;
4720
1.79k
  result = req_assign_url_path(req, url);
4721
1.79k
  if(result)
4722
5
    goto out;
4723
4724
1.78k
  Curl_dynhds_init(&req->headers, 0, DYN_HTTP_REQUEST);
4725
1.78k
  Curl_dynhds_init(&req->trailers, 0, DYN_HTTP_REQUEST);
4726
1.78k
  result = CURLE_OK;
4727
4728
1.80k
out:
4729
1.80k
  if(result && req)
4730
11
    Curl_http_req_free(req);
4731
1.80k
  *preq = result ? NULL : req;
4732
1.80k
  return result;
4733
1.78k
}
4734
4735
void Curl_http_req_free(struct httpreq *req)
4736
74.3k
{
4737
74.3k
  if(req) {
4738
36.3k
    curlx_free(req->scheme);
4739
36.3k
    curlx_free(req->authority);
4740
36.3k
    curlx_free(req->path);
4741
36.3k
    Curl_dynhds_cleanup(&req->headers);
4742
36.3k
    Curl_dynhds_cleanup(&req->trailers);
4743
36.3k
    curlx_free(req);
4744
36.3k
  }
4745
74.3k
}
4746
4747
struct name_const {
4748
  const char *name;
4749
  size_t namelen;
4750
};
4751
4752
static const struct name_const H2_NON_FIELD[] = {
4753
  { STRCONST("Host") },
4754
  { STRCONST("Upgrade") },
4755
  { STRCONST("Connection") },
4756
  { STRCONST("Keep-Alive") },
4757
  { STRCONST("Proxy-Connection") },
4758
  { STRCONST("Transfer-Encoding") },
4759
};
4760
4761
static bool h2_permissible_field(struct dynhds_entry *e)
4762
125k
{
4763
125k
  size_t i;
4764
666k
  for(i = 0; i < CURL_ARRAYSIZE(H2_NON_FIELD); ++i) {
4765
576k
    if(e->namelen == H2_NON_FIELD[i].namelen &&
4766
52.8k
       curl_strnequal(H2_NON_FIELD[i].name, e->name, e->namelen))
4767
35.5k
      return FALSE;
4768
576k
  }
4769
90.0k
  return TRUE;
4770
125k
}
4771
4772
static bool http_TE_has_token(const char *fvalue, const char *token)
4773
1.82k
{
4774
3.92k
  while(*fvalue) {
4775
3.13k
    struct Curl_str name;
4776
4777
    /* skip to first token */
4778
7.63k
    while(ISBLANK(*fvalue) || *fvalue == ',')
4779
4.49k
      fvalue++;
4780
3.13k
    if(curlx_str_cspn(&fvalue, &name, " \t\r;,"))
4781
851
      return FALSE;
4782
2.28k
    if(curlx_str_casecompare(&name, token))
4783
0
      return TRUE;
4784
4785
    /* skip any remainder after token, e.g. parameters with quoted strings */
4786
4.24k
    while(*fvalue && *fvalue != ',') {
4787
2.14k
      if(*fvalue == '"') {
4788
578
        struct Curl_str qw;
4789
        /* if we do not cleanly find a quoted word here, the header value
4790
         * does not follow HTTP syntax and we reject */
4791
578
        if(curlx_str_quotedword(&fvalue, &qw, CURL_MAX_HTTP_HEADER))
4792
187
          return FALSE;
4793
578
      }
4794
1.57k
      else
4795
1.57k
        fvalue++;
4796
2.14k
    }
4797
2.28k
  }
4798
786
  return FALSE;
4799
1.82k
}
4800
4801
CURLcode Curl_http_req_to_h2(struct dynhds *h2_headers,
4802
                             struct httpreq *req, struct Curl_easy *data)
4803
35.9k
{
4804
35.9k
  const char *scheme = NULL, *authority = NULL;
4805
35.9k
  struct dynhds_entry *e;
4806
35.9k
  uint32_t i, nheaders;
4807
35.9k
  CURLcode result;
4808
4809
35.9k
  DEBUGASSERT(req);
4810
35.9k
  DEBUGASSERT(h2_headers);
4811
4812
35.9k
  if(req->scheme) {
4813
1.63k
    scheme = req->scheme;
4814
1.63k
  }
4815
34.2k
  else if(strcmp("CONNECT", req->method)) {
4816
34.2k
    scheme = Curl_checkheaders(data, STRCONST(HTTP_PSEUDO_SCHEME));
4817
34.2k
    if(scheme) {
4818
0
      scheme += sizeof(HTTP_PSEUDO_SCHEME);
4819
0
      curlx_str_passblanks(&scheme);
4820
0
      infof(data, "set pseudo header %s to %s", HTTP_PSEUDO_SCHEME, scheme);
4821
0
    }
4822
34.2k
    else {
4823
34.2k
      scheme = data->state.origin->scheme->name;
4824
34.2k
    }
4825
34.2k
  }
4826
4827
35.9k
  if(req->authority) {
4828
1.58k
    authority = req->authority;
4829
1.58k
  }
4830
34.3k
  else {
4831
34.3k
    e = Curl_dynhds_get(&req->headers, STRCONST("Host"));
4832
34.3k
    if(e)
4833
33.9k
      authority = e->value;
4834
34.3k
  }
4835
4836
35.9k
  Curl_dynhds_reset(h2_headers);
4837
35.9k
  Curl_dynhds_set_opts(h2_headers, DYNHDS_OPT_LOWERCASE);
4838
35.9k
  result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_METHOD),
4839
35.9k
                           req->method, strlen(req->method));
4840
35.9k
  if(!result && scheme) {
4841
35.8k
    result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_SCHEME),
4842
35.8k
                             scheme, strlen(scheme));
4843
35.8k
  }
4844
35.9k
  if(!result && authority) {
4845
35.5k
    result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_AUTHORITY),
4846
35.5k
                             authority, strlen(authority));
4847
35.5k
  }
4848
35.9k
  if(!result && req->path) {
4849
35.7k
    result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_PATH),
4850
35.7k
                             req->path, strlen(req->path));
4851
35.7k
  }
4852
4853
35.9k
  nheaders = Curl_dynhds_count(&req->headers);
4854
163k
  for(i = 0; !result && (i < nheaders); ++i) {
4855
127k
    e = Curl_dynhds_getn(&req->headers, i);
4856
127k
    if(!e)
4857
0
      break;
4858
    /* "TE" is special in that it is only permissible when it
4859
     * has only value "trailers". RFC 9113 ch. 8.2.2 */
4860
127k
    if(e->namelen == 2 && curl_strequal("TE", e->name)) {
4861
1.82k
      if(http_TE_has_token(e->value, "trailers"))
4862
0
        result = Curl_dynhds_add(h2_headers, e->name, e->namelen,
4863
0
                                 "trailers", CURL_CSTRLEN("trailers"));
4864
1.82k
    }
4865
125k
    else if(h2_permissible_field(e)) {
4866
90.0k
      result = Curl_dynhds_add(h2_headers, e->name, e->namelen,
4867
90.0k
                               e->value, e->valuelen);
4868
90.0k
    }
4869
127k
  }
4870
4871
35.9k
  return result;
4872
35.9k
}
4873
4874
CURLcode Curl_http_resp_make(struct http_resp **presp,
4875
                             int status,
4876
                             const char *description)
4877
0
{
4878
0
  struct http_resp *resp;
4879
0
  CURLcode result = CURLE_OUT_OF_MEMORY;
4880
4881
0
  resp = curlx_calloc(1, sizeof(*resp));
4882
0
  if(!resp)
4883
0
    goto out;
4884
4885
0
  resp->status = status;
4886
0
  if(description) {
4887
0
    resp->description = curlx_strdup(description);
4888
0
    if(!resp->description)
4889
0
      goto out;
4890
0
  }
4891
0
  Curl_dynhds_init(&resp->headers, 0, DYN_HTTP_REQUEST);
4892
0
  Curl_dynhds_init(&resp->trailers, 0, DYN_HTTP_REQUEST);
4893
0
  result = CURLE_OK;
4894
4895
0
out:
4896
0
  if(result && resp)
4897
0
    Curl_http_resp_free(resp);
4898
0
  *presp = result ? NULL : resp;
4899
0
  return result;
4900
0
}
4901
4902
void Curl_http_resp_free(struct http_resp *resp)
4903
0
{
4904
0
  if(resp) {
4905
0
    curlx_free(resp->description);
4906
0
    Curl_dynhds_cleanup(&resp->headers);
4907
0
    Curl_dynhds_cleanup(&resp->trailers);
4908
0
    if(resp->prev)
4909
0
      Curl_http_resp_free(resp->prev);
4910
0
    curlx_free(resp);
4911
0
  }
4912
0
}
4913
4914
CURLcode Curl_http_setup_conn(struct Curl_easy *data,
4915
                              struct connectdata *conn)
4916
106k
{
4917
  /* allocate the HTTP-specific struct for the Curl_easy, only to survive
4918
     during this request */
4919
106k
  if(data->state.http_neg.wanted == CURL_HTTP_V3x) {
4920
    /* only HTTP/3, needs to work */
4921
0
    CURLcode result = Curl_conn_may_http3(data, conn);
4922
0
    if(result)
4923
0
      return result;
4924
0
  }
4925
106k
  return CURLE_OK;
4926
106k
}
4927
4928
CURLcode Curl_http_setup_filters(struct Curl_easy *data,
4929
                                 struct Curl_cfilter *cf)
4930
41.3k
{
4931
41.3k
  const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf, data);
4932
41.3k
  CURLcode result;
4933
4934
41.3k
  if(alpn && !strcmp("h3", alpn)) {
4935
0
    CURL_TRC_CF(data, cf, "using HTTP/3 (via ALPN)");
4936
0
  }
4937
41.3k
  else if(alpn && !strcmp("h2", alpn)) {
4938
0
    if(Curl_conn_cf_http_version(data, cf) != 20) {
4939
0
      CURL_TRC_CF(data, cf, "adding HTTP/2 filter");
4940
0
      result = Curl_http2_switch_at(data, cf);
4941
0
      if(result)
4942
0
        return result;
4943
0
    }
4944
0
    CURL_TRC_CF(data, cf, "using HTTP/2 (via ALPN)");
4945
0
  }
4946
41.3k
  else {
4947
    /* Check if user wants to use HTTP/2 with clear TCP */
4948
41.3k
    if(Curl_http2_may_switch_at(data, cf)) {
4949
18.6k
      CURL_TRC_CF(data, cf, "adding HTTP/2 filter");
4950
18.6k
      result = Curl_http2_switch_at(data, cf);
4951
18.6k
      if(result)
4952
12
        return result;
4953
18.6k
      CURL_TRC_CF(data, cf, "using HTTP/2 (over clean TCP)");
4954
18.6k
    }
4955
22.7k
    else
4956
22.7k
      CURL_TRC_CF(data, cf, "using HTTP/1.x");
4957
41.3k
  }
4958
41.3k
  return CURLE_OK;
4959
41.3k
}
4960
4961
/*
4962
 * HTTP handler interface.
4963
 */
4964
const struct Curl_protocol Curl_protocol_http = {
4965
  Curl_http_setup_conn,                 /* setup_connection */
4966
  Curl_http_setup_filters,              /* setup_filters */
4967
  Curl_http,                            /* do_it */
4968
  Curl_http_done,                       /* done */
4969
  ZERO_NULL,                            /* do_more */
4970
  ZERO_NULL,                            /* connect_it */
4971
  ZERO_NULL,                            /* connecting */
4972
  ZERO_NULL,                            /* doing */
4973
  ZERO_NULL,                            /* proto_pollset */
4974
  Curl_http_doing_pollset,              /* doing_pollset */
4975
  ZERO_NULL,                            /* domore_pollset */
4976
  Curl_http_perform_pollset,            /* perform_pollset */
4977
  ZERO_NULL,                            /* disconnect */
4978
  Curl_http_write_resp,                 /* write_resp */
4979
  Curl_http_write_resp_hd,              /* write_resp_hd */
4980
  ZERO_NULL,                            /* connection_is_dead */
4981
  ZERO_NULL,                            /* attach connection */
4982
  Curl_http_follow,                     /* follow */
4983
};
4984
4985
#endif /* CURL_DISABLE_HTTP */