Coverage Report

Created: 2026-12-01 06:27

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
21.9k
{
89
21.9k
  memset(neg, 0, sizeof(*neg));
90
21.9k
  neg->accept_09 = data->set.http09_allowed;
91
21.9k
  switch(data->set.httpwant) {
92
0
  case CURL_HTTP_VERSION_1_0:
93
0
    neg->wanted = neg->allowed = (CURL_HTTP_V1x);
94
0
    neg->only_10 = TRUE;
95
0
    break;
96
0
  case CURL_HTTP_VERSION_1_1:
97
0
    neg->wanted = neg->allowed = (CURL_HTTP_V1x);
98
0
    break;
99
0
  case CURL_HTTP_VERSION_2_0:
100
0
    neg->wanted = neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x);
101
0
    neg->h2_upgrade = TRUE;
102
0
    break;
103
0
  case CURL_HTTP_VERSION_2TLS:
104
0
    neg->wanted = neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x);
105
0
    break;
106
21.9k
  case CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE:
107
21.9k
    neg->wanted = neg->allowed = (CURL_HTTP_V2x);
108
21.9k
    data->state.http_neg.h2_prior_knowledge = TRUE;
109
21.9k
    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
0
  case CURL_HTTP_VERSION_NONE:
118
0
  default:
119
0
    neg->wanted = (CURL_HTTP_V1x | CURL_HTTP_V2x);
120
0
    neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x | CURL_HTTP_V3x);
121
0
    break;
122
21.9k
  }
123
21.9k
}
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.67k
{
158
3.67k
  bool value = !curlx_str_cspn(headerp, outp, ";:") &&
159
3.67k
    (!curlx_str_single(headerp, ':') || !curlx_str_single(headerp, ';'));
160
161
3.67k
  if(value) {
162
3.67k
    curlx_str_cspn(headerp, outp, "\r\n");
163
3.67k
    curlx_str_trimblanks(outp);
164
3.67k
  }
165
3.67k
  return value;
166
3.67k
}
167
168
static bool http_header_is_empty(const char *header)
169
1.93k
{
170
1.93k
  struct Curl_str out;
171
172
1.93k
  if(header_has_value(&header, &out)) {
173
1.93k
    return curlx_strlen(&out) == 0;
174
1.93k
  }
175
0
  return TRUE; /* invalid header format, treat as empty */
176
1.93k
}
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.74k
{
188
1.74k
  struct Curl_str out = { 0 };
189
190
  /* find the end of the header name */
191
1.74k
  if(header_has_value(&header, &out)) {
192
1.74k
    *valp = curlx_memdup0(curlx_str(&out), curlx_strlen(&out));
193
1.74k
    if(*valp)
194
1.74k
      return CURLE_OK;
195
0
    return CURLE_OUT_OF_MEMORY;
196
1.74k
  }
197
  /* bad input */
198
0
  *valp = NULL;
199
0
  return CURLE_BAD_FUNCTION_ARGUMENT;
200
1.74k
}
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
30.2k
{
214
30.2k
  struct Curl_str out;
215
216
  /* find the end of the header name */
217
30.2k
  if(!curlx_str_until(&header, &out, MAX_HTTP_RESP_HEADER_SIZE, ':') &&
218
30.2k
     !curlx_str_single(&header, ':')) {
219
30.2k
    curlx_str_untilnl(&header, &out, MAX_HTTP_RESP_HEADER_SIZE);
220
30.2k
    curlx_str_trimblanks(&out);
221
30.2k
    return curlx_memdup0(curlx_str(&out), curlx_strlen(&out));
222
30.2k
  }
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
736
{
242
736
  size_t size = 0;
243
736
  char *authorization = NULL;
244
736
  char **p_hd;
245
736
  CURLcode result;
246
736
  char *out;
247
248
  /* credentials are unique per transfer for HTTP, do not use the ones for the
249
     connection */
250
736
  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
736
  else {
258
736
    p_hd = &data->req.hd_auth;
259
736
  }
260
261
736
  if(!creds) {
262
0
    DEBUGASSERT(0);
263
0
    return CURLE_FAILED_INIT;
264
0
  }
265
266
736
  out = curl_maprintf("%s:%s", creds->user, creds->passwd);
267
736
  if(!out)
268
0
    return CURLE_OUT_OF_MEMORY;
269
270
736
  result = curlx_base64_encode((uint8_t *)out, strlen(out),
271
736
                               &authorization, &size);
272
736
  if(result)
273
1
    goto fail;
274
275
735
  if(!authorization) {
276
0
    result = CURLE_REMOTE_ACCESS_DENIED;
277
0
    goto fail;
278
0
  }
279
280
735
  curlx_free(*p_hd);
281
735
  *p_hd = curl_maprintf("%s: Basic %s\r\n", hd_name, authorization);
282
735
  curlx_free(authorization);
283
735
  if(!*p_hd) {
284
1
    result = CURLE_OUT_OF_MEMORY;
285
1
    goto fail;
286
1
  }
287
288
736
fail:
289
736
  curlx_free(out);
290
736
  return result;
291
735
}
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
311
{
324
311
  bool have_user_pass = Curl_creds_has_user_or_pass(creds);
325
311
  bool picked;
326
  /* only deal with authentication we want */
327
311
  unsigned long avail = pick->avail & pick->want & mask;
328
311
  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
311
  if(avail & CURLAUTH_NEGOTIATE)  /* available on empty creds */
333
0
    pick->picked = CURLAUTH_NEGOTIATE;
334
311
#ifndef CURL_DISABLE_BEARER_AUTH
335
311
  else if((avail & CURLAUTH_BEARER) && Curl_creds_has_oauth_bearer(creds))
336
0
    pick->picked = CURLAUTH_BEARER;
337
311
#endif
338
311
#ifndef CURL_DISABLE_DIGEST_AUTH
339
311
  else if((avail & CURLAUTH_DIGEST) && have_user_pass)
340
0
    pick->picked = CURLAUTH_DIGEST;
341
311
#endif
342
311
#ifndef CURL_DISABLE_BASIC_AUTH
343
311
  else if((avail & CURLAUTH_BASIC) && have_user_pass)
344
0
    pick->picked = CURLAUTH_BASIC;
345
311
#endif
346
311
#ifndef CURL_DISABLE_AWS
347
311
  else if(avail & CURLAUTH_AWS_SIGV4)
348
0
    pick->picked = CURLAUTH_AWS_SIGV4;
349
311
#endif
350
311
#ifndef CURL_DISABLE_HTTPSIG
351
311
  else if(avail & CURLAUTH_HTTPSIG)
352
0
    pick->picked = CURLAUTH_HTTPSIG;
353
311
#endif
354
311
  else {
355
311
    pick->picked = CURLAUTH_PICKNONE; /* we select to use nothing */
356
311
    picked = FALSE;
357
311
  }
358
311
  pick->avail = CURLAUTH_NONE; /* clear it here */
359
360
311
  return picked;
361
311
}
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
21.7k
{
374
21.7k
  curl_off_t bytessent = data->req.writebytecount;
375
21.7k
  curl_off_t expectsend = Curl_creader_total_length(data);
376
21.7k
  curl_off_t upload_remain = (expectsend >= 0) ? (expectsend - bytessent) : -1;
377
21.7k
  bool little_upload_remains = (upload_remain >= 0 && upload_remain < 2000);
378
21.7k
  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
21.7k
  bool abort_upload = (!data->req.upload_done && !little_upload_remains);
383
21.7k
  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
21.7k
  if(needs_rewind) {
392
5.11k
    infof(data, "Need to rewind upload for next request");
393
5.11k
    Curl_creader_set_rewind(data, TRUE);
394
5.11k
  }
395
396
21.7k
  if(conn->bits.close)
397
    /* If we already decided to close this connection, we cannot veto. */
398
21
    return CURLE_OK;
399
400
21.7k
  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
343
  }
416
417
21.7k
  if(abort_upload) {
418
343
    if(upload_remain >= 0)
419
106
      infof(data, "%s%sclose instead of sending %" FMT_OFF_T " more bytes",
420
343
            ongoing_auth ? ongoing_auth : "",
421
343
            ongoing_auth ? " send, " : "",
422
343
            upload_remain);
423
237
    else
424
237
      infof(data, "%s%sclose instead of sending unknown amount "
425
343
            "of more bytes",
426
343
            ongoing_auth ? ongoing_auth : "",
427
343
            ongoing_auth ? " send, " : "");
428
    /* We decided to abort the ongoing transfer */
429
343
    streamclose(conn);
430
343
    data->req.size = 0; /* do not download any more than 0 bytes */
431
343
    data->req.http_bodyless = TRUE;
432
343
  }
433
21.7k
  return CURLE_OK;
434
21.7k
}
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
47.7k
{
446
47.7k
  DEBUGASSERT(data);
447
47.7k
  DEBUGASSERT(data->conn);
448
449
  /*
450
   * If we have not been asked to fail on error,
451
   * do not fail.
452
   */
453
47.7k
  if(!data->set.http_fail_on_error)
454
47.6k
    return FALSE;
455
456
  /*
457
   * Any code < 400 is never terminal.
458
   */
459
176
  if(httpcode < 400)
460
113
    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
63
  if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
467
46
     httpcode == 416)
468
8
    return FALSE;
469
470
  /*
471
   * Any code >= 400 that is not 401 or 407 is always
472
   * a terminal error
473
   */
474
55
  if((httpcode != 401) && (httpcode != 407))
475
34
    return TRUE;
476
477
  /*
478
   * All we have left to deal with is 401 and 407
479
   */
480
21
  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
21
  if((httpcode == 401) && !data->state.creds)
498
13
    return TRUE;
499
8
#ifndef CURL_DISABLE_PROXY
500
8
  if((httpcode == 407) && !data->conn->http_proxy.creds)
501
1
    return TRUE;
502
7
#endif
503
504
7
  return (bool)data->state.authproblem;
505
8
}
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
24.0k
{
515
24.0k
  struct connectdata *conn = data->conn;
516
24.0k
  bool pickhost = FALSE;
517
24.0k
  bool pickproxy = FALSE;
518
24.0k
  CURLcode result = CURLE_OK;
519
24.0k
  unsigned long authmask = ~0UL;
520
521
24.0k
  if(!Curl_creds_has_oauth_bearer(data->state.creds))
522
22.6k
    authmask &= (unsigned long)~CURLAUTH_BEARER;
523
524
24.0k
  if(100 <= data->req.httpcode && data->req.httpcode <= 199)
525
    /* this is a transient response code, ignore */
526
0
    return CURLE_OK;
527
528
24.0k
  if(data->state.authproblem)
529
283
    return data->set.http_fail_on_error ? CURLE_HTTP_RETURNED_ERROR : CURLE_OK;
530
531
23.7k
  if(data->state.creds &&
532
4.33k
     ((data->req.httpcode == 401) ||
533
4.32k
      (data->req.authneg && data->req.httpcode < 300))) {
534
311
    pickhost = pickoneauth(&data->state.authhost, authmask, data->state.creds);
535
311
    if(!pickhost)
536
311
      data->state.authproblem = TRUE;
537
0
    else
538
0
      data->info.httpauthpicked = data->state.authhost.picked;
539
311
  }
540
23.7k
#ifndef CURL_DISABLE_PROXY
541
23.7k
  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
23.7k
#endif
553
554
23.7k
  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
23.7k
  else if((data->req.httpcode < 300) &&
569
1.11k
          !data->state.authhost.done &&
570
375
          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
304
          (data->state.httpreq != HTTPREQ_GET) &&
576
304
          (data->state.httpreq != HTTPREQ_HEAD)) {
577
    /* clone URL */
578
304
    data->req.newurl = Curl_bufref_dup(&data->state.url);
579
304
    if(!data->req.newurl)
580
0
      return CURLE_OUT_OF_MEMORY;
581
304
    data->state.authhost.done = TRUE;
582
304
  }
583
23.7k
  if(http_should_fail(data, data->req.httpcode)) {
584
3
    failf(data, "The requested URL returned error: %d",
585
3
          data->req.httpcode);
586
3
    result = CURLE_HTTP_RETURNED_ERROR;
587
3
  }
588
589
23.7k
  return result;
590
23.7k
}
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
6.34k
{
608
6.34k
  CURLcode result = CURLE_OK;
609
610
6.34k
  *pauth_used = NULL;
611
6.34k
  switch(authstatus->picked) {
612
613
0
#ifndef CURL_DISABLE_AWS
614
2.76k
  case CURLAUTH_AWS_SIGV4:
615
2.76k
    if(!proxy) {
616
      /* this method is never for proxy */
617
2.76k
      *pauth_used = "AWS_SIGV4";
618
2.76k
      result = Curl_output_aws_sigv4(data);
619
2.76k
    }
620
2.76k
    break;
621
0
#endif
622
623
0
#ifndef CURL_DISABLE_HTTPSIG
624
2
  case CURLAUTH_HTTPSIG:
625
2
    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
2
      if(Curl_auth_allowed_to_host(data)) {
630
2
        *pauth_used = "HTTPSIG";
631
2
        result = Curl_output_httpsig(data);
632
2
      }
633
0
      else
634
0
        authstatus->done = TRUE;
635
2
    }
636
2
    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
1.47k
  case CURLAUTH_DIGEST:
657
1.47k
    *pauth_used = "Digest";
658
1.47k
    result = Curl_output_digest(data, creds, hd_name, proxy,
659
1.47k
                                (const unsigned char *)method,
660
1.47k
                                (const unsigned char *)path);
661
1.47k
    break;
662
0
#endif
663
664
0
#ifndef CURL_DISABLE_BASIC_AUTH
665
1.15k
  case CURLAUTH_BASIC:
666
1.15k
    if(creds && Curl_creds_has_user_or_pass(creds) && (
667
736
#ifndef CURL_DISABLE_PROXY
668
736
       (proxy && !Curl_checkProxyheaders(data, conn, hd_name, hd_len)) ||
669
736
#endif
670
736
       (!proxy && !Curl_checkheaders(data, hd_name, hd_len)))) {
671
736
      *pauth_used = "Basic";
672
736
      result = http_output_basic(data, creds, hd_name, proxy);
673
736
      if(result)
674
2
        return result;
675
736
    }
676
    /* NOTE: this function should set 'done' TRUE, as the other auth
677
       functions work that way */
678
1.14k
    authstatus->done = TRUE;
679
1.14k
    break;
680
0
#endif
681
682
0
#ifndef CURL_DISABLE_BEARER_AUTH
683
68
  case CURLAUTH_BEARER:
684
68
    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
68
    authstatus->done = TRUE;
694
68
    break;
695
0
#endif
696
697
885
  default:
698
    /* a pick we do not support? */
699
885
    (void)conn;
700
#ifdef CURL_DISABLE_DIGEST_AUTH
701
    (void)method;
702
    (void)path;
703
#endif
704
885
    break;
705
6.34k
  } /* end of switch */
706
707
6.34k
  return result;
