Artifactspublic-v0

curl

master
ply4 git calls210 ms331 KiB2 cachedprotocol v2
5024 lines151 KiBRaw

Showing 4000 of 5024 lines.

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"
27#ifndef CURL_DISABLE_HTTP
29#ifdef HAVE_NETINET_IN_H
30#include <netinet/in.h>
31#endif
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
46#ifdef HAVE_SYS_PARAM_H
47#include <sys/param.h>
48#endif
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"
87void Curl_http_neg_init(struct Curl_easy *data, struct http_negotiation *neg)
89 memset(neg, 0, sizeof(*neg));
90 neg->accept_09 = data->set.http09_allowed;
91 switch(data->set.httpwant) {
92 case CURL_HTTP_VERSION_1_0:
93 neg->wanted = neg->allowed = (CURL_HTTP_V1x);
94 neg->only_10 = TRUE;
95 break;
96 case CURL_HTTP_VERSION_1_1:
97 neg->wanted = neg->allowed = (CURL_HTTP_V1x);
98 break;
99 case CURL_HTTP_VERSION_2_0:
100 neg->wanted = neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x);
101 neg->h2_upgrade = TRUE;
102 break;
103 case CURL_HTTP_VERSION_2TLS:
104 neg->wanted = neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x);
105 break;
106 case CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE:
107 neg->wanted = neg->allowed = (CURL_HTTP_V2x);
108 data->state.http_neg.h2_prior_knowledge = TRUE;
109 break;
110 case CURL_HTTP_VERSION_3:
111 neg->wanted = (CURL_HTTP_V1x | CURL_HTTP_V2x | CURL_HTTP_V3x);
112 neg->allowed = neg->wanted;
113 break;
114 case CURL_HTTP_VERSION_3ONLY:
115 neg->wanted = neg->allowed = (CURL_HTTP_V3x);
116 break;
117 case CURL_HTTP_VERSION_NONE:
118 default:
119 neg->wanted = (CURL_HTTP_V1x | CURL_HTTP_V2x);
120 neg->allowed = (CURL_HTTP_V1x | CURL_HTTP_V2x | CURL_HTTP_V3x);
121 break;
122 }
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 */
135char *Curl_checkProxyheaders(struct Curl_easy *data,
136 const struct connectdata *conn,
137 const char *thisheader,
138 const size_t thislen)
140 struct curl_slist *head;
142 for(head = (conn->http_proxy.peer && data->set.sep_headers) ?
143 data->set.proxyheaders : data->set.headers;
144 head; head = head->next) {
145 if(curl_strnequal(head->data, thisheader, thislen) &&
146 Curl_headersep(head->data[thislen]))
147 return head->data;
148 }
150 return NULL;
152#endif
154/* If the header has a value, this function returns TRUE and the value is in
155 'outp' with blanks trimmed off. */
156static bool header_has_value(const char **headerp, struct Curl_str *outp)
158 bool value = !curlx_str_cspn(headerp, outp, ";:") &&
159 (!curlx_str_single(headerp, ':') || !curlx_str_single(headerp, ';'));
161 if(value) {
162 curlx_str_cspn(headerp, outp, "\r\n");
163 curlx_str_trimblanks(outp);
164 }
165 return value;
168static bool http_header_is_empty(const char *header)
170 struct Curl_str out;
172 if(header_has_value(&header, &out)) {
173 return curlx_strlen(&out) == 0;
174 }
175 return TRUE; /* invalid header format, treat as empty */
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 */
186static CURLcode copy_custom_value(const char *header, char **valp)
188 struct Curl_str out = { 0 };
190 /* find the end of the header name */
191 if(header_has_value(&header, &out)) {
192 *valp = curlx_memdup0(curlx_str(&out), curlx_strlen(&out));
193 if(*valp)
194 return CURLE_OK;
195 return CURLE_OUT_OF_MEMORY;
196 }
197 /* bad input */
198 *valp = NULL;
199 return CURLE_BAD_FUNCTION_ARGUMENT;
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 */
212char *Curl_copy_header_value(const char *header)
214 struct Curl_str out;
216 /* find the end of the header name */
217 if(!curlx_str_until(&header, &out, MAX_HTTP_RESP_HEADER_SIZE, ':') &&
218 !curlx_str_single(&header, ':')) {
219 curlx_str_untilnl(&header, &out, MAX_HTTP_RESP_HEADER_SIZE);
220 curlx_str_trimblanks(&out);
221 return curlx_memdup0(curlx_str(&out), curlx_strlen(&out));
222 }
223 /* bad input, should never happen */
224 DEBUGASSERT(0);
225 return NULL;
228#ifndef CURL_DISABLE_HTTP_AUTH
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 */
237static CURLcode http_output_basic(struct Curl_easy *data,
238 struct connectdata *conn, bool proxy)
240 size_t size = 0;
241 char *authorization = NULL;
242 char **p_hd;
243 CURLcode result;
244 struct Curl_creds *creds = NULL;
245 char *out;
247 /* credentials are unique per transfer for HTTP, do not use the ones for the
248 connection */
249 if(proxy) {
250#ifndef CURL_DISABLE_PROXY
251 p_hd = &data->req.hd_proxy_auth;
252 creds = conn->http_proxy.creds;
253#else
254 (void)conn;
255 return CURLE_NOT_BUILT_IN;
256#endif
257 }
258 else {
259 p_hd = &data->req.hd_auth;
260 creds = data->state.creds;
261 }
263 if(!creds) {
264 DEBUGASSERT(0);
265 return CURLE_FAILED_INIT;
266 }
268 out = curl_maprintf("%s:%s", creds->user, creds->passwd);
269 if(!out)
270 return CURLE_OUT_OF_MEMORY;
272 result = curlx_base64_encode((uint8_t *)out, strlen(out),
273 &authorization, &size);
274 if(result)
275 goto fail;
277 if(!authorization) {
278 result = CURLE_REMOTE_ACCESS_DENIED;
279 goto fail;
280 }
282 curlx_free(*p_hd);
283 *p_hd = curl_maprintf("%sAuthorization: Basic %s\r\n",
284 proxy ? "Proxy-" : "",
285 authorization);
286 curlx_free(authorization);
287 if(!*p_hd) {
288 result = CURLE_OUT_OF_MEMORY;
289 goto fail;
290 }
292fail:
293 curlx_free(out);
294 return result;
297#endif
299#ifndef CURL_DISABLE_BEARER_AUTH
300/*
301 * http_output_bearer() sets up an Authorization: header
302 * for HTTP Bearer authentication.
303 *
304 * Returns CURLcode.
305 */
306static CURLcode http_output_bearer(struct Curl_easy *data)
308 char **userp;
309 CURLcode result = CURLE_OK;
311 DEBUGASSERT(Curl_creds_has_oauth_bearer(data->state.creds));
312 userp = &data->req.hd_auth;
313 curlx_free(*userp);
314 *userp = curl_maprintf("Authorization: Bearer %s\r\n",
315 Curl_creds_oauth_bearer(data->state.creds));
317 if(!*userp) {
318 result = CURLE_OUT_OF_MEMORY;
319 goto fail;
320 }
322fail:
323 return result;
325#endif
327#endif
329/* pickoneauth() selects the most favorable authentication method from the
330 * ones available and the ones we want.
331 *
332 * return TRUE if one was picked
333 */
334static bool pickoneauth(struct auth *pick, unsigned long mask,
335 struct Curl_creds *creds)
337 bool have_user_pass = Curl_creds_has_user_or_pass(creds);
338 bool picked;
339 /* only deal with authentication we want */
340 unsigned long avail = pick->avail & pick->want & mask;
341 picked = TRUE;
343 /* The order of these checks is highly relevant, as this will be the order
344 of preference in case of the existence of multiple accepted types. */
345 if(avail & CURLAUTH_NEGOTIATE) /* available on empty creds */
346 pick->picked = CURLAUTH_NEGOTIATE;
347#ifndef CURL_DISABLE_BEARER_AUTH
348 else if((avail & CURLAUTH_BEARER) && Curl_creds_has_oauth_bearer(creds))
349 pick->picked = CURLAUTH_BEARER;
350#endif
351#ifndef CURL_DISABLE_DIGEST_AUTH
352 else if((avail & CURLAUTH_DIGEST) && have_user_pass)
353 pick->picked = CURLAUTH_DIGEST;
354#endif
355#ifndef CURL_DISABLE_BASIC_AUTH
356 else if((avail & CURLAUTH_BASIC) && have_user_pass)
357 pick->picked = CURLAUTH_BASIC;
358#endif
359#ifndef CURL_DISABLE_AWS
360 else if(avail & CURLAUTH_AWS_SIGV4)
361 pick->picked = CURLAUTH_AWS_SIGV4;
362#endif
363#ifndef CURL_DISABLE_HTTPSIG
364 else if(avail & CURLAUTH_HTTPSIG)
365 pick->picked = CURLAUTH_HTTPSIG;
366#endif
367 else {
368 pick->picked = CURLAUTH_PICKNONE; /* we select to use nothing */
369 picked = FALSE;
370 }
371 pick->avail = CURLAUTH_NONE; /* clear it here */
373 return picked;
376/*
377 * http_perhapsrewind()
378 *
379 * The current request needs to be done again - maybe due to a follow
380 * or authentication negotiation. Check if:
381 * 1) a rewind of the data sent to the server is necessary
382 * 2) the current transfer should continue or be stopped early
383 */
384static CURLcode http_perhapsrewind(struct Curl_easy *data,
385 struct connectdata *conn)
387 curl_off_t bytessent = data->req.writebytecount;
388 curl_off_t expectsend = Curl_creader_total_length(data);
389 curl_off_t upload_remain = (expectsend >= 0) ? (expectsend - bytessent) : -1;
390 bool little_upload_remains = (upload_remain >= 0 && upload_remain < 2000);
391 bool needs_rewind = Curl_creader_needs_rewind(data);
392 /* By default, we would like to abort the transfer when little or unknown
393 * amount remains. This may be overridden by authentications further
394 * below! */
395 bool abort_upload = (!data->req.upload_done && !little_upload_remains);
396 VERBOSE(const char *ongoing_auth = NULL);
398 /* We need a rewind before uploading client read data again. The
399 * checks below influence of the upload is to be continued
400 * or aborted early.
401 * This depends on how much remains to be sent and in what state
402 * the authentication is. Some auth schemes such as Negotiate do not work
403 * for a new connection. */
404 if(needs_rewind) {
405 infof(data, "Need to rewind upload for next request");
406 Curl_creader_set_rewind(data, TRUE);
407 }
409 if(conn->bits.close)
410 /* If we already decided to close this connection, we cannot veto. */
411 return CURLE_OK;
413 if(abort_upload) {
414 /* We would like to abort the upload - but should we? */
415#ifdef USE_SPNEGO
416 /* There is still data left to send */
417 if((data->state.authproxy.picked == CURLAUTH_NEGOTIATE) ||
418 (data->state.authhost.picked == CURLAUTH_NEGOTIATE)) {
419 VERBOSE(ongoing_auth = "NEGOTIATE");
420 if((conn->http_negotiate_state != CURL_NEGO_AUTH_NONE) ||
421 (conn->proxy_negotiate_state != CURL_NEGO_AUTH_NONE)) {
422 /* The NEGOTIATE-negotiation has started, keep on sending.
423 * Need to do further work on same connection */
424 abort_upload = FALSE;
425 }
426 }
427#endif
428 }
430 if(abort_upload) {
431 if(upload_remain >= 0)
432 infof(data, "%s%sclose instead of sending %" FMT_OFF_T " more bytes",
433 ongoing_auth ? ongoing_auth : "",
434 ongoing_auth ? " send, " : "",
435 upload_remain);
436 else
437 infof(data, "%s%sclose instead of sending unknown amount "
438 "of more bytes",
439 ongoing_auth ? ongoing_auth : "",
440 ongoing_auth ? " send, " : "");
441 /* We decided to abort the ongoing transfer */
442 streamclose(conn);
443 data->req.size = 0; /* do not download any more than 0 bytes */
444 data->req.http_bodyless = TRUE;
445 }
446 return CURLE_OK;
449/**
450 * http_should_fail() determines whether an HTTP response code has gotten us
451 * into an error state or not.
452 *
453 * @retval FALSE communications should continue
454 *
455 * @retval TRUE communications should not continue
456 */
457static bool http_should_fail(struct Curl_easy *data, int httpcode)
459 DEBUGASSERT(data);
460 DEBUGASSERT(data->conn);
462 /*
463 * If we have not been asked to fail on error,
464 * do not fail.
465 */
466 if(!data->set.http_fail_on_error)
467 return FALSE;
469 /*
470 * Any code < 400 is never terminal.
471 */
472 if(httpcode < 400)
473 return FALSE;
475 /*
476 * A 416 response to a resume request is presumably because the file is
477 * already completely downloaded and thus not actually a fail.
478 */
479 if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
480 httpcode == 416)
481 return FALSE;
483 /*
484 * Any code >= 400 that is not 401 or 407 is always
485 * a terminal error
486 */
487 if((httpcode != 401) && (httpcode != 407))
488 return TRUE;
490 /*
491 * All we have left to deal with is 401 and 407
492 */
493 DEBUGASSERT((httpcode == 401) || (httpcode == 407));
495 /*
496 * Examine the current authentication state to see if this is an error. The
497 * idea is for this function to get called after processing all the headers
498 * in a response message. If we have been asked to authenticate at
499 * a particular stage, and we have done it, we are OK. If we are already
500 * completely authenticated, it is not OK to get another 401 or 407.
501 *
502 * It is possible for authentication to go stale such that the client needs
503 * to reauthenticate. Once that info is available, use it here.
504 */
506 /*
507 * Either we are not authenticating, or we are supposed to be authenticating
508 * something else. This is an error.
509 */
510 if((httpcode == 401) && !data->state.creds)
511 return TRUE;
512#ifndef CURL_DISABLE_PROXY
513 if((httpcode == 407) && !data->conn->http_proxy.creds)
514 return TRUE;
515#endif
517 return (bool)data->state.authproblem;
520/*
521 * Curl_http_auth_act() gets called when all HTTP headers have been received
522 * and it checks what authentication methods that are available and decides
523 * which one (if any) to use. It will set 'newurl' if an auth method was
524 * picked.
525 */
526CURLcode Curl_http_auth_act(struct Curl_easy *data)
528 struct connectdata *conn = data->conn;
529 bool pickhost = FALSE;
530 bool pickproxy = FALSE;
531 CURLcode result = CURLE_OK;
532 unsigned long authmask = ~0UL;
534 if(!Curl_creds_has_oauth_bearer(data->state.creds))
535 authmask &= (unsigned long)~CURLAUTH_BEARER;
537 if(100 <= data->req.httpcode && data->req.httpcode <= 199)
538 /* this is a transient response code, ignore */
539 return CURLE_OK;
541 if(data->state.authproblem)
542 return data->set.http_fail_on_error ? CURLE_HTTP_RETURNED_ERROR : CURLE_OK;
544 if(data->state.creds &&
545 ((data->req.httpcode == 401) ||
546 (data->req.authneg && data->req.httpcode < 300))) {
547 pickhost = pickoneauth(&data->state.authhost, authmask, data->state.creds);
548 if(!pickhost)
549 data->state.authproblem = TRUE;
550 else
551 data->info.httpauthpicked = data->state.authhost.picked;
552 }
553#ifndef CURL_DISABLE_PROXY
554 if(conn->http_proxy.creds &&
555 ((data->req.httpcode == 407) ||
556 (data->req.authneg && data->req.httpcode < 300))) {
557 pickproxy = pickoneauth(&data->state.authproxy,
558 authmask & ~CURLAUTH_BEARER,
559 conn->http_proxy.creds);
560 if(!pickproxy)
561 data->state.authproblem = TRUE;
562 else
563 data->info.proxyauthpicked = data->state.authproxy.picked;
564 }
565#endif
567 if(pickhost || pickproxy) {
568 result = http_perhapsrewind(data, conn);
569 if(result)
570 return result;
572 /* In case this is GSS auth, the newurl field is already allocated so
573 we must make sure to free it before allocating a new one. As figured
574 out in bug #2284386 */
575 curlx_free(data->req.newurl);
576 /* clone URL */
577 data->req.newurl = Curl_bufref_dup(&data->state.url);
578 if(!data->req.newurl)
579 return CURLE_OUT_OF_MEMORY;
580 }
581 else if((data->req.httpcode < 300) &&
582 !data->state.authhost.done &&
583 data->req.authneg &&
584 /* no (known) authentication available,
585 authentication is not "done" yet and
586 no authentication seems to be required and
587 we did not try HEAD or GET */
588 (data->state.httpreq != HTTPREQ_GET) &&
589 (data->state.httpreq != HTTPREQ_HEAD)) {
590 /* clone URL */
591 data->req.newurl = Curl_bufref_dup(&data->state.url);
592 if(!data->req.newurl)
593 return CURLE_OUT_OF_MEMORY;
594 data->state.authhost.done = TRUE;
595 }
596 if(http_should_fail(data, data->req.httpcode)) {
597 failf(data, "The requested URL returned error: %d",
598 data->req.httpcode);
599 result = CURLE_HTTP_RETURNED_ERROR;
600 }
602 return result;
605#ifndef CURL_DISABLE_HTTP_AUTH
606/*
607 * Output the correct authentication header depending on the auth type
608 * and whether or not it is to a proxy.
609 */
610static CURLcode output_auth_headers(struct Curl_easy *data,
611 struct connectdata *conn,
612 struct auth *authstatus,
613 const char *request,
614 const char *path,
615 bool proxy)
617 const char *auth = NULL;
618 CURLcode result = CURLE_OK;
619 (void)conn;
621#ifdef CURL_DISABLE_DIGEST_AUTH
622 (void)request;
623 (void)path;
624#endif
625#ifndef CURL_DISABLE_AWS
626 if((authstatus->picked == CURLAUTH_AWS_SIGV4) && !proxy) {
627 /* this method is never for proxy */
628 auth = "AWS_SIGV4";
629 result = Curl_output_aws_sigv4(data);
630 if(result)
631 return result;
632 }
633 else
634#endif
635#ifndef CURL_DISABLE_HTTPSIG
636 if((authstatus->picked == CURLAUTH_HTTPSIG) && !proxy) {
637 /* HTTPSIG uses its own configured key material rather than
638 data->state.creds. Do not let unrelated credentials from a
639 redirected URL bypass the cross-host auth boundary. */
640 if(Curl_auth_allowed_to_host(data)) {
641 auth = "HTTPSIG";
642 result = Curl_output_httpsig(data);
643 if(result)
644 return result;
645 }
646 else
647 authstatus->done = TRUE;
648 }
649 else
650#endif
651#ifdef USE_SPNEGO
652 if(authstatus->picked == CURLAUTH_NEGOTIATE) {
653 if(
654#ifndef CURL_DISABLE_PROXY
655 (proxy && !Curl_checkProxyheaders(data, conn,
656 STRCONST("Proxy-authorization"))) ||
657#endif
658 (!proxy && !Curl_checkheaders(data, STRCONST("Authorization")) &&
659 Curl_auth_allowed_to_host(data))) {
660 auth = "Negotiate";
661 result = Curl_output_negotiate(data, conn, proxy);
662 if(result)
663 return result;
664 }
665 else
666 authstatus->done = TRUE;
667 }
668 else
669#endif
670#ifndef CURL_DISABLE_DIGEST_AUTH
671 if(authstatus->picked == CURLAUTH_DIGEST) {
672 auth = "Digest";
673 result = Curl_output_digest(data,
674 proxy,
675 (const unsigned char *)request,
676 (const unsigned char *)path);
677 if(result)
678 return result;
679 }
680 else
681#endif
682#ifndef CURL_DISABLE_BASIC_AUTH
683 if(authstatus->picked == CURLAUTH_BASIC) {
684 /* Basic */
685 if(
686#ifndef CURL_DISABLE_PROXY
687 (proxy && conn->http_proxy.creds &&
688 Curl_creds_has_user_or_pass(conn->http_proxy.creds) &&
689 !Curl_checkProxyheaders(data, conn,
690 STRCONST("Proxy-authorization"))) ||
691#endif
692 (!proxy && data->state.creds &&
693 Curl_creds_has_user_or_pass(data->state.creds) &&
694 !Curl_checkheaders(data, STRCONST("Authorization")))) {
695 auth = "Basic";
696 result = http_output_basic(data, conn, proxy);
697 if(result)
698 return result;
699 }
701 /* NOTE: this function should set 'done' TRUE, as the other auth
702 functions work that way */
703 authstatus->done = TRUE;
704 }
705#endif
706#ifndef CURL_DISABLE_BEARER_AUTH
707 if(authstatus->picked == CURLAUTH_BEARER) {
708 /* Bearer */
709 if(!proxy && Curl_creds_has_oauth_bearer(data->state.creds) &&
710 !Curl_checkheaders(data, STRCONST("Authorization"))) {
711 auth = "Bearer";
712 result = http_output_bearer(data);
713 if(result)
714 return result;
715 }
717 /* NOTE: this function should set 'done' TRUE, as the other auth
718 functions work that way */
719 authstatus->done = TRUE;
720 }
721#endif
723 if(auth) {
724#ifndef CURL_DISABLE_PROXY
725 if(proxy)
726 data->info.proxyauthpicked = authstatus->picked;
727 else
728 data->info.httpauthpicked = authstatus->picked;
729 infof(data, "%s auth using %s with user '%s'",
730 proxy ? "Proxy" : "Server", auth,
731 proxy ? (conn->http_proxy.creds ?
732 conn->http_proxy.creds->user : "") :
733 (data->state.creds ?
734 data->state.creds->user : ""));
735#else
736 (void)proxy;
737 infof(data, "Server auth using %s with user '%s'",
738 auth, data->state.creds ?
739 data->state.creds->user : "");
740#endif
741 authstatus->multipass = !authstatus->done;
742 }
743 else {
744 authstatus->multipass = FALSE;
745 if(proxy)
746 data->info.proxyauthpicked = 0;
747 else
748 data->info.httpauthpicked = 0;
749 }
751 return result;
754CURLcode Curl_http_output_auth(struct Curl_easy *data,
755 struct connectdata *conn,
756 const char *request,
757 Curl_HttpReq httpreq,
758 const char *path,
759 const char *query,
760 bool is_connect)
762 CURLcode result = CURLE_OK;
763 struct auth *authhost;
764 struct auth *authproxy;
765 const char *path_and_query = path;
766 char *tmp_str = NULL;
768 DEBUGASSERT(data);
769 authhost = &data->state.authhost;
770 authproxy = &data->state.authproxy;
772 if(
773#ifndef CURL_DISABLE_PROXY
774 (!conn->http_proxy.peer || !conn->http_proxy.creds) &&
775#endif
776#ifdef USE_SPNEGO
777 !(authhost->want & CURLAUTH_NEGOTIATE) &&
778 !(authproxy->want & CURLAUTH_NEGOTIATE) &&
779#endif
780#ifndef CURL_DISABLE_HTTPSIG
781 !(authhost->want & CURLAUTH_HTTPSIG) &&
782#endif
783 !data->state.creds) {
784 /* no authentication with no user or password */
785 authhost->done = TRUE;
786 authproxy->done = TRUE;
787 result = CURLE_OK;
788 goto out;
789 }
791 if(query) {
792 tmp_str = curl_maprintf("%s?%s", path, query);
793 if(!tmp_str) {
794 result = CURLE_OUT_OF_MEMORY;
795 goto out;
796 }
797 path_and_query = tmp_str;
798 }
800 if(authhost->want && !authhost->picked)
801 /* The app has selected one or more methods, but none has been picked
802 so far by a server round-trip. Then we set the picked one to the
803 want one, and if this is one single bit it will be used instantly. */
804 authhost->picked = authhost->want;
806 if(authproxy->want && !authproxy->picked)
807 /* The app has selected one or more methods, but none has been picked so
808 far by a proxy round-trip. Then we set the picked one to the want one,
809 and if this is one single bit it will be used instantly. */
810 authproxy->picked = authproxy->want;
812#ifndef CURL_DISABLE_PROXY
813 /* Send proxy authentication header if needed */
814 if(conn->bits.origin_is_proxy || is_connect) {
815 result = output_auth_headers(data, conn, authproxy, request,
816 path_and_query, TRUE);
817 if(result)
818 goto out;
819 }
820 else
821#else
822 (void)is_connect;
823#endif /* CURL_DISABLE_PROXY */
824 /* we have no proxy so let's pretend we are done authenticating
825 with it */
826 authproxy->done = TRUE;
828 /* Either we have credentials for the origin we talk to or
829 performing authentication is allowed here */
830 if(data->state.creds || Curl_auth_allowed_to_host(data))
831 result = output_auth_headers(data, conn, authhost, request,
832 path_and_query, FALSE);
833 else
834 authhost->done = TRUE;
836 if(((authhost->multipass && !authhost->done) ||
837 (authproxy->multipass && !authproxy->done)) &&
838 (httpreq != HTTPREQ_GET) &&
839 (httpreq != HTTPREQ_HEAD)) {
840 /* Auth is required and we are not authenticated yet. Make a PUT or POST
841 with content-length zero as a "probe". */
842 data->req.authneg = TRUE;
843 }
844 else
845 data->req.authneg = FALSE;
847out:
848 curlx_free(tmp_str);
849 return result;
852#else /* !CURL_DISABLE_HTTP_AUTH */
853/* when disabled */
854CURLcode Curl_http_output_auth(struct Curl_easy *data,
855 struct connectdata *conn,
856 const char *request,
857 Curl_HttpReq httpreq,
858 const char *path,
859 const char *query,
860 bool is_connect)
862 (void)data;
863 (void)conn;
864 (void)request;
865 (void)httpreq;
866 (void)path;
867 (void)query;
868 (void)is_connect;
869 return CURLE_OK;
871#endif /* !CURL_DISABLE_HTTP_AUTH, else */
873#if defined(USE_SPNEGO) || \
874 !defined(CURL_DISABLE_DIGEST_AUTH) || \
875 !defined(CURL_DISABLE_BASIC_AUTH) || \
876 !defined(CURL_DISABLE_BEARER_AUTH)
877static bool authcmp(const char *auth, const char *line)
879 /* the auth string must not have an alnum following */
880 size_t n = strlen(auth);
881 return curl_strnequal(auth, line, n) && !ISALNUM(line[n]);
883#endif
885#ifdef USE_SPNEGO
886static CURLcode auth_spnego(struct Curl_easy *data,
887 bool proxy,
888 const char *auth,
889 struct auth *authp,
890 uint32_t *availp)
892 if((authp->avail & CURLAUTH_NEGOTIATE) || Curl_auth_is_spnego_supported()) {
893 *availp |= CURLAUTH_NEGOTIATE;
894 authp->avail |= CURLAUTH_NEGOTIATE;
896 if(authp->picked == CURLAUTH_NEGOTIATE) {
897 struct connectdata *conn = data->conn;
898 CURLcode result = Curl_input_negotiate(data, conn, proxy, auth);
899 uint8_t *negstate = proxy ? &conn->proxy_negotiate_state :
900 &conn->http_negotiate_state;
901 if(!result) {
902 curlx_free(data->req.newurl);
903 data->req.newurl = Curl_bufref_dup(&data->state.url);
904 if(!data->req.newurl)
905 return CURLE_OUT_OF_MEMORY;
906 data->state.authproblem = FALSE;
907 /* we received a GSS auth token and we dealt with it fine */
908 *negstate = CURL_NEGO_AUTH_RECV;
909 }
910 else
911 data->state.authproblem = TRUE;
912 }
913 }
914 return CURLE_OK;
916#endif
918#ifndef CURL_DISABLE_DIGEST_AUTH
919static CURLcode auth_digest(struct Curl_easy *data,
920 bool proxy,
921 const char *auth,
922 struct auth *authp,
923 uint32_t *availp)
925 if(authp->avail & CURLAUTH_DIGEST) {
926 *availp |= CURLAUTH_DIGEST;
927 infof(data, "Ignoring duplicate digest auth header.");
928 }
929 else if(Curl_auth_is_digest_supported()) {
930 CURLcode result;
932 *availp |= CURLAUTH_DIGEST;
933 authp->avail |= CURLAUTH_DIGEST;
935 /* We call this function on input Digest headers even if Digest
936 * authentication is not activated yet, as we need to store the
937 * incoming data from this header in case we are going to use
938 * Digest */
939 result = Curl_input_digest(data, proxy, auth);
940 if(result) {
941 if(result == CURLE_OUT_OF_MEMORY)
942 return result;
943 infof(data, "Digest authentication problem, ignoring.");
944 data->state.authproblem = TRUE;
945 }
946 }
947 return CURLE_OK;
949#endif
951#ifndef CURL_DISABLE_BASIC_AUTH
952static CURLcode auth_basic(struct Curl_easy *data,
953 struct auth *authp,
954 uint32_t *availp)
956 *availp |= CURLAUTH_BASIC;
957 authp->avail |= CURLAUTH_BASIC;
958 if(authp->picked == CURLAUTH_BASIC) {
959 /* We asked for Basic authentication but got a 40X back anyway, which
960 means our name+password is not valid. */
961 authp->avail = CURLAUTH_NONE;
962 infof(data, "Basic authentication problem, ignoring.");
963 data->state.authproblem = TRUE;
964 }
965 return CURLE_OK;
967#endif
969#ifndef CURL_DISABLE_BEARER_AUTH
970static CURLcode auth_bearer(struct Curl_easy *data,
971 struct auth *authp,
972 uint32_t *availp)
974 *availp |= CURLAUTH_BEARER;
975 authp->avail |= CURLAUTH_BEARER;
976 if(authp->picked == CURLAUTH_BEARER) {
977 /* We asked for Bearer authentication but got a 40X back anyway, which
978 means our token is not valid. */
979 authp->avail = CURLAUTH_NONE;
980 infof(data, "Bearer authentication problem, ignoring.");
981 data->state.authproblem = TRUE;
982 }
983 return CURLE_OK;
985#endif
987/*
988 * Curl_http_input_auth() deals with Proxy-Authenticate: and WWW-Authenticate:
989 * headers. They are dealt with both in the transfer.c main loop and in the
990 * proxy CONNECT loop.
991 *
992 * The 'auth' line ends with a NUL byte without CR or LF present.
993 */
994CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
995 const char *auth) /* the first non-space */
997 /*
998 * This resource requires authentication
999 */
1000#if defined(USE_SPNEGO) || \
1001 !defined(CURL_DISABLE_DIGEST_AUTH) || \
1002 !defined(CURL_DISABLE_BASIC_AUTH) || \
1003 !defined(CURL_DISABLE_BEARER_AUTH)
1005 uint32_t *availp;
1006 struct auth *authp;
1007 CURLcode result = CURLE_OK;
1008 DEBUGASSERT(auth);
1009 DEBUGASSERT(data);
1011 if(proxy) {
1012 availp = &data->info.proxyauthavail;
1013 authp = &data->state.authproxy;
1015 else {
1016 availp = &data->info.httpauthavail;
1017 authp = &data->state.authhost;
1020 /*
1021 * Here we check if we want the specific single authentication (using ==) and
1022 * if we do, we initiate usage of it.
1024 * If the provided authentication is wanted as one out of several accepted
1025 * types (using &), we OR this authentication type to the authavail
1026 * variable.
1028 * Note:
1030 * ->picked is first set to the 'want' value (one or more bits) before the
1031 * request is sent, and then it is again set _after_ all response 401/407
1032 * headers have been received but then only to a single preferred method
1033 * (bit).
1034 */
1036 while(*auth) {
1037#ifdef USE_SPNEGO
1038 if(authcmp("Negotiate", auth))
1039 result = auth_spnego(data, proxy, auth, authp, availp);
1040#endif
1041#ifndef CURL_DISABLE_DIGEST_AUTH
1042 if(!result && authcmp("Digest", auth))
1043 result = auth_digest(data, proxy, auth, authp, availp);
1044#endif
1045#ifndef CURL_DISABLE_BASIC_AUTH
1046 if(!result && authcmp("Basic", auth))
1047 result = auth_basic(data, authp, availp);
1048#endif
1049#ifndef CURL_DISABLE_BEARER_AUTH
1050 if(authcmp("Bearer", auth))
1051 result = auth_bearer(data, authp, availp);
1052#endif
1054 if(result)
1055 break;
1057 /* there may be multiple methods on one line, so keep reading */
1058 auth = strchr(auth, ',');
1059 if(auth) /* if we are on a comma, skip it */
1060 auth++;
1061 else
1062 break;
1063 curlx_str_passblanks(&auth);
1065 return result;
1066#else
1067 (void)data;
1068 (void)proxy;
1069 (void)auth;
1070 /* nothing to do when disabled */
1071 return CURLE_OK;
1072#endif
1075static void http_switch_to_get(struct Curl_easy *data, int code)
1077 const char *req = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
1079 if((req || data->state.httpreq != HTTPREQ_GET) &&
1080 (data->set.http_follow_mode == CURLFOLLOW_OBEYCODE)) {
1081 NOVERBOSE((void)code);
1082 infof(data, "Switch to GET because of %d response", code);
1083 data->state.http_ignorecustom = TRUE;
1085 else if(req && (data->set.http_follow_mode != CURLFOLLOW_FIRSTONLY))
1086 infof(data, "Stick to %s instead of GET", req);
1088 data->state.httpreq = HTTPREQ_GET;
1089 Curl_creader_set_rewind(data, FALSE);
1092#define HTTPREQ_IS_POST(data) \
1093 ((data)->state.httpreq == HTTPREQ_POST || \
1094 (data)->state.httpreq == HTTPREQ_POST_FORM || \
1095 (data)->state.httpreq == HTTPREQ_POST_MIME)
1097static CURLcode set_auto_referer(struct Curl_easy *data)
1099 CURLU *u;
1100 char *referer = NULL;
1101 CURLUcode uc;
1103 /* We are asked to automatically set the previous URL as the referer
1104 when we get the next URL. We pick the ->url field, which may or may
1105 not be 100% correct */
1106 Curl_bufref_free(&data->state.referer);
1108 /* Make a copy of the URL without credentials and fragment */
1109 u = curl_url();
1110 if(!u)
1111 return CURLE_OUT_OF_MEMORY;
1113 uc = curl_url_set(u, CURLUPART_URL,
1114 Curl_bufref_ptr(&data->state.url), 0);
1115 if(!uc)
1116 uc = curl_url_set(u, CURLUPART_FRAGMENT, NULL, 0);
1117 if(!uc)
1118 uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
1119 if(!uc)
1120 uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
1121 if(!uc)
1122 uc = curl_url_get(u, CURLUPART_URL, &referer, 0);
1124 curl_url_cleanup(u);
1126 if(uc || !referer)
1127 return CURLE_OUT_OF_MEMORY;
1129 Curl_bufref_set(&data->state.referer, referer, 0, curl_free);
1130 return CURLE_OK;
1133static CURLcode follow_parse_url(struct Curl_easy *data, const char *newurl,
1134 followtype type, char **follow_urlp,
1135 CURLUcode *ucp)
1137 CURLUcode uc;
1139 DEBUGASSERT(data->state.uh);
1140 uc = curl_url_set(data->state.uh, CURLUPART_URL, newurl, (unsigned int)
1141 ((type == FOLLOW_FAKE) ? CURLU_NON_SUPPORT_SCHEME :
1142 ((type == FOLLOW_REDIR) ? CURLU_URLENCODE : 0) |
1143 CURLU_ALLOW_SPACE |
1144 (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)));
1145 *ucp = uc;
1146 if(uc) {
1147 if((uc == CURLUE_OUT_OF_MEMORY) || (type != FOLLOW_FAKE)) {
1148 failf(data, "The redirect target URL could not be parsed: %s",
1149 curl_url_strerror(uc));
1150 return Curl_uc_to_curlcode(uc);
1153 /* the URL could not be parsed for some reason, but since this is FAKE
1154 mode, duplicate the field as-is */
1155 *follow_urlp = curlx_strdup(newurl);
1156 if(!*follow_urlp)
1157 return CURLE_OUT_OF_MEMORY;
1159 else {
1160 CURLU *u = curl_url();
1161 if(!u)
1162 return CURLE_OUT_OF_MEMORY;
1163 uc = curl_url_set(u, CURLUPART_URL,
1164 Curl_bufref_ptr(&data->state.url),
1165 CURLU_URLENCODE | CURLU_ALLOW_SPACE);
1166 if(!uc)
1167 uc = curl_url_get(data->state.uh, CURLUPART_URL, follow_urlp, 0);
1168 if(uc) {
1169 curl_url_cleanup(u);
1170 return Curl_uc_to_curlcode(uc);
1173#ifndef CURL_DISABLE_DIGEST_AUTH
1175 bool same_origin = Curl_url_same_origin(u, data->state.uh);
1176 curl_url_cleanup(u);
1177 if(!same_origin)
1178 Curl_auth_digest_cleanup(&data->state.digest);
1180#else
1181 curl_url_cleanup(u);
1182#endif
1184 return CURLE_OK;
1187static CURLcode follow_fake(struct Curl_easy *data, char *follow_url,
1188 CURLUcode uc, bool reachedmax)
1190 /* we are only figuring out the new URL if we would have followed locations
1191 but now we are done so we can get out! */
1192 if(!uc) {
1193 CURLU *u = curl_url();
1194 char *nocred = NULL;
1196 if(!u) {
1197 curlx_free(follow_url);
1198 return CURLE_OUT_OF_MEMORY;
1200 if(!curl_url_set(u, CURLUPART_URL, follow_url,
1201 CURLU_NON_SUPPORT_SCHEME |
1202 (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)) &&
1203 !curl_url_set(u, CURLUPART_USER, NULL, 0) &&
1204 !curl_url_set(u, CURLUPART_PASSWORD, NULL, 0))
1205 (void)curl_url_get(u, CURLUPART_URL, &nocred, CURLU_GET_EMPTY);
1206 curl_url_cleanup(u);
1207 if(nocred) {
1208 curlx_free(follow_url);
1209 follow_url = nocred;
1212 data->info.wouldredirect = follow_url;
1214 if(reachedmax) {
1215 failf(data, "Maximum (%d) redirects followed", data->set.maxredirs);
1216 return CURLE_TOO_MANY_REDIRECTS;
1218 return CURLE_OK;
1221static bool follow_switch_get(struct Curl_easy *data)
1223 /*
1224 * We get here when the HTTP code is 300-399 and 401 or 407. We need to
1225 * perform differently based on exactly what return code there was.
1226 */
1227 switch(data->info.httpcode) {
1228 case 301: /* Moved Permanently */
1229 /* (quote from RFC7231, section 6.4.2)
1231 * Note: For historical reasons, a user agent MAY change the request
1232 * method from POST to GET for the subsequent request. If this
1233 * behavior is undesired, the 307 (Temporary Redirect) status code
1234 * can be used instead.
1236 * ----
1238 * Many webservers expect this, so these servers often answers to a POST
1239 * request with an error page. To be sure that libcurl gets the page that
1240 * most user agents would get, libcurl has to force GET.
1242 * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
1243 * can be overridden with CURLOPT_POSTREDIR.
1244 */
1245 if(HTTPREQ_IS_POST(data) && !data->set.post301) {
1246 http_switch_to_get(data, 301);
1247 return TRUE;
1249 break;
1250 case 302: /* Found */
1251 /* (quote from RFC7231, section 6.4.3)
1253 * Note: For historical reasons, a user agent MAY change the request
1254 * method from POST to GET for the subsequent request. If this
1255 * behavior is undesired, the 307 (Temporary Redirect) status code
1256 * can be used instead.
1258 * ----
1260 * Many webservers expect this, so these servers often answers to a POST
1261 * request with an error page. To be sure that libcurl gets the page that
1262 * most user agents would get, libcurl has to force GET.
1264 * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
1265 * can be overridden with CURLOPT_POSTREDIR.
1266 */
1267 if(HTTPREQ_IS_POST(data) && !data->set.post302) {
1268 http_switch_to_get(data, 302);
1269 return TRUE;
1271 break;
1272 case 303: /* See Other */
1273 /* 'See Other' location is not the resource but a substitute for the
1274 * resource. In this case we switch the method to GET/HEAD, unless the
1275 * method is POST and the user specified to keep it as POST.
1276 */
1277 if(!HTTPREQ_IS_POST(data) || !data->set.post303) {
1278 http_switch_to_get(data, 303);
1279 return TRUE;
1281 break;
1283 /* 304 means we did a conditional request and it was "Not modified".
1284 * We should not get any Location: header in this response!
1285 */
1287 /* 305 (quote from RFC2616, section 10.3.6):
1288 * "The requested resource MUST be accessed through the proxy given
1289 * by the Location field. The Location field gives the URI of the
1290 * proxy. The recipient is expected to repeat this single request
1291 * via the proxy. 305 responses MUST only be generated by origin
1292 * servers."
1293 */
1294 default:
1295 break;
1297 return FALSE;
1300CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
1301 followtype type)
1303 bool disallowport = FALSE;
1304 bool reachedmax = FALSE;
1305 char *follow_url = NULL;
1306 CURLUcode uc;
1307 CURLcode result;
1308 CURLcode rewind_result;
1309 bool switch_to_get;
1311 DEBUGASSERT(type != FOLLOW_NONE);
1313 if(type != FOLLOW_FAKE)
1314 data->state.requests++; /* count all real follows */
1315 if(type == FOLLOW_REDIR) {
1316 if((data->set.maxredirs != -1) &&
1317 (data->state.followlocation >= data->set.maxredirs)) {
1318 reachedmax = TRUE;
1319 type = FOLLOW_FAKE; /* switch to fake to store the would-be-redirected
1320 to URL */
1322 else {
1323 data->state.followlocation++; /* count redirect-followings, including
1324 auth reloads */
1326 if(data->set.http_auto_referer) {
1327 result = set_auto_referer(data);
1328 if(result)
1329 return result;
1334 if((type != FOLLOW_RETRY) &&
1335 (data->req.httpcode != 401) && (data->req.httpcode != 407) &&
1336 Curl_is_absolute_url(newurl, NULL, 0, FALSE)) {
1337 /* If this is not redirect due to a 401 or 407 response and an absolute
1338 URL: do not allow a custom port number */
1339 disallowport = TRUE;
1342 result = follow_parse_url(data, newurl, type, &follow_url, &uc);
1343 if(result)
1344 return result;
1346 DEBUGASSERT(follow_url);
1348 if(type == FOLLOW_FAKE)
1349 return follow_fake(data, follow_url, uc, reachedmax);
1351 if(disallowport)
1352 data->state.allow_port = FALSE;
1354 Curl_bufref_set(&data->state.url, follow_url, 0, curl_free);
1355 rewind_result = Curl_req_soft_reset(&data->req, data);
1356 infof(data, "Issue another request to this URL: '%s'", follow_url);
1357 if((data->set.http_follow_mode == CURLFOLLOW_FIRSTONLY) &&
1358 !data->state.http_ignorecustom &&
1359 CURL_EASY_STR(data, STRING_CUSTOMREQUEST)) {
1360 data->state.http_ignorecustom = TRUE;
1361 infof(data, "Drop custom request method for next request");
1364 switch_to_get = follow_switch_get(data);
1366 /* When rewind of upload data failed and we are not switching to GET,
1367 * we need to fail the follow, as we cannot send the data again. */
1368 if(rewind_result && !switch_to_get)
1369 return rewind_result;
1371 Curl_pgrsTime(data, TIMER_REDIRECT);
1372 Curl_pgrsResetTransferSizes(data);
1374 return CURLE_OK;
1378 * Curl_compareheader()
1380 * Returns TRUE if 'headerline' contains the 'header' with given 'content'
1381 * (within a comma-separated list of tokens). Pass 'header' WITH the colon.
1383 * @unittest: 1625
1384 */
1385bool Curl_compareheader(const char *headerline, /* line to check */
1386 const char *header, /* header keyword _with_ colon */
1387 const size_t hlen, /* len of the keyword in bytes */
1388 const char *content, /* content string to find */
1389 const size_t clen) /* len of the content in bytes */
1391 /* RFC2616, section 4.2 says: "Each header field consists of a name followed
1392 * by a colon (":") and the field value. Field names are case-insensitive.
1393 * The field value MAY be preceded by any amount of LWS, though a single SP
1394 * is preferred." */
1396 const char *p;
1397 struct Curl_str val;
1398 DEBUGASSERT(hlen);
1399 DEBUGASSERT(clen);
1400 DEBUGASSERT(header);
1401 DEBUGASSERT(content);
1403 if(!curl_strnequal(headerline, header, hlen))
1404 return FALSE; /* does not start with header */
1406 /* pass the header */
1407 p = &headerline[hlen];
1409 if(curlx_str_cspn(&p, &val, "\r\n"))
1410 return FALSE;
1411 curlx_str_trimblanks(&val);
1413 /* find the content string in the rest of the line */
1414 if(curlx_strlen(&val) >= clen) {
1415 size_t len;
1416 p = curlx_str(&val);
1417 for(len = curlx_strlen(&val); len >= clen;) {
1418 struct Curl_str next;
1419 const char *o = p;
1420 /* after a match there must be a comma, space, newline or NUL byte */
1421 if(curl_strnequal(p, content, clen) &&
1422 ((p[clen] == ',') || ISBLANK(p[clen]) || ISNEWLINE(p[clen]) ||
1423 !p[clen]))
1424 return TRUE; /* match! */
1425 /* advance to the next comma */
1426 if(curlx_str_until(&p, &next, len, ',') ||
1427 curlx_str_single(&p, ','))
1428 break; /* no comma, get out */
1430 /* if there are more dummy commas, move over them as well */
1431 do
1432 curlx_str_passblanks(&p);
1433 while(!curlx_str_single(&p, ','));
1434 /* trailing blanks may move the parsing point past the value end,
1435 then there is nothing left to match */
1436 if((size_t)(p - o) > len)
1437 break;
1438 len -= (p - o);
1441 return FALSE; /* no match */
1444struct cr_exp100_ctx {
1445 struct Curl_creader super;
1446 struct curltime start; /* time started waiting */
1447 enum expect100 state;
1450/* Expect: 100-continue client reader, blocking uploads */
1452static void http_exp100_continue(struct Curl_easy *data,
1453 struct Curl_creader *reader)
1455 struct cr_exp100_ctx *ctx = reader->ctx;
1456 if(ctx->state > EXP100_SEND_DATA) {
1457 ctx->state = EXP100_SEND_DATA;
1458 Curl_expire_clear(data, EXPIRE_100_TIMEOUT);
1462static CURLcode cr_exp100_read(struct Curl_easy *data,
1463 struct Curl_creader *reader,
1464 char *buf, size_t blen,
1465 size_t *nread, bool *eos)
1467 struct cr_exp100_ctx *ctx = reader->ctx;
1468 timediff_t ms;
1470 switch(ctx->state) {
1471 case EXP100_SENDING_REQUEST:
1472 if(!Curl_req_sendbuf_empty(data)) {
1473 /* The initial request data has not been fully sent yet. Do
1474 * not start the timer yet. */
1475 DEBUGF(infof(data, "cr_exp100_read, request not full sent yet"));
1476 *nread = 0;
1477 *eos = FALSE;
1478 return CURLE_OK;
1480 /* We are now waiting for a reply from the server or
1481 * a timeout on our side IFF the request has been fully sent. */
1482 DEBUGF(infof(data, "cr_exp100_read, start AWAITING_CONTINUE, "
1483 "timeout %dms", data->set.expect_100_timeout));
1484 ctx->state = EXP100_AWAITING_CONTINUE;
1485 ctx->start = *Curl_pgrs_now(data);
1486 Curl_expire_set(data, EXPIRE_100_TIMEOUT,
1487 data->set.expect_100_timeout, &ctx->start);
1488 *nread = 0;
1489 *eos = FALSE;
1490 return CURLE_OK;
1491 case EXP100_FAILED:
1492 DEBUGF(infof(data, "cr_exp100_read, expectation failed, error"));
1493 *nread = 0;
1494 *eos = FALSE;
1495 return CURLE_READ_ERROR;
1496 case EXP100_AWAITING_CONTINUE:
1497 ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &ctx->start);
1498 if(ms < data->set.expect_100_timeout) {
1499 DEBUGF(infof(data, "cr_exp100_read, AWAITING_CONTINUE, not expired"));
1500 *nread = 0;
1501 *eos = FALSE;
1502 return CURLE_OK;
1504 /* we have waited long enough, continue anyway */
1505 http_exp100_continue(data, reader);
1506 infof(data, "Done waiting for 100-continue");
1507 FALLTHROUGH();
1508 default:
1509 DEBUGF(infof(data, "cr_exp100_read, pass through"));
1510 return Curl_creader_read(data, reader->next, buf, blen, nread, eos);
1514static void cr_exp100_done(struct Curl_easy *data,
1515 struct Curl_creader *reader, int premature)
1517 struct cr_exp100_ctx *ctx = reader->ctx;
1518 ctx->state = premature ? EXP100_FAILED : EXP100_SEND_DATA;
1519 Curl_expire_clear(data, EXPIRE_100_TIMEOUT);
1522static const struct Curl_crtype cr_exp100 = {
1523 "cr-exp100",
1524 Curl_creader_def_init,
1525 cr_exp100_read,
1526 Curl_creader_def_close,
1527 Curl_creader_def_needs_rewind,
1528 Curl_creader_def_total_length,
1529 Curl_creader_def_resume_from,
1530 Curl_creader_def_cntrl,
1531 Curl_creader_def_is_paused,
1532 cr_exp100_done,
1533 sizeof(struct cr_exp100_ctx)
1536static CURLcode http_exp100_add_reader(struct Curl_easy *data)
1538 struct Curl_creader *reader = NULL;
1539 CURLcode result;
1541 result = Curl_creader_create(&reader, data, &cr_exp100, CURL_CR_PROTOCOL);
1542 if(!result)
1543 result = Curl_creader_add(data, reader);
1544 if(!result) {
1545 struct cr_exp100_ctx *ctx = reader->ctx;
1546 ctx->state = EXP100_SENDING_REQUEST;
1549 if(result && reader)
1550 Curl_creader_free(data, reader);
1551 return result;
1554static void http_exp100_got100(struct Curl_easy *data)
1556 struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1557 if(r)
1558 http_exp100_continue(data, r);
1561static bool http_exp100_is_waiting(struct Curl_easy *data)
1563 struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1564 if(r) {
1565 struct cr_exp100_ctx *ctx = r->ctx;
1566 return ctx->state == EXP100_AWAITING_CONTINUE;
1568 return FALSE;
1571static void http_exp100_send_anyway(struct Curl_easy *data)
1573 struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1574 if(r)
1575 http_exp100_continue(data, r);
1578static bool http_exp100_is_selected(struct Curl_easy *data)
1580 struct Curl_creader *r = Curl_creader_get_by_type(data, &cr_exp100);
1581 return !!r;
1584/* this returns the socket to wait for in the DO and DOING state for the multi
1585 interface and then we are always _sending_ a request and thus we wait for
1586 the single socket to become writable only */
1587CURLcode Curl_http_doing_pollset(struct Curl_easy *data,
1588 struct easy_pollset *ps)
1590 /* write mode */
1591 return Curl_pollset_add_out(data, ps, data->conn->sock[FIRSTSOCKET]);
1594CURLcode Curl_http_perform_pollset(struct Curl_easy *data,
1595 struct easy_pollset *ps)
1597 struct connectdata *conn = data->conn;
1598 CURLcode result = CURLE_OK;
1600 if(CURL_REQ_WANT_RECV(data)) {
1601 result = Curl_pollset_add_in(data, ps, conn->sock[FIRSTSOCKET]);
1604 /* on a "Expect: 100-continue" timed wait, do not poll for outgoing */
1605 if(!result && Curl_req_want_send(data) && !http_exp100_is_waiting(data)) {
1606 result = Curl_pollset_add_out(data, ps, conn->sock[FIRSTSOCKET]);
1608 return result;
1611static CURLcode http_write_header(struct Curl_easy *data,
1612 const char *hd, size_t hdlen)
1614 CURLcode result;
1615 int writetype;
1617 /* now, only output this if the header AND body are requested:
1618 */
1619 Curl_debug(data, CURLINFO_HEADER_IN, hd, hdlen);
1621 writetype = CLIENTWRITE_HEADER |
1622 ((data->req.httpcode / 100 == 1) ? CLIENTWRITE_1XX : 0);
1624 result = Curl_client_write(data, writetype, hd, hdlen);
1625 if(result)
1626 return result;
1628 result = Curl_bump_headersize(data, hdlen, FALSE);
1629 if(result)
1630 return result;
1632 data->req.deductheadercount = (100 <= data->req.httpcode &&
1633 199 >= data->req.httpcode) ?
1634 data->req.headerbytecount : 0;
1635 return result;
1639 * Curl_http_done() gets called after a single HTTP request has been
1640 * performed.
1641 */
1643CURLcode Curl_http_done(struct Curl_easy *data,
1644 CURLcode status, bool premature)
1646 struct connectdata *conn = data->conn;
1648 /* Clear multipass flag. If authentication is not done yet, then it will get
1649 * a chance to be set back to true when we output the next auth header */
1650 data->state.authhost.multipass = FALSE;
1651 data->state.authproxy.multipass = FALSE;
1653 if(curlx_dyn_len(&data->state.headerb)) {
1654 (void)http_write_header(data, curlx_dyn_ptr(&data->state.headerb),
1655 curlx_dyn_len(&data->state.headerb));
1657 curlx_dyn_reset(&data->state.headerb);
1659 if(status)
1660 return status;
1662 if(!premature && /* this check is pointless when DONE is called before the
1663 entire operation is complete */
1664 !conn->bits.retry &&
1665 !data->set.connect_only &&
1666 (data->req.bytecount +
1667 data->req.headerbytecount -
1668 data->req.deductheadercount) <= 0) {
1669 /* If this connection is not closed to be retried, AND nothing was
1670 read from the HTTP server (that counts), this cannot be right so we
1671 return an error here */
1672 failf(data, "Empty reply from server");
1673 /* Mark it as closed to avoid the "left intact" message */
1674 streamclose(conn);
1675 return CURLE_GOT_NOTHING;
1678 return CURLE_OK;
1681/* Determine if we may use HTTP 1.1 for this request. */
1682static bool http_may_use_1_1(const struct Curl_easy *data)
1684 const struct connectdata *conn = data->conn;
1685 /* We have seen a previous response for *this* transfer with 1.0,
1686 * on another connection or the same one. */
1687 if(data->state.http_neg.rcvd_min == 10)
1688 return FALSE;
1689 /* We have seen a previous response on *this* connection with 1.0. */
1690 if(conn && conn->httpversion_seen == 10)
1691 return FALSE;
1692 /* We want 1.0 and have seen no previous response on *this* connection
1693 with a higher version (maybe no response at all yet). */
1694 if(data->state.http_neg.only_10 &&
1695 (!conn || conn->httpversion_seen <= 10))
1696 return FALSE;
1697 /* We are not restricted to use 1.0 only. */
1698 return !data->state.http_neg.only_10;
1701static unsigned char http_request_version(struct Curl_easy *data)
1703 unsigned char v = Curl_conn_http_version(data, data->conn);
1704 if(!v) {
1705 /* No specific HTTP connection filter installed. */
1706 v = http_may_use_1_1(data) ? 11 : 10;
1708 return v;
1711static const char *get_http_string(int httpversion)
1713 switch(httpversion) {
1714 case 30:
1715 return "3";
1716 case 20:
1717 return "2";
1718 case 11:
1719 return "1.1";
1720 default:
1721 return "1.0";
1725CURLcode Curl_add_custom_headers(struct Curl_easy *data,
1726 bool is_connect, int httpversion,
1727 struct dynbuf *req)
1729 struct curl_slist *h[2];
1730 struct curl_slist *headers;
1731 int numlists = 1; /* by default */
1732 int i;
1734#ifndef CURL_DISABLE_PROXY
1735 enum Curl_proxy_use proxy;
1737 if(is_connect)
1738 proxy = HEADER_CONNECT;
1739 else
1740 proxy = data->conn->bits.origin_is_proxy ? HEADER_PROXY : HEADER_SERVER;
1742 switch(proxy) {
1743 case HEADER_SERVER:
1744 h[0] = data->set.headers;
1745 break;
1746 case HEADER_PROXY:
1747 h[0] = data->set.headers;
1748 if(data->set.sep_headers) {
1749 h[1] = data->set.proxyheaders;
1750 numlists++;
1752 break;
1753 case HEADER_CONNECT:
1754 if(data->set.sep_headers)
1755 h[0] = data->set.proxyheaders;
1756 else
1757 h[0] = data->set.headers;
1758 break;
1759 case HEADER_CONNECT_UDP:
1760 if(data->set.sep_headers)
1761 h[0] = data->set.proxyheaders;
1762 else
1763 h[0] = data->set.headers;
1764 break;
1766#else
1767 (void)is_connect;
1768 h[0] = data->set.headers;
1769#endif
1771 /* loop through one or two lists */
1772 for(i = 0; i < numlists; i++) {
1773 for(headers = h[i]; headers; headers = headers->next) {
1774 CURLcode result = CURLE_OK;
1775 bool blankheader = FALSE;
1776 struct Curl_str name;
1777 const char *p = headers->data;
1778 const char *origp = p;
1779 size_t hlen = strlen(origp);
1781 /* explicitly asked to send header without content is done by a header
1782 that ends with a semicolon, but there must be no colon present in the
1783 name */
1784 if(!curlx_str_until(&p, &name, hlen, ';') &&
1785 !curlx_str_single(&p, ';') &&
1786 !curlx_str_single(&p, '\0') &&
1787 !memchr(curlx_str(&name), ':', curlx_strlen(&name)))
1788 blankheader = TRUE;
1789 else {
1790 p = origp;
1791 if(!curlx_str_until(&p, &name, hlen, ':') &&
1792 !curlx_str_single(&p, ':')) {
1793 struct Curl_str val;
1794 curlx_str_untilnl(&p, &val, hlen);
1795 curlx_str_trimblanks(&val);
1796 if(!curlx_strlen(&val))
1797 /* no content, do not send this */
1798 continue;
1800 else
1801 /* no colon */
1802 continue;
1805 /* a field name is a token and carries no surrounding whitespace, so
1806 trim the parsed name before matching. Otherwise `Authorization :`
1807 (space before the colon) slips past the Authorization/Cookie check
1808 below and gets forwarded to another host on a redirect. */
1809 curlx_str_trimblanks(&name);
1811 /* only send this if the contents was non-blank or done special */
1813 if(data->state.http_host &&
1814 /* a Host: header was sent already, do not pass on any custom
1815 Host: header as that will produce *two* in the same
1816 request! */
1817 curlx_str_casecompare(&name, "Host"))
1819 else if(data->state.httpreq == HTTPREQ_POST_FORM &&
1820 /* this header (extended by formdata.c) is sent later */
1821 curlx_str_casecompare(&name, "Content-Type"))
1823 else if(data->state.httpreq == HTTPREQ_POST_MIME &&
1824 /* this header is sent later */
1825 curlx_str_casecompare(&name, "Content-Type"))
1827 else if(data->req.authneg &&
1828 /* while doing auth neg, do not allow the custom length since
1829 we will force length zero then */
1830 curlx_str_casecompare(&name, "Content-Length"))
1832 else if(curlx_str_casecompare(&name, "Connection"))
1833 /* Connection headers are handled specially */
1835 else if((httpversion >= 20) &&
1836 curlx_str_casecompare(&name, "Transfer-Encoding"))
1837 /* HTTP/2 does not support chunked requests */
1839 else if((curlx_str_casecompare(&name, "Authorization") ||
1840 curlx_str_casecompare(&name, "Cookie")) &&
1841 /* be careful of sending this potentially sensitive header to
1842 other hosts */
1843 !Curl_auth_allowed_to_host(data))
1845 else if(blankheader) {
1846 result = curlx_dyn_addn(req, curlx_str(&name), curlx_strlen(&name));
1847 if(!result)
1848 result = curlx_dyn_addn(req, STRCONST(":\r\n"));
1850 else
1851 result = curlx_dyn_addf(req, "%s\r\n", origp);
1853 if(result)
1854 return result;
1858 return CURLE_OK;
1861#ifndef CURL_DISABLE_PARSEDATE
1862CURLcode Curl_add_timecondition(struct Curl_easy *data,
1863 struct dynbuf *req)
1865 const struct tm *tm;
1866 struct tm keeptime;
1867 CURLcode result;
1868 char datestr[80];
1869 const char *condp;
1870 size_t len;
1872 if(data->set.timecondition == CURL_TIMECOND_NONE)
1873 /* no condition was asked for */
1874 return CURLE_OK;
1876 result = curlx_gmtime(data->set.timevalue, &keeptime);
1877 if(result) {
1878 failf(data, "Invalid TIMEVALUE");
1879 return result;
1881 tm = &keeptime;
1883 switch(data->set.timecondition) {
1884 default:
1885 DEBUGF(infof(data, "invalid time condition"));
1886 return CURLE_BAD_FUNCTION_ARGUMENT;
1888 case CURL_TIMECOND_IFMODSINCE:
1889 condp = "If-Modified-Since";
1890 len = 17;
1891 break;
1892 case CURL_TIMECOND_IFUNMODSINCE:
1893 condp = "If-Unmodified-Since";
1894 len = 19;
1895 break;
1896 case CURL_TIMECOND_LASTMOD:
1897 condp = "Last-Modified";
1898 len = 13;
1899 break;
1902 if(Curl_checkheaders(data, condp, len)) {
1903 /* A custom header was specified; it will be sent instead. */
1904 return CURLE_OK;
1907 /* The If-Modified-Since header family should have their times set in
1908 * GMT as RFC2616 defines: "All HTTP date/time stamps MUST be
1909 * represented in Greenwich Mean Time (GMT), without exception. For the
1910 * purposes of HTTP, GMT is exactly equal to UTC (Coordinated Universal
1911 * Time)." (see page 20 of RFC2616).
1912 */
1914 /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
1915 curl_msnprintf(datestr, sizeof(datestr),
1916 "%s: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
1917 condp,
1918 Curl_wkday[tm->tm_wday ? tm->tm_wday - 1 : 6],
1919 tm->tm_mday,
1920 Curl_month[tm->tm_mon],
1921 tm->tm_year + 1900,
1922 tm->tm_hour,
1923 tm->tm_min,
1924 tm->tm_sec);
1926 result = curlx_dyn_add(req, datestr);
1927 return result;
1929#else
1930/* disabled */
1931CURLcode Curl_add_timecondition(struct Curl_easy *data,
1932 struct dynbuf *req)
1934 (void)data;
1935 (void)req;
1936 return CURLE_OK;
1938#endif
1940void Curl_http_method(struct Curl_easy *data,
1941 const char **method, Curl_HttpReq *reqp)
1943 Curl_HttpReq httpreq = (Curl_HttpReq)data->state.httpreq;
1944 const char *request;
1945#ifndef CURL_DISABLE_WEBSOCKETS
1946 if(data->conn->scheme->protocol & (CURLPROTO_WS | CURLPROTO_WSS))
1947 httpreq = HTTPREQ_GET;
1948 else
1949#endif
1950 if((data->conn->scheme->protocol & (PROTO_FAMILY_HTTP | CURLPROTO_FTP)) &&
1951 data->state.upload)
1952 httpreq = HTTPREQ_PUT;
1954 /* Now set the 'request' pointer to the proper request string */
1955 if(!data->state.http_ignorecustom &&
1956 CURL_EASY_STR(data, STRING_CUSTOMREQUEST)) {
1957 request = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
1959 else {
1960 if(data->req.no_body)
1961 request = "HEAD";
1962 else {
1963 DEBUGASSERT((httpreq >= HTTPREQ_GET) && (httpreq <= HTTPREQ_HEAD));
1964 switch(httpreq) {
1965 case HTTPREQ_POST:
1966 case HTTPREQ_POST_FORM:
1967 case HTTPREQ_POST_MIME:
1968 request = "POST";
1969 break;
1970 case HTTPREQ_PUT:
1971 request = "PUT";
1972 break;
1973 default: /* this should never happen */
1974 case HTTPREQ_GET:
1975 request = "GET";
1976 break;
1977 case HTTPREQ_HEAD:
1978 request = "HEAD";
1979 break;
1983 *method = request;
1984 *reqp = httpreq;
1987static CURLcode http_set_aptr_host(struct Curl_easy *data)
1989 struct connectdata *conn = data->conn;
1990 const char *ptr = NULL;
1992 curlx_safefree(data->state.http_host);
1993#ifndef CURL_DISABLE_COOKIES
1994 curlx_safefree(data->req.cookiehost);
1995#endif
1997 if(Curl_peer_equal(data->state.initial_origin, data->state.origin))
1998 ptr = Curl_checkheaders(data, STRCONST("Host"));
2000 if(ptr) {
2001#ifndef CURL_DISABLE_COOKIES
2002 /* If we have a given custom Host: header, we extract the hostname in
2003 order to possibly use it for cookie reasons later on. We only allow the
2004 custom Host: header if this is NOT a redirect, as setting Host: in the
2005 redirected request is being out on thin ice. Except if the hostname
2006 is the same as the first one! */
2007 char *cookiehost;
2008 CURLcode result = copy_custom_value(ptr, &cookiehost);
2009 if(result)
2010 return result;
2011 if(!*cookiehost)
2012 /* ignore empty data */
2013 curlx_free(cookiehost);
2014 else {
2015 /* If the host begins with '[', we start searching for the port after
2016 the bracket has been closed */
2017 if(*cookiehost == '[') {
2018 char *closingbracket;
2019 /* since the 'cookiehost' is an allocated memory area that will be
2020 freed later we cannot increment the pointer */
2021 memmove(cookiehost, cookiehost + 1, strlen(cookiehost) - 1);
2022 closingbracket = strchr(cookiehost, ']');
2023 if(closingbracket)
2024 *closingbracket = 0;
2026 else {
2027 int startsearch = 0;
2028 char *colon = strchr(cookiehost + startsearch, ':');
2029 if(colon)
2030 *colon = 0; /* The host must not include an embedded port number */
2032 data->req.cookiehost = cookiehost;
2034#endif
2036 if(!curl_strequal("Host:", ptr)) {
2037 data->state.http_host = curl_maprintf("Host:%s", &ptr[5]);
2038 if(!data->state.http_host)
2039 return CURLE_OUT_OF_MEMORY;
2042 else {
2043 /* This is the HTTP Host: header, so we want
2044 * - for IPv6 origins: "[ipv6-address]" where the IPv6 address is
2045 * found in origin->hostname, stripped of zoneid/scopeid.
2046 * - the (IDN converted) origin->hostname (DNS name or IPv4) otherwise.
2047 * Note: zoneid/scopeid only applies to local routing and has no
2048 * meaning on the remote HTTP server (eg. would confuse it). */
2049 bool ipv6 = (bool)data->state.origin->ipv6;
2050 struct dynbuf tmp;
2051 size_t hlen;
2052 CURLcode result;
2054 curlx_dyn_init(&tmp, DYN_HTTP_REQUEST);
2055 result = curlx_dyn_addn(&tmp, STRCONST("Host: "));
2056 if(!result && ipv6)
2057 result = curlx_dyn_addn(&tmp, STRCONST("["));
2058 if(!result)
2059 result = curlx_dyn_add(&tmp, data->state.origin->hostname);
2060 if(!result && ipv6)
2061 result = curlx_dyn_addn(&tmp, STRCONST("]"));
2062 if(!result &&
2063 ((data->state.origin->port != data->state.origin->scheme->defport) ||
2064 (data->state.origin->scheme->family != conn->scheme->family))) {
2065 result = curlx_dyn_addf(&tmp, ":%u", data->state.origin->port);
2068 data->state.http_host = result ? NULL : curlx_dyn_take(&tmp, &hlen);
2069 curlx_dyn_free(&tmp);
2070 return result;
2072 return CURLE_OK;
2076 * Append the request-target to the HTTP request
2077 */
2078static CURLcode http_target(struct Curl_easy *data,
2079 struct dynbuf *r)
2081 CURLcode result = CURLE_OK;
2082 const char *path = data->state.up.path;
2083 const char *query = data->state.up.query;
2084#ifndef CURL_DISABLE_PROXY
2085 struct connectdata *conn = data->conn;
2086#endif
2088 if(CURL_EASY_STR(data, STRING_TARGET)) {
2089 path = CURL_EASY_STR(data, STRING_TARGET);
2090 query = NULL;
2093#ifndef CURL_DISABLE_PROXY
2094 if(conn->bits.origin_is_proxy) {
2095 /* Using a proxy but does not tunnel through it */
2097 /* The path sent to the proxy is in fact the entire URL, but if the remote
2098 host is a IDN-name, we must make sure that the request we produce only
2099 uses the decoded hostname! */
2101 /* and no fragment part */
2102 CURLUcode uc;
2103 char *url;
2104 CURLU *h = curl_url_dup(data->state.uh);
2105 if(!h)
2106 return CURLE_OUT_OF_MEMORY;
2108 if(!data->state.origin->ipv6 &&
2109 (data->state.origin->user_hostname != data->state.origin->hostname)) {
2110 uc = curl_url_set(h, CURLUPART_HOST, data->state.origin->hostname, 0);
2111 if(uc) {
2112 curl_url_cleanup(h);
2113 return CURLE_OUT_OF_MEMORY;
2116 uc = curl_url_set(h, CURLUPART_FRAGMENT, NULL, 0);
2117 if(uc) {
2118 curl_url_cleanup(h);
2119 return CURLE_OUT_OF_MEMORY;
2122 if(data->state.origin->scheme == &Curl_scheme_http) {
2123 /* when getting HTTP, we do not want the userinfo the URL */
2124 uc = curl_url_set(h, CURLUPART_USER, NULL, 0);
2125 if(uc) {
2126 curl_url_cleanup(h);
2127 return CURLE_OUT_OF_MEMORY;
2129 uc = curl_url_set(h, CURLUPART_PASSWORD, NULL, 0);
2130 if(uc) {
2131 curl_url_cleanup(h);
2132 return CURLE_OUT_OF_MEMORY;
2135 else if(data->state.creds && (data->state.creds->source != CREDS_URL)) {
2136 /* credentials not from the URL need to be set */
2137 uc = curl_url_set(h, CURLUPART_USER,
2138 data->state.creds->user, CURLU_URLENCODE);
2139 if(!uc)
2140 uc = curl_url_set(h, CURLUPART_PASSWORD,
2141 data->state.creds->passwd, CURLU_URLENCODE);
2142 if(uc) {
2143 curl_url_cleanup(h);
2144 return Curl_uc_to_curlcode(uc);
2148 /* Extract the URL to use in the request. */
2149 uc = curl_url_get(h, CURLUPART_URL, &url, CURLU_NO_DEFAULT_PORT);
2150 if(uc) {
2151 curl_url_cleanup(h);
2152 return CURLE_OUT_OF_MEMORY;
2155 curl_url_cleanup(h);
2157 /* target or URL */
2158 result = curlx_dyn_add(r, CURL_EASY_STR(data, STRING_TARGET) ?
2159 CURL_EASY_STR(data, STRING_TARGET) : url);
2160 curlx_free(url);
2161 if(result)
2162 return result;
2164 if((data->state.origin->scheme == &Curl_scheme_ftp) &&
2165 data->set.proxy_transfer_mode) {
2166 /* when doing ftp, append ;type=<a|i> if not present */
2167 size_t len = strlen(path);
2168 bool type_present = FALSE;
2169 if((len >= 7) && !memcmp(&path[len - 7], ";type=", 6)) {
2170 switch(Curl_raw_toupper(path[len - 1])) {
2171 case 'A':
2172 case 'D':
2173 case 'I':
2174 type_present = TRUE;
2175 break;
2178 if(!type_present) {
2179 result = curlx_dyn_addf(r, ";type=%c",
2180 data->state.prefer_ascii ? 'a' : 'i');
2181 if(result)
2182 return result;
2187 else
2188#endif
2190 result = curlx_dyn_add(r, path);
2191 if(result)
2192 return result;
2193 if(query)
2194 result = curlx_dyn_addf(r, "?%s", query);
2197 return result;
2200#if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API)
2201static CURLcode set_post_reader(struct Curl_easy *data, Curl_HttpReq httpreq)
2203 CURLcode result;
2205 switch(httpreq) {
2206#ifndef CURL_DISABLE_MIME
2207 case HTTPREQ_POST_MIME:
2208 data->state.mimepost = data->set.mimepostp;
2209 break;
2210#endif
2211#ifndef CURL_DISABLE_FORM_API
2212 case HTTPREQ_POST_FORM:
2213 /* Convert the form structure into a mime structure, then keep
2214 the conversion */
2215 if(!data->state.formp) {
2216 data->state.formp = curlx_calloc(1, sizeof(curl_mimepart));
2217 if(!data->state.formp)
2218 return CURLE_OUT_OF_MEMORY;
2219 Curl_mime_cleanpart(data->state.formp);
2220 result = Curl_getformdata(data, data->state.formp, data->set.httppost,
2221 data->state.fread_func);
2222 if(result) {
2223 curlx_safefree(data->state.formp);
2224 return result;
2226 data->state.mimepost = data->state.formp;
2228 break;
2229#endif
2230 default:
2231 data->state.mimepost = NULL;
2232 break;
2235 switch(httpreq) {
2236 case HTTPREQ_POST_FORM:
2237 case HTTPREQ_POST_MIME:
2238 /* This is form posting using mime data. */
2239#ifndef CURL_DISABLE_MIME
2240 if(data->state.mimepost) {
2241 const char *cthdr = Curl_checkheaders(data, STRCONST("Content-Type"));
2243 /* Read and seek body only. */
2244 data->state.mimepost->flags |= MIME_BODY_ONLY;
2246 /* Prepare the mime structure headers & set content type. */
2248 if(cthdr)
2249 for(cthdr += 13; *cthdr == ' '; cthdr++)
2251 else if(data->state.mimepost->kind == MIMEKIND_MULTIPART)
2252 cthdr = "multipart/form-data";
2254 curl_mime_headers(data->state.mimepost, data->set.headers, 0);
2255 result = Curl_mime_prepare_headers(data, data->state.mimepost, cthdr,
2256 NULL, MIMESTRATEGY_FORM);
2257 if(result)
2258 return result;
2259 curl_mime_headers(data->state.mimepost, NULL, 0);
2260 result = Curl_creader_set_mime(data, data->state.mimepost);
2261 if(result)
2262 return result;
2264 else
2265#endif
2267 result = Curl_creader_set_null(data);
2269 data->state.infilesize = Curl_creader_total_length(data);
2270 return result;
2272 default:
2273 return Curl_creader_set_null(data);
2275 /* never reached */
2277#endif
2279static CURLcode set_reader(struct Curl_easy *data, Curl_HttpReq httpreq)
2281 CURLcode result = CURLE_OK;
2282 curl_off_t postsize = data->state.infilesize;
2284 DEBUGASSERT(data->conn);
2286 if(data->req.authneg) {
2287 return Curl_creader_set_null(data);
2290 switch(httpreq) {
2291 case HTTPREQ_PUT: /* Let's PUT the data to the server! */
2292 return postsize ? Curl_creader_set_fread(data, postsize) :
2293 Curl_creader_set_null(data);
2295#if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API)
2296 case HTTPREQ_POST_FORM:
2297 case HTTPREQ_POST_MIME:
2298 return set_post_reader(data, httpreq);
2299#endif
2301 case HTTPREQ_POST:
2302 /* this is the simple POST, using x-www-form-urlencoded style */
2303 /* the size of the post body */
2304 if(!postsize) {
2305 result = Curl_creader_set_null(data);
2307 else if(data->set.postfields) {
2308 size_t plen = curlx_sotouz_range(postsize, 0, SIZE_MAX);
2309 if(plen == SIZE_MAX)
2310 return CURLE_OUT_OF_MEMORY;
2311 else if(plen)
2312 result = Curl_creader_set_buf(data, data->set.postfields, plen);
2313 else
2314 result = Curl_creader_set_null(data);
2316 else {
2317 /* we read the bytes from the callback. In case "chunked" encoding
2318 * is forced by the application, we disregard `postsize`. This is
2319 * a backward compatibility decision to earlier versions where
2320 * chunking disregarded this. See issue #13229. */
2321 bool chunked = FALSE;
2322 char *ptr = Curl_checkheaders(data, STRCONST("Transfer-Encoding"));
2323 if(ptr) {
2324 /* Some kind of TE is requested, check if 'chunked' is chosen */
2325 chunked = Curl_compareheader(ptr, STRCONST("Transfer-Encoding:"),
2326 STRCONST("chunked"));
2328 result = Curl_creader_set_fread(data, chunked ? -1 : postsize);
2330 return result;
2332 default:
2333 /* HTTP GET/HEAD download, has no body, needs no Content-Length */
2334 data->state.infilesize = 0;
2335 return Curl_creader_set_null(data);
2337 /* not reached */
2340static CURLcode http_resume(struct Curl_easy *data, Curl_HttpReq httpreq)
2342 if((HTTPREQ_POST == httpreq || HTTPREQ_POST_FORM == httpreq ||
2343 HTTPREQ_POST_MIME == httpreq || HTTPREQ_PUT == httpreq) &&
2344 data->state.resume_from) {
2345 failf(data, "HTTP upload cannot be resumed");
2346 return CURLE_BAD_FUNCTION_ARGUMENT;
2348 return CURLE_OK;
2351static CURLcode http_req_set_TE(struct Curl_easy *data,
2352 struct dynbuf *req,
2353 int httpversion)
2355 CURLcode result = CURLE_OK;
2356 const char *ptr;
2358 ptr = Curl_checkheaders(data, STRCONST("Transfer-Encoding"));
2359 if(ptr) {
2360 /* Some kind of TE is requested, check if 'chunked' is chosen */
2361 data->req.upload_chunky =
2362 Curl_compareheader(ptr,
2363 STRCONST("Transfer-Encoding:"), STRCONST("chunked"));
2364 if(data->req.upload_chunky && (httpversion >= 20)) {
2365 infof(data, "suppressing chunked transfer encoding on connection "
2366 "using HTTP version 2 or higher");
2367 data->req.upload_chunky = FALSE;
2370 else {
2371 curl_off_t req_clen = Curl_creader_total_length(data);
2373 if(req_clen < 0) {
2374 /* indeterminate request content length */
2375 if(httpversion > 10) {
2376 /* On HTTP/1.1, enable chunked, on HTTP/2 and later we do not
2377 * need it */
2378 data->req.upload_chunky = (httpversion < 20);
2380 else {
2381 failf(data, "Chunky upload is not supported by HTTP 1.0");
2382 return CURLE_UPLOAD_FAILED;
2385 else {
2386 /* else, no chunky upload */
2387 data->req.upload_chunky = FALSE;
2390 if(data->req.upload_chunky)
2391 result = curlx_dyn_add(req, "Transfer-Encoding: chunked\r\n");
2393 return result;
2396static CURLcode addexpect(struct Curl_easy *data, struct dynbuf *r,
2397 int httpversion, bool *announced_exp100)
2399 CURLcode result;
2400 char *ptr;
2402 *announced_exp100 = FALSE;
2403 /* Avoid Expect: 100-continue if Upgrade: is used */
2404 if(data->req.upgr101 != UPGR101_NONE)
2405 return CURLE_OK;
2407 /* For really small puts we do not use Expect: headers at all, and for
2408 the somewhat bigger ones we allow the app to disable it. Make
2409 sure that the expect100header is always set to the preferred value
2410 here. */
2411 ptr = Curl_checkheaders(data, STRCONST("Expect"));
2412 if(ptr) {
2413 *announced_exp100 =
2414 Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
2416 else if(!data->state.disableexpect && (httpversion == 11)) {
2417 /* if not doing HTTP 1.0 or version 2, or disabled explicitly, we add an
2418 Expect: 100-continue to the headers which actually speeds up post
2419 operations (as there is one packet coming back from the web server) */
2420 curl_off_t client_len = Curl_creader_client_length(data);
2421 if(client_len > EXPECT_100_THRESHOLD || client_len < 0) {
2422 result = curlx_dyn_addn(r, STRCONST("Expect: 100-continue\r\n"));
2423 if(result)
2424 return result;
2425 *announced_exp100 = TRUE;
2428 return CURLE_OK;
2431static CURLcode http_add_content_hds(struct Curl_easy *data,
2432 struct dynbuf *r,
2433 int httpversion,
2434 Curl_HttpReq httpreq)
2436 CURLcode result = CURLE_OK;
2437 curl_off_t req_clen;
2438 bool announced_exp100 = FALSE;
2440 DEBUGASSERT(data->conn);
2441 if(data->req.upload_chunky) {
2442 result = Curl_httpchunk_add_reader(data);
2443 if(result)
2444 return result;
2447 /* Get the request body length that has been set up */
2448 req_clen = Curl_creader_total_length(data);
2449 switch(httpreq) {
2450 case HTTPREQ_PUT:
2451 case HTTPREQ_POST:
2452#if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API)
2453 case HTTPREQ_POST_FORM:
2454 case HTTPREQ_POST_MIME:
2455#endif
2456 /* We only set Content-Length and allow a custom Content-Length if
2457 we do not upload data chunked, as RFC2616 forbids us to set both
2458 kinds of headers (Transfer-Encoding: chunked and Content-Length).
2459 We do not override a custom "Content-Length" header, but during
2460 authentication negotiation that header is suppressed.
2461 */
2462 if(req_clen >= 0 && !data->req.upload_chunky &&
2463 (data->req.authneg ||
2464 !Curl_checkheaders(data, STRCONST("Content-Length")))) {
2465 /* we allow replacing this header if not during auth negotiation,
2466 although it is not wise to actually set your own */
2467 result = curlx_dyn_addf(r, "Content-Length: %" FMT_OFF_T "\r\n",
2468 req_clen);
2470 if(result)
2471 goto out;
2473#ifndef CURL_DISABLE_MIME
2474 /* Output mime-generated headers. */
2475 if(data->state.mimepost &&
2476 ((httpreq == HTTPREQ_POST_FORM) || (httpreq == HTTPREQ_POST_MIME))) {
2477 struct curl_slist *hdr;
2479 for(hdr = data->state.mimepost->curlheaders; hdr; hdr = hdr->next) {
2480 result = curlx_dyn_addf(r, "%s\r\n", hdr->data);
2481 if(result)
2482 goto out;
2485#endif
2486 if(httpreq == HTTPREQ_POST &&
2487 !Curl_checkheaders(data, STRCONST("Content-Type"))) {
2488 result = curlx_dyn_addn(r, STRCONST("Content-Type: application/"
2489 "x-www-form-urlencoded\r\n"));
2490 if(result)
2491 goto out;
2493 result = addexpect(data, r, httpversion, &announced_exp100);
2494 if(result)
2495 goto out;
2496 break;
2497 default:
2498 break;
2501 Curl_pgrsSetUploadSize(data, req_clen);
2502 if(announced_exp100)
2503 result = http_exp100_add_reader(data);
2505out:
2506 return result;
2509#ifndef CURL_DISABLE_COOKIES
2511static CURLcode http_cookies(struct Curl_easy *data,
2512 struct dynbuf *r)
2514 CURLcode result = CURLE_OK;
2515 const char *addcookies = NULL;
2516 bool linecap = FALSE;
2517 if(CURL_EASY_STR(data, STRING_COOKIE) &&
2518 !Curl_checkheaders(data, STRCONST("Cookie")) &&
2519 Curl_auth_allowed_to_host(data))
2520 addcookies = CURL_EASY_STR(data, STRING_COOKIE);
2522 if(data->cookies || addcookies) {
2523 struct Curl_llist list;
2524 int count = 0;
2526 if(data->cookies && data->state.cookie_engine) {
2527 bool okay;
2528 const char *host = data->req.cookiehost ?
2529 data->req.cookiehost : data->state.origin->hostname;
2530 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
2531 result = Curl_cookie_getlist(data, &okay, host, &list);
2532 if(!result && okay) {
2533 struct Curl_llist_node *n;
2534 size_t clen = 8; /* hold the size of the generated Cookie: header */
2536 /* loop through all cookies that matched */
2537 for(n = Curl_llist_head(&list); n; n = Curl_node_next(n)) {
2538 struct Cookie *co = Curl_node_elem(n);
2539 if(co->value) {
2540 size_t add;
2541 if(!count) {
2542 result = curlx_dyn_addn(r, STRCONST("Cookie: "));
2543 if(result)
2544 break;
2546 add = strlen(co->name) + strlen(co->value) + 1;
2547 if(clen + add >= MAX_COOKIE_HEADER_LEN) {
2548 infof(data, "Restricted outgoing cookies due to header size, "
2549 "'%s' not sent", co->name);
2550 linecap = TRUE;
2551 break;
2553 result = curlx_dyn_addf(r, "%s%s=%s", count ? "; " : "",
2554 co->name, co->value);
2555 if(result)
2556 break;
2557 clen += add + (count ? 2 : 0);
2558 count++;
2561 Curl_llist_destroy(&list, NULL);
2563 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
2565 if(addcookies && !result && !linecap) {
2566 if(!count)
2567 result = curlx_dyn_addn(r, STRCONST("Cookie: "));
2568 if(!result) {
2569 result = curlx_dyn_addf(r, "%s%s", count ? "; " : "", addcookies);
2570 count++;
2573 if(count && !result)
2574 result = curlx_dyn_addn(r, STRCONST("\r\n"));
2576 if(result)
2577 return result;
2579 return result;
2581#else
2582#define http_cookies(a, b) CURLE_OK
2583#endif
2585static CURLcode http_range(struct Curl_easy *data,
2586 Curl_HttpReq httpreq)
2588 if(data->state.use_range) {
2589 /*
2590 * A range is selected. We use different headers whether we are downloading
2591 * or uploading and we always let customized headers override our internal
2592 * ones if any such are specified.
2593 */
2594 if(((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD)) &&
2595 !Curl_checkheaders(data, STRCONST("Range"))) {
2596 /* if a line like this was already allocated, free the previous one */
2597 curlx_free(data->state.rangeline);
2598 data->state.rangeline = curl_maprintf("Range: bytes=%s\r\n",
2599 data->state.range);
2600 if(!data->state.rangeline)
2601 return CURLE_OUT_OF_MEMORY;
2603 else if((httpreq == HTTPREQ_POST || httpreq == HTTPREQ_PUT) &&
2604 !Curl_checkheaders(data, STRCONST("Content-Range"))) {
2605 curl_off_t req_clen = Curl_creader_total_length(data);
2606 /* if a line like this was already allocated, free the previous one */
2607 curlx_free(data->state.rangeline);
2609 if(data->set.set_resume_from < 0) {
2610 /* Upload resume was asked for, but we do not know the size of the
2611 remote part so we tell the server (and act accordingly) that we
2612 upload the whole file (again) */
2613 data->state.rangeline =
2614 curl_maprintf("Content-Range: bytes 0-%" FMT_OFF_T "/"
2615 "%" FMT_OFF_T "\r\n", req_clen - 1, req_clen);
2617 else if(data->state.resume_from) {
2618 /* This is because "resume" was selected */
2619 /* Not sure if we want to send this header during authentication
2620 * negotiation, but test1084 checks for it. In which case we have a
2621 * "null" client reader installed that gives an unexpected length. */
2622 curl_off_t total_len = data->req.authneg ?
2623 data->state.infilesize :
2624 (data->state.resume_from + req_clen);
2625 data->state.rangeline =
2626 curl_maprintf("Content-Range: bytes %s%" FMT_OFF_T "/"
2627 "%" FMT_OFF_T "\r\n",
2628 data->state.range, total_len - 1, total_len);
2630 else {
2631 /* Range was selected and then we pass the incoming range and append
2632 total size */
2633 data->state.rangeline =
2634 curl_maprintf("Content-Range: bytes %s/%" FMT_OFF_T "\r\n",
2635 data->state.range, req_clen);
2637 if(!data->state.rangeline)
2638 return CURLE_OUT_OF_MEMORY;
2641 return CURLE_OK;
2644static CURLcode http_firstwrite(struct Curl_easy *data)
2646 struct connectdata *conn = data->conn;
2647 struct SingleRequest *k = &data->req;
2649 if(data->req.newurl) {
2650 if(conn->bits.close) {
2651 /* Abort after the headers if "follow Location" is set
2652 and we are set to close anyway. */
2653 CURL_REQ_CLEAR_RECV(data);
2654 k->done = TRUE;
2655 return CURLE_OK;
2657 /* We have a new URL to load, but since we want to be able to reuse this
2658 connection properly, we read the full response in "ignore more" */
2659 k->ignorebody = TRUE;
2660 infof(data, "Ignoring the response-body");
2662 if(data->state.resume_from && !k->content_range &&
2663 (data->state.httpreq == HTTPREQ_GET) &&
2664 !k->ignorebody) {
2666 if(k->size == data->state.resume_from) {
2667 /* The resume point is at the end of file, consider this fine even if it
2668 does not allow resume from here. */
2669 infof(data, "The entire document is already downloaded");
2670 streamclose(conn);
2671 /* Abort download */
2672 CURL_REQ_CLEAR_RECV(data);
2673 k->done = TRUE;
2674 return CURLE_OK;
2677 /* we wanted to resume a download, although the server does not seem to
2678 * support this and we did this with a GET (if it was not a GET we did a
2679 * POST or PUT resume) */
2680 failf(data, "HTTP server does not seem to support "
2681 "byte ranges. Cannot resume.");
2682 return CURLE_RANGE_ERROR;
2685 if(data->set.timecondition && !data->state.range &&
2686 /* A time condition has been set AND no ranges have been requested. This
2687 seems to be what chapter 13.3.4 of RFC 2616 defines to be the correct
2688 action for an HTTP/1.1 client */
2689 !Curl_meets_timecondition(data, k->timeofdoc)) {
2690 k->done = TRUE;
2691 /* We are simulating an HTTP 304 from server so we return
2692 what should have been returned from the server */
2693 data->info.httpcode = 304;
2694 infof(data, "Simulate an HTTP 304 response");
2695 /* we abort the transfer before it is completed == we ruin the
2696 reuse ability. Close the connection */
2697 streamclose(conn);
2698 return CURLE_OK;
2699 } /* we have a time condition */
2701 return CURLE_OK;
2704static CURLcode http_add_connection_hd(struct Curl_easy *data,
2705 struct dynbuf *req)
2707 struct curl_slist *head;
2708 const char *sep = "Connection: ";
2709 CURLcode result = CURLE_OK;
2710 size_t rlen = curlx_dyn_len(req);
2711 bool skip;
2713 /* Add the 1st custom "Connection: " header, if there is one */
2714 for(head = data->set.headers; head; head = head->next) {
2715 if(curl_strnequal(head->data, "Connection", 10) &&
2716 Curl_headersep(head->data[10]) &&
2717 !http_header_is_empty(head->data)) {
2718 char *value;
2719 result = copy_custom_value(head->data, &value);
2720 if(result)
2721 return result;
2722 result = curlx_dyn_addf(req, "%s%s", sep, value);
2723 sep = ", ";
2724 curlx_free(value);
2725 break; /* leave, having added 1st one */
2729 /* add our internal Connection: header values, if we have any */
2730 if(!result && data->state.http_hd_te) {
2731 result = curlx_dyn_addf(req, "%s%s", sep, "TE");
2732 sep = ", ";
2734 if(!result && data->state.http_hd_upgrade) {
2735 result = curlx_dyn_addf(req, "%s%s", sep, "Upgrade");
2736 sep = ", ";
2738 if(!result && data->state.http_hd_h2_settings) {
2739 result = curlx_dyn_addf(req, "%s%s", sep, "HTTP2-Settings");
2741 if(!result && (rlen < curlx_dyn_len(req)))
2742 result = curlx_dyn_addn(req, STRCONST("\r\n"));
2743 if(result)
2744 return result;
2746 /* Add all user-defined Connection: headers after the first */
2747 skip = TRUE;
2748 for(head = data->set.headers; head; head = head->next) {
2749 if(curl_strnequal(head->data, "Connection", 10) &&
2750 Curl_headersep(head->data[10]) &&
2751 !http_header_is_empty(head->data)) {
2752 if(skip) {
2753 skip = FALSE;
2754 continue;
2756 result = curlx_dyn_addf(req, "%s\r\n", head->data);
2757 if(result)
2758 return result;
2762 return CURLE_OK;
2765/* Header identifier in order we send them by default */
2766typedef enum {
2767 H1_HD_REQUEST,
2768 H1_HD_HOST,
2769#ifndef CURL_DISABLE_PROXY
2770 H1_HD_PROXY_AUTH,
2771#endif
2772 H1_HD_AUTH,
2773 H1_HD_RANGE,
2774 H1_HD_USER_AGENT,
2775 H1_HD_ACCEPT,
2776 H1_HD_TE,
2777 H1_HD_ACCEPT_ENCODING,
2778 H1_HD_REFERER,
2779#ifndef CURL_DISABLE_PROXY
2780 H1_HD_PROXY_CONNECTION,
2781#endif
2782 H1_HD_TRANSFER_ENCODING,
2783#ifndef CURL_DISABLE_ALTSVC
2784 H1_HD_ALT_USED,
2785#endif
2786 H1_HD_UPGRADE,
2787 H1_HD_COOKIES,
2788 H1_HD_CONDITIONALS,
2789 H1_HD_CUSTOM,
2790 H1_HD_CONTENT,
2791 H1_HD_CONNECTION,
2792 H1_HD_LAST /* the last, empty header line */
2793} http_hd_t;
2795static CURLcode http_add_hd(struct Curl_easy *data,
2796 struct dynbuf *req,
2797 http_hd_t id,
2798 unsigned char httpversion,
2799 const char *method,
2800 Curl_HttpReq httpreq)
2802 CURLcode result = CURLE_OK;
2803#if !defined(CURL_DISABLE_ALTSVC) || \
2804 !defined(CURL_DISABLE_PROXY) || \
2805 !defined(CURL_DISABLE_WEBSOCKETS)
2806 struct connectdata *conn = data->conn;
2807#endif
2808 switch(id) {
2809 case H1_HD_REQUEST:
2810 /* add the main request stuff */
2811 /* GET/HEAD/POST/PUT */
2812 result = curlx_dyn_addf(req, "%s ", method);
2813 if(!result)
2814 result = http_target(data, req);
2815 if(!result)
2816 result = curlx_dyn_addf(req, " HTTP/%s\r\n",
2817 get_http_string(httpversion));
2818 break;
2820 case H1_HD_HOST:
2821 if(data->state.http_host) {
2822 result = curlx_dyn_add(req, data->state.http_host);
2823 if(!result)
2824 result = curlx_dyn_addn(req, STRCONST("\r\n"));
2826 break;
2828#ifndef CURL_DISABLE_PROXY
2829 case H1_HD_PROXY_AUTH:
2830 if(data->req.hd_proxy_auth)
2831 result = curlx_dyn_add(req, data->req.hd_proxy_auth);
2832 break;
2833#endif
2835 case H1_HD_AUTH:
2836 if(data->req.hd_auth)
2837 result = curlx_dyn_add(req, data->req.hd_auth);
2838 break;
2840 case H1_HD_RANGE:
2841 if(data->state.use_range && data->state.rangeline)
2842 result = curlx_dyn_add(req, data->state.rangeline);
2843 break;
2845 case H1_HD_USER_AGENT: {
2846 const char *ua = CURL_EASY_STR(data, STRING_USERAGENT);
2847 if(ua && *ua && !Curl_checkheaders(data, STRCONST("User-Agent")))
2848 result = curlx_dyn_addf(req, "User-Agent: %s\r\n", ua);
2849 break;
2852 case H1_HD_ACCEPT:
2853 if(!Curl_checkheaders(data, STRCONST("Accept")))
2854 result = curlx_dyn_add(req, "Accept: */*\r\n");
2855 break;
2857 case H1_HD_TE:
2858#ifdef HAVE_LIBZ
2859 if(!Curl_checkheaders(data, STRCONST("TE")) &&
2860 data->set.http_transfer_encoding) {
2861 data->state.http_hd_te = TRUE;
2862 result = curlx_dyn_add(req, "TE: gzip\r\n");
2864#endif
2865 break;
2867 case H1_HD_ACCEPT_ENCODING: {
2868 const char *enc = CURL_EASY_STR(data, STRING_ENCODING);
2869 if(enc && !Curl_checkheaders(data, STRCONST("Accept-Encoding")))
2870 result = curlx_dyn_addf(req, "Accept-Encoding: %s\r\n", enc);
2871 break;
2874 case H1_HD_REFERER:
2875 if(Curl_bufref_ptr(&data->state.referer) &&
2876 !Curl_checkheaders(data, STRCONST("Referer")))
2877 result = curlx_dyn_addf(req, "Referer: %s\r\n",
2878 Curl_bufref_ptr(&data->state.referer));
2879 break;
2881#ifndef CURL_DISABLE_PROXY
2882 case H1_HD_PROXY_CONNECTION:
2883 if(conn->bits.origin_is_proxy &&
2884 !Curl_checkheaders(data, STRCONST("Proxy-Connection")) &&
2885 !Curl_checkProxyheaders(data, data->conn, STRCONST("Proxy-Connection")))
2886 result = curlx_dyn_add(req, "Proxy-Connection: Keep-Alive\r\n");
2887 break;
2888#endif
2890 case H1_HD_TRANSFER_ENCODING:
2891 result = http_req_set_TE(data, req, httpversion);
2892 break;
2894#ifndef CURL_DISABLE_ALTSVC
2895 case H1_HD_ALT_USED:
2896 if(conn->bits.altused && conn->via_peer &&
2897 !Curl_checkheaders(data, STRCONST("Alt-Used")))
2898 result = curlx_dyn_addf(req, "Alt-Used: %s:%u\r\n",
2899 conn->via_peer->hostname, conn->via_peer->port);
2900 break;
2901#endif
2903 case H1_HD_UPGRADE:
2904 if(!Curl_conn_is_ssl(data->conn, FIRSTSOCKET) && (httpversion < 20) &&
2905 (data->state.http_neg.wanted & CURL_HTTP_V2x) &&
2906 data->state.http_neg.h2_upgrade) {
2907 /* append HTTP2 upgrade magic stuff to the HTTP request if it is not done
2908 over SSL */
2909 result = Curl_http2_request_upgrade(req, data);
2911#ifndef CURL_DISABLE_WEBSOCKETS
2912 if(!result && conn->scheme->protocol & (CURLPROTO_WS | CURLPROTO_WSS))
2913 result = Curl_ws_request(data, req);
2914#endif
2915 break;
2917 case H1_HD_COOKIES:
2918 result = http_cookies(data, req);
2919 break;
2921 case H1_HD_CONDITIONALS:
2922 result = Curl_add_timecondition(data, req);
2923 break;
2925 case H1_HD_CUSTOM:
2926 result = Curl_add_custom_headers(data, FALSE, httpversion, req);
2927 break;
2929 case H1_HD_CONTENT:
2930 result = http_add_content_hds(data, req, httpversion, httpreq);
2931 break;
2933 case H1_HD_CONNECTION: {
2934 result = http_add_connection_hd(data, req);
2935 break;
2938 case H1_HD_LAST:
2939 result = curlx_dyn_addn(req, STRCONST("\r\n"));
2940 break;
2942 return result;
2946 * Curl_http() gets called from the generic multi_do() function when an HTTP
2947 * request is to be performed. This creates and sends a properly constructed
2948 * HTTP request.
2949 */
2950CURLcode Curl_http(struct Curl_easy *data, bool *done)
2952 CURLcode result = CURLE_OK;
2953 Curl_HttpReq httpreq;
2954 const char *method;
2955 struct dynbuf req;
2956 unsigned char httpversion;
2957 size_t hd_id;
2959 /* Always consider the DO phase done after this function call, even if there
2960 may be parts of the request that are not yet sent, since we can deal with
2961 the rest of the request in the PERFORM phase. */
2962 *done = TRUE;
2963 /* initialize a dynamic send-buffer */
2964 curlx_dyn_init(&req, DYN_HTTP_REQUEST);
2965 /* make sure the header buffer is reset - if there are leftovers from a
2966 previous transfer */
2967 curlx_dyn_reset(&data->state.headerb);
2968 data->state.maybe_folded = FALSE;
2970 /* Add collecting of headers written to client. For a new connection,
2971 * we might have done that already, but reuse
2972 * or multiplex needs it here as well. */
2973 result = Curl_headers_init(data);
2974 if(result)
2975 goto out;
2977 data->state.http_hd_te = FALSE;
2978 data->state.http_hd_upgrade = FALSE;
2979 data->state.http_hd_h2_settings = FALSE;
2981 /* what kind of request do we need to send? */
2982 Curl_http_method(data, &method, &httpreq);
2984 /* select host to send */
2985 result = http_set_aptr_host(data);
2986 /* setup the authentication headers, how that method and host are known */
2987 if(!result)
2988 result = Curl_http_output_auth(data, data->conn, method, httpreq,
2989 data->state.up.path,
2990 data->state.up.query, FALSE);
2991 /* Setup input reader, resume information and ranges */
2992 if(!result)
2993 result = set_reader(data, httpreq);
2994 if(!result)
2995 result = http_resume(data, httpreq);
2996 if(!result)
2997 result = http_range(data, httpreq);
2998 if(result)
2999 goto out;
3001 httpversion = http_request_version(data);
3002 /* Add request line and all headers to `req` */
3003 for(hd_id = 0; hd_id <= H1_HD_LAST; ++hd_id) {
3004 result = http_add_hd(data, &req, (http_hd_t)hd_id,
3005 httpversion, method, httpreq);
3006 if(result)
3007 goto out;
3010 /* setup variables for the upcoming transfer and send */
3011 Curl_xfer_setup_sendrecv(data, FIRSTSOCKET, -1);
3012 result = Curl_req_send(data, &req, httpversion);
3014 if((httpversion >= 20) && data->req.upload_chunky)
3015 /* upload_chunky was set above to set up the request in a chunky fashion,
3016 but is disabled here again to avoid that the chunked encoded version is
3017 actually used when sending the request body over h2 */
3018 data->req.upload_chunky = FALSE;
3020out:
3021 if(result == CURLE_TOO_LARGE)
3022 failf(data, "HTTP request too large");
3024 curlx_dyn_free(&req);
3025 return result;
3028typedef enum {
3029 STATUS_UNKNOWN, /* not enough data to tell yet */
3030 STATUS_DONE, /* a status line was read */
3031 STATUS_BAD /* not a status line */
3032} statusline;
3034/* Check a string for a prefix. Check no more than 'len' bytes */
3035static bool checkprefixmax(const char *prefix, const char *buffer, size_t len)
3037 size_t ch = CURLMIN(strlen(prefix), len);
3038 return curl_strnequal(prefix, buffer, ch);
3042 * checkhttpprefix()
3044 * Returns TRUE if member of the list matches prefix of string
3045 */
3046static statusline checkhttpprefix(struct Curl_easy *data,
3047 const char *s, size_t len)
3049 struct curl_slist *head = data->set.http200aliases;
3050 statusline rc = STATUS_BAD;
3051 statusline onmatch = len >= 5 ? STATUS_DONE : STATUS_UNKNOWN;
3053 while(head) {
3054 if(checkprefixmax(head->data, s, len)) {
3055 rc = onmatch;
3056 break;
3058 head = head->next;
3061 if((rc != STATUS_DONE) && checkprefixmax("HTTP/", s, len))
3062 rc = onmatch;
3064 return rc;
3067#ifndef CURL_DISABLE_RTSP
3068static statusline checkrtspprefix(struct Curl_easy *data,
3069 const char *s, size_t len)
3071 statusline status = STATUS_BAD;
3072 statusline onmatch = len >= 5 ? STATUS_DONE : STATUS_UNKNOWN;
3073 (void)data;
3074 if(checkprefixmax("RTSP/", s, len))
3075 status = onmatch;
3077 return status;
3079#endif /* CURL_DISABLE_RTSP */
3081static statusline checkprotoprefix(struct Curl_easy *data,
3082 struct connectdata *conn,
3083 const char *s, size_t len)
3085#ifndef CURL_DISABLE_RTSP
3086 if(conn->scheme->protocol & CURLPROTO_RTSP)
3087 return checkrtspprefix(data, s, len);
3088#else
3089 (void)conn;
3090#endif /* CURL_DISABLE_RTSP */
3092 return checkhttpprefix(data, s, len);
3095/* HTTP header has field name `n` (a string constant) */
3096#define HD_IS(hd, hdlen, n) \
3097 (((hdlen) >= (sizeof(n) - 1)) && curl_strnequal(n, hd, sizeof(n) - 1))
3099#define HD_VAL(hd, hdlen, n) \
3100 ((((hdlen) >= (sizeof(n) - 1)) && (hd) && \
3101 curl_strnequal(n, hd, sizeof(n) - 1)) ? ((hd) + (sizeof(n) - 1)) : NULL)
3103/* HTTP header has field name `n` (a string constant) and contains `v`
3104 * (a string constant) in its value(s) */
3105#define HD_IS_AND_SAYS(hd, hdlen, n, v) \
3106 (HD_IS(hd, hdlen, n) && \
3107 ((hdlen) > ((sizeof(n) - 1) + (sizeof(v) - 1))) && \
3108 Curl_compareheader(hd, STRCONST(n), STRCONST(v)))
3111 * http_header_a() parses a single response header starting with A.
3112 */
3113static CURLcode http_header_a(struct Curl_easy *data,
3114 const char *hd, size_t hdlen)
3116#ifndef CURL_DISABLE_ALTSVC
3117 const char *v;
3118 v = (data->asi &&
3119 (Curl_xfer_is_secure(data) ||
3120#ifdef DEBUGBUILD
3121 /* allow debug builds to circumvent the HTTPS restriction */
3122 getenv("CURL_ALTSVC_HTTP")
3123#else
3125#endif
3126 )) ? HD_VAL(hd, hdlen, "Alt-Svc:") : NULL;
3127 if(v) {
3128 /* the ALPN of the current request */
3129 struct SingleRequest *k = &data->req;
3130 enum alpnid id = (k->httpversion == 30) ? ALPN_h3 :
3131 (k->httpversion == 20) ? ALPN_h2 : ALPN_h1;
3132 return Curl_altsvc_parse(data, data->asi, v, data->state.origin, id);
3134#else
3135 (void)data;
3136 (void)hd;
3137 (void)hdlen;
3138#endif
3139 return CURLE_OK;
3143 * http_header_c() parses a single response header starting with C.
3144 */
3145static CURLcode http_header_c(struct Curl_easy *data,
3146 const char *hd, size_t hdlen)
3148 struct connectdata *conn = data->conn;
3149 struct SingleRequest *k = &data->req;
3150 const char *v;
3152 /* Check for Content-Length: header lines to get size. Browsers insist we
3153 should accept multiple Content-Length headers and that a comma separated
3154 list also is fine and then we should accept them all as long as they are
3155 the same value. Different values trigger error.
3156 */
3157 v = (!k->http_bodyless && !data->set.ignorecl && !k->ignore_cl) ?
3158 HD_VAL(hd, hdlen, "Content-Length:") : NULL;
3159 if(v) {
3160 do {
3161 curl_off_t contentlength;
3162 int offt = curlx_str_numblanks(&v, &contentlength);
3164 if(offt == STRE_OVERFLOW) {
3165 /* out of range */
3166 if(data->set.max_filesize) {
3167 failf(data, "Maximum file size exceeded");
3168 return CURLE_FILESIZE_EXCEEDED;
3170 streamclose(conn);
3171 infof(data, "Overflow Content-Length: value");
3172 return CURLE_OK;
3174 else {
3175 if((offt == STRE_OK) &&
3176 ((k->size == -1) || /* not set to something before */
3177 (k->size == contentlength))) { /* or the same value */
3179 k->size = contentlength;
3180 curlx_str_passblanks(&v);
3182 /* on a comma, loop and get the next instead */
3183 if(!curlx_str_single(&v, ','))
3184 continue;
3186 if(!curlx_str_newline(&v)) {
3187 k->maxdownload = k->size;
3188 return CURLE_OK;
3191 /* negative, different value or rubbish - bad HTTP */
3192 failf(data, "Invalid Content-Length: value");
3193 return CURLE_WEIRD_SERVER_REPLY;
3195 } while(1);
3197 v = (!k->http_bodyless && CURL_EASY_STR(data, STRING_ENCODING)) ?
3198 HD_VAL(hd, hdlen, "Content-Encoding:") : NULL;
3199 if(v) {
3200 /*
3201 * Process Content-Encoding. Look for the values: identity, gzip, deflate,
3202 * compress, x-gzip and x-compress. x-gzip and x-compress are the same as
3203 * gzip and compress. (Sec 3.5 RFC 2616). zlib cannot handle compress.
3204 * Errors are handled further down when the response body is processed
3205 */
3206 return Curl_build_unencoding_stack(data, v, FALSE);
3208 /* check for Content-Type: header lines to get the MIME-type */
3209 v = HD_VAL(hd, hdlen, "Content-Type:");
3210 if(v) {
3211 char *contenttype = Curl_copy_header_value(hd);
3212 if(!contenttype)
3213 return CURLE_OUT_OF_MEMORY;
3214 if(!*contenttype)
3215 /* ignore empty data */
3216 curlx_free(contenttype);
3217 else {
3218 curlx_free(data->info.contenttype);
3219 data->info.contenttype = contenttype;
3221 return CURLE_OK;
3223 if((k->httpversion < 20) &&
3224 HD_IS_AND_SAYS(hd, hdlen, "Connection:", "close")) {
3225 /*
3226 * [RFC 2616, section 8.1.2.1]
3227 * "Connection: close" is HTTP/1.1 language and means that
3228 * the connection will close when this request has been
3229 * served.
3230 */
3231 connclose(conn);
3232 return CURLE_OK;
3234 if((k->httpversion == 10) &&
3235 HD_IS_AND_SAYS(hd, hdlen, "Connection:", "keep-alive")) {
3236 /*
3237 * An HTTP/1.0 reply with the 'Connection: keep-alive' line
3238 * tells us the connection will be kept alive for our
3239 * pleasure. Default action for 1.0 is to close.
3241 * [RFC2068, section 19.7.1] */
3242 connkeep(conn);
3243 infof(data, "HTTP/1.0 connection set to keep alive");
3244 return CURLE_OK;
3246 v = !k->http_bodyless ? HD_VAL(hd, hdlen, "Content-Range:") : NULL;
3247 if(v) {
3248 /* Content-Range: bytes [num]-
3249 Content-Range: bytes: [num]-
3250 Content-Range: [num]-
3251 Content-Range: [asterisk]/[total]
3253 The second format was added since Sun's webserver
3254 JavaWebServer/1.1.1 obviously sends the header this way!
3255 The third added since some servers use that!
3256 The fourth means the requested range was unsatisfied.
3257 */
3259 const char *ptr = v;
3261 /* Move forward until first digit or asterisk */
3262 while(*ptr && !ISDIGIT(*ptr) && *ptr != '*')
3263 ptr++;
3265 /* if it truly stopped on a digit */
3266 if(ISDIGIT(*ptr)) {
3267 if(!curlx_str_number(&ptr, &k->offset, CURL_OFF_T_MAX) &&
3268 (data->state.resume_from == k->offset))
3269 /* we asked for a resume and we got it */
3270 k->content_range = TRUE;
3272 else if(k->httpcode < 300)
3273 data->state.resume_from = 0; /* get everything */
3275 return CURLE_OK;
3279 * http_header_l() parses a single response header starting with L.
3280 */
3281static CURLcode http_header_l(struct Curl_easy *data,
3282 const char *hd, size_t hdlen)
3284 struct connectdata *conn = data->conn;
3285 struct SingleRequest *k = &data->req;
3286 const char *v = (!k->http_bodyless &&
3287 (data->set.timecondition || data->set.get_filetime)) ?
3288 HD_VAL(hd, hdlen, "Last-Modified:") : NULL;
3289 if(v) {
3290 if(Curl_getdate_capped(v, &k->timeofdoc))
3291 k->timeofdoc = 0;
3292 if(data->set.get_filetime)
3293 data->info.filetime = k->timeofdoc;
3294 return CURLE_OK;
3296 if(HD_IS(hd, hdlen, "Location:")) {
3297 /* this is the URL that the server advises us to use instead */
3298 char *location = Curl_copy_header_value(hd);
3299 if(!location)
3300 return CURLE_OUT_OF_MEMORY;
3301 if(!*location ||
3302 (data->req.location && !strcmp(data->req.location, location))) {
3303 /* ignore empty header, or exact repeat of a previous one */
3304 curlx_free(location);
3305 return CURLE_OK;
3307 else {
3308 /* has value and is not an exact repeat */
3309 if(data->req.location) {
3310 failf(data, "Multiple Location headers");
3311 curlx_free(location);
3312 return CURLE_WEIRD_SERVER_REPLY;
3314 data->req.location = location;
3316 if((k->httpcode >= 300 && k->httpcode < 400) &&
3317 data->set.http_follow_mode) {
3318 CURLcode result;
3319 DEBUGASSERT(!data->req.newurl);
3320 data->req.newurl = curlx_strdup(data->req.location); /* clone */
3321 if(!data->req.newurl)
3322 return CURLE_OUT_OF_MEMORY;
3324 /* some cases of POST and PUT etc needs to rewind the data
3325 stream at this point */
3326 result = http_perhapsrewind(data, conn);
3327 if(result)
3328 return result;
3330 /* mark the next request as a followed location: */
3331 data->state.this_is_a_follow = TRUE;
3335 return CURLE_OK;
3339 * http_header_p() parses a single response header starting with P.
3340 */
3341static CURLcode http_header_p(struct Curl_easy *data,
3342 const char *hd, size_t hdlen)
3344 struct SingleRequest *k = &data->req;
3346#ifndef CURL_DISABLE_PROXY
3347 const char *v = HD_VAL(hd, hdlen, "Proxy-Connection:");
3348 if(v) {
3349 struct connectdata *conn = data->conn;
3350 if((k->httpversion == 10) && conn->http_proxy.peer &&
3351 HD_IS_AND_SAYS(hd, hdlen, "Proxy-Connection:", "keep-alive")) {
3352 /*
3353 * When an HTTP/1.0 reply comes when using a proxy, the
3354 * 'Proxy-Connection: keep-alive' line tells us the
3355 * connection will be kept alive for our pleasure.
3356 * Default action for 1.0 is to close.
3357 */
3358 connkeep(conn); /* do not close */
3359 infof(data, "HTTP/1.0 proxy connection set to keep alive");
3361 else if((k->httpversion == 11) && conn->http_proxy.peer &&
3362 HD_IS_AND_SAYS(hd, hdlen, "Proxy-Connection:", "close")) {
3363 /*
3364 * We get an HTTP/1.1 response from a proxy and it says it will
3365 * close down after this transfer.
3366 */
3367 connclose(conn);
3368 infof(data, "HTTP/1.1 proxy connection set close");
3370 return CURLE_OK;
3372#endif
3373 if((407 == k->httpcode) && HD_IS(hd, hdlen, "Proxy-authenticate:")) {
3374 char *auth;
3375 CURLcode result;
3376#ifndef CURL_DISABLE_PROXY
3377 if(!data->conn->bits.origin_is_proxy)
3378 /* this response is not from a forward proxy, not for us */
3379 return CURLE_OK;
3380#endif
3381 auth = Curl_copy_header_value(hd);
3382 result = auth ? CURLE_OK : CURLE_OUT_OF_MEMORY;
3383 if(!result) {
3384 result = Curl_http_input_auth(data, TRUE, auth);
3385 curlx_free(auth);
3387 return result;
3389#ifdef USE_SPNEGO
3390 if(HD_IS(hd, hdlen, "Persistent-Auth:")) {
3391 struct connectdata *conn = data->conn;
3392 struct negotiatedata *negdata = Curl_auth_nego_get(conn, FALSE);
3393 struct auth *authp = &data->state.authhost;
3394 if(!negdata)
3395 return CURLE_OUT_OF_MEMORY;
3396 if(authp->picked == CURLAUTH_NEGOTIATE) {
3397 char *persistentauth = Curl_copy_header_value(hd);
3398 if(!persistentauth)
3399 return CURLE_OUT_OF_MEMORY;
3400 negdata->noauthpersist = !!checkprefix("false", persistentauth);
3401 negdata->havenoauthpersist = TRUE;
3402 infof(data, "Negotiate: noauthpersist -> %d, header part: %s",
3403 negdata->noauthpersist, persistentauth);
3404 curlx_free(persistentauth);
3407#endif
3408 return CURLE_OK;
3412 * http_header_r() parses a single response header starting with R.
3413 */
3414static CURLcode http_header_r(struct Curl_easy *data,
3415 const char *hd, size_t hdlen)
3417 const char *v = HD_VAL(hd, hdlen, "Retry-After:");
3418 if(v) {
3419 /* Retry-After = HTTP-date / delay-seconds */
3420 curl_off_t retry_after = 0; /* zero for unknown or "now" */
3421 time_t date = 0;
3422 curlx_str_passblanks(&v);
3424 /* try it as a date first, because a date can otherwise start with and
3425 get treated as a number */
3426 if(!Curl_getdate_capped(v, &date)) {
3427 time_t current = time(NULL);
3428 if(date >= current)
3429 /* convert date to number of seconds into the future */
3430 retry_after = date - current;
3432 else
3433 /* Try it as a decimal number, ignore errors */
3434 (void)curlx_str_number(&v, &retry_after, CURL_OFF_T_MAX);
3435 /* limit to 6 hours max. this is not documented so that it can be changed
3436 in the future if necessary. */
3437 if(retry_after > 21600)
3438 retry_after = 21600;
3439 data->info.retry_after = retry_after;
3441 return CURLE_OK;
3445 * http_header_s() parses a single response header starting with S.
3446 */
3447static CURLcode http_header_s(struct Curl_easy *data,
3448 const char *hd, size_t hdlen)
3450#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_HSTS)
3451 const char *v;
3452#else
3453 (void)data;
3454 (void)hd;
3455 (void)hdlen;
3456#endif
3458#ifndef CURL_DISABLE_COOKIES
3459 v = (data->cookies && data->state.cookie_engine) ?
3460 HD_VAL(hd, hdlen, "Set-Cookie:") : NULL;
3461 if(v) {
3462 /* If there is a custom-set Host: name, use it here, or else use
3463 * real peer hostname. */
3464 const char *host = data->req.cookiehost ?
3465 data->req.cookiehost : data->state.origin->hostname;
3466 const unsigned char secure_context = Curl_secure_context(data, host) ?
3467 COOKIE_SECURE : 0;
3468 CURLcode result;
3469 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
3470 result = Curl_cookie_add(data, data->cookies, v, host,
3471 data->state.up.path,
3472 COOKIE_HTTPHEADER | secure_context);
3473 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
3474 return result;
3476#endif
3477#ifndef CURL_DISABLE_HSTS
3478 /* If enabled, the header is incoming and this is over HTTPS */
3479 v = (data->hsts &&
3480 (Curl_xfer_is_secure(data) ||
3481#ifdef DEBUGBUILD
3482 /* allow debug builds to circumvent the HTTPS restriction */
3483 getenv("CURL_HSTS_HTTP")
3484#else
3486#endif
3488 ) ? HD_VAL(hd, hdlen, "Strict-Transport-Security:") : NULL;
3489 if(v) {
3490 CURLcode result = Curl_hsts_parse(
3491 data->hsts, data->state.origin->hostname, v);
3492 if(result) {
3493 if(result == CURLE_OUT_OF_MEMORY)
3494 return result;
3495 infof(data, "Illegal STS header skipped");
3497#ifdef DEBUGBUILD
3498 else
3499 infof(data, "Parsed STS header fine (%zu entries)",
3500 Curl_llist_count(&data->hsts->list));
3501#endif
3503#endif
3505 return CURLE_OK;
3509 * http_header_t() parses a single response header starting with T.
3510 */
3511static CURLcode http_header_t(struct Curl_easy *data,
3512 const char *hd, size_t hdlen)
3514 struct connectdata *conn = data->conn;
3515 struct SingleRequest *k = &data->req;
3517 /* RFC 9112, ch. 6.1
3518 * "Transfer-Encoding MAY be sent in a response to a HEAD request or
3519 * in a 304 (Not Modified) response (Section 15.4.5 of [HTTP]) to a
3520 * GET request, neither of which includes a message body, to indicate
3521 * that the origin server would have applied a transfer coding to the
3522 * message body if the request had been an unconditional GET."
3524 * Read: in these cases the 'Transfer-Encoding' does not apply
3525 * to any data following the response headers. Do not add any decoders.
3526 */
3527 const char *v = (!k->http_bodyless &&
3528 (data->state.httpreq != HTTPREQ_HEAD) &&
3529 (k->httpcode != 304)) ?
3530 HD_VAL(hd, hdlen, "Transfer-Encoding:") : NULL;
3531 if(v) {
3532 /* One or more encodings. We check for chunked and/or a compression
3533 algorithm. */
3534 CURLcode result = Curl_build_unencoding_stack(data, v, TRUE);
3535 if(result)
3536 return result;
3537 if(!k->chunk) {
3538 /* if this is not chunked, only close can signal the end of this
3539 * transfer as Content-Length is said not to be trusted for
3540 * transfer-encoding! */
3541 CURL_TRC_M(data, "HTTP/1.1 transfer-encoding without chunks");
3542 connclose(conn);
3543 k->ignore_cl = TRUE;
3545 return CURLE_OK;
3547 v = HD_VAL(hd, hdlen, "Trailer:");
3548 if(v) {
3549 data->req.resp_trailer = TRUE;
3550 return CURLE_OK;
3552 return CURLE_OK;
3556 * http_header_w() parses a single response header starting with W.
3557 */
3558static CURLcode http_header_w(struct Curl_easy *data,
3559 const char *hd, size_t hdlen)
3561 struct SingleRequest *k = &data->req;
3562 CURLcode result = CURLE_OK;
3564 if((401 == k->httpcode) && HD_IS(hd, hdlen, "WWW-Authenticate:")) {
3565 char *auth = Curl_copy_header_value(hd);
3566 if(!auth)
3567 result = CURLE_OUT_OF_MEMORY;
3568 else {
3569 result = Curl_http_input_auth(data, FALSE, auth);
3570 curlx_free(auth);
3573 return result;
3577 * http_header() parses a single response header.
3578 */
3579static CURLcode http_header(struct Curl_easy *data,
3580 const char *hd, size_t hdlen)
3582 CURLcode result = CURLE_OK;
3584 switch(hd[0]) {
3585 case 'a':
3586 case 'A':
3587 result = http_header_a(data, hd, hdlen);
3588 break;
3589 case 'c':
3590 case 'C':
3591 result = http_header_c(data, hd, hdlen);
3592 break;
3593 case 'l':
3594 case 'L':
3595 result = http_header_l(data, hd, hdlen);
3596 break;
3597 case 'p':
3598 case 'P':
3599 result = http_header_p(data, hd, hdlen);
3600 break;
3601 case 'r':
3602 case 'R':
3603 result = http_header_r(data, hd, hdlen);
3604 break;
3605 case 's':
3606 case 'S':
3607 result = http_header_s(data, hd, hdlen);
3608 break;
3609 case 't':
3610 case 'T':
3611 result = http_header_t(data, hd, hdlen);
3612 break;
3613 case 'w':
3614 case 'W':
3615 result = http_header_w(data, hd, hdlen);
3616 break;
3619 if(!result) {
3620 struct connectdata *conn = data->conn;
3621 if(conn->scheme->protocol & CURLPROTO_RTSP)
3622 result = Curl_rtsp_parseheader(data, hd);
3624 return result;
3628 * Called after the first HTTP response line (the status line) has been
3629 * received and parsed.
3630 */
3631static CURLcode http_statusline(struct Curl_easy *data,
3632 struct connectdata *conn)
3634 struct SingleRequest *k = &data->req;
3636 switch(k->httpversion) {
3637 case 10:
3638 case 11:
3639#ifdef USE_HTTP2
3640 case 20:
3641#endif
3642#ifdef USE_HTTP3
3643 case 30:
3644#endif
3645 /* no major version switch mid-connection */
3646 if(k->httpversion_sent &&
3647 (k->httpversion / 10 != k->httpversion_sent / 10)) {
3648 failf(data, "Version mismatch (from HTTP/%d to HTTP/%d)",
3649 k->httpversion_sent / 10, k->httpversion / 10);
3650 return CURLE_WEIRD_SERVER_REPLY;
3652 break;
3653 default:
3654 failf(data, "Unsupported HTTP version (%d.%d) in response",
3655 k->httpversion / 10, k->httpversion % 10);
3656 return CURLE_UNSUPPORTED_PROTOCOL;
3659 data->info.httpcode = k->httpcode;
3660 data->info.httpversion = k->httpversion;
3661 conn->httpversion_seen = k->httpversion;
3663 if(!data->state.http_neg.rcvd_min ||
3664 data->state.http_neg.rcvd_min > k->httpversion)
3665 /* store the lowest server version we encounter */
3666 data->state.http_neg.rcvd_min = k->httpversion;
3668 /*
3669 * This code executes as part of processing the header. As a
3670 * result, it is not totally clear how to interpret the
3671 * response code yet as that depends on what other headers may
3672 * be present. 401 and 407 may be errors, but may be OK
3673 * depending on how authentication is working. Other codes
3674 * are definitely errors, so give up here.
3675 */
3676 if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
3677 k->httpcode == 416) {
3678 /* "Requested Range Not Satisfiable", proceed and pretend this is no
3679 error */
3680 k->ignorebody = TRUE; /* Avoid appending error msg to good data. */
3683 if(k->httpversion == 10) {
3684 /* Default action for HTTP/1.0 must be to close, unless
3685 we get one of those fancy headers that tell us the
3686 server keeps it open for us! */
3687 infof(data, "HTTP 1.0, assume close after body");
3688 connclose(conn);
3691 k->http_bodyless = k->httpcode >= 100 && k->httpcode < 200;
3692 switch(k->httpcode) {
3693 case 304:
3694 /* (quote from RFC2616, section 10.3.5): The 304 response
3695 * MUST NOT contain a message-body, and thus is always
3696 * terminated by the first empty line after the header
3697 * fields. */
3698 if(data->set.timecondition)
3699 data->info.timecond = TRUE;
3700 FALLTHROUGH();
3701 case 204:
3702 /* (quote from RFC2616, section 10.2.5): The server has
3703 * fulfilled the request but does not need to return an
3704 * entity-body ... The 204 response MUST NOT include a
3705 * message-body, and thus is always terminated by the first
3706 * empty line after the header fields. */
3707 k->size = 0;
3708 k->maxdownload = 0;
3709 k->http_bodyless = TRUE;
3710 break;
3711 default:
3712 break;
3714 return CURLE_OK;
3717/* Content-Length must be ignored if any Transfer-Encoding is present in the
3718 response. Refer to RFC 7230 section 3.3.3 and RFC2616 section 4.4. This is
3719 figured out here after all headers have been received but before the final
3720 call to the user's header callback, so that a valid content length can be
3721 retrieved by the user in the final call. */
3722static CURLcode http_size(struct Curl_easy *data)
3724 struct SingleRequest *k = &data->req;
3725 if(data->req.ignore_cl || k->chunk) {
3726 k->size = k->maxdownload = -1;
3728 else if(k->size != -1) {
3729 if(data->set.max_filesize &&
3730 !k->ignorebody &&
3731 (k->size > data->set.max_filesize)) {
3732 failf(data, "Maximum file size exceeded");
3733 return CURLE_FILESIZE_EXCEEDED;
3735 if(k->ignorebody)
3736 infof(data, "setting size while ignoring");
3737 Curl_pgrsSetDownloadSize(data, k->size);
3738 k->maxdownload = k->size;
3740 return CURLE_OK;
3743CURLcode Curl_verify_header(struct Curl_easy *data,
3744 const char *hd, size_t hdlen)
3746 struct SingleRequest *k = &data->req;
3747 const char *nul = memchr(hd, '\0', hdlen);
3748 const char *cr = memchr(hd, '\r', nul ? (size_t)(nul - hd) : hdlen);
3749 /* CR may only precede the LF, nothing else */
3750 if(cr && ((size_t)(cr - hd) < hdlen - 2)) {
3751 failf(data, "Carriage return found in header");
3752 return CURLE_WEIRD_SERVER_REPLY;
3754 if(nul) {
3755 /* this is bad, bail out */
3756 failf(data, "NUL byte in header");
3757 return CURLE_WEIRD_SERVER_REPLY;
3759 if(k->headerline < 2)
3760 /* the first "header" is the status-line and it has no colon */
3761 return CURLE_OK;
3762 if(((hd[0] == ' ') || (hd[0] == '\t')) && k->headerline > 2)
3763 /* line folding, cannot happen on line 2 */
3765 else if(!memchr(hd, ':', hdlen)) {
3766 /* this is bad, bail out */
3767 failf(data, "Header without colon");
3768 return CURLE_WEIRD_SERVER_REPLY;
3770 return CURLE_OK;
3773CURLcode Curl_bump_headersize(struct Curl_easy *data,
3774 size_t delta,
3775 bool connect_only)
3777 size_t bad = 0;
3778 unsigned int max = MAX_HTTP_RESP_HEADER_SIZE;
3779 if(delta < MAX_HTTP_RESP_HEADER_SIZE) {
3780 data->info.header_size += (unsigned int)delta;
3781 data->req.allheadercount += (unsigned int)delta;
3782 if(!connect_only)
3783 data->req.headerbytecount += (unsigned int)delta;
3784 if(data->req.allheadercount > max)
3785 bad = data->req.allheadercount;
3786 else if(data->info.header_size > (max * 20)) {
3787 bad = data->info.header_size;
3788 max *= 20;
3791 else
3792 bad = data->req.allheadercount + delta;
3793 if(bad) {
3794 failf(data, "Too large response headers: %zu > %u", bad, max);
3795 return CURLE_RECV_ERROR;
3797 return CURLE_OK;
3801 * Handle a 101 Switching Protocols response. Performs the actual protocol
3802 * upgrade to HTTP/2 or WebSocket based on what was requested.
3803 */
3804static CURLcode http_on_101_upgrade(struct Curl_easy *data,
3805 const char *buf, size_t blen,
3806 size_t *pconsumed,
3807 bool *conn_changed)
3809 struct connectdata *conn = data->conn;
3810 struct SingleRequest *k = &data->req;
3812#if !defined(USE_NGHTTP2) && defined(CURL_DISABLE_WEBSOCKETS)
3813 (void)buf;
3814 (void)blen;
3815 (void)pconsumed;
3816#else
3817 CURLcode result;
3818 int upgr101_requested = k->upgr101;
3819#endif
3821 if(k->httpversion_sent != 11) {
3822 /* invalid for other HTTP versions */
3823 failf(data, "server sent 101 response while not talking HTTP/1.1");
3824 return CURLE_WEIRD_SERVER_REPLY;
3827 /* Whatever the success, upgrade was selected. */
3828 k->upgr101 = UPGR101_RECEIVED;
3829 conn->bits.upgrade_in_progress = FALSE;
3830 *conn_changed = TRUE;
3832 /* To be fully compliant, we would check the "Upgrade:" response header to
3833 * mention the protocol we requested. */
3834#ifdef USE_NGHTTP2
3835 if(upgr101_requested == UPGR101_H2) {
3836 /* Switch to HTTP/2, where we will get more responses. blen bytes in buf
3837 * are already h2 protocol bytes */
3838 infof(data, "Received 101, Switching to HTTP/2");
3839 result = Curl_http2_upgrade(data, conn, FIRSTSOCKET, buf, blen);
3840 if(!result)
3841 *pconsumed += blen;
3842 return result;
3844#endif
3845#ifndef CURL_DISABLE_WEBSOCKETS
3846 if(upgr101_requested == UPGR101_WS) {
3847 /* Switch to WebSocket, where we now stream ws frames. blen bytes in buf
3848 * are already ws protocol bytes */
3849 infof(data, "Received 101, Switching to WebSocket");
3850 result = Curl_ws_accept(data, buf, blen);
3851 if(!result)
3852 *pconsumed += blen; /* ws accept handled the data */
3853 return result;
3855#endif
3856 /* We silently accept this as the final response. What are we switching to
3857 * if we did not ask for an Upgrade? Maybe the application provided an
3858 * `Upgrade: xxx` header? */
3859 k->header = FALSE;
3860 return CURLE_OK;
3864 * Handle 1xx intermediate HTTP responses. Sets up state for more
3865 * headers and processes 100-continue and 101 upgrade responses.
3866 */
3867static CURLcode http_on_1xx_response(struct Curl_easy *data,
3868 const char *buf, size_t blen,
3869 size_t *pconsumed,
3870 bool *conn_changed)
3872 struct SingleRequest *k = &data->req;
3874 /* "A user agent MAY ignore unexpected 1xx status responses."
3875 * By default, we expect to get more responses after this one. */
3876 k->header = TRUE;
3877 k->headerline = 0; /* restart the header line counter */
3879 switch(k->httpcode) {
3880 case 100:
3881 /* We have made an HTTP PUT or POST and this is 1.1-lingo that tells us
3882 * that the server is OK with this and ready to receive the data. */
3883 http_exp100_got100(data);
3884 break;
3885 case 101:
3886 return http_on_101_upgrade(data, buf, blen, pconsumed, conn_changed);
3887 default:
3888 /* The server may send us other 1xx responses, like informative 103. This
3889 * has no influence on request processing and we expect to receive a
3890 * final response eventually. */
3891 break;
3893 return CURLE_OK;
3896#ifdef USE_SPNEGO
3898 * Check if SPNEGO authentication negotiation failed due to connection closure
3899 * (typically on HTTP/1.0 servers).
3900 */
3901static void http_check_auth_closure(struct Curl_easy *data,
3902 struct connectdata *conn)
3904 /* At this point we have some idea about the fate of the connection. If we
3905 are closing the connection it may result auth failure. */
3906 if(conn->bits.close &&
3907 (((data->req.httpcode == 401) &&
3908 (conn->http_negotiate_state == CURL_NEGO_AUTH_RECV)) ||
3909 ((data->req.httpcode == 407) &&
3910 (conn->proxy_negotiate_state == CURL_NEGO_AUTH_RECV)))) {
3911 infof(data, "Connection closure while negotiating auth (HTTP 1.0?)");
3912 data->state.authproblem = TRUE;
3914 if((conn->http_negotiate_state == CURL_NEGO_AUTH_DONE) &&
3915 (data->req.httpcode != 401)) {
3916 conn->http_negotiate_state = CURL_NEGO_AUTH_SUCC;
3918 if((conn->proxy_negotiate_state == CURL_NEGO_AUTH_DONE) &&
3919 (data->req.httpcode != 407)) {
3920 conn->proxy_negotiate_state = CURL_NEGO_AUTH_SUCC;
3923#else
3924#define http_check_auth_closure(x, y) /* empty */
3925#endif
3928 * Handle an error response (>= 300) received while still sending the
3929 * request body. Deals with 417 Expectation Failed retries, keep-sending
3930 * on error, and aborting the send.
3931 */
3932static CURLcode http_handle_send_error(struct Curl_easy *data)
3934 struct connectdata *conn = data->conn;
3935 struct SingleRequest *k = &data->req;
3936 CURLcode result = CURLE_OK;
3938 if(!data->req.authneg && !conn->bits.close &&
3939 !Curl_creader_will_rewind(data)) {
3940 /*
3941 * General treatment of errors when about to send data.
3942 * Including: "417 Expectation Failed", while waiting for
3943 * 100-continue.
3945 * The check for close above is done because if something
3946 * else has already deemed the connection to get closed then
3947 * something else should have considered the big picture and
3948 * we avoid this check.
3949 */
3951 switch(data->state.httpreq) {
3952 case HTTPREQ_PUT:
3953 case HTTPREQ_POST:
3954 case HTTPREQ_POST_FORM:
3955 case HTTPREQ_POST_MIME:
3956 /* We got an error response. If this happened before the
3957 * whole request body has been sent we stop sending and
3958 * mark the connection for closure after we have read the
3959 * entire response. */
3960 if(!Curl_req_done_sending(data)) {
3961 if((k->httpcode == 417) && http_exp100_is_selected(data)) {
3962 /* 417 Expectation Failed - try again without the
3963 Expect header */
3964 if(!k->writebytecount && http_exp100_is_waiting(data)) {
3965 infof(data, "Got HTTP failure 417 while waiting for a 100");
3967 else {
3968 infof(data, "Got HTTP failure 417 while sending data");
3969 streamclose(conn);
3970 result = http_perhapsrewind(data, conn);
3971 if(result)
3972 return result;
3974 data->state.disableexpect = TRUE;
3975 Curl_req_abort_sending(data);
3976 DEBUGASSERT(!data->req.newurl);
3977 data->req.newurl = Curl_bufref_dup(&data->state.url);
3978 if(!data->req.newurl)
3979 return CURLE_OUT_OF_MEMORY;
3981 else if(data->set.http_keep_sending_on_error) {
3982 infof(data, "HTTP error before end of send, keep sending");
3983 http_exp100_send_anyway(data);
3985 else {
3986 infof(data, "HTTP error before end of send, stop sending");
3987 streamclose(conn);
3988 result = Curl_req_abort_sending(data);
3989 if(result)
3990 return result;
3993 break;
3995 default: /* default label present to avoid compiler warnings */
3996 break;
4000 if(Curl_creader_will_rewind(data) && !Curl_req_done_sending(data)) {
Diagnostics4 git calls · 210 ms
CallStatusSizeTime0210 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=205
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20044 KiB205 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=45295
git.response.cache=MISS
git.response.duration_ms=205
git.fetch.wants=6d4df294a4c2477d3c84f7267226d804156d19b8
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=45295
git.pkt.payload.max=45274
git.sideband.band1.bytes=45273
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e21714db32ecd4
Request
96e10921…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format