708
6.34k
}
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
42.8k
{
718
42.8k
  CURLcode result = CURLE_OK;
719
42.8k
  struct auth *authhost;
720
42.8k
  struct auth *authproxy;
721
42.8k
  const char *path_and_query = path;
722
42.8k
  char *tmp_str = NULL;
723
42.8k
  const char *auth_used;
724
725
42.8k
  DEBUGASSERT(data);
726
42.8k
  authhost = &data->state.authhost;
727
42.8k
  authproxy = &data->state.authproxy;
728
729
42.8k
  if(
730
42.8k
#ifndef CURL_DISABLE_PROXY
731
42.8k
    (!conn->http_proxy.peer || !conn->http_proxy.creds) &&
732
42.8k
#endif
733
#ifdef USE_SPNEGO
734
    !(authhost->want & CURLAUTH_NEGOTIATE) &&
735
    !(authproxy->want & CURLAUTH_NEGOTIATE) &&
736
#endif
737
42.8k
#ifndef CURL_DISABLE_HTTPSIG
738
42.8k
    !(authhost->want & CURLAUTH_HTTPSIG) &&
739
42.6k
#endif
740
42.6k
    !data->state.creds) {
741
    /* no authentication with no user or password */
742
36.5k
    authhost->done = TRUE;
743
36.5k
    authproxy->done = TRUE;
744
36.5k
    result = CURLE_OK;
745
36.5k
    goto out;
746
36.5k
  }
747
748
6.34k
  if(query) {
749
434
    tmp_str = curl_maprintf("%s?%s", path, query);
750
434
    if(!tmp_str) {
751
1
      result = CURLE_OUT_OF_MEMORY;
752
1
      goto out;
753
1
    }
754
433
    path_and_query = tmp_str;
755
433
  }
756
757
6.34k
  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
1.76k
    authhost->picked = authhost->want;
762
763
6.34k
  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
1.76k
    authproxy->picked = authproxy->want;
768
769
6.34k
#ifndef CURL_DISABLE_PROXY
770
  /* Send proxy authentication header only with a forwarding proxy
771
   * or when doing a CONNECT* request. */
772
6.34k
  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
6.34k
  else
787
6.34k
    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
6.34k
  if(!is_connect &&
797
6.34k
     (data->state.creds || Curl_auth_allowed_to_host(data))) {
798
6.34k
    result = output_auth_headers(data, conn, authhost,
799
6.34k
                                 method, path_and_query,
800
6.34k
                                 FALSE, data->state.creds,
801
6.34k
                                 STRCONST("Authorization"),
802
6.34k
                                 &auth_used);
803
6.34k
    if(result)
804
93
      goto out;
805
6.25k
    data->info.httpauthpicked = auth_used ? authhost->picked : 0;
806
6.25k
    authhost->multipass = auth_used ? !authhost->done : FALSE;
807
6.25k
    if(auth_used)
808
4.92k
      infof(data, "Server auth using %s with user '%s'", auth_used,
809
6.25k
            data->state.creds ? data->state.creds->user : "");
810
6.25k
  }
811
0
  else
812
0
    authhost->done = TRUE;
813
814
6.25k
  if(((authhost->multipass && !authhost->done) ||
815
4.77k
      (authproxy->multipass && !authproxy->done)) &&
816
1.47k
     (httpreq != HTTPREQ_GET) &&
817
1.36k
     (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
1.20k
    data->req.authneg = TRUE;
821
1.20k
  }
822
5.04k
  else
823
5.04k
    data->req.authneg = FALSE;
824
825
42.8k
out:
826
42.8k
  curlx_free(tmp_str);
827
42.8k
  return result;
828
6.25k
}
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
702
{
857
  /* the auth string must not have an alnum following */
858
702
  size_t n = strlen(auth);
859
702
  return curl_strnequal(auth, line, n) && !ISALNUM(line[n]);
860
702
}
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
74
{
976
  /*
977
   * This resource requires authentication
978
   */
979
74
#if defined(USE_SPNEGO) ||                      \
980
74
  !defined(CURL_DISABLE_DIGEST_AUTH) ||         \
981
74
  !defined(CURL_DISABLE_BASIC_AUTH) ||          \
982
74
  !defined(CURL_DISABLE_BEARER_AUTH)
983
984
74
  uint32_t *availp;
985
74
  struct auth *authp;
986
74
  CURLcode result = CURLE_OK;
987
74
  DEBUGASSERT(auth);
988
74
  DEBUGASSERT(data);
989
990
74
  if(proxy) {
991
0
    availp = &data->info.proxyauthavail;
992
0
    authp = &data->state.authproxy;
993
0
  }
994
74
  else {
995
74
    availp = &data->info.httpauthavail;
996
74
    authp = &data->state.authhost;
997
74
  }
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
286
  while(*auth) {
1016
#ifdef USE_SPNEGO
1017
    if(authcmp("Negotiate", auth))
1018
      result = auth_spnego(data, proxy, auth, authp, availp);
1019
#endif
1020
234
#ifndef CURL_DISABLE_DIGEST_AUTH
1021
234
    if(!result && authcmp("Digest", auth))
1022
0
      result = auth_digest(data, proxy, auth, authp, availp);
1023
234
#endif
1024
234
#ifndef CURL_DISABLE_BASIC_AUTH
1025
234
    if(!result && authcmp("Basic", auth))
1026
0
      result = auth_basic(data, authp, availp);
1027
234
#endif
1028
234
#ifndef CURL_DISABLE_BEARER_AUTH
1029
234
    if(authcmp("Bearer", auth))
1030
0
      result = auth_bearer(data, authp, availp);
1031
234
#endif
1032
1033
234
    if(result)
1034
0
      break;
1035
1036
    /* there may be multiple methods on one line, so keep reading */
1037
234
    auth = strchr(auth, ',');
1038
234
    if(auth) /* if we are on a comma, skip it */
1039
212
      auth++;
1040
22
    else
1041
22
      break;
1042
212
    curlx_str_passblanks(&auth);
1043
212
  }
1044
74
  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
74
}
1053
1054
static void http_switch_to_get(struct Curl_easy *data, int code)
1055
443
{
1056
443
  const char *req = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
1057
1058
443
  if((req || data->state.httpreq != HTTPREQ_GET) &&
1059
278
     (data->set.http_follow_mode == CURLFOLLOW_OBEYCODE)) {
1060
54
    NOVERBOSE((void)code);
1061
54
    infof(data, "Switch to GET because of %d response", code);
1062
54
    data->state.http_ignorecustom = TRUE;
1063
54
  }
1064
389
  else if(req && (data->set.http_follow_mode != CURLFOLLOW_FIRSTONLY))
1065
43
    infof(data, "Stick to %s instead of GET", req);
1066
1067
443
  data->state.httpreq = HTTPREQ_GET;
1068
443
  Curl_creader_set_rewind(data, FALSE);
1069
443
}
1070
1071
#define HTTPREQ_IS_POST(data)                    \
1072
1.63k
  ((data)->state.httpreq == HTTPREQ_POST ||      \
1073
818
   (data)->state.httpreq == HTTPREQ_POST_FORM || \
1074
1.63k
   (data)->state.httpreq == HTTPREQ_POST_MIME)
1075
1076
static CURLcode set_auto_referer(struct Curl_easy *data)
1077
2.65k
{
1078
2.65k
  CURLU *u;
1079
2.65k
  char *referer = NULL;
1080
2.65k
  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
2.65k
  Curl_bufref_free(&data->state.referer);
1086
1087
  /* Make a copy of the URL without credentials and fragment */
1088
2.65k
  u = curl_url();
1089
2.65k
  if(!u)
1090
0
    return CURLE_OUT_OF_MEMORY;
1091
1092
2.65k
  uc = curl_url_set(u, CURLUPART_URL,
1093
2.65k
                    Curl_bufref_ptr(&data->state.url), 0);
1094
2.65k
  if(!uc)
1095
2.64k
    uc = curl_url_set(u, CURLUPART_FRAGMENT, NULL, 0);
1096
2.65k
  if(!uc)
1097
2.64k
    uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
1098
2.65k
  if(!uc)
1099
2.64k
    uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
1100
2.65k
  if(!uc)
1101
2.64k
    uc = curl_url_get(u, CURLUPART_URL, &referer, 0);
1102
1103
2.65k
  curl_url_cleanup(u);
1104
1105
2.65k
  if(uc || !referer)
1106
2
    return CURLE_OUT_OF_MEMORY;
1107
1108
2.64k
  Curl_bufref_set(&data->state.referer, referer, 0, curl_free);
1109
2.64k
  return CURLE_OK;
1110
2.65k
}
1111
1112
static CURLcode follow_parse_url(struct Curl_easy *data, const char *newurl,
1113
                                 followtype type, char **follow_urlp,
1114
                                 CURLUcode *ucp)
1115
22.9k
{
1116
22.9k
  CURLUcode uc;
1117
1118
22.9k
  DEBUGASSERT(data->state.uh);
1119
22.9k
  uc = curl_url_set(data->state.uh, CURLUPART_URL, newurl, (unsigned int)
1120
22.9k
                    ((type == FOLLOW_FAKE) ? CURLU_NON_SUPPORT_SCHEME :
1121
22.9k
                     ((type == FOLLOW_REDIR) ? CURLU_URLENCODE : 0) |
1122
22.3k
                     CURLU_ALLOW_SPACE |
1123
22.3k
                     (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)));
1124
22.9k
  *ucp = uc;
1125
22.9k
  if(uc) {
1126
189
    if((uc == CURLUE_OUT_OF_MEMORY) || (type != FOLLOW_FAKE)) {
1127
49
      failf(data, "The redirect target URL could not be parsed: %s",
1128
49
            curl_url_strerror(uc));
1129
49
      return Curl_uc_to_curlcode(uc);
1130
49
    }
1131
1132
    /* the URL could not be parsed for some reason, but since this is FAKE
1133
       mode, duplicate the field as-is */
1134
140
    *follow_urlp = curlx_strdup(newurl);
1135
140
    if(!*follow_urlp)
1136
0
      return CURLE_OUT_OF_MEMORY;
1137
140
  }
1138
22.7k
  else {
1139
22.7k
    CURLU *u = curl_url();
1140
22.7k
    if(!u)
1141
3
      return CURLE_OUT_OF_MEMORY;
1142
22.7k
    uc = curl_url_set(u, CURLUPART_URL,
1143
22.7k
                      Curl_bufref_ptr(&data->state.url),
1144
22.7k
                      CURLU_URLENCODE | CURLU_ALLOW_SPACE);
1145
22.7k
    if(!uc)
1146
22.7k
      uc = curl_url_get(data->state.uh, CURLUPART_URL, follow_urlp, 0);
1147
22.7k
    if(uc) {
1148
18
      curl_url_cleanup(u);
1149
18
      return Curl_uc_to_curlcode(uc);
1150
18
    }
1151
1152
22.7k
#ifndef CURL_DISABLE_DIGEST_AUTH
1153
22.7k
    {
1154
22.7k
      bool same_origin = Curl_url_same_origin(u, data->state.uh);
1155
22.7k
      curl_url_cleanup(u);
1156
22.7k
      if(!same_origin)
1157
379
        Curl_auth_digest_cleanup(&data->state.digest);
1158
22.7k
    }
1159
#else
1160
    curl_url_cleanup(u);
1161
#endif
1162
22.7k
  }
1163
22.8k
  return CURLE_OK;
1164
22.9k
}
1165
1166
static CURLcode follow_fake(struct Curl_easy *data, char *follow_url,
1167
                            CURLUcode uc, bool reachedmax)
1168
503
{
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
503
  if(!uc) {
1172
363
    CURLU *u = curl_url();
1173
363
    char *nocred = NULL;
1174
1175
363
    if(!u) {
1176
6
      curlx_free(follow_url);
1177
6
      return CURLE_OUT_OF_MEMORY;
1178
6
    }
1179
357
    if(!curl_url_set(u, CURLUPART_URL, follow_url,
1180
357
                     CURLU_NON_SUPPORT_SCHEME |
1181
357
                     (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)) &&
1182
335
       !curl_url_set(u, CURLUPART_USER, NULL, 0) &&
1183
335
       !curl_url_set(u, CURLUPART_PASSWORD, NULL, 0))
1184
335
      (void)curl_url_get(u, CURLUPART_URL, &nocred, CURLU_GET_EMPTY);
1185
357
    curl_url_cleanup(u);
1186
357
    if(nocred) {
1187
332
      curlx_free(follow_url);
1188
332
      follow_url = nocred;
1189
332
    }
1190
357
  }
1191
497
  data->info.wouldredirect = follow_url;
1192
1193
497
  if(reachedmax) {
1194
4
    failf(data, "Maximum (%d) redirects followed", data->set.maxredirs);
1195
4
    return CURLE_TOO_MANY_REDIRECTS;
1196
4
  }
1197
493
  return CURLE_OK;
1198
497
}
1199
1200
static bool follow_switch_get(struct Curl_easy *data)
1201
22.3k
{
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
22.3k
  switch(data->info.httpcode) {
1207
134
  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
134
    if(HTTPREQ_IS_POST(data) && !data->set.post301) {
1225
9
      http_switch_to_get(data, 301);
1226
9
      return TRUE;
1227
9
    }
1228
125
    break;
1229
251
  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
251
    if(HTTPREQ_IS_POST(data) && !data->set.post302) {
1247
20
      http_switch_to_get(data, 302);
1248
20
      return TRUE;
1249
20
    }
1250
231
    break;
1251
433
  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
433
    if(!HTTPREQ_IS_POST(data) || !data->set.post303) {
1257
414
      http_switch_to_get(data, 303);
1258
414
      return TRUE;
1259
414
    }
1260
19
    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
21.5k
  default:
1274
21.5k
    break;
1275
22.3k
  }
1276
21.9k
  return FALSE;
1277
22.3k
}
1278
1279
CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
1280
                          followtype type)
1281
22.9k
{
1282
22.9k
  bool disallowport = FALSE;
1283
22.9k
  bool reachedmax = FALSE;
1284
22.9k
  char *follow_url = NULL;
1285
22.9k
  CURLUcode uc;
1286
22.9k
  CURLcode result;
1287
22.9k
  CURLcode rewind_result;
1288
22.9k
  bool switch_to_get;
1289
1290
22.9k
  DEBUGASSERT(type != FOLLOW_NONE);
1291
1292
22.9k
  if(type != FOLLOW_FAKE)
1293
22.4k
    data->state.requests++; /* count all real follows */
1294
22.9k
  if(type == FOLLOW_REDIR) {
1295
21.8k
    if((data->set.maxredirs != -1) &&
1296
21.7k
       (data->state.followlocation >= data->set.maxredirs)) {
1297
4
      reachedmax = TRUE;
1298
4
      type = FOLLOW_FAKE; /* switch to fake to store the would-be-redirected
1299
                             to URL */
1300
4
    }
1301
21.8k
    else {
1302
21.8k
      data->state.followlocation++; /* count redirect-followings, including
1303
                                       auth reloads */
1304
1305
21.8k
      if(data->set.http_auto_referer) {
1306
2.65k
        result = set_auto_referer(data);
1307
2.65k
        if(result)
1308
2
          return result;
1309
2.65k
      }
1310
21.8k
    }
1311
21.8k
  }
1312
1313
22.9k
  if((type != FOLLOW_RETRY) &&
1314
22.3k
     (data->req.httpcode != 401) && (data->req.httpcode != 407) &&
1315
22.3k
     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
604
    disallowport = TRUE;
1319
604
  }
1320
1321
22.9k
  result = follow_parse_url(data, newurl, type, &follow_url, &uc);
1322
22.9k
  if(result)
1323
70
    return result;
1324
1325
22.8k
  DEBUGASSERT(follow_url);
1326
1327
22.8k
  if(type == FOLLOW_FAKE)
1328
503
    return follow_fake(data, follow_url, uc, reachedmax);
1329
1330
22.3k
  if(disallowport)
1331
409
    data->state.allow_port = FALSE;
1332
1333
22.3k
  Curl_bufref_set(&data->state.url, follow_url, 0, curl_free);
1334
22.3k
  rewind_result = Curl_req_soft_reset(&data->req, data);
1335
22.3k
  infof(data, "Issue another request to this URL: '%s'", follow_url);
1336
22.3k
  if((data->set.http_follow_mode == CURLFOLLOW_FIRSTONLY) &&
1337
2.79k
     !data->state.http_ignorecustom &&
1338
2.67k
     CURL_EASY_STR(data, STRING_CUSTOMREQUEST)) {
1339
16
    data->state.http_ignorecustom = TRUE;
1340
16
    infof(data, "Drop custom request method for next request");
1341
16
  }
1342
1343
22.3k
  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
22.3k
  if(rewind_result && !switch_to_get)
1348
66
    return rewind_result;
1349
1350
22.2k
  Curl_pgrsTime(data, TIMER_REDIRECT);
1351
22.2k
  Curl_pgrsResetTransferSizes(data);
1352
1353
22.2k
  return CURLE_OK;
1354
22.3k
}
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
271
{
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
271
  const char *p;
1376
271
  struct Curl_str val;
1377
271
  DEBUGASSERT(hlen);
1378
271
  DEBUGASSERT(clen);
1379
271
  DEBUGASSERT(header);
1380
271
  DEBUGASSERT(content);
1381
1382
271
  if(!curl_strnequal(headerline, header, hlen))
1383
34
    return FALSE; /* does not start with header */
1384
1385
  /* pass the header */
1386
237
  p = &headerline[hlen];
1387
1388
237
  if(curlx_str_cspn(&p, &val, "\r\n"))
1389
34
    return FALSE;
1390
203
  curlx_str_trimblanks(&val);
1391
1392
  /* find the content string in the rest of the line */
1393
203
  if(curlx_strlen(&val) >= clen) {
1394
169
    size_t len;
1395
169
    p = curlx_str(&val);
1396
541
    for(len = curlx_strlen(&val); len >= clen;) {
1397
487
      struct Curl_str next;
1398
487
      const char *o = p;
1399
      /* after a match there must be a comma, space, newline or NUL byte */
1400
487
      if(curl_strnequal(p, content, clen) &&
1401
0
         ((p[clen] == ',') || ISBLANK(p[clen]) || ISNEWLINE(p[clen]) ||
1402
0
          !p[clen]))
1403
0
        return TRUE; /* match! */
1404
      /* advance to the next comma */
1405
487
      if(curlx_str_until(&p, &next, len, ',') ||
1406
450
         curlx_str_single(&p, ','))
1407
80
        break; /* no comma, get out */
1408
1409
      /* if there are more dummy commas, move over them as well */
1410
407
      do
1411
1.60k
        curlx_str_passblanks(&p);
1412
1.60k
      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
407
      if((size_t)(p - o) > len)
1416
35
        break;
1417
372
      len -= (p - o);
1418
372
    }
1419
169
  }
1420
203
  return FALSE; /* no match */
1421
203
}
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
0
{
1434
0
  struct cr_exp100_ctx *ctx = reader->ctx;
1435
0
  if(ctx->state > EXP100_SEND_DATA) {
1436
0
    ctx->state = EXP100_SEND_DATA;
1437
0
    Curl_expire_clear(data, EXPIRE_100_TIMEOUT);
1438
0
  }
1439
0
}
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
0
{
1446
0
  struct cr_exp100_ctx *ctx = reader->ctx;
1447
0
  timediff_t ms;
1448
1449
0
  switch(ctx->state) {
1450
0
  case EXP100_SENDING_REQUEST:
1451
0
    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
0
      DEBUGF(infof(data, "cr_exp100_read, request not full sent yet"));
1455
0
      *nread = 0;
1456
0
      *eos = FALSE;
1457
0
      return CURLE_OK;
1458
0
    }
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
0
    DEBUGF(infof(data, "cr_exp100_read, start AWAITING_CONTINUE, "
1462
0
                 "timeout %dms", data->set.expect_100_timeout));
1463
0
    ctx->state = EXP100_AWAITING_CONTINUE;
1464
0
    ctx->start = *Curl_pgrs_now(data);
1465
0
    Curl_expire_set(data, EXPIRE_100_TIMEOUT,
1466
0
                    data->set.expect_100_timeout, &ctx->start);
1467
0
    *nread = 0;
1468
0
    *eos = FALSE;
1469
0
    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
0
  case EXP100_AWAITING_CONTINUE:
1476
0
    ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &ctx->start);
1477
0
    if(ms < data->set.expect_100_timeout) {
1478
0
      DEBUGF(infof(data, "cr_exp100_read, AWAITING_CONTINUE, not expired"));
1479
0
      *nread = 0;
1480
0
      *eos = FALSE;
1481
0
      return CURLE_OK;
1482
0
    }
1483
    /* we have waited long enough, continue anyway */
1484
0
    http_exp100_continue(data, reader);
1485
0
    infof(data, "Done waiting for 100-continue");
1486
0
    FALLTHROUGH();
1487
0
  default:
1488
0
    DEBUGF(infof(data, "cr_exp100_read, pass through"));
1489
0
    return Curl_creader_read(data, reader->next, buf, blen, nread, eos);
1490
0
  }
1491
0
}
1492
1493
static void cr_exp100_done(struct Curl_easy *data,
1494
                           struct Curl_creader *reader, int premature)
1495
0
{
1496
0
  struct cr_exp100_ctx *ctx = reader->ctx;
1497
0
  ctx->state = premature ? EXP100_FAILED : EXP100_SEND_DATA;
1498
0
  Curl_expire_clear(data, EXPIRE_100_TIMEOUT);
1499
0
}
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
0
{
1517
0
  struct Curl_creader *reader = NULL;
1518
0
  CURLcode result;
1519
1520
0
  result = Curl_creader_create(&reader, data, &cr_exp100, CURL_CR_PROTOCOL);
1521
0
  if(!result)
1522
0
    result = Curl_creader_add(data, reader);
1523
0
  if(!result) {
1524
0
    struct cr_exp100_ctx *ctx = reader->ctx;
1525
0
    ctx->state = EXP100_SENDING_REQUEST;
1526
0
  }
1527
1528
0
  if(result && reader)
1529
0
    Curl_creader_free(data, reader);
1530
0
  return result;
1531
0
}
1532
1533
static void http_exp100_got100(struct Curl_easy *data)
1534
149
{
1535
149
  struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1536
149
  if(r)
1537
0
    http_exp100_continue(data, r);
1538
149
}
1539
1540
static bool http_exp100_is_waiting(struct Curl_easy *data)
1541
0
{
1542
0
  struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1543
0
  if(r) {
1544
0
    struct cr_exp100_ctx *ctx = r->ctx;
1545
0
    return ctx->state == EXP100_AWAITING_CONTINUE;
1546
0
  }
1547
0
  return FALSE;
1548
0
}
1549
1550
static void http_exp100_send_anyway(struct Curl_easy *data)
1551
49
{
1552
49
  struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1553
49
  if(r)
1554
0
    http_exp100_continue(data, r);
1555
49
}
1556
1557
static bool http_exp100_is_selected(struct Curl_easy *data)
1558
1
{
1559
1
  struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1560
1
  return !!r;
1561
1
}
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
0
{
1576
0
  struct connectdata *conn = data->conn;
1577
0
  CURLcode result = CURLE_OK;
1578
1579
0
  if(CURL_REQ_WANT_RECV(data)) {
1580
0
    result = Curl_pollset_add_in(data, ps, conn->chain.sock);
1581
0
  }
1582
1583
  /* on a "Expect: 100-continue" timed wait, do not poll for outgoing */
1584
0
  if(!result && Curl_req_want_send(data) && !http_exp100_is_waiting(data)) {
1585
0
    result = Curl_pollset_add_out(data, ps, conn->chain.sock);
1586
0
  }
1587
0
  return result;
1588
0
}
1589
1590
static CURLcode http_write_header(struct Curl_easy *data,
1591
                                  const char *hd, size_t hdlen)
1592
26.4k
{
1593
26.4k
  CURLcode result;
1594
26.4k
  int writetype;
1595
1596
  /* now, only output this if the header AND body are requested:
1597
   */
1598
26.4k
  Curl_debug(data, CURLINFO_HEADER_IN, hd, hdlen);
1599
1600
26.4k
  writetype = CLIENTWRITE_HEADER |
1601
26.4k
    ((data->req.httpcode / 100 == 1) ? CLIENTWRITE_1XX : 0);
1602
1603
26.4k
  result = Curl_client_write(data, writetype, hd, hdlen);
1604
26.4k
  if(result)
1605
2
    return result;
1606
1607
26.4k
  result = Curl_bump_headersize(data, hdlen, FALSE);
1608
26.4k
  if(result)
1609
1
    return result;
1610
1611
26.4k
  data->req.deductheadercount = (100 <= data->req.httpcode &&
1612
26.4k
                                 199 >= data->req.httpcode) ?
1613
24.0k
    data->req.headerbytecount : 0;
1614
26.4k
  return result;
1615
26.4k
}
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
44.1k
{
1625
44.1k
  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
44.1k
  data->state.authhost.multipass = FALSE;
1630
44.1k
  data->state.authproxy.multipass = FALSE;
1631
1632
44.1k
  if(curlx_dyn_len(&data->state.headerb)) {
1633
0
    (void)http_write_header(data, curlx_dyn_ptr(&data->state.headerb),
1634
0
                            curlx_dyn_len(&data->state.headerb));
1635
0
  }
1636
44.1k
  curlx_dyn_reset(&data->state.headerb);
1637
1638
44.1k
  if(status)
1639
12.1k
    return status;
1640
1641
32.0k
  if(!premature && /* this check is pointless when DONE is called before the
1642
                      entire operation is complete */
1643
23.6k
     !conn->bits.retry &&
1644
23.2k
     !data->set.connect_only &&
1645
22.9k
     (data->req.bytecount +
1646
22.9k
      data->req.headerbytecount -
1647
22.9k
      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
51
    failf(data, "Empty reply from server");
1652
    /* Mark it as closed to avoid the "left intact" message */
1653
51
    streamclose(conn);
1654
51
    return CURLE_GOT_NOTHING;
1655
51
  }
1656
1657
31.9k
  return CURLE_OK;
1658
32.0k
}
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
0
{
1663
0
  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
0
  if(data->state.http_neg.rcvd_min == 10)
1667
0
    return FALSE;
1668
  /* We have seen a previous response on *this* connection with 1.0. */
1669
0
  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
0
  if(data->state.http_neg.only_10 &&
1674
0
     (!conn || conn->httpversion_seen <= 10))
1675
0
    return FALSE;
1676
  /* We are not restricted to use 1.0 only. */
1677
0
  return !data->state.http_neg.only_10;
1678
0
}
1679
1680
static unsigned char http_request_version(struct Curl_easy *data)
1681
42.6k
{
1682
42.6k
  unsigned char v = Curl_conn_http_version(data, data->conn);
1683
42.6k
  if(!v) {
1684
    /* No specific HTTP connection filter installed. */
1685
0
    v = http_may_use_1_1(data) ? 11 : 10;
1686
0
  }
1687
42.6k
  return v;
1688
42.6k
}
1689
1690
static const char *get_http_string(int httpversion)
1691
42.6k
{
1692
42.6k
  switch(httpversion) {
1693
0
  case 30:
1694
0
    return "3";
1695
42.6k
  case 20:
1696
42.6k
    return "2";
1697
0
  case 11:
1698
0
    return "1.1";
1699
0
  default:
1700
0
    return "1.0";
1701
42.6k
  }
1702
42.6k
}
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
27.1k
{
1715
27.1k
  struct http_custom_ctx *x = user_data;
1716
27.1k
  CURLcode result;
1717
1718
27.1k
  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
25.5k
     curlx_str_casecompare2(name, STRCONST("Host")))
1723
1.32k
    goto out;
1724
25.7k
  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
25.7k
  else if(x->data->state.httpreq == HTTPREQ_POST_MIME &&
1729
          /* this header is sent later */
1730
8.27k
          curlx_str_casecompare2(name, STRCONST("Content-Type")))
1731
85
    goto out;
1732
25.6k
  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
1.09k
          curlx_str_casecompare2(name, STRCONST("Content-Length")))
1736
0
    goto out;
1737
25.6k
  else if(curlx_str_casecompare2(name, STRCONST("Connection")))
1738
    /* Connection headers are handled specially */
1739
378
    goto out;
1740
25.3k
  else if((x->httpversion >= 20) &&
1741
25.3k
          curlx_str_casecompare2(name, STRCONST("Transfer-Encoding")))
1742
    /* HTTP/2 does not support chunked requests */
1743
0
    goto out;
1744
25.3k
  else if((curlx_str_casecompare2(name, STRCONST("Authorization")) ||
1745
25.3k
           curlx_str_casecompare2(name, STRCONST("Cookie"))) &&
1746
          /* be careful of sending this potentially sensitive header to
1747
             other hosts */
1748
1.12k
          !Curl_auth_allowed_to_host(x->data))
1749
0
    goto out;
1750
1751
25.3k
  if(!curlx_strlen(value)) {
1752
8.52k
    result = curlx_dyn_addn(x->req, curlx_str(name), curlx_strlen(name));
1753
8.52k
    if(!result)
1754
8.51k
      result = curlx_dyn_addn(x->req, STRCONST(":\r\n"));
1755
8.52k
  }
1756
16.7k
  else
1757
16.7k
    result = curlx_dyn_addf(x->req, "%s\r\n", orig_data);
1758
1759
25.3k
  if(result)
1760
2
    return result;
1761
1762
27.0k
out:
1763
27.0k
  return CURLE_AGAIN;
1764
25.3k
}
1765
1766
CURLcode Curl_add_custom_headers(struct Curl_easy *data,
1767
                                 int httpversion,
1768
                                 struct dynbuf *req)
1769
42.6k
{
1770
42.6k
  struct curl_slist *h[2];
1771
42.6k
  struct http_custom_ctx custom_ctx;
1772
42.6k
  int numlists = 0;
1773
42.6k
  int i;
1774
1775
42.6k
  h[numlists++] = data->set.headers;
1776
42.6k
#ifndef CURL_DISABLE_PROXY
1777
42.6k
  if(data->conn->bits.origin_is_proxy &&
1778
0
     data->set.sep_headers)
1779
0
      h[numlists++] = data->set.proxyheaders;
1780
42.6k
#endif
1781
1782
  /* loop through one or two lists */
1783
42.6k
  custom_ctx.data = data;
1784
42.6k
  custom_ctx.req = req;
1785
42.6k
  custom_ctx.httpversion = httpversion;
1786
85.3k
  for(i = 0; i < numlists; i++) {
1787
42.6k
    CURLcode result =
1788
42.6k
      Curl_dynhds_add_slist(NULL, h[i], http_custom_treat, &custom_ctx);
1789
42.6k
    if(result)
1790
2
      return result;
1791
42.6k
  }
1792
1793
42.6k
  return CURLE_OK;
1794
42.6k
}
1795
1796
#ifndef CURL_DISABLE_PARSEDATE
1797
CURLcode Curl_add_timecondition(struct Curl_easy *data,
1798
                                struct dynbuf *req)
1799
42.6k
{
1800
42.6k
  const struct tm *tm;
1801
42.6k
  struct tm keeptime;
1802
42.6k
  CURLcode result;
1803
42.6k
  char datestr[80];
1804
42.6k
  const char *condp;
1805
42.6k
  size_t len;
1806
1807
42.6k
  if(data->set.timecondition == CURL_TIMECOND_NONE)
1808
    /* no condition was asked for */
1809
41.9k
    return CURLE_OK;
1810
1811
687
  result = curlx_gmtime(data->set.timevalue, &keeptime);
1812
687
  if(result) {
1813
2
    failf(data, "Invalid TIMEVALUE");
1814
2
    return result;
1815
2
  }
1816
685
  tm = &keeptime;
1817
1818
685
  switch(data->set.timecondition) {
1819
0
  default:
1820
0
    DEBUGF(infof(data, "invalid time condition"));
1821
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1822
1823
407
  case CURL_TIMECOND_IFMODSINCE:
1824
407
    condp = "If-Modified-Since";
1825
407
    len = 17;
1826
407
    break;
1827
149
  case CURL_TIMECOND_IFUNMODSINCE:
1828
149
    condp = "If-Unmodified-Since";
1829
149
    len = 19;
1830
149
    break;
1831
129
  case CURL_TIMECOND_LASTMOD:
1832
129
    condp = "Last-Modified";
1833
129
    len = 13;
1834
129
    break;
1835
685
  }
1836
1837
685
  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
685
  curl_msnprintf(datestr, sizeof(datestr),
1851
685
                 "%s: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
1852
685
                 condp,
1853
685
                 Curl_wkday[tm->tm_wday ? tm->tm_wday - 1 : 6],
1854
685
                 tm->tm_mday,
1855
685
                 Curl_month[tm->tm_mon],
1856
685
                 tm->tm_year + 1900,
1857
685
                 tm->tm_hour,
1858
685
                 tm->tm_min,
1859
685
                 tm->tm_sec);
1860
1861
685
  result = curlx_dyn_add(req, datestr);
1862
685
  return result;
1863
685
}
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
45.6k
{
1878
45.6k
  Curl_HttpReq httpreq = (Curl_HttpReq)data->state.httpreq;
1879
45.6k
  const char *request;
1880
45.6k
#ifndef CURL_DISABLE_WEBSOCKETS
1881
45.6k
  if(data->conn->scheme->protocol & (CURLPROTO_WS | CURLPROTO_WSS))
1882
0
    httpreq = HTTPREQ_GET;
1883
45.6k
  else
1884
45.6k
#endif
1885
45.6k
  if((data->conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_FTP)) &&
1886
45.6k
     data->state.upload)
1887
907
    httpreq = HTTPREQ_PUT;
1888
1889
  /* Now set the 'request' pointer to the proper request string */
1890
45.6k
  if(!data->state.http_ignorecustom &&
1891
45.3k
     CURL_EASY_STR(data, STRING_CUSTOMREQUEST)) {
1892
874
    request = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
1893
874
  }
1894
44.7k
  else {
1895
44.7k
    if(data->req.no_body)
1896
4.92k
      request = "HEAD";
1897
39.8k
    else {
1898
39.8k
      DEBUGASSERT((httpreq >= HTTPREQ_GET) && (httpreq <= HTTPREQ_HEAD));
1899
39.8k
      switch(httpreq) {
1900
1.43k
      case HTTPREQ_POST:
1901
1.43k
      case HTTPREQ_POST_FORM:
1902
9.74k
      case HTTPREQ_POST_MIME:
1903
9.74k
        request = "POST";
1904
9.74k
        break;
1905
903
      case HTTPREQ_PUT:
1906
903
        request = "PUT";
1907
903
        break;
1908
0
      default: /* this should never happen */
1909
29.1k
      case HTTPREQ_GET:
1910
29.1k
        request = "GET";
1911
29.1k
        break;
1912
0
      case HTTPREQ_HEAD:
1913
0
        request = "HEAD";
1914
0
        break;
1915
39.8k
      }
1916
39.8k
    }
1917
44.7k
  }
1918
45.6k
  *method = request;
1919
45.6k
  *reqp = httpreq;
1920
45.6k
}
1921
1922
static CURLcode http_set_aptr_host(struct Curl_easy *data)
1923
42.8k
{
1924
42.8k
  struct connectdata *conn = data->conn;
1925
42.8k
  const char *ptr = NULL;
1926
1927
42.8k
  curlx_safefree(data->state.http_host);
1928
42.8k
#ifndef CURL_DISABLE_COOKIES
1929
42.8k
  curlx_safefree(data->req.cookiehost);
1930
42.8k
#endif
1931
1932
42.8k
  if(Curl_peer_equal(data->state.initial_origin, data->state.origin))
1933
42.8k
    ptr = Curl_checkheaders(data, STRCONST("Host"));
1934
1935
42.8k
  if(ptr) {
1936
1.52k
#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.52k
    char *cookiehost;
1943
1.52k
    CURLcode result = copy_custom_value(ptr, &cookiehost);
1944
1.52k
    if(result)
1945
0
      return result;
1946
1.52k
    if(!*cookiehost)
1947
      /* ignore empty data */
1948
486
      curlx_free(cookiehost);
1949
1.04k
    else {
1950
      /* If the host begins with '[', we start searching for the port after
1951
         the bracket has been closed */
1952
1.04k
      if(*cookiehost == '[') {
1953
259
        char *closingbracket;
1954
        /* since the 'cookiehost' is an allocated memory area that will be
1955
           freed later we cannot increment the pointer */
1956
259
        memmove(cookiehost, cookiehost + 1, strlen(cookiehost) - 1);
1957
259
        closingbracket = strchr(cookiehost, ']');
1958
259
        if(closingbracket)
1959
58
          *closingbracket = 0;
1960
259
      }
1961
783
      else {
1962
783
        int startsearch = 0;
1963
783
        char *colon = strchr(cookiehost + startsearch, ':');
1964
783
        if(colon)
1965
132
          *colon = 0; /* The host must not include an embedded port number */
1966
783
      }
1967
1.04k
      data->req.cookiehost = cookiehost;
1968
1.04k
    }
1969
1.52k
#endif
1970
1971
1.52k
    if(!curl_strequal("Host:", ptr)) {
1972
1.05k
      data->state.http_host = curl_maprintf("Host:%s", &ptr[5]);
1973
1.05k
      if(!data->state.http_host)
1974
1
        return CURLE_OUT_OF_MEMORY;
1975
1.05k
    }
1976
1.52k
  }
1977
41.3k
  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
41.3k
    bool ipv6 = (bool)data->state.origin->ipv6;
1985
41.3k
    struct dynbuf tmp;
1986
41.3k
    size_t hlen;
1987
41.3k
    CURLcode result;
1988
1989
41.3k
    curlx_dyn_init(&tmp, DYN_HTTP_REQUEST);
1990
41.3k
    result = curlx_dyn_addn(&tmp, STRCONST("Host: "));
1991
41.3k
    if(!result && ipv6)
1992
0
      result = curlx_dyn_addn(&tmp, STRCONST("["));
1993
41.3k
    if(!result)
1994
41.3k
      result = curlx_dyn_add(&tmp, data->state.origin->hostname);
1995
41.3k
    if(!result && ipv6)
1996
0
      result = curlx_dyn_addn(&tmp, STRCONST("]"));
1997
41.3k
    if(!result &&
1998
41.3k
       ((data->state.origin->port != data->state.origin->scheme->defport) ||
1999
41.3k
       (data->state.origin->scheme->family != conn->scheme->family))) {
2000
0
      result = curlx_dyn_addf(&tmp, ":%u", data->state.origin->port);
2001
0
    }
2002
2003
41.3k
    data->state.http_host = result ? NULL : curlx_dyn_take(&tmp, &hlen);
2004
41.3k
    curlx_dyn_free(&tmp);
2005
41.3k
    return result;
2006
41.3k
  }
2007
1.52k
  return CURLE_OK;
2008
42.8k
}
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
42.6k
{
2016
42.6k
  CURLcode result = CURLE_OK;
2017
42.6k
  const char *path = data->state.up.path;
2018
42.6k
  const char *query = data->state.up.query;
2019
42.6k
#ifndef CURL_DISABLE_PROXY
2020
42.6k
  struct connectdata *conn = data->conn;
2021
42.6k
#endif
2022
2023
42.6k
  if(CURL_EASY_STR(data, STRING_TARGET)) {
2024
3.11k
    path = CURL_EASY_STR(data, STRING_TARGET);
2025
3.11k
    query = NULL;
2026
3.11k
  }
2027
2028
42.6k
#ifndef CURL_DISABLE_PROXY
2029
42.6k
  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
42.6k
  else
2123
42.6k
#endif
2124
42.6k
  {
2125
42.6k
    result = curlx_dyn_add(r, path);
2126
42.6k
    if(result)
2127
0
      return result;
2128
42.6k
    if(query)
2129
861
      result = curlx_dyn_addf(r, "?%s", query);
2130
42.6k
  }
2131
2132
42.6k
  return result;
2133
42.6k
}
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
7.09k
{
2138
7.09k
  CURLcode result;
2139
2140
7.09k
  switch(httpreq) {
2141
0
#ifndef CURL_DISABLE_MIME
2142
7.09k
  case HTTPREQ_POST_MIME:
2143
7.09k
    data->state.mimepost = data->set.a.mimepostp;
2144
7.09k
    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
7.09k
  }
2169
2170
7.09k
  switch(httpreq) {
2171
0
  case HTTPREQ_POST_FORM:
2172
7.09k
  case HTTPREQ_POST_MIME:
2173
    /* This is form posting using mime data. */
2174
7.09k
#ifndef CURL_DISABLE_MIME
2175
7.09k
    if(data->state.mimepost) {
2176
7.09k
      const char *cthdr = Curl_checkheaders(data, STRCONST("Content-Type"));
2177
2178
      /* Read and seek body only. */
2179
7.09k
      data->state.mimepost->flags |= MIME_BODY_ONLY;
2180
2181
      /* Prepare the mime structure headers & set content type. */
2182
2183
7.09k
      if(cthdr)
2184
325
        for(cthdr += 13; *cthdr == ' '; cthdr++)
2185
247
          ;
2186
7.01k
      else if(data->state.mimepost->kind == MIMEKIND_MULTIPART)
2187
7.01k
        cthdr = "multipart/form-data";
2188
2189
7.09k
      curl_mime_headers(data->state.mimepost, data->set.headers, 0);
2190
7.09k
      result = Curl_mime_prepare_headers(data, data->state.mimepost, cthdr,
2191
7.09k
                                         NULL, MIMESTRATEGY_FORM);
2192
7.09k
      curl_mime_headers(data->state.mimepost, NULL, 0);
2193
7.09k
      if(result)
2194
11
        return result;
2195
7.08k
      result = Curl_creader_set_mime(data, data->state.mimepost);
2196
7.08k
      if(result)
2197
3
        return result;
2198
7.08k
    }
2199
0
    else
2200
0
#endif
2201
0
    {
2202
0
      result = Curl_creader_set_null(data);
2203
0
    }
2204
7.07k
    data->state.infilesize = Curl_creader_total_length(data);
2205
7.07k
    return result;
2206
2207
0
  default:
2208
0
    return Curl_creader_set_null(data);
2209
7.09k
  }
2210
  /* never reached */
2211
7.09k
}
2212
#endif
2213
2214
static CURLcode set_reader(struct Curl_easy *data, Curl_HttpReq httpreq)
2215
42.7k
{
2216
42.7k
  CURLcode result = CURLE_OK;
2217
42.7k
  curl_off_t postsize = data->state.infilesize;
2218
2219
42.7k
  DEBUGASSERT(data->conn);
2220
2221
42.7k
  if(data->req.authneg) {
2222
1.20k
    return Curl_creader_set_null(data);
2223
1.20k
  }
2224
2225
41.5k
  switch(httpreq) {
2226
849
  case HTTPREQ_PUT: /* Let's PUT the data to the server! */
2227
849
    return postsize ? Curl_creader_set_fread(data, postsize) :
2228
849
      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
7.09k
  case HTTPREQ_POST_MIME:
2233
7.09k
    return set_post_reader(data, httpreq);
2234
0
#endif
2235
2236
1.31k
  case HTTPREQ_POST:
2237
    /* this is the simple POST, using x-www-form-urlencoded style */
2238
    /* the size of the post body */
2239
1.31k
    if(!postsize) {
2240
153
      result = Curl_creader_set_null(data);
2241
153
    }
2242
1.16k
    else if(data->set.postfields) {
2243
537
      size_t plen = curlx_sotouz_range(postsize, 0, SIZE_MAX);
2244
537
      if(plen == SIZE_MAX)
2245
0
        return CURLE_OUT_OF_MEMORY;
2246
537
      else if(plen)
2247
537
        result = Curl_creader_set_buf(data, data->set.postfields, plen);
2248
0
      else
2249
0
        result = Curl_creader_set_null(data);
2250
537
    }
2251
623
    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
623
      bool chunked = FALSE;
2257
623
      char *ptr = Curl_checkheaders(data, STRCONST("Transfer-Encoding"));
2258
623
      if(ptr) {
2259
        /* Some kind of TE is requested, check if 'chunked' is chosen */
2260
0
        chunked = Curl_compareheader(ptr, STRCONST("Transfer-Encoding:"),
2261
0
                                     STRCONST("chunked"));
2262
0
      }
2263
623
      result = Curl_creader_set_fread(data, chunked ? -1 : postsize);
2264
623
    }
2265
1.31k
    return result;
2266
2267
32.3k
  default:
2268
    /* HTTP GET/HEAD download, has no body, needs no Content-Length */
2269
32.3k
    data->state.infilesize = 0;
2270
32.3k
    return Curl_creader_set_null(data);
2271
41.5k
  }
2272
  /* not reached */
2273
41.5k
}
2274
2275
static CURLcode http_resume(struct Curl_easy *data, Curl_HttpReq httpreq)
2276
42.7k
{
2277
42.7k
  if((HTTPREQ_POST == httpreq || HTTPREQ_POST_FORM == httpreq ||
2278
41.3k
      HTTPREQ_POST_MIME == httpreq || HTTPREQ_PUT == httpreq) &&
2279
10.4k
     data->state.resume_from) {
2280
61
    failf(data, "HTTP upload cannot be resumed");
2281
61
    return CURLE_BAD_FUNCTION_ARGUMENT;
2282
61
  }
2283
42.6k
  return CURLE_OK;
2284
42.7k
}
2285
2286
static CURLcode http_req_set_TE(struct Curl_easy *data,
2287
                                struct dynbuf *req,
2288
                                int httpversion)
2289
42.6k
{
2290
42.6k
  CURLcode result = CURLE_OK;
2291
42.6k
  const char *ptr;
2292
2293
42.6k
  ptr = Curl_checkheaders(data, STRCONST("Transfer-Encoding"));
2294
42.6k
  if(ptr) {
2295
    /* Some kind of TE is requested, check if 'chunked' is chosen */
2296
0
    data->req.upload_chunky =
2297
0
      Curl_compareheader(ptr,
2298
0
                         STRCONST("Transfer-Encoding:"), STRCONST("chunked"));
2299
0
    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
0
  }
2305
42.6k
  else {
2306
42.6k
    curl_off_t req_clen = Curl_creader_total_length(data);
2307
2308
42.6k
    if(req_clen < 0) {
2309
      /* indeterminate request content length */
2310
2.28k
      if(httpversion > 10) {
2311
        /* On HTTP/1.1, enable chunked, on HTTP/2 and later we do not
2312
         * need it */
2313
2.28k
        data->req.upload_chunky = (httpversion < 20);
2314
2.28k
      }
2315
0
      else {
2316
0
        failf(data, "Chunky upload is not supported by HTTP 1.0");
2317
0
        return CURLE_UPLOAD_FAILED;
2318
0
      }
2319
2.28k
    }
2320
40.3k
    else {
2321
      /* else, no chunky upload */
2322
40.3k
      data->req.upload_chunky = FALSE;
2323
40.3k
    }
2324
2325
42.6k
    if(data->req.upload_chunky)
2326
0
      result = curlx_dyn_add(req, "Transfer-Encoding: chunked\r\n");
2327
42.6k
  }
2328
42.6k
  return result;
2329
42.6k
}
2330
2331
static CURLcode addexpect(struct Curl_easy *data, struct dynbuf *r,
2332
                          int httpversion, bool *announced_exp100)
2333
10.3k
{
2334
10.3k
  CURLcode result;
2335
10.3k
  char *ptr;
2336
2337
10.3k
  *announced_exp100 = FALSE;
2338
  /* Avoid Expect: 100-continue if Upgrade: is used */
2339
10.3k
  if(data->req.upgr101 != UPGR101_NONE)
2340
0
    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
10.3k
  ptr = Curl_checkheaders(data, STRCONST("Expect"));
2347
10.3k
  if(ptr) {
2348
271
    *announced_exp100 =
2349
271
      Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
2350
271
  }
2351
10.1k
  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
0
    curl_off_t client_len = Curl_creader_client_length(data);
2356
0
    if(client_len > EXPECT_100_THRESHOLD || client_len < 0) {
2357
0
      result = curlx_dyn_addn(r, STRCONST("Expect: 100-continue\r\n"));
2358
0
      if(result)
2359
0
        return result;
2360
0
      *announced_exp100 = TRUE;
2361
0
    }
2362
0
  }
2363
10.3k
  return CURLE_OK;
2364
10.3k
}
2365
2366
static CURLcode http_add_content_hds(struct Curl_easy *data,
2367
                                     struct dynbuf *r,
2368
                                     int httpversion,
2369
                                     Curl_HttpReq httpreq)
2370
42.6k
{
2371
42.6k
  CURLcode result = CURLE_OK;
2372
42.6k
  curl_off_t req_clen;
2373
42.6k
  bool announced_exp100 = FALSE;
2374
2375
42.6k
  DEBUGASSERT(data->conn);
2376
42.6k
  if(data->req.upload_chunky) {
2377
0
    result = Curl_httpchunk_add_reader(data);
2378
0
    if(result)
2379
0
      return result;
2380
0
  }
2381
2382
  /* Get the request body length that has been set up */
2383
42.6k
  req_clen = Curl_creader_total_length(data);
2384
42.6k
  switch(httpreq) {
2385
869
  case HTTPREQ_PUT:
2386
2.22k
  case HTTPREQ_POST:
2387
2.22k
#if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API)
2388
2.22k
  case HTTPREQ_POST_FORM:
2389
10.3k
  case HTTPREQ_POST_MIME:
2390
10.3k
#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
10.3k
    if(req_clen >= 0 && !data->req.upload_chunky &&
2398
8.09k
       (data->req.authneg ||
2399
8.09k
        !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
8.09k
      result = curlx_dyn_addf(r, "Content-Length: %" FMT_OFF_T "\r\n",
2403
8.09k
                              req_clen);
2404
8.09k
    }
2405
10.3k
    if(result)
2406
1
      goto out;
2407
2408
10.3k
#ifndef CURL_DISABLE_MIME
2409
    /* Output mime-generated headers. */
2410
10.3k
    if(data->state.mimepost &&
2411
7.01k
       ((httpreq == HTTPREQ_POST_FORM) || (httpreq == HTTPREQ_POST_MIME))) {
2412
7.01k
      struct curl_slist *hdr;
2413
2414
14.0k
      for(hdr = data->state.mimepost->curlheaders; hdr; hdr = hdr->next) {
2415
7.01k
        result = curlx_dyn_addf(r, "%s\r\n", hdr->data);
2416
7.01k
        if(result)
2417
1
          goto out;
2418
7.01k
      }
2419
7.01k
    }
2420
10.3k
#endif
2421
10.3k
    if(httpreq == HTTPREQ_POST &&
2422
1.35k
       !Curl_checkheaders(data, STRCONST("Content-Type"))) {
2423
1.32k
      result = curlx_dyn_addn(r, STRCONST("Content-Type: application/"
2424
1.32k
                                          "x-www-form-urlencoded\r\n"));
2425
1.32k
      if(result)
2426
3
        goto out;
2427
1.32k
    }
2428
10.3k
    result = addexpect(data, r, httpversion, &announced_exp100);
2429
10.3k
    if(result)
2430
0
      goto out;
2431
10.3k
    break;
2432
32.2k
  default:
2433
32.2k
    break;
2434
42.6k
  }
2435
2436
42.6k
  Curl_pgrsSetUploadSize(data, req_clen);
2437
42.6k
  if(announced_exp100)
2438
0
    result = http_exp100_add_reader(data);
2439
2440
42.6k
out:
2441
42.6k
  return result;
2442
42.6k
}
2443
2444
#ifndef CURL_DISABLE_COOKIES
2445
2446
static CURLcode http_cookies(struct Curl_easy *data,
2447
                             struct dynbuf *r)
2448
42.6k
{
2449
42.6k
  CURLcode result = CURLE_OK;
2450
42.6k
  const char *addcookies = NULL;
2451
42.6k
  bool linecap = FALSE;
2452
42.6k
  if(CURL_EASY_STR(data, STRING_COOKIE) &&
2453
653
     !Curl_checkheaders(data, STRCONST("Cookie")) &&
2454
619
     Curl_auth_allowed_to_host(data))
2455
619
    addcookies = CURL_EASY_STR(data, STRING_COOKIE);
2456
2457
42.6k
  if(data->cookies || addcookies) {
2458
42.6k
    struct Curl_llist list;
2459
42.6k
    int count = 0;
2460
2461
42.6k
    if(data->cookies && data->state.cookie_engine) {
2462
42.6k
      bool okay;
2463
42.6k
      const char *host = data->req.cookiehost ?
2464
41.5k
        data->req.cookiehost : data->state.origin->hostname;
2465
42.6k
      Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
2466
42.6k
      result = Curl_cookie_getlist(data, &okay, host, &list);
2467
42.6k
      if(!result && okay) {
2468
1.47k
        struct Curl_llist_node *n;
2469
1.47k
        size_t clen = 8; /* hold the size of the generated Cookie: header */
2470
2471
        /* loop through all cookies that matched */
2472
5.66k
        for(n = Curl_llist_head(&list); n; n = Curl_node_next(n)) {
2473
4.19k
          struct Cookie *co = Curl_node_elem(n);
2474
4.19k
          if(co->value) {
2475
4.19k
            size_t add;
2476
4.19k
            if(!count) {
2477
913
              result = curlx_dyn_addn(r, STRCONST("Cookie: "));
2478
913
              if(result)
2479
0
                break;
2480
913
            }
2481
4.19k
            add = strlen(co->name) + strlen(co->value) + 1;
2482
4.19k
            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
4.19k
            result = curlx_dyn_addf(r, "%s%s=%s", count ? "; " : "",
2489
4.19k
                                    co->name, co->value);
2490
4.19k
            if(result)
2491
0
              break;
2492
4.19k
            clen += add + (count ? 2 : 0);
2493
4.19k
            count++;
2494
4.19k
          }
2495
4.19k
        }
2496
1.47k
        Curl_llist_destroy(&list, NULL);
2497
1.47k
      }
2498
42.6k
      Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
2499
42.6k
    }
2500
42.6k
    if(addcookies && !result && !linecap) {
2501
619
      if(!count)
2502
552
        result = curlx_dyn_addn(r, STRCONST("Cookie: "));
2503
619
      if(!result) {
2504
618
        result = curlx_dyn_addf(r, "%s%s", count ? "; " : "", addcookies);
2505
618
        count++;
2506
618
      }
2507
619
    }
2508
42.6k
    if(count && !result)
2509
1.46k
      result = curlx_dyn_addn(r, STRCONST("\r\n"));
2510
2511
42.6k
    if(result)
2512
2
      return result;
2513
42.6k
  }
2514
42.6k
  return result;
2515
42.6k
}
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
42.6k
{
2523
42.6k
  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.15k
    if(((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD)) &&
2530
860
       !Curl_checkheaders(data, STRCONST("Range"))) {
2531
      /* if a line like this was already allocated, free the previous one */
2532
860
      curlx_free(data->state.rangeline);
2533
860
      data->state.rangeline = curl_maprintf("Range: bytes=%s\r\n",
2534
860
                                                 data->state.range);
2535
860
      if(!data->state.rangeline)
2536
0
        return CURLE_OUT_OF_MEMORY;
2537
860
    }
2538
295
    else if((httpreq == HTTPREQ_POST || httpreq == HTTPREQ_PUT) &&
2539
202
            !Curl_checkheaders(data, STRCONST("Content-Range"))) {
2540
202
      curl_off_t req_clen = Curl_creader_total_length(data);
2541
      /* if a line like this was already allocated, free the previous one */
2542
202
      curlx_free(data->state.rangeline);
2543
2544
202
      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
202
      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
202
      else {
2566
        /* Range was selected and then we pass the incoming range and append
2567
           total size */
2568
202
        data->state.rangeline =
2569
202
          curl_maprintf("Content-Range: bytes %s/%" FMT_OFF_T "\r\n",
2570
202
                        data->state.range, req_clen);
2571
202
      }
2572
202
      if(!data->state.rangeline)
2573
0
        return CURLE_OUT_OF_MEMORY;
2574
202
    }
2575
1.15k
  }
2576
42.6k
  return CURLE_OK;
2577
42.6k
}
2578
2579
static CURLcode http_firstwrite(struct Curl_easy *data)
2580
24.0k
{
2581
24.0k
  struct connectdata *conn = data->conn;
2582
24.0k
  struct SingleRequest *k = &data->req;
2583
2584
24.0k
  if(data->req.newurl) {
2585
22.0k
    if(conn->bits.close) {
2586
      /* Abort after the headers if "follow Location" is set
2587
         and we are set to close anyway. */
2588
24
      CURL_REQ_CLEAR_RECV(data);
2589
24
      k->done = TRUE;
2590
24
      return CURLE_OK;
2591
24
    }
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
21.9k
    k->ignorebody = TRUE;
2595
21.9k
    infof(data, "Ignoring the response-body");
2596
21.9k
  }
2597
23.9k
  if(data->state.resume_from && !k->content_range &&
2598
405
     (data->state.httpreq == HTTPREQ_GET) &&
2599
236
     !k->ignorebody) {
2600
2601
112
    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
3
      infof(data, "The entire document is already downloaded");
2605
3
      streamclose(conn);
2606
      /* Abort download */
2607
3
      CURL_REQ_CLEAR_RECV(data);
2608
3
      k->done = TRUE;
2609
3
      return CURLE_OK;
2610
3
    }
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
109
    failf(data, "HTTP server does not seem to support "
2616
109
          "byte ranges. Cannot resume.");
2617
109
    return CURLE_RANGE_ERROR;
2618
112
  }
2619
2620
23.8k
  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
528
     !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
23.8k
  return CURLE_OK;
2637
23.8k
}
2638
2639
static CURLcode http_add_connection_hd(struct Curl_easy *data,
2640
                                       struct dynbuf *req)
2641
42.6k
{
2642
42.6k
  struct curl_slist *head;
2643
42.6k
  const char *sep = "Connection: ";
2644
42.6k
  CURLcode result = CURLE_OK;
2645
42.6k
  size_t rlen = curlx_dyn_len(req);
2646
42.6k
  bool skip;
2647
2648
  /* Add the 1st custom "Connection: " header, if there is one */
2649
97.3k
  for(head = data->set.headers; head; head = head->next) {
2650
54.8k
    if(curl_strnequal(head->data, "Connection", 10) &&
2651
938
       Curl_headersep(head->data[10]) &&
2652
734
       !http_header_is_empty(head->data)) {
2653
214
      char *value;
2654
214
      result = copy_custom_value(head->data, &value);
2655
214
      if(result)
2656
0
        return result;
2657
214
      result = curlx_dyn_addf(req, "%s%s", sep, value);
2658
214
      sep = ", ";
2659
214
      curlx_free(value);
2660
214
      break; /* leave, having added 1st one */
2661
214
    }
2662
54.8k
  }
2663
2664
  /* add our internal Connection: header values, if we have any */
2665
42.6k
  if(!result && data->state.http_hd_te) {
2666
329
    result = curlx_dyn_addf(req, "%s%s", sep, "TE");
2667
329
    sep = ", ";
2668
329
  }
2669
42.6k
  if(!result && data->state.http_hd_upgrade) {
2670
0
    result = curlx_dyn_addf(req, "%s%s", sep, "Upgrade");
2671
0
    sep = ", ";
2672
0
  }
2673
42.6k
  if(!result && data->state.http_hd_h2_settings) {
2674
0
    result = curlx_dyn_addf(req, "%s%s", sep, "HTTP2-Settings");
2675
0
  }
2676
42.6k
  if(!result && (rlen < curlx_dyn_len(req)))
2677
543
    result = curlx_dyn_addn(req, STRCONST("\r\n"));
2678
42.6k
  if(result)
2679
0
    return result;
2680
2681
  /* Add all user-defined Connection: headers after the first */
2682
42.6k
  skip = TRUE;
2683
98.0k
  for(head = data->set.headers; head; head = head->next) {
2684
55.4k
    if(curl_strnequal(head->data, "Connection", 10) &&
2685
1.40k
       Curl_headersep(head->data[10]) &&
2686
1.19k
       !http_header_is_empty(head->data)) {
2687
678
      if(skip) {
2688
214
        skip = FALSE;
2689
214
        continue;
2690
214
      }
2691
464
      result = curlx_dyn_addf(req, "%s\r\n", head->data);
2692
464
      if(result)
2693
0
        return result;
2694
464
    }
2695
55.4k
  }
2696
2697
42.6k
  return CURLE_OK;
2698
42.6k
}
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
853k
{
2738
853k
  struct connectdata *conn = data->conn;
2739
853k
  CURLcode result = CURLE_OK;
2740
2741
853k
  switch(id) {
2742
42.6k
  case H1_HD_REQUEST:
2743
    /* add the main request stuff */
2744
    /* GET/HEAD/POST/PUT */
2745
42.6k
    result = curlx_dyn_addf(req, "%s ", method);
2746
42.6k
    if(!result)
2747
42.6k
      result = http_target(data, req);
2748
42.6k
    if(!result)
2749
42.6k
      result = curlx_dyn_addf(req, " HTTP/%s\r\n",
2750
42.6k
                              get_http_string(httpversion));
2751
42.6k
    break;
2752
2753
42.6k
  case H1_HD_HOST:
2754
42.6k
    if(data->state.http_host) {
2755
42.2k
      result = curlx_dyn_add(req, data->state.http_host);
2756
42.2k
      if(!result)
2757
42.2k
        result = curlx_dyn_addn(req, STRCONST("\r\n"));
2758
42.2k
    }
2759
42.6k
    break;
2760
2761
0
#ifndef CURL_DISABLE_PROXY
2762
42.6k
  case H1_HD_PROXY_AUTH:
2763
42.6k
    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
42.6k
    break;
2766
0
#endif
2767
2768
42.6k
  case H1_HD_AUTH:
2769
42.6k
    if(data->req.hd_auth && !is_connect)
2770
3.43k
      result = curlx_dyn_add(req, data->req.hd_auth);
2771
42.6k
    break;
2772
2773
42.6k
  case H1_HD_RANGE:
2774
42.6k
    if(data->state.use_range && data->state.rangeline)
2775
1.06k
      result = curlx_dyn_add(req, data->state.rangeline);
2776
42.6k
    break;
2777
2778
42.6k
  case H1_HD_USER_AGENT: {
2779
42.6k
    const char *ua = CURL_EASY_STR(data, STRING_USERAGENT);
2780
42.6k
    if(ua && *ua && !Curl_checkheaders(data, STRCONST("User-Agent")))
2781
243
      result = curlx_dyn_addf(req, "User-Agent: %s\r\n", ua);
2782
42.6k
    break;
2783
0
  }
2784
2785
42.6k
  case H1_HD_ACCEPT:
2786
42.6k
    if(!Curl_checkheaders(data, STRCONST("Accept")))
2787
42.5k
      result = curlx_dyn_add(req, "Accept: */*\r\n");
2788
42.6k
    break;
2789
2790
42.6k
  case H1_HD_TE:
2791
42.6k
#ifdef HAVE_LIBZ
2792
42.6k
    if(!Curl_checkheaders(data, STRCONST("TE")) &&
2793
42.2k
       data->set.http_transfer_encoding) {
2794
331
      data->state.http_hd_te = TRUE;
2795
331
      result = curlx_dyn_add(req, "TE: gzip\r\n");
2796
331
    }
2797
42.6k
#endif
2798
42.6k
    break;
2799
2800
42.6k
  case H1_HD_ACCEPT_ENCODING: {
2801
42.6k
    const char *enc = CURL_EASY_STR(data, STRING_ENCODING);
2802
42.6k
    if(enc && !Curl_checkheaders(data, STRCONST("Accept-Encoding")))
2803
614
      result = curlx_dyn_addf(req, "Accept-Encoding: %s\r\n", enc);
2804
42.6k
    break;
2805
0
  }
2806
2807
42.6k
  case H1_HD_REFERER:
2808
42.6k
    if(Curl_bufref_ptr(&data->state.referer) &&
2809
2.75k
       !Curl_checkheaders(data, STRCONST("Referer")))
2810
2.75k
      result = curlx_dyn_addf(req, "Referer: %s\r\n",
2811
2.75k
                              Curl_bufref_ptr(&data->state.referer));
2812
42.6k
    break;
2813
2814
0
#ifndef CURL_DISABLE_PROXY
2815
42.6k
  case H1_HD_PROXY_CONNECTION:
2816
42.6k
    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
42.6k
    break;
2821
0
#endif
2822
2823
42.6k
  case H1_HD_TRANSFER_ENCODING:
2824
42.6k
    result = http_req_set_TE(data, req, httpversion);
2825
42.6k
    break;
2826
2827
0
#ifndef CURL_DISABLE_ALTSVC
2828
42.6k
  case H1_HD_ALT_USED:
2829
42.6k
    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
42.6k
    break;
2834
0
#endif
2835
2836
42.6k
  case H1_HD_UPGRADE:
2837
42.6k
    if(!Curl_conn_is_ssl(conn) && (httpversion < 20) &&
2838
0
       (data->state.http_neg.wanted & CURL_HTTP_V2x) &&
2839
0
       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
0
      result = Curl_http2_request_upgrade(req, data);
2843
0
    }
2844
42.6k
#ifndef CURL_DISABLE_WEBSOCKETS
2845
42.6k
    if(!result && conn->scheme->protocol & (CURLPROTO_WS | CURLPROTO_WSS))
2846
0
      result = Curl_ws_request(data, req);
2847
42.6k
#endif
2848
42.6k
    break;
2849
2850
42.6k
  case H1_HD_COOKIES:
2851
42.6k
    result = http_cookies(data, req);
2852
42.6k
    break;
2853
2854
42.6k
  case H1_HD_CONDITIONALS:
2855
42.6k
    result = Curl_add_timecondition(data, req);
2856
42.6k
    break;
2857
2858
42.6k
  case H1_HD_CUSTOM:
2859
42.6k
    result = Curl_add_custom_headers(data, httpversion, req);
2860
42.6k
    break;
2861
2862
42.6k
  case H1_HD_CONTENT:
2863
42.6k
    result = http_add_content_hds(data, req, httpversion, httpreq);
2864
42.6k
    break;
2865
2866
42.6k
  case H1_HD_CONNECTION: {
2867
42.6k
    result = http_add_connection_hd(data, req);
2868
42.6k
    break;
2869
0
  }
2870
2871
42.6k
  case H1_HD_LAST:
2872
42.6k
    result = curlx_dyn_addn(req, STRCONST("\r\n"));
2873
42.6k
    break;
2874
853k
  }
2875
853k
  return result;
2876
853k
}
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
42.8k
{
2885
42.8k
  CURLcode result = CURLE_OK;
2886
42.8k
  Curl_HttpReq httpreq;
2887
42.8k
  const char *method;
2888
42.8k
  struct dynbuf req;
2889
42.8k
  unsigned char httpversion;
2890
42.8k
  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
42.8k
  *done = TRUE;
2896
  /* initialize a dynamic send-buffer */
2897
42.8k
  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
42.8k
  curlx_dyn_reset(&data->state.headerb);
2901
42.8k
  data->state.maybe_folded = FALSE;
2902
42.8k
  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
42.8k
  result = Curl_headers_init(data);
2908
42.8k
  if(result)
2909
7
    goto out;
2910
2911
42.8k
  data->state.http_hd_te = FALSE;
2912
42.8k
  data->state.http_hd_upgrade = FALSE;
2913
42.8k
  data->state.http_hd_h2_settings = FALSE;
2914
2915
  /* what kind of request do we need to send? */
2916
42.8k
  Curl_http_method(data, &method, &httpreq);
2917
2918
  /* select host to send */
2919
42.8k
  result = http_set_aptr_host(data);
2920
  /* setup the authentication headers, how that method and host are known */
2921
42.8k
  if(!result)
2922
42.8k
    result = Curl_http_output_auth(data, data->conn, httpreq, method,
2923
42.8k
                                   data->state.up.path,
2924
42.8k
                                   data->state.up.query, FALSE);
2925
  /* Setup input reader, resume information and ranges */
2926
42.8k
  if(!result)
2927
42.7k
    result = set_reader(data, httpreq);
2928
42.8k
  if(!result)
2929
42.7k
    result = http_resume(data, httpreq);
2930
42.8k
  if(!result)
2931
42.6k
    result = http_range(data, httpreq);
2932
42.8k
  if(result)
2933
180
    goto out;
2934
2935
42.6k
  httpversion = http_request_version(data);
2936
  /* Add request line and all headers to `req` */
2937
896k
  for(hd_id = 0; hd_id <= H1_HD_LAST; ++hd_id) {
2938
853k
    result = http_add_hd(data, &req, (http_hd_t)hd_id,
2939
853k
                         httpversion, method, FALSE, httpreq);
2940
853k
    if(result)
2941
25
      goto out;
2942
853k
  }
2943
2944
  /* setup variables for the upcoming transfer and send */
2945
42.6k
  Curl_xfer_setup_sendrecv(data, -1);
2946
42.6k
  result = Curl_req_send(data, &req, httpversion);
2947
2948
42.6k
  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
42.8k
out:
2955
42.8k
  if(result == CURLE_TOO_LARGE)
2956
2
    failf(data, "HTTP request too large");
2957
2958
42.8k
  curlx_dyn_free(&req);
2959
42.8k
  return result;
2960
42.6k
}
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
140
{
2972
140
  size_t ch = CURLMIN(plen, blen);
2973
140
  return curl_strnequal(prefix, buffer, ch);
2974
140
}
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
140
{
2984
140
  statusline rc = STATUS_BAD;
2985
140
  statusline onmatch = len >= 5 ? STATUS_DONE : STATUS_UNKNOWN;
2986
140
  size_t i;
2987
2988
140
  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
140
  if((rc != STATUS_DONE) && checkprefixmax(STRCONST("HTTP/"), s, len))
2997
0
    rc = onmatch;
2998
2999
140
  return rc;
3000
140
}
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
0
{
3020
0
#ifndef CURL_DISABLE_RTSP
3021
0
  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
0
  return checkhttpprefix(data, s, len);
3028
0
}
3029
3030
/* HTTP header has field name `n` (a string constant) */
3031
#define HD_IS(hd, hdlen, n) \
3032
49.7k
  (((hdlen) >= (sizeof(n) - 1)) && curl_strnequal(n, hd, sizeof(n) - 1))
3033
3034
#define HD_VAL(hd, hdlen, n) \
3035
71.5k
  ((((hdlen) >= (sizeof(n) - 1)) && (hd) && \
3036
71.5k
    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
0
  (HD_IS(hd, hdlen, n) && \
3042
0
   ((hdlen) > ((sizeof(n) - 1) + (sizeof(v) - 1))) && \
3043
0
   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
1.70k
{
3051
1.70k
#ifndef CURL_DISABLE_ALTSVC
3052
1.70k
  const char *v;
3053
1.70k
  v = (data->asi &&
3054
1.37k
       (Curl_xfer_is_secure(data) ||
3055
1.37k
#ifdef DEBUGBUILD
3056
        /* allow debug builds to circumvent the HTTPS restriction */
3057
1.37k
        getenv("CURL_ALTSVC_HTTP")
3058
#else
3059
        0
3060
#endif
3061
1.70k
         )) ? HD_VAL(hd, hdlen, "Alt-Svc:") : NULL;
3062
1.70k
  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
1.70k
  return CURLE_OK;
3075
1.70k
}
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
12.1k
{
3083
12.1k
  struct connectdata *conn = data->conn;
3084
12.1k
  struct SingleRequest *k = &data->req;
3085
12.1k
  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
12.1k
  v = (!k->http_bodyless && !data->set.ignorecl && !k->ignore_cl) ?
3093
12.1k
    HD_VAL(hd, hdlen, "Content-Length:") : NULL;
3094
12.1k
  if(v) {
3095
3
    do {
3096
3
      curl_off_t contentlength;
3097
3
      int offt = curlx_str_numblanks(&v, &contentlength);
3098
3099
3
      if(offt == STRE_OVERFLOW) {
3100
        /* out of range */
3101
0
        if(data->set.max_filesize) {
3102
0
          failf(data, "Maximum file size exceeded");
3103
0
          return CURLE_FILESIZE_EXCEEDED;
3104
0
        }
3105
0
        streamclose(conn);
3106
0
        infof(data, "Overflow Content-Length: value");
3107
0
        return CURLE_OK;
3108
0
      }
3109
3
      else {
3110
3
        if((offt == STRE_OK) &&
3111
3
           ((k->size == -1) || /* not set to something before */
3112
3
            (k->size == contentlength))) { /* or the same value */
3113
3114
3
          k->size = contentlength;
3115
3
          curlx_str_passblanks(&v);
3116
3117
          /* on a comma, loop and get the next instead */
3118
3
          if(!curlx_str_single(&v, ','))
3119
0
            continue;
3120
3121
3
          if(!curlx_str_newline(&v)) {
3122
3
            k->maxdownload = k->size;
3123
3
            return CURLE_OK;
3124
3
          }
3125
3
        }
3126
        /* negative, different value or rubbish - bad HTTP */
3127
0
        failf(data, "Invalid Content-Length: value");
3128
0
        return CURLE_WEIRD_SERVER_REPLY;
3129
3
      }
3130
3
    } while(1);
3131
3
  }
3132
12.1k
  v = (!k->http_bodyless && CURL_EASY_STR(data, STRING_ENCODING)) ?
3133
12.1k
    HD_VAL(hd, hdlen, "Content-Encoding:") : NULL;
3134
12.1k
  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
1.15k
    return Curl_build_unencoding_stack(data, v, FALSE);
3142
1.15k
  }
3143
  /* check for Content-Type: header lines to get the MIME-type */
3144
11.0k
  v = HD_VAL(hd, hdlen, "Content-Type:");
3145
11.0k
  if(v) {
3146
6.64k
    char *contenttype = Curl_copy_header_value(hd);
3147
6.64k
    if(!contenttype)
3148
0
      return CURLE_OUT_OF_MEMORY;
3149
6.64k
    if(!*contenttype)
3150
      /* ignore empty data */
3151
6.42k
      curlx_free(contenttype);
3152
216
    else {
3153
216
      curlx_free(data->info.contenttype);
3154
216
      data->info.contenttype = contenttype;
3155
216
    }
3156
6.64k
    return CURLE_OK;
3157
6.64k
  }
3158
4.38k
  if((k->httpversion < 20) &&
3159
0
     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
0
    connclose(conn);
3167
0
    return CURLE_OK;
3168
0
  }
3169
4.38k
  if((k->httpversion == 10) &&
3170
0
     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
4.38k
  v = !k->http_bodyless ? HD_VAL(hd, hdlen, "Content-Range:") : NULL;
3182
4.38k
  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
2.19k
    const char *ptr = v;
3195
3196
    /* Move forward until first digit or asterisk */
3197
13.3k
    while(*ptr && !ISDIGIT(*ptr) && *ptr != '*')
3198
11.1k
      ptr++;
3199
3200
    /* if it truly stopped on a digit */
3201
2.19k
    if(ISDIGIT(*ptr)) {
3202
724
      if(!curlx_str_number(&ptr, &k->offset, CURL_OFF_T_MAX) &&
3203
503
         (data->state.resume_from == k->offset))
3204
        /* we asked for a resume and we got it */
3205
209
        k->content_range = TRUE;
3206
724
    }
3207
1.46k
    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
1.46k
    else if(k->httpcode < 300)
3222
415
      data->state.resume_from = 0; /* get everything */
3223
2.19k
  }
3224
4.38k
  return CURLE_OK;
3225
4.38k
}
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
25.3k
{
3233
25.3k
  struct connectdata *conn = data->conn;
3234
25.3k
  struct SingleRequest *k = &data->req;
3235
25.3k
  const char *v = (!k->http_bodyless &&
3236
19.7k
                   (data->set.timecondition || data->set.get_filetime)) ?
3237
25.3k
    HD_VAL(hd, hdlen, "Last-Modified:") : NULL;
3238
25.3k
  if(v) {
3239
545
    if(Curl_getdate_capped(v, &k->timeofdoc))
3240
545
      k->timeofdoc = 0;
3241
545
    if(data->set.get_filetime)
3242
215
      data->info.filetime = k->timeofdoc;
3243
545
    return CURLE_OK;
3244
545
  }
3245
24.8k
  if(HD_IS(hd, hdlen, "Location:") && (k->httpcode/100 >= 2)) {
3246
    /* this is the URL that the server advises us to use instead */
3247
23.5k
    char *location = Curl_copy_header_value(hd);
3248
23.5k
    if(!location)
3249
1
      return CURLE_OUT_OF_MEMORY;
3250
23.5k
    if(!*location ||
3251
22.8k
       (data->req.location && !strcmp(data->req.location, location))) {
3252
      /* ignore empty header, or exact repeat of a previous one */
3253
1.02k
      curlx_free(location);
3254
1.02k
      return CURLE_OK;
3255
1.02k
    }
3256
22.5k
    else {
3257
      /* has value and is not an exact repeat */
3258
22.5k
      if(data->req.location) {
3259
111
        failf(data, "Multiple Location headers");
3260
111
        curlx_free(location);
3261
111
        return CURLE_WEIRD_SERVER_REPLY;
3262
111
      }
3263
22.3k
      data->req.location = location;
3264
3265
22.3k
      if(data->set.http_follow_mode && (k->httpcode/100 == 3)) {
3266
21.7k
        CURLcode result;
3267
21.7k
        DEBUGASSERT(!data->req.newurl);
3268
21.7k
        data->req.newurl = curlx_strdup(data->req.location); /* clone */
3269
21.7k
        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
21.7k
        result = http_perhapsrewind(data, conn);
3275
21.7k
        if(result)
3276
0
          return result;
3277
3278
        /* mark the next request as a followed location: */
3279
21.7k
        data->state.this_is_a_follow = TRUE;
3280
21.7k
      }
3281
22.3k
    }
3282
23.5k
  }
3283
23.6k
  return CURLE_OK;
3284
24.8k
}
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
2.77k
{
3292
2.77k
  struct SingleRequest *k = &data->req;
3293
3294
2.77k
#ifndef CURL_DISABLE_PROXY
3295
2.77k
  const char *v = HD_VAL(hd, hdlen, "Proxy-Connection:");
3296
2.77k
  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
2.77k
#endif
3321
2.77k
  if((407 == k->httpcode) && HD_IS(hd, hdlen, "Proxy-authenticate:")) {
3322
18
    char *auth;
3323
18
    CURLcode result;
3324
18
#ifndef CURL_DISABLE_PROXY
3325
18
    if(!data->conn->bits.origin_is_proxy)
3326
      /* this response is not from a forward proxy, not for us */
3327
18
      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
18
  }
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
2.75k
  return CURLE_OK;
3357
2.77k
}
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
23.0k
{
3365
23.0k
  const char *v = HD_VAL(hd, hdlen, "Retry-After:");
3366
23.0k
  if(v) {
3367
    /* Retry-After = HTTP-date / delay-seconds */
3368
22.3k
    curl_off_t retry_after = 0; /* zero for unknown or "now" */
3369
22.3k
    time_t date = 0;
3370
22.3k
    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
22.3k
    if(!Curl_getdate_capped(v, &date)) {
3375
814
      time_t current = time(NULL);
3376
814
      if(date >= current)
3377
        /* convert date to number of seconds into the future */
3378
266
        retry_after = date - current;
3379
814
    }
3380
21.5k
    else
3381
      /* Try it as a decimal number, ignore errors */
3382
21.5k
      (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
22.3k
    if(retry_after > 21600)
3386
2.55k
      retry_after = 21600;
3387
22.3k
    data->info.retry_after = retry_after;
3388
22.3k
  }
3389
23.0k
  return CURLE_OK;
3390
23.0k
}
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
10.4k
{
3398
10.4k
#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_HSTS)
3399
10.4k
  const char *v;
3400
#else
3401
  (void)data;
3402
  (void)hd;
3403
  (void)hdlen;
3404
#endif
3405
3406
10.4k
#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
10.4k
  v = (data->cookies && data->state.cookie_engine &&
3410
10.1k
       (data->req.httpcode/100 >= 2)) ?
3411
10.4k
    HD_VAL(hd, hdlen, "Set-Cookie:") : NULL;
3412
10.4k
  if(v) {
3413
    /* If there is a custom-set Host: name, use it here, or else use
3414
     * real peer hostname. */
3415
7.22k
    const char *host = data->req.cookiehost ?
3416
4.69k
      data->req.cookiehost : data->state.origin->hostname;
3417
7.22k
    const unsigned char secure_context =
3418
7.22k
      Curl_secure_context(data, host, strlen(host)) ? COOKIE_SECURE : 0;
3419
7.22k
    CURLcode result;
3420
7.22k
    Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
3421
7.22k
    result = Curl_cookie_add(data, data->cookies, v, host,
3422
7.22k
                             data->state.up.path,
3423
7.22k
                             COOKIE_HTTPHEADER | secure_context);
3424
7.22k
    Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
3425
7.22k
    return result;
3426
7.22k
  }
3427
3.21k
#endif
3428
3.21k
#ifndef CURL_DISABLE_HSTS
3429
  /* If enabled, the header is incoming and this is over HTTPS */
3430
3.21k
  v = (data->hsts &&
3431
2.78k
       (Curl_xfer_is_secure(data) ||
3432
2.78k
#ifdef DEBUGBUILD
3433
        /* allow debug builds to circumvent the HTTPS restriction */
3434
2.78k
        getenv("CURL_HSTS_HTTP")
3435
#else
3436
        0
3437
#endif
3438
2.78k
         )
3439
3.21k
    ) ? HD_VAL(hd, hdlen, "Strict-Transport-Security:") : NULL;
3440
3.21k
  if(v) {
3441
1.99k
    CURLcode result = Curl_hsts_parse(
3442
1.99k
      data->hsts, data->state.origin->hostname, v);
3443
1.99k
    if(result) {
3444
1.99k
      if(result == CURLE_OUT_OF_MEMORY)
3445
0
        return result;
3446
1.99k
      infof(data, "Illegal STS header skipped");
3447
1.99k
    }
3448
0
#ifdef DEBUGBUILD
3449
0
    else
3450
0
      infof(data, "Parsed STS header fine (%zu entries)",
3451
1.99k
            Curl_llist_count(&data->hsts->list));
3452
1.99k
#endif
3453
1.99k
  }
3454
3.21k
#endif
3455
3456
3.21k
  return CURLE_OK;
3457
3.21k
}
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
1.56k
{
3465
1.56k
  struct connectdata *conn = data->conn;
3466
1.56k
  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
1.56k
  const char *v = (!k->http_bodyless &&
3479
1.03k
                   (data->state.httpreq != HTTPREQ_HEAD) &&
3480
810
                   (k->httpcode != 304)) ?
3481
1.56k
    HD_VAL(hd, hdlen, "Transfer-Encoding:") : NULL;
3482
1.56k
  if(v) {
3483
    /* One or more encodings. We check for chunked and/or a compression
3484
       algorithm. */
3485
0
    CURLcode result = Curl_build_unencoding_stack(data, v, TRUE);
3486
0
    if(result)
3487
0
      return result;
3488
0
    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
0
      CURL_TRC_M(data, "HTTP/1.1 transfer-encoding without chunks");
3493
0
      connclose(conn);
3494
0
      k->ignore_cl = TRUE;
3495
0
    }
3496
0
    return CURLE_OK;
3497
0
  }
3498
1.56k
  v = HD_VAL(hd, hdlen, "Trailer:");
3499
1.56k
  if(v) {
3500
298
    data->req.resp_trailer = TRUE;
3501
298
    return CURLE_OK;
3502
298
  }
3503
1.26k
  return CURLE_OK;
3504
1.56k
}
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
445
{
3512
445
  struct SingleRequest *k = &data->req;
3513
445
  CURLcode result = CURLE_OK;
3514
3515
445
  if((401 == k->httpcode) && HD_IS(hd, hdlen, "WWW-Authenticate:")) {
3516
74
    char *auth = Curl_copy_header_value(hd);
3517
74
    if(!auth)
3518
0
      result = CURLE_OUT_OF_MEMORY;
3519
74
    else {
3520
74
      result = Curl_http_input_auth(data, FALSE, auth);
3521
74
      curlx_free(auth);
3522
74
    }
3523
74
  }
3524
445
  return result;
3525
445
}
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
123k
{
3533
123k
  CURLcode result = CURLE_OK;
3534
3535
123k
  switch(hd[0]) {
3536
1.70k
  case 'a':
3537
1.70k
  case 'A':
3538
1.70k
    result = http_header_a(data, hd, hdlen);
3539
1.70k
    break;
3540
12.1k
  case 'c':
3541
12.1k
  case 'C':
3542
12.1k
    result = http_header_c(data, hd, hdlen);
3543
12.1k
    break;
3544
25.3k
  case 'l':
3545
25.3k
  case 'L':
3546
25.3k
    result = http_header_l(data, hd, hdlen);
3547
25.3k
    break;
3548
2.77k
  case 'p':
3549
2.77k
  case 'P':
3550
2.77k
    result = http_header_p(data, hd, hdlen);
3551
2.77k
    break;
3552
23.0k
  case 'r':
3553
23.0k
  case 'R':
3554
23.0k
    result = http_header_r(data, hd, hdlen);
3555
23.0k
    break;
3556
10.4k
  case 's':
3557
10.4k
  case 'S':
3558
10.4k
    result = http_header_s(data, hd, hdlen);
3559
10.4k
    break;
3560
1.56k
  case 't':
3561
1.56k
  case 'T':
3562
1.56k
    result = http_header_t(data, hd, hdlen);
3563
1.56k
    break;
3564
445
  case 'w':
3565
445
  case 'W':
3566
445
    result = http_header_w(data, hd, hdlen);
3567
445
    break;
3568
123k
  }
3569
3570
123k
  if(!result) {
3571
123k
    struct connectdata *conn = data->conn;
3572
123k
    if(conn->scheme->protocol & CURLPROTO_RTSP)
3573
0
      result = Curl_rtsp_parseheader(data, hd);
3574
123k
  }
3575
123k
  return result;
3576
123k
}
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
28.0k
{
3585
28.0k
  struct SingleRequest *k = &data->req;
3586
3587
28.0k
  switch(k->httpversion) {
3588
0
  case 10:
3589
0
  case 11:
3590
0
#ifdef USE_HTTP2
3591
28.0k
  case 20:
3592
28.0k
#endif
3593
#ifdef USE_HTTP3
3594
  case 30:
3595
#endif
3596
    /* no major version switch mid-connection */
3597
28.0k
    if(k->httpversion_sent &&
3598
27.6k
       (k->httpversion / 10 != k->httpversion_sent / 10)) {
3599
0
      failf(data, "Version mismatch (from HTTP/%d to HTTP/%d)",
3600
0
            k->httpversion_sent / 10, k->httpversion / 10);
3601
0
      return CURLE_WEIRD_SERVER_REPLY;
3602
0
    }
3603
28.0k
    break;
3604
28.0k
  default:
3605
0
    failf(data, "Unsupported HTTP version (%d.%d) in response",
3606
0
          k->httpversion / 10, k->httpversion % 10);
3607
0
    return CURLE_UNSUPPORTED_PROTOCOL;
3608
28.0k
  }
3609
3610
28.0k
  data->info.httpcode = k->httpcode;
3611
28.0k
  data->info.httpversion = k->httpversion;
3612
28.0k
  conn->httpversion_seen = k->httpversion;
3613
3614
28.0k
  if(!data->state.http_neg.rcvd_min ||
3615
19.5k
     data->state.http_neg.rcvd_min > k->httpversion)
3616
    /* store the lowest server version we encounter */
3617
8.44k
    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
28.0k
  if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
3628
317
     k->httpcode == 416) {
3629
    /* "Requested Range Not Satisfiable", proceed and pretend this is no
3630
       error */
3631
5
    k->ignorebody = TRUE; /* Avoid appending error msg to good data. */
3632
5
  }
3633
3634
28.0k
  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
0
    infof(data, "HTTP 1.0, assume close after body");
3639
0
    connclose(conn);
3640
0
  }
3641
3642
28.0k
  k->http_bodyless = k->httpcode >= 100 && k->httpcode < 200;
3643
28.0k
  switch(k->httpcode) {
3644
5.25k
  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
5.25k
    if(data->set.timecondition)
3650
125
      data->info.timecond = TRUE;
3651
5.25k
    FALLTHROUGH();
3652
5.30k
  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
5.30k
    k->size = 0;
3659
5.30k
    k->maxdownload = 0;
3660
5.30k
    k->http_bodyless = TRUE;
3661
5.30k
    break;
3662
22.7k
  default:
3663
22.7k
    break;
3664
28.0k
  }
3665
28.0k
  return CURLE_OK;
3666
28.0k
}
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
23.8k
{
3675
23.8k
  struct SingleRequest *k = &data->req;
3676
23.8k
  if(data->req.ignore_cl || k->chunk) {
3677
0
    k->size = k->maxdownload = -1;
3678
0
  }
3679
23.8k
  else if(k->size != -1) {
3680
5.48k
    if(data->set.max_filesize &&
3681
97
       !k->ignorebody &&
3682
61
       (k->size > data->set.max_filesize)) {
3683
0
      failf(data, "Maximum file size exceeded");
3684
0
      return CURLE_FILESIZE_EXCEEDED;
3685
0
    }
3686
5.48k
    if(k->ignorebody)
3687
5.17k
      infof(data, "setting size while ignoring");
3688
5.48k
    Curl_pgrsSetDownloadSize(data, k->size);
3689
5.48k
    k->maxdownload = k->size;
3690
5.48k
  }
3691
23.8k
  return CURLE_OK;
3692
23.8k
}
3693
3694
CURLcode Curl_verify_header(struct Curl_easy *data,
3695
                            const char *hd, size_t hdlen)
3696
123k
{
3697
123k
  struct SingleRequest *k = &data->req;
3698
123k
  const char *nul = memchr(hd, '\0', hdlen);
3699
123k
  const char *cr = memchr(hd, '\r', nul ? (size_t)(nul - hd) : hdlen);
3700
  /* CR may only precede the LF, nothing else */
3701
123k
  if(cr && ((size_t)(cr - hd) < hdlen - 2)) {
3702
0
    failf(data, "Carriage return found in header");
3703
0
    return CURLE_WEIRD_SERVER_REPLY;
3704
0
  }
3705
123k
  if(nul) {
3706
    /* this is bad, bail out */
3707
0
    failf(data, "NUL byte in header");
3708
0
    return CURLE_WEIRD_SERVER_REPLY;
3709
0
  }
3710
123k
  if(k->headerline < 2)
3711
    /* the first "header" is the status-line and it has no colon */
3712
28.0k
    return CURLE_OK;
3713
95.5k
  if(((hd[0] == ' ') || (hd[0] == '\t')) && k->headerline > 2)
3714
    /* line folding, cannot happen on line 2 */
3715
0
    ;
3716
95.5k
  else if(!memchr(hd, ':', hdlen)) {
3717
    /* this is bad, bail out */
3718
0
    failf(data, "Header without colon");
3719
0
    return CURLE_WEIRD_SERVER_REPLY;
3720
0
  }
3721
95.5k
  return CURLE_OK;
3722
95.5k
}
3723
3724
CURLcode Curl_bump_headersize(struct Curl_easy *data,
3725
                              size_t delta,
3726
                              bool connect_only)
3727
150k
{
3728
150k
  size_t bad = 0;
3729
150k
  unsigned int max = MAX_HTTP_RESP_HEADER_SIZE;
3730
150k
  if(delta < MAX_HTTP_RESP_HEADER_SIZE) {
3731
150k
    data->info.header_size += (unsigned int)delta;
3732
150k
    data->req.allheadercount += (unsigned int)delta;
3733
150k
    if(!connect_only)
3734
150k
      data->req.headerbytecount += (unsigned int)delta;
3735
150k
    if(data->req.allheadercount > max)
3736
4
      bad = data->req.allheadercount;
3737
150k
    else if(data->info.header_size > (max * 20)) {
3738
0
      bad = data->info.header_size;
3739
0
      max *= 20;
3740
0
    }
3741
150k
  }
3742
0
  else
3743
0
    bad = data->req.allheadercount + delta;
3744
150k
  if(bad) {
3745
4
    failf(data, "Too large response headers: %zu > %u", bad, max);
3746
4
    return CURLE_RECV_ERROR;
3747
4
  }
3748
150k
  return CURLE_OK;
3749
150k
}
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
0
{
3760
0
  struct connectdata *conn = data->conn;
3761
0
  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
0
  CURLcode result;
3769
0
  int upgr101_requested = k->upgr101;
3770
0
#endif
3771
3772
0
  if(k->httpversion_sent != 11) {
3773
    /* invalid for other HTTP versions */
3774
0
    failf(data, "server sent 101 response while not talking HTTP/1.1");
3775
0
    return CURLE_WEIRD_SERVER_REPLY;
3776
0
  }
3777
3778
  /* Whatever the success, upgrade was selected. */
3779
0
  k->upgr101 = UPGR101_RECEIVED;
3780
0
  conn->bits.upgrade_in_progress = FALSE;
3781
0
  *conn_changed = TRUE;
3782
3783
  /* To be fully compliant, we would check the "Upgrade:" response header to
3784
   * mention the protocol we requested. */
3785
0
#ifdef USE_NGHTTP2
3786
0
  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
0
    infof(data, "Received 101, Switching to HTTP/2");
3790
0
    result = Curl_http2_upgrade(data, buf, blen);
3791
0
    if(!result)
3792
0
      *pconsumed += blen;
3793
0
    return result;
3794
0
  }
3795
0
#endif
3796
0
#ifndef CURL_DISABLE_WEBSOCKETS
3797
0
  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
0
#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
0
  k->header = FALSE;
3811
0
  return CURLE_OK;
3812
0
}
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
2.40k
{
3823
2.40k
  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
2.40k
  k->header = TRUE;
3828
2.40k
  k->headerline = 0; /* restart the header line counter */
3829
3830
2.40k
  switch(k->httpcode) {
3831
149
  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
149
    http_exp100_got100(data);
3835
149
    break;
3836
0
  case 101:
3837
0
    return http_on_101_upgrade(data, buf, blen, pconsumed, conn_changed);
3838
2.25k
  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
2.25k
    break;
3843
2.40k
  }
3844
2.40k
  return CURLE_OK;
3845
2.40k
}
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
22.8k
{
3885
22.8k
  struct connectdata *conn = data->conn;
3886
22.8k
  struct SingleRequest *k = &data->req;
3887
22.8k
  CURLcode result = CURLE_OK;
3888
3889
22.8k
  if(!data->req.authneg && !conn->bits.close &&
3890
21.9k
     !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
16.8k
    switch(data->state.httpreq) {
3903
67
    case HTTPREQ_PUT:
3904
201
    case HTTPREQ_POST:
3905
201
    case HTTPREQ_POST_FORM:
3906
370
    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
370
      if(!Curl_req_done_sending(data)) {
3912
106
        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
106
        else if(data->set.http_keep_sending_on_error) {
3933
49
          infof(data, "HTTP error before end of send, keep sending");
3934
49
          http_exp100_send_anyway(data);
3935
49
        }
3936
57
        else {
3937
57
          infof(data, "HTTP error before end of send, stop sending");
3938
57
          streamclose(conn);
3939
57
          result = Curl_req_abort_sending(data);
3940
57
          if(result)
3941
0
            return result;
3942
57
        }
3943
106
      }
3944
370
      break;
3945
3946
16.4k
    default: /* default label present to avoid compiler warnings */
3947
16.4k
      break;
3948
16.8k
    }
3949
16.8k
  }
3950
3951
22.8k
  if(Curl_creader_will_rewind(data) && !Curl_req_done_sending(data)) {
3952
    /* We rewind before next send, continue sending now */
3953
426
    infof(data, "Keep sending data to get tossed away");
3954
426
    CURL_REQ_SET_SEND(data);
3955
426
  }
3956
22.8k
  return result;
3957
22.8k
}
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
26.4k
{
3964
26.4k
  struct connectdata *conn = data->conn;
3965
26.4k
  CURLcode result = CURLE_OK;
3966
26.4k
  struct SingleRequest *k = &data->req;
3967
26.4k
  bool conn_changed = FALSE;
3968
3969
26.4k
  (void)buf; /* not used without HTTP2 enabled */
3970
26.4k
  *pconsumed = 0;
3971
3972
26.4k
  if(k->upgr101 == UPGR101_RECEIVED) {
3973
    /* supposedly upgraded to http2 now */
3974
0
    if(data->req.httpversion != 20)
3975
0
      infof(data, "Lying server, not serving HTTP/2");
3976
0
  }
3977
3978
26.4k
  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
2.40k
    result = http_write_header(data, last_hd, last_hd_len);
3982
2.40k
    last_hd = NULL; /* handled it */
3983
2.40k
    if(result)
3984
1
      goto out;
3985
2.40k
  }
3986
3987
26.4k
  if(k->httpcode < 100) {
3988
0
    failf(data, "Unsupported response code in HTTP response");
3989
0
    result = CURLE_UNSUPPORTED_PROTOCOL;
3990
0
    goto out;
3991
0
  }
3992
26.4k
  else if(k->httpcode < 200) {
3993
2.40k
    result = http_on_1xx_response(data, buf, blen, pconsumed, &conn_changed);
3994
2.40k
    goto out;
3995
2.40k
  }
3996
3997
  /* k->httpcode >= 200, final response */
3998
24.0k
  k->header = FALSE;
3999
24.0k
  if(conn->bits.upgrade_in_progress) {
4000
    /* Asked for protocol upgrade, but it was not selected */
4001
0
    conn->bits.upgrade_in_progress = FALSE;
4002
0
    conn_changed = TRUE;
4003
0
  }
4004
4005
24.0k
  if((k->size == -1) && !k->chunk && !conn->bits.close &&
4006
18.5k
     (k->httpversion == 11) &&
4007
0
     !(conn->scheme->protocol & CURLPROTO_RTSP) &&
4008
0
     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
0
    infof(data, "no chunk, no close, no size. Assume close to signal end");
4014
0
    streamclose(conn);
4015
0
  }
4016
4017
24.0k
  http_check_auth_closure(data, conn);
4018
4019
24.0k
#ifndef CURL_DISABLE_WEBSOCKETS
4020
  /* All >=200 HTTP status codes are errors when wanting ws */
4021
24.0k
  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
24.0k
#endif
4027
4028
  /* Check if this response means the transfer errored. */
4029
24.0k
  if(http_should_fail(data, data->req.httpcode)) {
4030
49
    failf(data, "The requested URL returned error: %d",
4031
49
          k->httpcode);
4032
49
    result = CURLE_HTTP_RETURNED_ERROR;
4033
49
    goto out;
4034
49
  }
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
24.0k
  result = Curl_http_auth_act(data);
4040
24.0k
  if(result)
4041
4
    goto out;
4042
4043
24.0k
  if(k->httpcode >= 300) {
4044
22.8k
    result = http_handle_send_error(data);
4045
22.8k
    if(result)
4046
0
      goto out;
4047
22.8k
  }
4048
4049
  /* final response without error, prepare to receive the body */
4050
24.0k
  result = http_firstwrite(data);
4051
24.0k
  if(result)
4052
109
    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
23.8k
  result = http_size(data);
4057
23.8k
  if(result)
4058
0
    goto out;
4059
4060
23.8k
#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
23.8k
  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
23.8k
#endif
4069
4070
  /* If we requested a "no body", this is a good time to get
4071
   * out and return home.
4072
   */
4073
23.8k
  if(data->req.no_body)
4074
4.58k
    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
23.8k
  if((k->maxdownload == 0) && (k->httpversion_sent < 20))
4081
21
    k->download_done = TRUE;
4082
4083
26.4k
out:
4084
26.4k
  if(last_hd)
4085
    /* if not written yet, write it now */
4086
24.0k
    result = Curl_1st_fatal(result,
4087
24.0k
                            http_write_header(data, last_hd, last_hd_len));
4088
26.4k
  if(conn_changed)
4089
    /* poke the multi handle to allow pending pipewait to retry */
4090
0
    Curl_multi_connchanged(data->multi);
4091
26.4k
  return result;
4092
23.8k
}
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
150k
{
4099
150k
  CURLcode result = CURLE_OK;
4100
150k
  struct SingleRequest *k = &data->req;
4101
150k
  int writetype;
4102
150k
  DEBUGASSERT(!hd[hdlen]); /* null-terminated */
4103
4104
150k
  *pconsumed = 0;
4105
150k
  if((0x0a == *hd) || (0x0d == *hd)) {
4106
    /* Empty header line means end of headers! */
4107
26.4k
    struct dynbuf last_header;
4108
26.4k
    size_t consumed;
4109
4110
26.4k
    if((hdlen != 1 || hd[0] != '\n') &&
4111
26.4k
       (hdlen != 2 || hd[0] != '\r' || hd[1] != '\n')) {
4112
0
      failf(data, "Invalid header termination");
4113
0
      return CURLE_WEIRD_SERVER_REPLY;
4114
0
    }
4115
4116
26.4k
    curlx_dyn_init(&last_header, hdlen + 1);
4117
26.4k
    result = curlx_dyn_addn(&last_header, hd, hdlen);
4118
26.4k
    if(result)
4119
4
      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
26.4k
    curlx_dyn_reset(&data->state.headerb);
4126
26.4k
    result = http_on_response(data, curlx_dyn_ptr(&last_header),
4127
26.4k
                              curlx_dyn_len(&last_header),
4128
26.4k
                              buf_remain, blen, &consumed);
4129
26.4k
    *pconsumed += consumed;
4130
26.4k
    curlx_dyn_free(&last_header);
4131
26.4k
    return result;
4132
26.4k
  }
4133
4134
  /*
4135
   * Checks for special headers coming up.
4136
   */
4137
4138
123k
  writetype = CLIENTWRITE_HEADER;
4139
123k
  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
28.1k
    bool fine_statusline = FALSE;
4143
4144
28.1k
    k->httpversion = 0; /* Do not know yet */
4145
28.1k
    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
28.1k
      const char *p = hd;
4154
4155
28.1k
      curlx_str_passblanks(&p);
4156
28.1k
      if(!strncmp(p, "HTTP/", 5)) {
4157
28.0k
        p += 5;
4158
28.0k
        switch(*p) {
4159
0
        case '1':
4160
0
          p++;
4161
0
          if((p[0] == '.') && (p[1] == '0' || p[1] == '1')) {
4162
0
            if(ISBLANK(p[2])) {
4163
0
              k->httpversion = (unsigned char)(10 + (p[1] - '0'));
4164
0
              p += 3;
4165
0
              if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
4166
0
                k->httpcode = ((p[0] - '0') * 100) + ((p[1] - '0') * 10) +
4167
0
                  (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
0
                fine_statusline = TRUE;
4171
0
              }
4172
0
            }
4173
0
          }
4174
0
          if(!fine_statusline) {
4175
0
            failf(data, "Unsupported HTTP/1 subversion in response");
4176
0
            return CURLE_UNSUPPORTED_PROTOCOL;
4177
0
          }
4178
0
          break;
4179
28.0k
        case '2':
4180
28.0k
        case '3':
4181
28.0k
          if(!ISBLANK(p[1]))
4182
0
            break;
4183
28.0k
          k->httpversion = (unsigned char)((*p - '0') * 10);
4184
28.0k
          p += 2;
4185
28.0k
          if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
4186
28.0k
            k->httpcode = ((p[0] - '0') * 100) + ((p[1] - '0') * 10) +
4187
28.0k
              (p[2] - '0');
4188
28.0k
            p += 3;
4189
28.0k
            if(!ISBLANK(*p))
4190
0
              break;
4191
28.0k
            fine_statusline = TRUE;
4192
28.0k
          }
4193
28.0k
          break;
4194
28.0k
        default: /* unsupported */
4195
0
          failf(data, "Unsupported HTTP version in response");
4196
0
          return CURLE_UNSUPPORTED_PROTOCOL;
4197
28.0k
        }
4198
28.0k
      }
4199
4200
28.1k
      if(!fine_statusline) {
4201
        /* If user has set option HTTP200ALIASES,
4202
           compare header line against list of aliases */
4203
140
        statusline check = checkhttpprefix(data, hd, hdlen);
4204
140
        if(check == STATUS_DONE) {
4205
0
          fine_statusline = TRUE;
4206
0
          k->httpcode = 200;
4207
0
          k->httpversion = 10;
4208
0
        }
4209
140
      }
4210
28.1k
    }
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
28.1k
    if(fine_statusline) {
4232
28.0k
      result = http_statusline(data, data->conn);
4233
28.0k
      if(result)
4234
0
        return result;
4235
28.0k
      writetype |= CLIENTWRITE_STATUS;
4236
28.0k
    }
4237
140
    else {
4238
140
      k->header = FALSE;   /* this is not a header line */
4239
140
      return CURLE_WEIRD_SERVER_REPLY;
4240
140
    }
4241
28.1k
  }
4242
4243
123k
  result = Curl_verify_header(data, hd, hdlen);
4244
123k
  if(result)
4245
0
    return result;
4246
4247
123k
  result = http_header(data, hd, hdlen);
4248
123k
  if(result)
4249
122
    return result;
4250
4251
  /*
4252
   * Taken in one (more) header. Write it to the client.
4253
   */
4254
123k
  Curl_debug(data, CURLINFO_HEADER_IN, hd, hdlen);
4255
4256
123k
  if(k->httpcode / 100 == 1)
4257
7.75k
    writetype |= CLIENTWRITE_1XX;
4258
123k
  result = Curl_client_write(data, writetype, hd, hdlen);
4259
123k
  if(result)
4260
16
    return result;
4261
4262
123k
  result = Curl_bump_headersize(data, hdlen, FALSE);
4263
123k
  if(result)
4264
3
    return result;
4265
4266
123k
  return CURLE_OK;
4267
123k
}
4268
4269
/* remove trailing CRLF then all trailing whitespace */
4270
void Curl_http_to_fold(struct dynbuf *bf)
4271
0
{
4272
0
  size_t len = curlx_dyn_len(bf);
4273
0
  const char *hd = curlx_dyn_ptr(bf);
4274
0
  if(len && (hd[len - 1] == '\n'))
4275
0
    len--;
4276
0
  if(len && (hd[len - 1] == '\r'))
4277
0
    len--;
4278
0
  while(len && ISBLANK(hd[len - 1])) /* strip off trailing whitespace */
4279
0
    len--;
4280
0
  curlx_dyn_setlen(bf, len);
4281
0
}
4282
4283
static void unfold_header(struct Curl_easy *data)
4284
0
{
4285
0
  Curl_http_to_fold(&data->state.headerb);
4286
0
  data->state.leading_unfold = TRUE;
4287
0
}
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
0
{
4296
0
  struct connectdata *conn = data->conn;
4297
0
  CURLcode result = CURLE_OK;
4298
0
  struct SingleRequest *k = &data->req;
4299
0
  const char *end_ptr;
4300
0
  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
0
  if(data->state.maybe_folded && blen) {
4305
0
    if(ISBLANK(buf[0])) {
4306
      /* folded, remove the trailing newlines and append the next header */
4307
0
      unfold_header(data);
4308
0
    }
4309
0
    else {
4310
      /* the header data we hold is a complete header, pass it on */
4311
0
      size_t ignore_this;
4312
0
      result = http_rw_hd(data, curlx_dyn_ptr(&data->state.headerb),
4313
0
                          curlx_dyn_len(&data->state.headerb),
4314
0
                          NULL, 0, &ignore_this);
4315
0
      curlx_dyn_reset(&data->state.headerb);
4316
0
      if(result)
4317
0
        return result;
4318
0
    }
4319
0
    data->state.maybe_folded = FALSE;
4320
0
  }
4321
4322
  /* header line within buffer loop */
4323
0
  *pconsumed = 0;
4324
0
  while(blen && k->header) {
4325
0
    size_t consumed;
4326
0
    size_t hlen;
4327
0
    const char *hd;
4328
0
    size_t unfold_len = 0;
4329
4330
0
    if(data->state.leading_unfold) {
4331
      /* immediately after an unfold, keep only a single whitespace */
4332
0
      while(blen && ISBLANK(buf[0])) {
4333
0
        buf++;
4334
0
        blen--;
4335
0
        unfold_len++;
4336
0
      }
4337
0
      if(blen) {
4338
        /* insert a single space */
4339
0
        result = curlx_dyn_addn(&data->state.headerb, " ", 1);
4340
0
        if(result)
4341
0
          return result;
4342
0
        data->state.leading_unfold = FALSE; /* done now */
4343
0
      }
4344
0
    }
4345
4346
0
    end_ptr = memchr(buf, '\n', blen);
4347
0
    if(!end_ptr) {
4348
      /* Not a complete header line within buffer, append the data to
4349
         the end of the headerbuff. */
4350
0
      result = curlx_dyn_addn(&data->state.headerb, buf, blen);
4351
0
      if(result)
4352
0
        return result;
4353
0
      *pconsumed += blen + unfold_len;
4354
4355
0
      if(!k->headerline) {
4356
        /* check if this looks like a protocol header */
4357
0
        statusline st =
4358
0
          checkprotoprefix(data, conn,
4359
0
                           curlx_dyn_ptr(&data->state.headerb),
4360
0
                           curlx_dyn_len(&data->state.headerb));
4361
4362
0
        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
0
          k->header = FALSE;
4366
0
          streamclose(conn);
4367
0
          if((k->httpversion >= 10) || conn->bits.reuse) {
4368
0
            failf(data, "Invalid status line");
4369
0
            return CURLE_WEIRD_SERVER_REPLY;
4370
0
          }
4371
0
          if(!data->state.http_neg.accept_09) {
4372
0
            failf(data, "Received HTTP/0.9 when not allowed");
4373
0
            return CURLE_UNSUPPORTED_PROTOCOL;
4374
0
          }
4375
0
          leftover_body = TRUE;
4376
0
          goto out;
4377
0
        }
4378
0
      }
4379
0
      goto out; /* read more and try again */
4380
0
    }
4381
4382
    /* the size of the remaining header line */
4383
0
    consumed = (end_ptr - buf) + 1;
4384
4385
0
    result = curlx_dyn_addn(&data->state.headerb, buf, consumed);
4386
0
    if(result)
4387
0
      return result;
4388
0
    blen -= consumed;
4389
0
    buf += consumed;
4390
0
    *pconsumed += consumed + unfold_len;
4391
4392
    /****
4393
     * We now have a FULL header line in 'headerb'.
4394
     *****/
4395
4396
0
    hlen = curlx_dyn_len(&data->state.headerb);
4397
0
    hd = curlx_dyn_ptr(&data->state.headerb);
4398
4399
0
    if(!k->headerline) {
4400
      /* the first read "header", the status line */
4401
0
      statusline st = checkprotoprefix(data, conn, hd, hlen);
4402
0
      if(st == STATUS_BAD) {
4403
0
        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
0
        if((k->httpversion >= 10) || conn->bits.reuse) {
4407
0
          failf(data, "Invalid status line");
4408
0
          return CURLE_WEIRD_SERVER_REPLY;
4409
0
        }
4410
0
        if(!data->state.http_neg.accept_09) {
4411
0
          failf(data, "Received HTTP/0.9 when not allowed");
4412
0
          return CURLE_UNSUPPORTED_PROTOCOL;
4413
0
        }
4414
0
        k->header = FALSE;
4415
0
        leftover_body = TRUE;
4416
0
        goto out;
4417
0
      }
4418
0
    }
4419
0
    else {
4420
0
      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
0
        if(blen && ISBLANK(buf[0])) {
4425
          /* remove the trailing CRLF and append the next header */
4426
0
          unfold_header(data);
4427
0
          continue;
4428
0
        }
4429
0
        else if(!blen) {
4430
          /* this might be a folded header so deal with it in next invoke */
4431
0
          data->state.maybe_folded = TRUE;
4432
0
          break;
4433
0
        }
4434
0
      }
4435
0
    }
4436
4437
0
    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
0
    curlx_dyn_reset(&data->state.headerb);
4442
0
    if(consumed) {
4443
0
      blen -= consumed;
4444
0
      buf += consumed;
4445
0
      *pconsumed += consumed;
4446
0
    }
4447
0
    if(result)
4448
0
      return result;
4449
0
  }
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
0
out:
4455
0
  if(!k->header && !leftover_body) {
4456
0
    curlx_dyn_free(&data->state.headerb);
4457
0
  }
4458
0
  return CURLE_OK;
4459
0
}
4460
4461
CURLcode Curl_http_write_resp_hd(struct Curl_easy *data,
4462
                                 const char *hd, size_t hdlen,
4463
                                 bool is_eos)
4464
150k
{
4465
150k
  CURLcode result;
4466
150k
  size_t consumed;
4467
150k
  char tmp = 0;
4468
150k
  DEBUGASSERT(!hd[hdlen]); /* null-terminated */
4469
4470
150k
  result = http_rw_hd(data, hd, hdlen, &tmp, 0, &consumed);
4471
150k
  if(!result && is_eos) {
4472
0
    result = Curl_client_write(data, (CLIENTWRITE_BODY | CLIENTWRITE_EOS),
4473
0
                               &tmp, 0);
4474
0
  }
4475
150k
  return result;
4476
150k
}
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
23.6k
{
4486
23.6k
  if(!data->req.header) {
4487
23.6k
    *pconsumed = 0;
4488
23.6k
    return CURLE_OK;
4489
23.6k
  }
4490
0
  else {
4491
0
    CURLcode result;
4492
4493
0
    result = http_parse_headers(data, buf, blen, pconsumed);
4494
0
    if(!result && !data->req.header) {
4495
0
      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
0
        result = Curl_client_write(data, CLIENTWRITE_BODY,
4500
0
                                   curlx_dyn_ptr(&data->state.headerb),
4501
0
                                   curlx_dyn_len(&data->state.headerb));
4502
0
      }
4503
0
      curlx_dyn_free(&data->state.headerb);
4504
0
    }
4505
0
    return result;
4506
0
  }
4507
23.6k
}
4508
4509
CURLcode Curl_http_write_resp(struct Curl_easy *data,
4510
                              const char *buf, size_t blen,
4511
                              bool is_eos)
4512
23.6k
{
4513
23.6k
  CURLcode result;
4514
23.6k
  size_t consumed;
4515
23.6k
  int flags;
4516
4517
23.6k
  result = Curl_http_write_resp_hds(data, buf, blen, &consumed);
4518
23.6k
  if(result || data->req.done)
4519
11
    goto out;
4520
4521
23.6k
  DEBUGASSERT(consumed <= blen);
4522
23.6k
  blen -= consumed;
4523
23.6k
  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
23.6k
  DEBUGASSERT(!blen || !data->req.header);
4527
23.6k
  if(!data->req.header && (blen || is_eos)) {
4528
    /* BODY data after header been parsed, write and consume */
4529
23.6k
    flags = CLIENTWRITE_BODY;
4530
23.6k
    if(is_eos)
4531
22.8k
      flags |= CLIENTWRITE_EOS;
4532
23.6k
    result = Curl_client_write(data, flags, buf, blen);
4533
23.6k
  }
4534
23.6k
out:
4535
23.6k
  return result;
4536
23.6k
}
4537
4538
/* Decode HTTP status code string. */
4539
CURLcode Curl_http_decode_status(int32_t *pstatus, const char *s, size_t len)
4540
28.0k
{
4541
28.0k
  CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
4542
28.0k
  int32_t status = 0;
4543
28.0k
  int i;
4544
4545
28.0k
  if(len != 3)
4546
0
    goto out;
4547
4548
112k
  for(i = 0; i < 3; ++i) {
4549
84.0k
    char c = s[i];
4550
4551
84.0k
    if(c < '0' || c > '9')
4552
0
      goto out;
4553
4554
84.0k
    status *= 10;
4555
84.0k
    status += c - '0';
4556
84.0k
  }
4557
28.0k
  result = CURLE_OK;
4558
28.0k
out:
4559
28.0k
  *pstatus = result ? -1 : status;
4560
28.0k
  return result;
4561
28.0k
}
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
39.9k
{
4569
39.9k
  struct httpreq *req;
4570
39.9k
  CURLcode result = CURLE_OUT_OF_MEMORY;
4571
4572
39.9k
  DEBUGASSERT(method && m_len);
4573
4574
39.9k
  req = curlx_calloc(1, sizeof(*req) + m_len);
4575
39.9k
  if(!req)
4576
3
    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
39.9k
  memcpy(req->method, method, m_len);
4585
#if defined(__GNUC__) && __GNUC__ >= 13
4586
#pragma GCC diagnostic pop
4587
#endif
4588
39.9k
  if(scheme) {
4589
0
    req->scheme = curlx_memdup0(scheme, s_len);
4590
0
    if(!req->scheme)
4591
0
      goto out;
4592
0
  }
4593
39.9k
  if(authority) {
4594
235
    req->authority = curlx_memdup0(authority, a_len);
4595
235
    if(!req->authority)
4596
0
      goto out;
4597
235
  }
4598
39.9k
  if(path) {
4599
39.7k
    req->path = curlx_memdup0(path, p_len);
4600
39.7k
    if(!req->path)
4601
1
      goto out;
4602
39.7k
  }
4603
39.9k
  Curl_dynhds_init(&req->headers, 0, DYN_HTTP_REQUEST);
4604
39.9k
  Curl_dynhds_init(&req->trailers, 0, DYN_HTTP_REQUEST);
4605
39.9k
  result = CURLE_OK;
4606
4607
39.9k
out:
4608
39.9k
  if(result && req)
4609
1
    Curl_http_req_free(req);
4610
39.9k
  *preq = result ? NULL : req;
4611
39.9k
  return result;
4612
39.9k
}
4613
4614
static CURLcode req_assign_url_authority(struct httpreq *req, CURLU *url)
4615
2.47k
{
4616
2.47k
  char *host, *port;
4617
2.47k
  struct dynbuf buf;
4618
2.47k
  CURLUcode uc;
4619
2.47k
  CURLcode result = CURLE_URL_MALFORMAT;
4620
4621
2.47k
  host = port = NULL;
4622
2.47k
  curlx_dyn_init(&buf, DYN_HTTP_REQUEST);
4623
4624
2.47k
  uc = curl_url_get(url, CURLUPART_HOST, &host, 0);
4625
2.47k
  if(uc && uc != CURLUE_NO_HOST)
4626
1
    goto out;
4627
2.47k
  if(!host) {
4628
298
    req->authority = NULL;
4629
298
    result = CURLE_OK;
4630
298
    goto out;
4631
298
  }
4632
4633
2.17k
  uc = curl_url_get(url, CURLUPART_PORT, &port, CURLU_NO_DEFAULT_PORT);
4634
2.17k
  if(uc && uc != CURLUE_NO_PORT)
4635
0
    goto out;
4636
4637
2.17k
  result = curlx_dyn_add(&buf, host);
4638
2.17k
  if(result)
4639
3
    goto out;
4640
2.17k
  if(port) {
4641
478
    result = curlx_dyn_addf(&buf, ":%s", port);
4642
478
    if(result)
4643
0
      goto out;
4644
478
  }
4645
2.17k
  req->authority = curlx_dyn_ptr(&buf);
4646
2.47k
out:
4647
2.47k
  curlx_free(host);
4648
2.47k
  curlx_free(port);
4649
2.47k
  if(result)
4650
4
    curlx_dyn_free(&buf);
4651
2.47k
  return result;
4652
2.17k
}
4653
4654
static CURLcode req_assign_url_path(struct httpreq *req, CURLU *url)
4655
2.47k
{
4656
2.47k
  char *path, *query;
4657
2.47k
  struct dynbuf buf;
4658
2.47k
  CURLUcode uc;
4659
2.47k
  CURLcode result = CURLE_URL_MALFORMAT;
4660
4661
2.47k
  path = query = NULL;
4662
2.47k
  curlx_dyn_init(&buf, DYN_HTTP_REQUEST);
4663
4664
2.47k
  uc = curl_url_get(url, CURLUPART_PATH, &path, 0);
4665
2.47k
  if(uc)
4666
3
    goto out;
4667
2.47k
  uc = curl_url_get(url, CURLUPART_QUERY, &query, 0);
4668
2.47k
  if(uc && uc != CURLUE_NO_QUERY)
4669
0
    goto out;
4670
4671
2.47k
  if(!query) {
4672
2.14k
    req->path = path;
4673
2.14k
    path = NULL;
4674
2.14k
  }
4675
326
  else {
4676
326
    result = curlx_dyn_add(&buf, path);
4677
326
    if(!result)
4678
325
      result = curlx_dyn_addf(&buf, "?%s", query);
4679
326
    if(result)
4680
1
      goto out;
4681
325
    req->path = curlx_dyn_ptr(&buf);
4682
325
  }
4683
2.46k
  result = CURLE_OK;
4684
4685
2.47k
out:
4686
2.47k
  curlx_free(path);
4687
2.47k
  curlx_free(query);
4688
2.47k
  if(result)
4689
4
    curlx_dyn_free(&buf);
4690
2.47k
  return result;
4691
2.46k
}
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
2.47k
{
4697
2.47k
  struct httpreq *req;
4698
2.47k
  CURLcode result = CURLE_OUT_OF_MEMORY;
4699
2.47k
  CURLUcode uc;
4700
4701
2.47k
  DEBUGASSERT(method && m_len);
4702
4703
2.47k
  req = curlx_calloc(1, sizeof(*req) + m_len);
4704
2.47k
  if(!req)
4705
0
    goto out;
4706
2.47k
  memcpy(req->method, method, m_len);
4707
4708
2.47k
  uc = curl_url_get(url, CURLUPART_SCHEME, &req->scheme, 0);
4709
2.47k
  if(uc && uc != CURLUE_NO_SCHEME)
4710
1
    goto out;
4711
2.47k
  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
2.47k
  result = req_assign_url_authority(req, url);
4718
2.47k
  if(result)
4719
4
    goto out;
4720
2.47k
  result = req_assign_url_path(req, url);
4721
2.47k
  if(result)
4722
4
    goto out;
4723
4724
2.46k
  Curl_dynhds_init(&req->headers, 0, DYN_HTTP_REQUEST);
4725
2.46k
  Curl_dynhds_init(&req->trailers, 0, DYN_HTTP_REQUEST);
4726
2.46k
  result = CURLE_OK;
4727
4728
2.47k
out:
4729
2.47k
  if(result && req)
4730
9
    Curl_http_req_free(req);
4731
2.47k
  *preq = result ? NULL : req;
4732
2.47k
  return result;
4733
2.46k
}
4734
4735
void Curl_http_req_free(struct httpreq *req)
4736
90.7k
{
4737
90.7k
  if(req) {
4738
42.4k
    curlx_free(req->scheme);
4739
42.4k
    curlx_free(req->authority);
4740
42.4k
    curlx_free(req->path);
4741
42.4k
    Curl_dynhds_cleanup(&req->headers);
4742
42.4k
    Curl_dynhds_cleanup(&req->trailers);
4743
42.4k
    curlx_free(req);
4744
42.4k
  }
4745
90.7k
}
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
184k
{
4763
184k
  size_t i;
4764
1.03M
  for(i = 0; i < CURL_ARRAYSIZE(H2_NON_FIELD); ++i) {
4765
896k
    if(e->namelen == H2_NON_FIELD[i].namelen &&
4766
60.3k
       curl_strnequal(H2_NON_FIELD[i].name, e->name, e->namelen))
4767
42.5k
      return FALSE;
4768
896k
  }
4769
142k
  return TRUE;
4770
184k
}
4771
4772
static bool http_TE_has_token(const char *fvalue, const char *token)
4773
1.74k
{
4774
3.14k
  while(*fvalue) {
4775
2.33k
    struct Curl_str name;
4776
4777
    /* skip to first token */
4778
4.93k
    while(ISBLANK(*fvalue) || *fvalue == ',')
4779
2.60k
      fvalue++;
4780
2.33k
    if(curlx_str_cspn(&fvalue, &name, " \t\r;,"))
4781
233
      return FALSE;
4782
2.10k
    if(curlx_str_casecompare(&name, token))
4783
102
      return TRUE;
4784
4785
    /* skip any remainder after token, e.g. parameters with quoted strings */
4786
3.54k
    while(*fvalue && *fvalue != ',') {
4787
2.14k
      if(*fvalue == '"') {
4788
872
        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
872
        if(curlx_str_quotedword(&fvalue, &qw, CURL_MAX_HTTP_HEADER))
4792
597
          return FALSE;
4793
872
      }
4794
1.27k
      else
4795
1.27k
        fvalue++;
4796
2.14k
    }
4797
1.99k
  }
4798
814
  return FALSE;
4799
1.74k
}
4800
4801
CURLcode Curl_http_req_to_h2(struct dynhds *h2_headers,
4802
                             struct httpreq *req, struct Curl_easy *data)
4803
42.3k
{
4804
42.3k
  const char *scheme = NULL, *authority = NULL;
4805
42.3k
  struct dynhds_entry *e;
4806
42.3k
  uint32_t i, nheaders;
4807
42.3k
  CURLcode result;
4808
4809
42.3k
  DEBUGASSERT(req);
4810
42.3k
  DEBUGASSERT(h2_headers);
4811
4812
42.3k
  if(req->scheme) {
4813
2.46k
    scheme = req->scheme;
4814
2.46k
  }
4815
39.9k
  else if(strcmp("CONNECT", req->method)) {
4816
39.8k
    scheme = Curl_checkheaders(data, STRCONST(HTTP_PSEUDO_SCHEME));
4817
39.8k
    if(scheme) {
4818
142
      scheme += sizeof(HTTP_PSEUDO_SCHEME);
4819
142
      curlx_str_passblanks(&scheme);
4820
142
      infof(data, "set pseudo header %s to %s", HTTP_PSEUDO_SCHEME, scheme);
4821
142
    }
4822
39.6k
    else {
4823
39.6k
      scheme = data->state.origin->scheme->name;
4824
39.6k
    }
4825
39.8k
  }
4826
4827
42.3k
  if(req->authority) {
4828
2.40k
    authority = req->authority;
4829
2.40k
  }
4830
39.9k
  else {
4831
39.9k
    e = Curl_dynhds_get(&req->headers, STRCONST("Host"));
4832
39.9k
    if(e)
4833
39.5k
      authority = e->value;
4834
39.9k
  }
4835
4836
42.3k
  Curl_dynhds_reset(h2_headers);
4837
42.3k
  Curl_dynhds_set_opts(h2_headers, DYNHDS_OPT_LOWERCASE);
4838
42.3k
  result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_METHOD),
4839
42.3k
                           req->method, strlen(req->method));
4840
42.3k
  if(!result && scheme) {
4841
42.2k
    result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_SCHEME),
4842
42.2k
                             scheme, strlen(scheme));
4843
42.2k
  }
4844
42.3k
  if(!result && authority) {
4845
41.9k
    result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_AUTHORITY),
4846
41.9k
                             authority, strlen(authority));
4847
41.9k
  }
4848
42.3k
  if(!result && req->path) {
4849
42.1k
    result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_PATH),
4850
42.1k
                             req->path, strlen(req->path));
4851
42.1k
  }
4852
4853
42.3k
  nheaders = Curl_dynhds_count(&req->headers);
4854
228k
  for(i = 0; !result && (i < nheaders); ++i) {
4855
186k
    e = Curl_dynhds_getn(&req->headers, i);
4856
186k
    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
186k
    if(e->namelen == 2 && curl_strequal("TE", e->name)) {
4861
1.74k
      if(http_TE_has_token(e->value, "trailers"))
4862
102
        result = Curl_dynhds_add(h2_headers, e->name, e->namelen,
4863
102
                                 "trailers", CURL_CSTRLEN("trailers"));
4864
1.74k
    }
4865
184k
    else if(h2_permissible_field(e)) {
4866
142k
      result = Curl_dynhds_add(h2_headers, e->name, e->namelen,
4867
142k
                               e->value, e->valuelen);
4868
142k
    }
4869
186k
  }
4870
4871
42.3k
  return result;
4872
42.3k
}
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
43.9k
{
4917
  /* allocate the HTTP-specific struct for the Curl_easy, only to survive
4918
     during this request */
4919
43.9k
  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
43.9k
  return CURLE_OK;
4926
43.9k
}
4927
4928
CURLcode Curl_http_setup_filters(struct Curl_easy *data,
4929
                                 struct Curl_cfilter *cf)
4930
21.5k
{
4931
21.5k
  const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf, data);
4932
21.5k
  CURLcode result;
4933
4934
21.5k
  if(alpn && !strcmp("h3", alpn)) {
4935
0
    CURL_TRC_CF(data, cf, "using HTTP/3 (via ALPN)");
4936
0
  }
4937
21.5k
  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
21.5k
  else {
4947
    /* Check if user wants to use HTTP/2 with clear TCP */
4948
21.5k
    if(Curl_http2_may_switch_at(data, cf)) {
4949
21.5k
      CURL_TRC_CF(data, cf, "adding HTTP/2 filter");
4950
21.5k
      result = Curl_http2_switch_at(data, cf);
4951
21.5k
      if(result)
4952
3
        return result;
4953
21.5k
      CURL_TRC_CF(data, cf, "using HTTP/2 (over clean TCP)");
4954
21.5k
    }
4955
0
    else
4956
0
      CURL_TRC_CF(data, cf, "using HTTP/1.x");
4957
21.5k
  }
4958
21.5k
  return CURLE_OK;
4959
21.5k
}
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 */