Skip to content

curl-impersonate: A special build of curl that can impersonate Chrome & Firefox

Move curl-impersonate options to separate files

lwthiker9d9e393d0ec2
diff --git a/chrome/patches/curl-impersonate.patch b/chrome/patches/curl-impersonate.patch
index 9969b59..1f3e120 100644
--- a/chrome/patches/curl-impersonate.patch
+++ b/chrome/patches/curl-impersonate.patch
@@ -243,1030 +243,1069 @@ index 769363941..cd59ad4b2 100644
libcurlu_la_SOURCES = $(CSOURCES) $(HHEADERS)
CHECKSRC = $(CS_$(V))
+diff --git a/lib/Makefile.inc b/lib/Makefile.inc
+index 3e9ddec12..fb883832d 100644
+--- a/lib/Makefile.inc
++++ b/lib/Makefile.inc
+@@ -157,6 +157,7 @@ LIB_CFILES = \
+ idn_win32.c \
+ if2ip.c \
+ imap.c \
++ impersonate.c \
+ inet_ntop.c \
+ inet_pton.c \
+ krb5.c \
diff --git a/lib/easy.c b/lib/easy.c
-index 20293a710..8b6a0f4e1 100644
+index 20293a710..79e0ea1e6 100644
--- a/lib/easy.c
+++ b/lib/easy.c
-@@ -80,6 +80,7 @@
+@@ -80,6 +80,8 @@
#include "dynbuf.h"
#include "altsvc.h"
#include "hsts.h"
+#include "strcase.h"
++#include "impersonate.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
-@@ -282,6 +283,454 @@ void curl_global_cleanup(void)
+@@ -282,6 +284,119 @@ void curl_global_cleanup(void)
init_flags = 0;
}
+/*
-+ * curl-impersonate: Options to be set for each supported target browser.
-+ * Note: this does not include the HTTP headers, which are handled separately
-+ * in Curl_http().
-+ */
-+#define IMPERSONATE_MAX_HEADERS 32
-+static const struct impersonate_opts {
-+ const char *target;
-+ int httpversion;
-+ int ssl_version;
-+ const char *ciphers;
-+ /* Elliptic curves (TLS extension 10).
-+ * Passed to CURLOPT_SSL_EC_CURVES */
-+ const char *curves;
-+ /* Signature hash algorithms (TLS extension 13).
-+ * Passed to CURLOPT_SSL_SIG_HASH_ALGS */
-+ const char *sig_hash_algs;
-+ /* Enable TLS NPN extension. */
-+ bool npn;
-+ /* Enable TLS ALPN extension. */
-+ bool alpn;
-+ /* Enable TLS ALPS extension. */
-+ bool alps;
-+ /* Enable TLS session ticket extension. */
-+ bool tls_session_ticket;
-+ /* TLS certificate compression algorithms.
-+ * (TLS extension 27) */
-+ const char *cert_compression;
-+ const char *http_headers[IMPERSONATE_MAX_HEADERS];
-+ const char *http2_pseudo_headers_order;
-+ /* Other TLS options will come here in the future once they are
-+ * configurable through curl_easy_setopt() */
-+} impersonations[] = {
-+ {
-+ .target = "chrome99",
-+ .httpversion = CURL_HTTP_VERSION_2_0,
-+ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
-+ .ciphers =
-+ "TLS_AES_128_GCM_SHA256,"
-+ "TLS_AES_256_GCM_SHA384,"
-+ "TLS_CHACHA20_POLY1305_SHA256,"
-+ "ECDHE-ECDSA-AES128-GCM-SHA256,"
-+ "ECDHE-RSA-AES128-GCM-SHA256,"
-+ "ECDHE-ECDSA-AES256-GCM-SHA384,"
-+ "ECDHE-RSA-AES256-GCM-SHA384,"
-+ "ECDHE-ECDSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-AES128-SHA,"
-+ "ECDHE-RSA-AES256-SHA,"
-+ "AES128-GCM-SHA256,"
-+ "AES256-GCM-SHA384,"
-+ "AES128-SHA,"
-+ "AES256-SHA",
-+ .npn = false,
-+ .alpn = true,
-+ .alps = true,
-+ .tls_session_ticket = true,
-+ .cert_compression = "brotli",
-+ .http_headers = {
-+ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
-+ "sec-ch-ua-mobile: ?0",
-+ "sec-ch-ua-platform: \"Windows\"",
-+ "Upgrade-Insecure-Requests: 1",
-+ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
-+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
-+ "Sec-Fetch-Site: none",
-+ "Sec-Fetch-Mode: navigate",
-+ "Sec-Fetch-User: ?1",
-+ "Sec-Fetch-Dest: document",
-+ "Accept-Encoding: gzip, deflate, br",
-+ "Accept-Language: en-US,en;q=0.9"
-+ }
-+ },
-+ {
-+ .target = "chrome100",
-+ .httpversion = CURL_HTTP_VERSION_2_0,
-+ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
-+ .ciphers =
-+ "TLS_AES_128_GCM_SHA256,"
-+ "TLS_AES_256_GCM_SHA384,"
-+ "TLS_CHACHA20_POLY1305_SHA256,"
-+ "ECDHE-ECDSA-AES128-GCM-SHA256,"
-+ "ECDHE-RSA-AES128-GCM-SHA256,"
-+ "ECDHE-ECDSA-AES256-GCM-SHA384,"
-+ "ECDHE-RSA-AES256-GCM-SHA384,"
-+ "ECDHE-ECDSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-AES128-SHA,"
-+ "ECDHE-RSA-AES256-SHA,"
-+ "AES128-GCM-SHA256,"
-+ "AES256-GCM-SHA384,"
-+ "AES128-SHA,"
-+ "AES256-SHA",
-+ .npn = false,
-+ .alpn = true,
-+ .alps = true,
-+ .tls_session_ticket = true,
-+ .cert_compression = "brotli",
-+ .http_headers = {
-+ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"",
-+ "sec-ch-ua-mobile: ?0",
-+ "sec-ch-ua-platform: \"Windows\"",
-+ "Upgrade-Insecure-Requests: 1",
-+ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36",
-+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
-+ "Sec-Fetch-Site: none",
-+ "Sec-Fetch-Mode: navigate",
-+ "Sec-Fetch-User: ?1",
-+ "Sec-Fetch-Dest: document",
-+ "Accept-Encoding: gzip, deflate, br",
-+ "Accept-Language: en-US,en;q=0.9"
-+ }
-+ },
-+ {
-+ .target = "chrome101",
-+ .httpversion = CURL_HTTP_VERSION_2_0,
-+ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
-+ .ciphers =
-+ "TLS_AES_128_GCM_SHA256,"
-+ "TLS_AES_256_GCM_SHA384,"
-+ "TLS_CHACHA20_POLY1305_SHA256,"
-+ "ECDHE-ECDSA-AES128-GCM-SHA256,"
-+ "ECDHE-RSA-AES128-GCM-SHA256,"
-+ "ECDHE-ECDSA-AES256-GCM-SHA384,"
-+ "ECDHE-RSA-AES256-GCM-SHA384,"
-+ "ECDHE-ECDSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-AES128-SHA,"
-+ "ECDHE-RSA-AES256-SHA,"
-+ "AES128-GCM-SHA256,"
-+ "AES256-GCM-SHA384,"
-+ "AES128-SHA,"
-+ "AES256-SHA",
-+ .npn = false,
-+ .alpn = true,
-+ .alps = true,
-+ .tls_session_ticket = true,
-+ .cert_compression = "brotli",
-+ .http_headers = {
-+ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"",
-+ "sec-ch-ua-mobile: ?0",
-+ "sec-ch-ua-platform: \"Windows\"",
-+ "Upgrade-Insecure-Requests: 1",
-+ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36",
-+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
-+ "Sec-Fetch-Site: none",
-+ "Sec-Fetch-Mode: navigate",
-+ "Sec-Fetch-User: ?1",
-+ "Sec-Fetch-Dest: document",
-+ "Accept-Encoding: gzip, deflate, br",
-+ "Accept-Language: en-US,en;q=0.9"
-+ }
-+ },
-+ {
-+ .target = "chrome99_android",
-+ .httpversion = CURL_HTTP_VERSION_2_0,
-+ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
-+ .ciphers =
-+ "TLS_AES_128_GCM_SHA256,"
-+ "TLS_AES_256_GCM_SHA384,"
-+ "TLS_CHACHA20_POLY1305_SHA256,"
-+ "ECDHE-ECDSA-AES128-GCM-SHA256,"
-+ "ECDHE-RSA-AES128-GCM-SHA256,"
-+ "ECDHE-ECDSA-AES256-GCM-SHA384,"
-+ "ECDHE-RSA-AES256-GCM-SHA384,"
-+ "ECDHE-ECDSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-AES128-SHA,"
-+ "ECDHE-RSA-AES256-SHA,"
-+ "AES128-GCM-SHA256,"
-+ "AES256-GCM-SHA384,"
-+ "AES128-SHA,"
-+ "AES256-SHA",
-+ .npn = false,
-+ .alpn = true,
-+ .alps = true,
-+ .tls_session_ticket = true,
-+ .cert_compression = "brotli",
-+ .http_headers = {
-+ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
-+ "sec-ch-ua-mobile: ?1",
-+ "sec-ch-ua-platform: \"Android\"",
-+ "Upgrade-Insecure-Requests: 1",
-+ "User-Agent: Mozilla/5.0 (Linux; Android 12; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.58 Mobile Safari/537.36",
-+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
-+ "Sec-Fetch-Site: none",
-+ "Sec-Fetch-Mode: navigate",
-+ "Sec-Fetch-User: ?1",
-+ "Sec-Fetch-Dest: document",
-+ "Accept-Encoding: gzip, deflate, br",
-+ "Accept-Language: en-US,en;q=0.9"
-+ }
-+ },
-+ {
-+ .target = "edge99",
-+ .httpversion = CURL_HTTP_VERSION_2_0,
-+ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
-+ .ciphers =
-+ "TLS_AES_128_GCM_SHA256,"
-+ "TLS_AES_256_GCM_SHA384,"
-+ "TLS_CHACHA20_POLY1305_SHA256,"
-+ "ECDHE-ECDSA-AES128-GCM-SHA256,"
-+ "ECDHE-RSA-AES128-GCM-SHA256,"
-+ "ECDHE-ECDSA-AES256-GCM-SHA384,"
-+ "ECDHE-RSA-AES256-GCM-SHA384,"
-+ "ECDHE-ECDSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-AES128-SHA,"
-+ "ECDHE-RSA-AES256-SHA,"
-+ "AES128-GCM-SHA256,"
-+ "AES256-GCM-SHA384,"
-+ "AES128-SHA,"
-+ "AES256-SHA",
-+ .npn = false,
-+ .alpn = true,
-+ .alps = true,
-+ .tls_session_ticket = true,
-+ .cert_compression = "brotli",
-+ .http_headers = {
-+ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Microsoft Edge\";v=\"99\"",
-+ "sec-ch-ua-mobile: ?0",
-+ "sec-ch-ua-platform: \"Windows\"",
-+ "Upgrade-Insecure-Requests: 1",
-+ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.30",
-+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
-+ "Sec-Fetch-Site: none",
-+ "Sec-Fetch-Mode: navigate",
-+ "Sec-Fetch-User: ?1",
-+ "Sec-Fetch-Dest: document",
-+ "Accept-Encoding: gzip, deflate, br",
-+ "Accept-Language: en-US,en;q=0.9"
++ * curl-impersonate:
++ * Call curl_easy_setopt() with all the needed options as defined in the
++ * 'impersonations' array.
++ * */
++CURLcode curl_easy_impersonate(struct Curl_easy *data, const char *target)
++{
++ int i;
++ int ret;
++ const struct impersonate_opts *opts = NULL;
++ struct curl_slist *headers = NULL;
++
++ for(opts = impersonations; opts->target != NULL; opts++) {
++ if (Curl_safe_strcasecompare(target, opts->target)) {
++ break;
+ }
-+ },
-+ {
-+ .target = "edge101",
-+ .httpversion = CURL_HTTP_VERSION_2_0,
-+ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
-+ .ciphers =
-+ "TLS_AES_128_GCM_SHA256,"
-+ "TLS_AES_256_GCM_SHA384,"
-+ "TLS_CHACHA20_POLY1305_SHA256,"
-+ "ECDHE-ECDSA-AES128-GCM-SHA256,"
-+ "ECDHE-RSA-AES128-GCM-SHA256,"
-+ "ECDHE-ECDSA-AES256-GCM-SHA384,"
-+ "ECDHE-RSA-AES256-GCM-SHA384,"
-+ "ECDHE-ECDSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-CHACHA20-POLY1305,"
-+ "ECDHE-RSA-AES128-SHA,"
-+ "ECDHE-RSA-AES256-SHA,"
-+ "AES128-GCM-SHA256,"
-+ "AES256-GCM-SHA384,"
-+ "AES128-SHA,"
-+ "AES256-SHA",
-+ .npn = false,
-+ .alpn = true,
-+ .alps = true,
-+ .tls_session_ticket = true,
-+ .cert_compression = "brotli",
-+ .http_headers = {
-+ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Microsoft Edge\";v=\"101\"",
-+ "sec-ch-ua-mobile: ?0",
-+ "sec-ch-ua-platform: \"Windows\"",
-+ "Upgrade-Insecure-Requests: 1",
-+ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47",
-+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
-+ "Sec-Fetch-Site: none",
-+ "Sec-Fetch-Mode: navigate",
-+ "Sec-Fetch-User: ?1",
-+ "Sec-Fetch-Dest: document",
-+ "Accept-Encoding: gzip, deflate, br",
-+ "Accept-Language: en-US,en;q=0.9"
++ }
++
++ if(opts->target == NULL) {
++ DEBUGF(fprintf(stderr, "Error: unknown impersonation target '%s'\n",
++ target));
++ return CURLE_BAD_FUNCTION_ARGUMENT;
++ }
++
++ if(opts->httpversion != CURL_HTTP_VERSION_NONE) {
++ ret = curl_easy_setopt(data, CURLOPT_HTTP_VERSION, opts->httpversion);
++ if(ret)
++ return ret;
++ }
++
++ if (opts->ssl_version != CURL_SSLVERSION_DEFAULT) {
++ ret = curl_easy_setopt(data, CURLOPT_SSLVERSION, opts->ssl_version);
++ if(ret)
++ return ret;
++ }
++
++ if(opts->ciphers) {
++ ret = curl_easy_setopt(data, CURLOPT_SSL_CIPHER_LIST, opts->ciphers);
++ if (ret)
++ return ret;
++ }
++
++ if(opts->curves) {
++ ret = curl_easy_setopt(data, CURLOPT_SSL_EC_CURVES, opts->curves);
++ if(ret)
++ return ret;
++ }
++
++ if(opts->sig_hash_algs) {
++ ret = curl_easy_setopt(data, CURLOPT_SSL_SIG_HASH_ALGS,
++ opts->sig_hash_algs);
++ if(ret)
++ return ret;
++ }
++
++ ret = curl_easy_setopt(data, CURLOPT_SSL_ENABLE_NPN, opts->npn ? 1 : 0);
++ if(ret)
++ return ret;
++
++ ret = curl_easy_setopt(data, CURLOPT_SSL_ENABLE_ALPN, opts->alpn ? 1 : 0);
++ if(ret)
++ return ret;
++
++ ret = curl_easy_setopt(data, CURLOPT_SSL_ENABLE_ALPS, opts->alps ? 1 : 0);
++ if(ret)
++ return ret;
++
++ ret = curl_easy_setopt(data, CURLOPT_SSL_ENABLE_TICKET,
++ opts->tls_session_ticket ? 1 : 0);
++ if(ret)
++ return ret;
++
++ if(opts->cert_compression) {
++ ret = curl_easy_setopt(data,
++ CURLOPT_SSL_CERT_COMPRESSION,
++ opts->cert_compression);
++ if(ret)
++ return ret;
++ }
++
++ /* Build a linked list out of the static array of headers. */
++ for(i = 0; i < IMPERSONATE_MAX_HEADERS; i++) {
++ if(opts->http_headers[i]) {
++ headers = curl_slist_append(headers, opts->http_headers[i]);
++ if(!headers) {
++ return CURLE_OUT_OF_MEMORY;
++ }
+ }
-+ },
-+ {
-+ .target = "safari15_3",
-+ .httpversion = CURL_HTTP_VERSION_2_0,
-+ .ssl_version = CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_DEFAULT,
-+ .ciphers =
-+ "TLS_AES_128_GCM_SHA256,"
-+ "TLS_AES_256_GCM_SHA384,"
-+ "TLS_CHACHA20_POLY1305_SHA256,"
-+ "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,"
-+ "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,"
-+ "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,"
-+ "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,"
-+ "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,"
-+ "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,"
-+ "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,"
-+ "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,"
-+ "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,"
-+ "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,"
-+ "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,"
-+ "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,"
-+ "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,"
-+ "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,"
-+ "TLS_RSA_WITH_AES_256_GCM_SHA384,"
-+ "TLS_RSA_WITH_AES_128_GCM_SHA256,"
-+ "TLS_RSA_WITH_AES_256_CBC_SHA256,"
-+ "TLS_RSA_WITH_AES_128_CBC_SHA256,"
-+ "TLS_RSA_WITH_AES_256_CBC_SHA,"
-+ "TLS_RSA_WITH_AES_128_CBC_SHA,"
-+ "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,"
-+ "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,"
-+ "TLS_RSA_WITH_3DES_EDE_CBC_SHA,",
-+ .curves = "X25519:P-256:P-384:P-521",
-+ .sig_hash_algs =
-+ "ecdsa_secp256r1_sha256,"
-+ "rsa_pss_rsae_sha256,"
-+ "rsa_pkcs1_sha256,"
-+ "ecdsa_secp384r1_sha384,"
-+ "ecdsa_sha1,"
-+ "rsa_pss_rsae_sha384,"
-+ "rsa_pss_rsae_sha384,"
-+ "rsa_pkcs1_sha384,"
-+ "rsa_pss_rsae_sha512,"
-+ "rsa_pkcs1_sha512,"
-+ "rsa_pkcs1_sha1",
-+ .npn = false,
-+ .alpn = true,
-+ .alps = false,
-+ .tls_session_ticket = false,
-+ .http_headers = {
-+ "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15",
-+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
-+ "Accept-Language: en-us",
-+ "Accept-Encoding: gzip, deflate, br"
-+ },
-+ .http2_pseudo_headers_order = "mspa"
+ }
-+};
+
-+#define NUM_IMPERSONATIONS \
-+ sizeof(impersonations) / sizeof(impersonations[0])
++ if(headers) {
++ ret = curl_easy_setopt(data, CURLOPT_HTTPBASEHEADER, headers);
++ curl_slist_free_all(headers);
++ if(ret)
++ return ret;
++ }
++
++ if(opts->http2_pseudo_headers_order) {
++ ret = curl_easy_setopt(data,
++ CURLOPT_HTTP2_PSEUDO_HEADERS_ORDER,
++ opts->http2_pseudo_headers_order);
++ if(ret)
++ return ret;
++ }
++
++ /* Always enable all supported compressions. */
++ ret = curl_easy_setopt(data, CURLOPT_ACCEPT_ENCODING, "");
++ if(ret)
++ return ret;
++
++ return CURLE_OK;
++}
++
+ /*
+ * curl_easy_init() is the external interface to alloc, setup and init an
+ * easy handle that is returned. If anything goes wrong, NULL is returned.
+@@ -290,6 +405,7 @@ struct Curl_easy *curl_easy_init(void)
+ {
+ CURLcode result;
+ struct Curl_easy *data;
++ char *target;
+
+ /* Make sure we inited the global SSL stuff */
+ if(!initialized) {
+@@ -308,6 +424,22 @@ struct Curl_easy *curl_easy_init(void)
+ return NULL;
+ }
+
++ /*
++ * curl-impersonate: Hook into curl_easy_init() to set the required options
++ * from an environment variable.
++ * This is a bit hacky but allows seamless integration of libcurl-impersonate
++ * without code modifications to the app.
++ */
++ target = curl_getenv("CURL_IMPERSONATE");
++ if(target) {
++ result = curl_easy_impersonate(data, target);
++ free(target);
++ if(result) {
++ Curl_close(&data);
++ return NULL;
++ }
++ }
++
+ return data;
+ }
+
+@@ -878,6 +1010,13 @@ struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
+ outcurl->state.referer_alloc = TRUE;
+ }
+
++ if(data->state.base_headers) {
++ outcurl->state.base_headers =
++ Curl_slist_duplicate(data->state.base_headers);
++ if(!outcurl->state.base_headers)
++ goto fail;
++ }
++
+ /* Reinitialize an SSL engine for the new handle
+ * note: the engine name has already been copied by dupset */
+ if(outcurl->set.str[STRING_SSL_ENGINE]) {
+@@ -967,6 +1106,8 @@ struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
+ */
+ void curl_easy_reset(struct Curl_easy *data)
+ {
++ char *target;
++
+ Curl_free_request_state(data);
+
+ /* zero out UserDefined data: */
+@@ -991,6 +1132,12 @@ void curl_easy_reset(struct Curl_easy *data)
+ #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
+ Curl_http_auth_cleanup_digest(data);
+ #endif
++
++ target = curl_getenv("CURL_IMPERSONATE");
++ if(target) {
++ curl_easy_impersonate(data, target);
++ free(target);
++ }
+ }
+
+ /*
+diff --git a/lib/easyoptions.c b/lib/easyoptions.c
+index 04871ad1e..ce280eaa3 100644
+--- a/lib/easyoptions.c
++++ b/lib/easyoptions.c
+@@ -128,8 +128,11 @@ struct curl_easyoption Curl_easyopts[] = {
+ {"HSTS_CTRL", CURLOPT_HSTS_CTRL, CURLOT_LONG, 0},
+ {"HTTP09_ALLOWED", CURLOPT_HTTP09_ALLOWED, CURLOT_LONG, 0},
+ {"HTTP200ALIASES", CURLOPT_HTTP200ALIASES, CURLOT_SLIST, 0},
++ {"HTTP2_PSEUDO_HEADERS_ORDER", CURLOPT_HTTP2_PSEUDO_HEADERS_ORDER,
++ CURLOT_STRING, 0},
+ {"HTTPAUTH", CURLOPT_HTTPAUTH, CURLOT_VALUES, 0},
+ {"HTTPGET", CURLOPT_HTTPGET, CURLOT_LONG, 0},
++ {"HTTPBASEHEADER", CURLOPT_HTTPBASEHEADER, CURLOT_SLIST, 0},
+ {"HTTPHEADER", CURLOPT_HTTPHEADER, CURLOT_SLIST, 0},
+ {"HTTPPOST", CURLOPT_HTTPPOST, CURLOT_OBJECT, 0},
+ {"HTTPPROXYTUNNEL", CURLOPT_HTTPPROXYTUNNEL, CURLOT_LONG, 0},
+@@ -293,15 +296,19 @@ struct curl_easyoption Curl_easyopts[] = {
+ {"SSLKEYTYPE", CURLOPT_SSLKEYTYPE, CURLOT_STRING, 0},
+ {"SSLKEY_BLOB", CURLOPT_SSLKEY_BLOB, CURLOT_BLOB, 0},
+ {"SSLVERSION", CURLOPT_SSLVERSION, CURLOT_VALUES, 0},
++ {"SSL_CERT_COMPRESSION", CURLOPT_SSL_CERT_COMPRESSION, CURLOT_STRING, 0},
+ {"SSL_CIPHER_LIST", CURLOPT_SSL_CIPHER_LIST, CURLOT_STRING, 0},
+ {"SSL_CTX_DATA", CURLOPT_SSL_CTX_DATA, CURLOT_CBPTR, 0},
+ {"SSL_CTX_FUNCTION", CURLOPT_SSL_CTX_FUNCTION, CURLOT_FUNCTION, 0},
+ {"SSL_EC_CURVES", CURLOPT_SSL_EC_CURVES, CURLOT_STRING, 0},
+ {"SSL_ENABLE_ALPN", CURLOPT_SSL_ENABLE_ALPN, CURLOT_LONG, 0},
++ {"SSL_ENABLE_ALPS", CURLOPT_SSL_ENABLE_ALPS, CURLOT_LONG, 0},
+ {"SSL_ENABLE_NPN", CURLOPT_SSL_ENABLE_NPN, CURLOT_LONG, 0},
++ {"SSL_ENABLE_TICKET", CURLOPT_SSL_ENABLE_TICKET, CURLOT_LONG, 0},
+ {"SSL_FALSESTART", CURLOPT_SSL_FALSESTART, CURLOT_LONG, 0},
+ {"SSL_OPTIONS", CURLOPT_SSL_OPTIONS, CURLOT_VALUES, 0},
+ {"SSL_SESSIONID_CACHE", CURLOPT_SSL_SESSIONID_CACHE, CURLOT_LONG, 0},
++ {"SSL_SIG_HASH_ALGS", CURLOPT_SSL_SIG_HASH_ALGS, CURLOT_STRING, 0},
+ {"SSL_VERIFYHOST", CURLOPT_SSL_VERIFYHOST, CURLOT_LONG, 0},
+ {"SSL_VERIFYPEER", CURLOPT_SSL_VERIFYPEER, CURLOT_LONG, 0},
+ {"SSL_VERIFYSTATUS", CURLOPT_SSL_VERIFYSTATUS, CURLOT_LONG, 0},
+@@ -360,6 +367,6 @@ struct curl_easyoption Curl_easyopts[] = {
+ */
+ int Curl_easyopts_check(void)
+ {
+- return ((CURLOPT_LASTENTRY%10000) != (315 + 1));
++ return ((CURLOPT_LASTENTRY%10000) != (320 + 1));
+ }
+ #endif
+diff --git a/lib/http.c b/lib/http.c
+index f08a343e3..2bbce4b23 100644
+--- a/lib/http.c
++++ b/lib/http.c
+@@ -84,6 +84,7 @@
+ #include "altsvc.h"
+ #include "hsts.h"
+ #include "c-hyper.h"
++#include "slist.h"
+
+ /* The last 3 #include files should be in this order */
+ #include "curl_printf.h"
+@@ -1795,6 +1796,15 @@ CURLcode Curl_add_custom_headers(struct Curl_easy *data,
+ int numlists = 1; /* by default */
+ int i;
+
++ /*
++ * curl-impersonate: Use the merged list of headers if it exists (i.e. when
++ * the CURLOPT_HTTPBASEHEADER option was set.
++ */
++ struct curl_slist *noproxyheaders =
++ (data->state.merged_headers ?
++ data->state.merged_headers :
++ data->set.headers);
+
+ #ifndef CURL_DISABLE_PROXY
+ enum proxy_use proxy;
+
+@@ -1806,10 +1816,10 @@ CURLcode Curl_add_custom_headers(struct Curl_easy *data,
+
+ switch(proxy) {
+ case HEADER_SERVER:
+- h[0] = data->set.headers;
++ h[0] = noproxyheaders;
+ break;
+ case HEADER_PROXY:
+- h[0] = data->set.headers;
++ h[0] = noproxyheaders;
+ if(data->set.sep_headers) {
+ h[1] = data->set.proxyheaders;
+ numlists++;
+@@ -1819,12 +1829,12 @@ CURLcode Curl_add_custom_headers(struct Curl_easy *data,
+ if(data->set.sep_headers)
+ h[0] = data->set.proxyheaders;
+ else
+- h[0] = data->set.headers;
++ h[0] = noproxyheaders;
+ break;
+ }
+ #else
+ (void)is_connect;
+- h[0] = data->set.headers;
++ h[0] = noproxyheaders;
+ #endif
+
+ /* loop through one or two lists */
+@@ -2059,6 +2069,92 @@ void Curl_http_method(struct Curl_easy *data, struct connectdata *conn,
+ *reqp = httpreq;
+ }
+
+/*
+ * curl-impersonate:
-+ * Call curl_easy_setopt() with all the needed options as defined in the
-+ * 'impersonations' array.
-+ * */
-+CURLcode curl_easy_impersonate(struct Curl_easy *data, const char *target)
++ * Create a new linked list of headers.
++ * The new list is a merge between the "base" headers and the application given
++ * headers. The "base" headers contain curl-impersonate's list of headers
++ * used by default by the impersonated browser.
++ *
++ * The application given headers will override the "base" headers if supplied.
++ */
++CURLcode Curl_http_merge_headers(struct Curl_easy *data)
+{
+ int i;
+ int ret;
-+ const struct impersonate_opts *opts = NULL;
-+ struct curl_slist *headers = NULL;
-+
-+ for(i = 0; i < NUM_IMPERSONATIONS; i++) {
-+ if (Curl_safe_strcasecompare(target, impersonations[i].target)) {
-+ opts = &impersonations[i];
-+ break;
-+ }
-+ }
-+
-+ if(!opts) {
-+ DEBUGF(fprintf(stderr, "Error: unknown impersonation target '%s'\n",
-+ target));
-+ return CURLE_BAD_FUNCTION_ARGUMENT;
-+ }
-+
-+ if(opts->httpversion != CURL_HTTP_VERSION_NONE) {
-+ ret = curl_easy_setopt(data, CURLOPT_HTTP_VERSION, opts->httpversion);
-+ if(ret)
-+ return ret;
-+ }
-+
-+ if (opts->ssl_version != CURL_SSLVERSION_DEFAULT) {
-+ ret = curl_easy_setopt(data, CURLOPT_SSLVERSION, opts->ssl_version);
-+ if(ret)
-+ return ret;
-+ }
++ struct curl_slist *head;
++ struct curl_slist *dup = NULL;
++ struct curl_slist *new_list = NULL;
+
-+ if(opts->ciphers) {
-+ ret = curl_easy_setopt(data, CURLOPT_SSL_CIPHER_LIST, opts->ciphers);
-+ if (ret)
-+ return ret;
-+ }
++ if (!data->state.base_headers)
++ return CURLE_OK;
+
-+ if(opts->curves) {
-+ ret = curl_easy_setopt(data, CURLOPT_SSL_EC_CURVES, opts->curves);
-+ if(ret)
-+ return ret;
++ /* Duplicate the list for temporary use. */
++ if (data->set.headers) {
++ dup = Curl_slist_duplicate(data->set.headers);
++ if(!dup)
++ return CURLE_OUT_OF_MEMORY;
+ }
+
-+ if(opts->sig_hash_algs) {
-+ ret = curl_easy_setopt(data, CURLOPT_SSL_SIG_HASH_ALGS,
-+ opts->sig_hash_algs);
-+ if(ret)
-+ return ret;
-+ }
++ for(head = data->state.base_headers; head; head = head->next) {
++ char *sep;
++ size_t prefix_len;
++ bool found = FALSE;
++ struct curl_slist *head2;
+
-+ ret = curl_easy_setopt(data, CURLOPT_SSL_ENABLE_NPN, opts->npn ? 1 : 0);
-+ if(ret)
-+ return ret;
++ sep = strchr(head->data, ':');
++ if(!sep)
++ continue;
+
-+ ret = curl_easy_setopt(data, CURLOPT_SSL_ENABLE_ALPN, opts->alpn ? 1 : 0);
-+ if(ret)
-+ return ret;
++ prefix_len = sep - head->data;
+
-+ ret = curl_easy_setopt(data, CURLOPT_SSL_ENABLE_ALPS, opts->alps ? 1 : 0);
-+ if(ret)
-+ return ret;
++ /* Check if this header was added by the application. */
++ for(head2 = dup; head2; head2 = head2->next) {
++ if(head2->data &&
++ strncasecompare(head2->data, head->data, prefix_len) &&
++ Curl_headersep(head2->data[prefix_len]) ) {
++ new_list = curl_slist_append(new_list, head2->data);
++ /* Free and set to NULL to mark that it's been added. */
++ Curl_safefree(head2->data);
++ found = TRUE;
++ break;
++ }
++ }
+
-+ ret = curl_easy_setopt(data, CURLOPT_SSL_ENABLE_TICKET,
-+ opts->tls_session_ticket ? 1 : 0);
-+ if(ret)
-+ return ret;
++ if (!found) {
++ new_list = curl_slist_append(new_list, head->data);
++ }
+
-+ if(opts->cert_compression) {
-+ ret = curl_easy_setopt(data,
-+ CURLOPT_SSL_CERT_COMPRESSION,
-+ opts->cert_compression);
-+ if(ret)
-+ return ret;
++ if (!new_list) {
++ ret = CURLE_OUT_OF_MEMORY;
++ goto fail;
++ }
+ }
+
-+ /* Build a linked list out of the static array of headers. */
-+ for(i = 0; i < IMPERSONATE_MAX_HEADERS; i++) {
-+ if(opts->http_headers[i]) {
-+ headers = curl_slist_append(headers, opts->http_headers[i]);
-+ if(!headers) {
-+ return CURLE_OUT_OF_MEMORY;
++ /* Now go over any additional application-supplied headers. */
++ for(head = dup; head; head = head->next) {
++ if(head->data) {
++ new_list = curl_slist_append(new_list, head->data);
++ if(!new_list) {
++ ret = CURLE_OUT_OF_MEMORY;
++ goto fail;
+ }
+ }
+ }
+
-+ if(headers) {
-+ ret = curl_easy_setopt(data, CURLOPT_HTTPBASEHEADER, headers);
-+ curl_slist_free_all(headers);
-+ if(ret)
-+ return ret;
-+ }
-+
-+ if(opts->http2_pseudo_headers_order) {
-+ ret = curl_easy_setopt(data,
-+ CURLOPT_HTTP2_PSEUDO_HEADERS_ORDER,
-+ opts->http2_pseudo_headers_order);
-+ if(ret)
-+ return ret;
-+ }
-+
-+ /* Always enable all supported compressions. */
-+ ret = curl_easy_setopt(data, CURLOPT_ACCEPT_ENCODING, "");
-+ if(ret)
-+ return ret;
++ curl_slist_free_all(dup);
++ /* Save the new, merged list separately, so it can be freed later. */
++ curl_slist_free_all(data->state.merged_headers);
++ data->state.merged_headers = new_list;
+
+ return CURLE_OK;
++
++fail:
++ Curl_safefree(dup);
++ curl_slist_free_all(new_list);
++ return ret;
+}
+
- /*
- * curl_easy_init() is the external interface to alloc, setup and init an
- * easy handle that is returned. If anything goes wrong, NULL is returned.
-@@ -290,6 +739,7 @@ struct Curl_easy *curl_easy_init(void)
+ CURLcode Curl_http_useragent(struct Curl_easy *data)
{
- CURLcode result;
- struct Curl_easy *data;
-+ char *target;
-
- /* Make sure we inited the global SSL stuff */
- if(!initialized) {
-@@ -308,6 +758,22 @@ struct Curl_easy *curl_easy_init(void)
- return NULL;
- }
+ /* The User-Agent string might have been allocated in url.c already, because
+@@ -3063,6 +3159,11 @@ CURLcode Curl_http(struct Curl_easy *data, bool *done)
+ http = data->req.p.http;
+ DEBUGASSERT(http);
-+ /*
-+ * curl-impersonate: Hook into curl_easy_init() to set the required options
-+ * from an environment variable.
-+ * This is a bit hacky but allows seamless integration of libcurl-impersonate
-+ * without code modifications to the app.
-+ */
-+ target = curl_getenv("CURL_IMPERSONATE");
-+ if(target) {
-+ result = curl_easy_impersonate(data, target);
-+ free(target);
-+ if(result) {
-+ Curl_close(&data);
-+ return NULL;
-+ }
-+ }
++ /* curl-impersonate: Add HTTP headers to impersonate real browsers. */
++ result = Curl_http_merge_headers(data);
++ if (result)
++ return result;
+
- return data;
- }
+ result = Curl_http_host(data, conn);
+ if(result)
+ return result;
+diff --git a/lib/http.h b/lib/http.h
+index b4aaba2a2..0d716640c 100644
+--- a/lib/http.h
++++ b/lib/http.h
+@@ -278,7 +278,8 @@ struct http_conn {
+ struct h2settings settings;
-@@ -878,6 +1344,13 @@ struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
- outcurl->state.referer_alloc = TRUE;
- }
+ /* list of settings that will be sent */
+- nghttp2_settings_entry local_settings[3];
++ /* curl-impersonate: Align HTTP/2 settings to Chrome's */
++ nghttp2_settings_entry local_settings[4];
+ size_t local_settings_num;
+ #else
+ int unused; /* prevent a compiler warning */
+diff --git a/lib/http2.c b/lib/http2.c
+index e74400a4c..aec67c975 100644
+--- a/lib/http2.c
++++ b/lib/http2.c
+@@ -41,6 +41,7 @@
+ #include "curl_printf.h"
+ #include "curl_memory.h"
+ #include "memdebug.h"
++#include "rand.h"
-+ if(data->state.base_headers) {
-+ outcurl->state.base_headers =
-+ Curl_slist_duplicate(data->state.base_headers);
-+ if(!outcurl->state.base_headers)
-+ goto fail;
-+ }
-+
- /* Reinitialize an SSL engine for the new handle
- * note: the engine name has already been copied by dupset */
- if(outcurl->set.str[STRING_SSL_ENGINE]) {
-@@ -967,6 +1440,8 @@ struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
- */
- void curl_easy_reset(struct Curl_easy *data)
- {
-+ char *target;
-+
- Curl_free_request_state(data);
+ #define H2_BUFSIZE 32768
- /* zero out UserDefined data: */
-@@ -991,6 +1466,12 @@ void curl_easy_reset(struct Curl_easy *data)
- #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
- Curl_http_auth_cleanup_digest(data);
+@@ -56,7 +57,7 @@
+ #define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1
#endif
+
+-#define HTTP2_HUGE_WINDOW_SIZE (32 * 1024 * 1024) /* 32 MB */
++#define HTTP2_HUGE_WINDOW_SIZE (15 * 1024 * 1024) /* 15 MB */
+
+ #ifdef DEBUG_HTTP2
+ #define H2BUGF(x) x
+@@ -75,13 +76,20 @@ static int h2_process_pending_input(struct Curl_easy *data,
+ struct http_conn *httpc,
+ CURLcode *err);
+
++/*
++ * curl-impersonate: Use Chrome's default HTTP/2 stream weight.
++ */
++
++#define CHROME_DEFAULT_STREAM_WEIGHT (256)
+
-+ target = curl_getenv("CURL_IMPERSONATE");
-+ if(target) {
-+ curl_easy_impersonate(data, target);
-+ free(target);
-+ }
+ /*
+ * Curl_http2_init_state() is called when the easy handle is created and
+ * allows for HTTP/2 specific init of state.
+ */
+ void Curl_http2_init_state(struct UrlState *state)
+ {
+- state->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
++ state->stream_weight = CHROME_DEFAULT_STREAM_WEIGHT;
++ state->stream_depends_e = TRUE;
}
/*
-diff --git a/lib/easyoptions.c b/lib/easyoptions.c
-index 04871ad1e..ce280eaa3 100644
---- a/lib/easyoptions.c
-+++ b/lib/easyoptions.c
-@@ -128,8 +128,11 @@ struct curl_easyoption Curl_easyopts[] = {
- {"HSTS_CTRL", CURLOPT_HSTS_CTRL, CURLOT_LONG, 0},
- {"HTTP09_ALLOWED", CURLOPT_HTTP09_ALLOWED, CURLOT_LONG, 0},
- {"HTTP200ALIASES", CURLOPT_HTTP200ALIASES, CURLOT_SLIST, 0},
-+ {"HTTP2_PSEUDO_HEADERS_ORDER", CURLOPT_HTTP2_PSEUDO_HEADERS_ORDER,
-+ CURLOT_STRING, 0},
- {"HTTPAUTH", CURLOPT_HTTPAUTH, CURLOT_VALUES, 0},
- {"HTTPGET", CURLOPT_HTTPGET, CURLOT_LONG, 0},
-+ {"HTTPBASEHEADER", CURLOPT_HTTPBASEHEADER, CURLOT_SLIST, 0},
- {"HTTPHEADER", CURLOPT_HTTPHEADER, CURLOT_SLIST, 0},
- {"HTTPPOST", CURLOPT_HTTPPOST, CURLOT_OBJECT, 0},
- {"HTTPPROXYTUNNEL", CURLOPT_HTTPPROXYTUNNEL, CURLOT_LONG, 0},
-@@ -293,15 +296,19 @@ struct curl_easyoption Curl_easyopts[] = {
- {"SSLKEYTYPE", CURLOPT_SSLKEYTYPE, CURLOT_STRING, 0},
- {"SSLKEY_BLOB", CURLOPT_SSLKEY_BLOB, CURLOT_BLOB, 0},
- {"SSLVERSION", CURLOPT_SSLVERSION, CURLOT_VALUES, 0},
-+ {"SSL_CERT_COMPRESSION", CURLOPT_SSL_CERT_COMPRESSION, CURLOT_STRING, 0},
- {"SSL_CIPHER_LIST", CURLOPT_SSL_CIPHER_LIST, CURLOT_STRING, 0},
- {"SSL_CTX_DATA", CURLOPT_SSL_CTX_DATA, CURLOT_CBPTR, 0},
- {"SSL_CTX_FUNCTION", CURLOPT_SSL_CTX_FUNCTION, CURLOT_FUNCTION, 0},
- {"SSL_EC_CURVES", CURLOPT_SSL_EC_CURVES, CURLOT_STRING, 0},
- {"SSL_ENABLE_ALPN", CURLOPT_SSL_ENABLE_ALPN, CURLOT_LONG, 0},
-+ {"SSL_ENABLE_ALPS", CURLOPT_SSL_ENABLE_ALPS, CURLOT_LONG, 0},
- {"SSL_ENABLE_NPN", CURLOPT_SSL_ENABLE_NPN, CURLOT_LONG, 0},
-+ {"SSL_ENABLE_TICKET", CURLOPT_SSL_ENABLE_TICKET, CURLOT_LONG, 0},
- {"SSL_FALSESTART", CURLOPT_SSL_FALSESTART, CURLOT_LONG, 0},
- {"SSL_OPTIONS", CURLOPT_SSL_OPTIONS, CURLOT_VALUES, 0},
- {"SSL_SESSIONID_CACHE", CURLOPT_SSL_SESSIONID_CACHE, CURLOT_LONG, 0},
-+ {"SSL_SIG_HASH_ALGS", CURLOPT_SSL_SIG_HASH_ALGS, CURLOT_STRING, 0},
- {"SSL_VERIFYHOST", CURLOPT_SSL_VERIFYHOST, CURLOT_LONG, 0},
- {"SSL_VERIFYPEER", CURLOPT_SSL_VERIFYPEER, CURLOT_LONG, 0},
- {"SSL_VERIFYSTATUS", CURLOPT_SSL_VERIFYSTATUS, CURLOT_LONG, 0},
-@@ -360,6 +367,6 @@ struct curl_easyoption Curl_easyopts[] = {
+@@ -90,7 +98,8 @@ void Curl_http2_init_state(struct UrlState *state)
*/
- int Curl_easyopts_check(void)
+ void Curl_http2_init_userset(struct UserDefined *set)
{
-- return ((CURLOPT_LASTENTRY%10000) != (315 + 1));
-+ return ((CURLOPT_LASTENTRY%10000) != (320 + 1));
+- set->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
++ set->stream_weight = CHROME_DEFAULT_STREAM_WEIGHT;
++ set->stream_depends_e = TRUE;
}
- #endif
-diff --git a/lib/http.c b/lib/http.c
-index f08a343e3..2bbce4b23 100644
---- a/lib/http.c
-+++ b/lib/http.c
-@@ -84,6 +84,7 @@
- #include "altsvc.h"
- #include "hsts.h"
- #include "c-hyper.h"
-+#include "slist.h"
- /* The last 3 #include files should be in this order */
- #include "curl_printf.h"
-@@ -1795,6 +1796,15 @@ CURLcode Curl_add_custom_headers(struct Curl_easy *data,
- int numlists = 1; /* by default */
- int i;
+ static int http2_getsock(struct Curl_easy *data,
+@@ -1193,16 +1202,30 @@ static void populate_settings(struct Curl_easy *data,
+ {
+ nghttp2_settings_entry *iv = httpc->local_settings;
-+ /*
-+ * curl-impersonate: Use the merged list of headers if it exists (i.e. when
-+ * the CURLOPT_HTTPBASEHEADER option was set.
-+ */
-+ struct curl_slist *noproxyheaders =
-+ (data->state.merged_headers ?
-+ data->state.merged_headers :
-+ data->set.headers);
+- iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
+- iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
++ /* curl-impersonate: Align HTTP/2 settings to Chrome's */
++ iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
++ iv[0].value = 0x10000;
+
- #ifndef CURL_DISABLE_PROXY
- enum proxy_use proxy;
++ iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
++ iv[1].value = Curl_multi_max_concurrent_streams(data->multi);
-@@ -1806,10 +1816,10 @@ CURLcode Curl_add_custom_headers(struct Curl_easy *data,
+- iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
+- iv[1].value = HTTP2_HUGE_WINDOW_SIZE;
++ iv[2].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
++ iv[2].value = 0x600000;
- switch(proxy) {
- case HEADER_SERVER:
-- h[0] = data->set.headers;
-+ h[0] = noproxyheaders;
- break;
- case HEADER_PROXY:
-- h[0] = data->set.headers;
-+ h[0] = noproxyheaders;
- if(data->set.sep_headers) {
- h[1] = data->set.proxyheaders;
- numlists++;
-@@ -1819,12 +1829,12 @@ CURLcode Curl_add_custom_headers(struct Curl_easy *data,
- if(data->set.sep_headers)
- h[0] = data->set.proxyheaders;
- else
-- h[0] = data->set.headers;
-+ h[0] = noproxyheaders;
- break;
+- iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
+- iv[2].value = data->multi->push_cb != NULL;
++ iv[3].settings_id = NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE;
++ iv[3].value = 0x40000;
+
+- httpc->local_settings_num = 3;
++ // iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
++ // iv[2].value = data->multi->push_cb != NULL;
++
++ // curl-impersonate:
++ // Up until Chrome 98, there was a randomly chosen setting number in the
++ // HTTP2 SETTINGS frame. This might be something similar to TLS GREASE.
++ // However, it seems to have been removed since.
++ // Curl_rand(data, (unsigned char *)&iv[4].settings_id, sizeof(iv[4].settings_id));
++ // Curl_rand(data, (unsigned char *)&iv[4].value, sizeof(iv[4].value));
++
++ httpc->local_settings_num = 4;
+ }
+
+ void Curl_http2_done(struct Curl_easy *data, bool premature)
+@@ -1816,10 +1839,6 @@ static ssize_t http2_recv(struct Curl_easy *data, int sockindex,
+ return -1;
+ }
+
+-/* Index where :authority header field will appear in request header
+- field list. */
+-#define AUTHORITY_DST_IDX 3
+-
+ /* USHRT_MAX is 65535 == 0xffff */
+ #define HEADER_OVERFLOW(x) \
+ (x.namelen > 0xffff || x.valuelen > 0xffff - x.namelen)
+@@ -1890,6 +1909,53 @@ static header_instruction inspect_header(const char *name, size_t namelen,
+ }
+ }
+
++/*
++ * curl-impersonate:
++ * Determine the position of HTTP/2 pseudo headers.
++ * The pseudo headers ":method", ":path", ":scheme", ":authority"
++ * are sent in different order by different browsers. An important part of the
++ * impersonation is ordering them like the browser does.
++ */
++static int http2_pseudo_header_index(struct Curl_easy *data,
++ const char *header,
++ size_t *index)
++{
++ char *off;
++ // Use the Chrome ordering by default:
++ // :method, :authority, :scheme, :path
++ char *order = "masp";
++ if(data->set.str[STRING_HTTP2_PSEUDO_HEADERS_ORDER])
++ order = data->set.str[STRING_HTTP2_PSEUDO_HEADERS_ORDER];
++
++ if(strlen(order) != 4)
++ return CURLE_BAD_FUNCTION_ARGUMENT;
++
++ // :method should always be first
++ if(order[0] != 'm')
++ return CURLE_BAD_FUNCTION_ARGUMENT;
++
++ // All pseudo-headers must be present
++ if(!strchr(order, 'm') ||
++ !strchr(order, 'a') ||
++ !strchr(order, 's') ||
++ !strchr(order, 'p'))
++ return CURLE_BAD_FUNCTION_ARGUMENT;
++
++ if(strcasecompare(header, ":method"))
++ off = strchr(order, 'm');
++ else if(strcasecompare(header, ":authority"))
++ off = strchr(order, 'a');
++ else if(strcasecompare(header, ":scheme"))
++ off = strchr(order, 's');
++ else if(strcasecompare(header, ":path"))
++ off = strchr(order, 'p');
++ else
++ return CURLE_BAD_FUNCTION_ARGUMENT;
++
++ *index = off - order;
++ return CURLE_OK;
++}
++
+ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
+ const void *mem, size_t len, CURLcode *err)
+ {
+@@ -1905,6 +1971,7 @@ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
+ nghttp2_nv *nva = NULL;
+ size_t nheader;
+ size_t i;
++ size_t header_idx;
+ size_t authority_idx;
+ char *hdbuf = (char *)mem;
+ char *end, *line_end;
+@@ -2010,12 +2077,21 @@ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
+ end = memchr(hdbuf, ' ', line_end - hdbuf);
+ if(!end || end == hdbuf)
+ goto fail;
+- nva[0].name = (unsigned char *)":method";
+- nva[0].namelen = strlen((char *)nva[0].name);
+- nva[0].value = (unsigned char *)hdbuf;
+- nva[0].valuelen = (size_t)(end - hdbuf);
+- nva[0].flags = NGHTTP2_NV_FLAG_NONE;
+- if(HEADER_OVERFLOW(nva[0])) {
++ /* curl-impersonate: Set the index of ":method" based on libcurl option */
++ if(http2_pseudo_header_index(data, ":authority", &authority_idx))
++ goto fail;
++ if(http2_pseudo_header_index(data, ":method", &header_idx))
++ goto fail;
++ /* This is needed to overcome the fact that curl will only move the authority
++ * header into its place after all other headers have been placed. */
++ if(header_idx > authority_idx)
++ header_idx--;
++ nva[header_idx].name = (unsigned char *)":method";
++ nva[header_idx].namelen = strlen((char *)nva[header_idx].name);
++ nva[header_idx].value = (unsigned char *)hdbuf;
++ nva[header_idx].valuelen = (size_t)(end - hdbuf);
++ nva[header_idx].flags = NGHTTP2_NV_FLAG_NONE;
++ if(HEADER_OVERFLOW(nva[header_idx])) {
+ failf(data, "Failed sending HTTP request: Header overflow");
+ goto fail;
+ }
+@@ -2032,25 +2108,35 @@ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
+ }
+ if(!end || end == hdbuf)
+ goto fail;
+- nva[1].name = (unsigned char *)":path";
+- nva[1].namelen = strlen((char *)nva[1].name);
+- nva[1].value = (unsigned char *)hdbuf;
+- nva[1].valuelen = (size_t)(end - hdbuf);
+- nva[1].flags = NGHTTP2_NV_FLAG_NONE;
+- if(HEADER_OVERFLOW(nva[1])) {
++ /* curl-impersonate: Set the index of ":path" based on libcurl option */
++ if(http2_pseudo_header_index(data, ":path", &header_idx))
++ goto fail;
++ if(header_idx > authority_idx)
++ header_idx--;
++ nva[header_idx].name = (unsigned char *)":path";
++ nva[header_idx].namelen = strlen((char *)nva[header_idx].name);
++ nva[header_idx].value = (unsigned char *)hdbuf;
++ nva[header_idx].valuelen = (size_t)(end - hdbuf);
++ nva[header_idx].flags = NGHTTP2_NV_FLAG_NONE;
++ if(HEADER_OVERFLOW(nva[header_idx])) {
+ failf(data, "Failed sending HTTP request: Header overflow");
+ goto fail;
}
- #else
- (void)is_connect;
-- h[0] = data->set.headers;
-+ h[0] = noproxyheaders;
- #endif
- /* loop through one or two lists */
-@@ -2059,6 +2069,92 @@ void Curl_http_method(struct Curl_easy *data, struct connectdata *conn,
- *reqp = httpreq;
- }
+- nva[2].name = (unsigned char *)":scheme";
+- nva[2].namelen = strlen((char *)nva[2].name);
++ /* curl-impersonate: Set the index of ":scheme" based on libcurl option */
++ if(http2_pseudo_header_index(data, ":scheme", &header_idx))
++ goto fail;
++ if(header_idx > authority_idx)
++ header_idx--;
++ nva[header_idx].name = (unsigned char *)":scheme";
++ nva[header_idx].namelen = strlen((char *)nva[header_idx].name);
+ if(conn->handler->flags & PROTOPT_SSL)
+- nva[2].value = (unsigned char *)"https";
++ nva[header_idx].value = (unsigned char *)"https";
+ else
+- nva[2].value = (unsigned char *)"http";
+- nva[2].valuelen = strlen((char *)nva[2].value);
+- nva[2].flags = NGHTTP2_NV_FLAG_NONE;
+- if(HEADER_OVERFLOW(nva[2])) {
++ nva[header_idx].value = (unsigned char *)"http";
++ nva[header_idx].valuelen = strlen((char *)nva[header_idx].value);
++ nva[header_idx].flags = NGHTTP2_NV_FLAG_NONE;
++ if(HEADER_OVERFLOW(nva[header_idx])) {
+ failf(data, "Failed sending HTTP request: Header overflow");
+ goto fail;
+ }
+@@ -2117,10 +2203,13 @@ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
+ ++i;
+ }
-+/*
-+ * curl-impersonate:
-+ * Create a new linked list of headers.
-+ * The new list is a merge between the "base" headers and the application given
-+ * headers. The "base" headers contain curl-impersonate's list of headers
-+ * used by default by the impersonated browser.
-+ *
-+ * The application given headers will override the "base" headers if supplied.
-+ */
-+CURLcode Curl_http_merge_headers(struct Curl_easy *data)
-+{
-+ int i;
-+ int ret;
-+ struct curl_slist *head;
-+ struct curl_slist *dup = NULL;
-+ struct curl_slist *new_list = NULL;
-+
-+ if (!data->state.base_headers)
-+ return CURLE_OK;
-+
-+ /* Duplicate the list for temporary use. */
-+ if (data->set.headers) {
-+ dup = Curl_slist_duplicate(data->set.headers);
-+ if(!dup)
-+ return CURLE_OUT_OF_MEMORY;
-+ }
-+
-+ for(head = data->state.base_headers; head; head = head->next) {
-+ char *sep;
-+ size_t prefix_len;
-+ bool found = FALSE;
-+ struct curl_slist *head2;
-+
-+ sep = strchr(head->data, ':');
-+ if(!sep)
-+ continue;
++ /* curl-impersonate: Set the index of ":authority" based on libcurl option */
++ if(http2_pseudo_header_index(data, ":authority", &header_idx))
++ goto fail;
+ /* :authority must come before non-pseudo header fields */
+- if(authority_idx && authority_idx != AUTHORITY_DST_IDX) {
++ if(authority_idx && authority_idx != header_idx) {
+ nghttp2_nv authority = nva[authority_idx];
+- for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
++ for(i = authority_idx; i > header_idx; --i) {
+ nva[i] = nva[i - 1];
+ }
+ nva[i] = authority;
+diff --git a/lib/http2.h b/lib/http2.h
+index d6986d97f..fa5c90e7f 100644
+--- a/lib/http2.h
++++ b/lib/http2.h
+@@ -29,7 +29,8 @@
+
+ /* value for MAX_CONCURRENT_STREAMS we use until we get an updated setting
+ from the peer */
+-#define DEFAULT_MAX_CONCURRENT_STREAMS 100
++/* curl-impersonate: Use 1000 concurrent streams like Chrome. */
++#define DEFAULT_MAX_CONCURRENT_STREAMS 1000
+
+ /*
+ * Store nghttp2 version info in this buffer.
+diff --git a/lib/impersonate.c b/lib/impersonate.c
+new file mode 100644
+index 000000000..10154a5bc
+--- /dev/null
++++ b/lib/impersonate.c
+@@ -0,0 +1,306 @@
++#include "curl_setup.h"
+
-+ prefix_len = sep - head->data;
++#include "impersonate.h"
+
-+ /* Check if this header was added by the application. */
-+ for(head2 = dup; head2; head2 = head2->next) {
-+ if(head2->data &&
-+ strncasecompare(head2->data, head->data, prefix_len) &&
-+ Curl_headersep(head2->data[prefix_len]) ) {
-+ new_list = curl_slist_append(new_list, head2->data);
-+ /* Free and set to NULL to mark that it's been added. */
-+ Curl_safefree(head2->data);
-+ found = TRUE;
-+ break;
-+ }
++const struct impersonate_opts impersonations[] = {
++ {
++ .target = "chrome99",
++ .httpversion = CURL_HTTP_VERSION_2_0,
++ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
++ .ciphers =
++ "TLS_AES_128_GCM_SHA256,"
++ "TLS_AES_256_GCM_SHA384,"
++ "TLS_CHACHA20_POLY1305_SHA256,"
++ "ECDHE-ECDSA-AES128-GCM-SHA256,"
++ "ECDHE-RSA-AES128-GCM-SHA256,"
++ "ECDHE-ECDSA-AES256-GCM-SHA384,"
++ "ECDHE-RSA-AES256-GCM-SHA384,"
++ "ECDHE-ECDSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-AES128-SHA,"
++ "ECDHE-RSA-AES256-SHA,"
++ "AES128-GCM-SHA256,"
++ "AES256-GCM-SHA384,"
++ "AES128-SHA,"
++ "AES256-SHA",
++ .npn = false,
++ .alpn = true,
++ .alps = true,
++ .tls_session_ticket = true,
++ .cert_compression = "brotli",
++ .http_headers = {
++ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
++ "sec-ch-ua-mobile: ?0",
++ "sec-ch-ua-platform: \"Windows\"",
++ "Upgrade-Insecure-Requests: 1",
++ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
++ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
++ "Sec-Fetch-Site: none",
++ "Sec-Fetch-Mode: navigate",
++ "Sec-Fetch-User: ?1",
++ "Sec-Fetch-Dest: document",
++ "Accept-Encoding: gzip, deflate, br",
++ "Accept-Language: en-US,en;q=0.9"
+ }
-+
-+ if (!found) {
-+ new_list = curl_slist_append(new_list, head->data);
++ },
++ {
++ .target = "chrome100",
++ .httpversion = CURL_HTTP_VERSION_2_0,
++ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
++ .ciphers =
++ "TLS_AES_128_GCM_SHA256,"
++ "TLS_AES_256_GCM_SHA384,"
++ "TLS_CHACHA20_POLY1305_SHA256,"
++ "ECDHE-ECDSA-AES128-GCM-SHA256,"
++ "ECDHE-RSA-AES128-GCM-SHA256,"
++ "ECDHE-ECDSA-AES256-GCM-SHA384,"
++ "ECDHE-RSA-AES256-GCM-SHA384,"
++ "ECDHE-ECDSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-AES128-SHA,"
++ "ECDHE-RSA-AES256-SHA,"
++ "AES128-GCM-SHA256,"
++ "AES256-GCM-SHA384,"
++ "AES128-SHA,"
++ "AES256-SHA",
++ .npn = false,
++ .alpn = true,
++ .alps = true,
++ .tls_session_ticket = true,
++ .cert_compression = "brotli",
++ .http_headers = {
++ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"",
++ "sec-ch-ua-mobile: ?0",
++ "sec-ch-ua-platform: \"Windows\"",
++ "Upgrade-Insecure-Requests: 1",
++ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36",
++ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
++ "Sec-Fetch-Site: none",
++ "Sec-Fetch-Mode: navigate",
++ "Sec-Fetch-User: ?1",
++ "Sec-Fetch-Dest: document",
++ "Accept-Encoding: gzip, deflate, br",
++ "Accept-Language: en-US,en;q=0.9"
++ }
++ },
++ {
++ .target = "chrome101",
++ .httpversion = CURL_HTTP_VERSION_2_0,
++ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
++ .ciphers =
++ "TLS_AES_128_GCM_SHA256,"
++ "TLS_AES_256_GCM_SHA384,"
++ "TLS_CHACHA20_POLY1305_SHA256,"
++ "ECDHE-ECDSA-AES128-GCM-SHA256,"
++ "ECDHE-RSA-AES128-GCM-SHA256,"
++ "ECDHE-ECDSA-AES256-GCM-SHA384,"
++ "ECDHE-RSA-AES256-GCM-SHA384,"
++ "ECDHE-ECDSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-AES128-SHA,"
++ "ECDHE-RSA-AES256-SHA,"
++ "AES128-GCM-SHA256,"
++ "AES256-GCM-SHA384,"
++ "AES128-SHA,"
++ "AES256-SHA",
++ .npn = false,
++ .alpn = true,
++ .alps = true,
++ .tls_session_ticket = true,
++ .cert_compression = "brotli",
++ .http_headers = {
++ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"",
++ "sec-ch-ua-mobile: ?0",
++ "sec-ch-ua-platform: \"Windows\"",
++ "Upgrade-Insecure-Requests: 1",
++ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36",
++ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
++ "Sec-Fetch-Site: none",
++ "Sec-Fetch-Mode: navigate",
++ "Sec-Fetch-User: ?1",
++ "Sec-Fetch-Dest: document",
++ "Accept-Encoding: gzip, deflate, br",
++ "Accept-Language: en-US,en;q=0.9"
++ }
++ },
++ {
++ .target = "chrome99_android",
++ .httpversion = CURL_HTTP_VERSION_2_0,
++ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
++ .ciphers =
++ "TLS_AES_128_GCM_SHA256,"
++ "TLS_AES_256_GCM_SHA384,"
++ "TLS_CHACHA20_POLY1305_SHA256,"
++ "ECDHE-ECDSA-AES128-GCM-SHA256,"
++ "ECDHE-RSA-AES128-GCM-SHA256,"
++ "ECDHE-ECDSA-AES256-GCM-SHA384,"
++ "ECDHE-RSA-AES256-GCM-SHA384,"
++ "ECDHE-ECDSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-AES128-SHA,"
++ "ECDHE-RSA-AES256-SHA,"
++ "AES128-GCM-SHA256,"
++ "AES256-GCM-SHA384,"
++ "AES128-SHA,"
++ "AES256-SHA",
++ .npn = false,
++ .alpn = true,
++ .alps = true,
++ .tls_session_ticket = true,
++ .cert_compression = "brotli",
++ .http_headers = {
++ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
++ "sec-ch-ua-mobile: ?1",
++ "sec-ch-ua-platform: \"Android\"",
++ "Upgrade-Insecure-Requests: 1",
++ "User-Agent: Mozilla/5.0 (Linux; Android 12; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.58 Mobile Safari/537.36",
++ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
++ "Sec-Fetch-Site: none",
++ "Sec-Fetch-Mode: navigate",
++ "Sec-Fetch-User: ?1",
++ "Sec-Fetch-Dest: document",
++ "Accept-Encoding: gzip, deflate, br",
++ "Accept-Language: en-US,en;q=0.9"
+ }
-+
-+ if (!new_list) {
-+ ret = CURLE_OUT_OF_MEMORY;
-+ goto fail;
++ },
++ {
++ .target = "edge99",
++ .httpversion = CURL_HTTP_VERSION_2_0,
++ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
++ .ciphers =
++ "TLS_AES_128_GCM_SHA256,"
++ "TLS_AES_256_GCM_SHA384,"
++ "TLS_CHACHA20_POLY1305_SHA256,"
++ "ECDHE-ECDSA-AES128-GCM-SHA256,"
++ "ECDHE-RSA-AES128-GCM-SHA256,"
++ "ECDHE-ECDSA-AES256-GCM-SHA384,"
++ "ECDHE-RSA-AES256-GCM-SHA384,"
++ "ECDHE-ECDSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-AES128-SHA,"
++ "ECDHE-RSA-AES256-SHA,"
++ "AES128-GCM-SHA256,"
++ "AES256-GCM-SHA384,"
++ "AES128-SHA,"
++ "AES256-SHA",
++ .npn = false,
++ .alpn = true,
++ .alps = true,
++ .tls_session_ticket = true,
++ .cert_compression = "brotli",
++ .http_headers = {
++ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Microsoft Edge\";v=\"99\"",
++ "sec-ch-ua-mobile: ?0",
++ "sec-ch-ua-platform: \"Windows\"",
++ "Upgrade-Insecure-Requests: 1",
++ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.30",
++ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
++ "Sec-Fetch-Site: none",
++ "Sec-Fetch-Mode: navigate",
++ "Sec-Fetch-User: ?1",
++ "Sec-Fetch-Dest: document",
++ "Accept-Encoding: gzip, deflate, br",
++ "Accept-Language: en-US,en;q=0.9"
+ }
-+ }
-+
-+ /* Now go over any additional application-supplied headers. */
-+ for(head = dup; head; head = head->next) {
-+ if(head->data) {
-+ new_list = curl_slist_append(new_list, head->data);
-+ if(!new_list) {
-+ ret = CURLE_OUT_OF_MEMORY;
-+ goto fail;
-+ }
++ },
++ {
++ .target = "edge101",
++ .httpversion = CURL_HTTP_VERSION_2_0,
++ .ssl_version = CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT,
++ .ciphers =
++ "TLS_AES_128_GCM_SHA256,"
++ "TLS_AES_256_GCM_SHA384,"
++ "TLS_CHACHA20_POLY1305_SHA256,"
++ "ECDHE-ECDSA-AES128-GCM-SHA256,"
++ "ECDHE-RSA-AES128-GCM-SHA256,"
++ "ECDHE-ECDSA-AES256-GCM-SHA384,"
++ "ECDHE-RSA-AES256-GCM-SHA384,"
++ "ECDHE-ECDSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-CHACHA20-POLY1305,"
++ "ECDHE-RSA-AES128-SHA,"
++ "ECDHE-RSA-AES256-SHA,"
++ "AES128-GCM-SHA256,"
++ "AES256-GCM-SHA384,"
++ "AES128-SHA,"
++ "AES256-SHA",
++ .npn = false,
++ .alpn = true,
++ .alps = true,
++ .tls_session_ticket = true,
++ .cert_compression = "brotli",
++ .http_headers = {
++ "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Microsoft Edge\";v=\"101\"",
++ "sec-ch-ua-mobile: ?0",
++ "sec-ch-ua-platform: \"Windows\"",
++ "Upgrade-Insecure-Requests: 1",
++ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47",
++ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
++ "Sec-Fetch-Site: none",
++ "Sec-Fetch-Mode: navigate",
++ "Sec-Fetch-User: ?1",
++ "Sec-Fetch-Dest: document",
++ "Accept-Encoding: gzip, deflate, br",
++ "Accept-Language: en-US,en;q=0.9"
+ }
-+ }
-+
-+ curl_slist_free_all(dup);
-+ /* Save the new, merged list separately, so it can be freed later. */
-+ curl_slist_free_all(data->state.merged_headers);
-+ data->state.merged_headers = new_list;
-+
-+ return CURLE_OK;
-+
-+fail:
-+ Curl_safefree(dup);
-+ curl_slist_free_all(new_list);
-+ return ret;
-+}
-+
- CURLcode Curl_http_useragent(struct Curl_easy *data)
- {
- /* The User-Agent string might have been allocated in url.c already, because
-@@ -3063,6 +3159,11 @@ CURLcode Curl_http(struct Curl_easy *data, bool *done)
- http = data->req.p.http;
- DEBUGASSERT(http);
-
-+ /* curl-impersonate: Add HTTP headers to impersonate real browsers. */
-+ result = Curl_http_merge_headers(data);
-+ if (result)
-+ return result;
-+
- result = Curl_http_host(data, conn);
- if(result)
- return result;
-diff --git a/lib/http.h b/lib/http.h
-index b4aaba2a2..0d716640c 100644
---- a/lib/http.h
-+++ b/lib/http.h
-@@ -278,7 +278,8 @@ struct http_conn {
- struct h2settings settings;
-
- /* list of settings that will be sent */
-- nghttp2_settings_entry local_settings[3];
-+ /* curl-impersonate: Align HTTP/2 settings to Chrome's */
-+ nghttp2_settings_entry local_settings[4];
- size_t local_settings_num;
- #else
- int unused; /* prevent a compiler warning */
-diff --git a/lib/http2.c b/lib/http2.c
-index e74400a4c..aec67c975 100644
---- a/lib/http2.c
-+++ b/lib/http2.c
-@@ -41,6 +41,7 @@
- #include "curl_printf.h"
- #include "curl_memory.h"
- #include "memdebug.h"
-+#include "rand.h"
-
- #define H2_BUFSIZE 32768
-
-@@ -56,7 +57,7 @@
- #define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1
- #endif
-
--#define HTTP2_HUGE_WINDOW_SIZE (32 * 1024 * 1024) /* 32 MB */
-+#define HTTP2_HUGE_WINDOW_SIZE (15 * 1024 * 1024) /* 15 MB */
-
- #ifdef DEBUG_HTTP2
- #define H2BUGF(x) x
-@@ -75,13 +76,20 @@ static int h2_process_pending_input(struct Curl_easy *data,
- struct http_conn *httpc,
- CURLcode *err);
-
-+/*
-+ * curl-impersonate: Use Chrome's default HTTP/2 stream weight.
-+ */
-+
-+#define CHROME_DEFAULT_STREAM_WEIGHT (256)
-+
- /*
- * Curl_http2_init_state() is called when the easy handle is created and
- * allows for HTTP/2 specific init of state.
- */
- void Curl_http2_init_state(struct UrlState *state)
- {
-- state->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
-+ state->stream_weight = CHROME_DEFAULT_STREAM_WEIGHT;
-+ state->stream_depends_e = TRUE;
- }
-
- /*
-@@ -90,7 +98,8 @@ void Curl_http2_init_state(struct UrlState *state)
- */
- void Curl_http2_init_userset(struct UserDefined *set)
- {
-- set->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
-+ set->stream_weight = CHROME_DEFAULT_STREAM_WEIGHT;
-+ set->stream_depends_e = TRUE;
- }
-
- static int http2_getsock(struct Curl_easy *data,
-@@ -1193,16 +1202,30 @@ static void populate_settings(struct Curl_easy *data,
- {
- nghttp2_settings_entry *iv = httpc->local_settings;
-
-- iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
-- iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
-+ /* curl-impersonate: Align HTTP/2 settings to Chrome's */
-+ iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
-+ iv[0].value = 0x10000;
++ },
++ {
++ .target = "safari15_3",
++ .httpversion = CURL_HTTP_VERSION_2_0,
++ .ssl_version = CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_DEFAULT,
++ .ciphers =
++ "TLS_AES_128_GCM_SHA256,"
++ "TLS_AES_256_GCM_SHA384,"
++ "TLS_CHACHA20_POLY1305_SHA256,"
++ "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,"
++ "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,"
++ "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,"
++ "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,"
++ "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,"
++ "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,"
++ "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,"
++ "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,"
++ "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,"
++ "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,"
++ "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,"
++ "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,"
++ "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,"
++ "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,"
++ "TLS_RSA_WITH_AES_256_GCM_SHA384,"
++ "TLS_RSA_WITH_AES_128_GCM_SHA256,"
++ "TLS_RSA_WITH_AES_256_CBC_SHA256,"
++ "TLS_RSA_WITH_AES_128_CBC_SHA256,"
++ "TLS_RSA_WITH_AES_256_CBC_SHA,"
++ "TLS_RSA_WITH_AES_128_CBC_SHA,"
++ "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,"
++ "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,"
++ "TLS_RSA_WITH_3DES_EDE_CBC_SHA,",
++ .curves = "X25519:P-256:P-384:P-521",
++ .sig_hash_algs =
++ "ecdsa_secp256r1_sha256,"
++ "rsa_pss_rsae_sha256,"
++ "rsa_pkcs1_sha256,"
++ "ecdsa_secp384r1_sha384,"
++ "ecdsa_sha1,"
++ "rsa_pss_rsae_sha384,"
++ "rsa_pss_rsae_sha384,"
++ "rsa_pkcs1_sha384,"
++ "rsa_pss_rsae_sha512,"
++ "rsa_pkcs1_sha512,"
++ "rsa_pkcs1_sha1",
++ .npn = false,
++ .alpn = true,
++ .alps = false,
++ .tls_session_ticket = false,
++ .http_headers = {
++ "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15",
++ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
++ "Accept-Language: en-us",
++ "Accept-Encoding: gzip, deflate, br"
++ },
++ .http2_pseudo_headers_order = "mspa"
++ },
++ {
++ /* Last one must be NULL. */
++ .target = NULL
++ }
++};
+diff --git a/lib/impersonate.h b/lib/impersonate.h
+new file mode 100644
+index 000000000..9546a7833
+--- /dev/null
++++ b/lib/impersonate.h
+@@ -0,0 +1,43 @@
++#ifndef HEADER_CURL_IMPERSONATE_H
++#define HEADER_CURL_IMPERSONATE_H
+
-+ iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
-+ iv[1].value = Curl_multi_max_concurrent_streams(data->multi);
-
-- iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
-- iv[1].value = HTTP2_HUGE_WINDOW_SIZE;
-+ iv[2].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
-+ iv[2].value = 0x600000;
-
-- iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
-- iv[2].value = data->multi->push_cb != NULL;
-+ iv[3].settings_id = NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE;
-+ iv[3].value = 0x40000;
-
-- httpc->local_settings_num = 3;
-+ // iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
-+ // iv[2].value = data->multi->push_cb != NULL;
-+
-+ // curl-impersonate:
-+ // Up until Chrome 98, there was a randomly chosen setting number in the
-+ // HTTP2 SETTINGS frame. This might be something similar to TLS GREASE.
-+ // However, it seems to have been removed since.
-+ // Curl_rand(data, (unsigned char *)&iv[4].settings_id, sizeof(iv[4].settings_id));
-+ // Curl_rand(data, (unsigned char *)&iv[4].value, sizeof(iv[4].value));
++#define IMPERSONATE_MAX_HEADERS 32
+
-+ httpc->local_settings_num = 4;
- }
-
- void Curl_http2_done(struct Curl_easy *data, bool premature)
-@@ -1816,10 +1839,6 @@ static ssize_t http2_recv(struct Curl_easy *data, int sockindex,
- return -1;
- }
-
--/* Index where :authority header field will appear in request header
-- field list. */
--#define AUTHORITY_DST_IDX 3
--
- /* USHRT_MAX is 65535 == 0xffff */
- #define HEADER_OVERFLOW(x) \
- (x.namelen > 0xffff || x.valuelen > 0xffff - x.namelen)
-@@ -1890,6 +1909,53 @@ static header_instruction inspect_header(const char *name, size_t namelen,
- }
- }
-
+/*
-+ * curl-impersonate:
-+ * Determine the position of HTTP/2 pseudo headers.
-+ * The pseudo headers ":method", ":path", ":scheme", ":authority"
-+ * are sent in different order by different browsers. An important part of the
-+ * impersonation is ordering them like the browser does.
++ * curl-impersonate: Options to be set for each supported target browser.
+ */
-+static int http2_pseudo_header_index(struct Curl_easy *data,
-+ const char *header,
-+ size_t *index)
-+{
-+ char *off;
-+ // Use the Chrome ordering by default:
-+ // :method, :authority, :scheme, :path
-+ char *order = "masp";
-+ if(data->set.str[STRING_HTTP2_PSEUDO_HEADERS_ORDER])
-+ order = data->set.str[STRING_HTTP2_PSEUDO_HEADERS_ORDER];
-+
-+ if(strlen(order) != 4)
-+ return CURLE_BAD_FUNCTION_ARGUMENT;
-+
-+ // :method should always be first
-+ if(order[0] != 'm')
-+ return CURLE_BAD_FUNCTION_ARGUMENT;
-+
-+ // All pseudo-headers must be present
-+ if(!strchr(order, 'm') ||
-+ !strchr(order, 'a') ||
-+ !strchr(order, 's') ||
-+ !strchr(order, 'p'))
-+ return CURLE_BAD_FUNCTION_ARGUMENT;
-+
-+ if(strcasecompare(header, ":method"))
-+ off = strchr(order, 'm');
-+ else if(strcasecompare(header, ":authority"))
-+ off = strchr(order, 'a');
-+ else if(strcasecompare(header, ":scheme"))
-+ off = strchr(order, 's');
-+ else if(strcasecompare(header, ":path"))
-+ off = strchr(order, 'p');
-+ else
-+ return CURLE_BAD_FUNCTION_ARGUMENT;
++struct impersonate_opts {
++ const char *target;
++ int httpversion;
++ int ssl_version;
++ const char *ciphers;
++ /* Elliptic curves (TLS extension 10).
++ * Passed to CURLOPT_SSL_EC_CURVES */
++ const char *curves;
++ /* Signature hash algorithms (TLS extension 13).
++ * Passed to CURLOPT_SSL_SIG_HASH_ALGS */
++ const char *sig_hash_algs;
++ /* Enable TLS NPN extension. */
++ bool npn;
++ /* Enable TLS ALPN extension. */
++ bool alpn;
++ /* Enable TLS ALPS extension. */
++ bool alps;
++ /* Enable TLS session ticket extension. */
++ bool tls_session_ticket;
++ /* TLS certificate compression algorithms.
++ * (TLS extension 27) */
++ const char *cert_compression;
++ const char *http_headers[IMPERSONATE_MAX_HEADERS];
++ const char *http2_pseudo_headers_order;
++ /* Other TLS options will come here in the future once they are
++ * configurable through curl_easy_setopt() */
++};
+
-+ *index = off - order;
-+ return CURLE_OK;
-+}
++/*
++ * curl-impersonate: Global array of supported browsers and their
++ * impersonation options.
++ */
++extern const struct impersonate_opts impersonations[];
+
- static ssize_t http2_send(struct Curl_easy *data, int sockindex,
- const void *mem, size_t len, CURLcode *err)
- {
-@@ -1905,6 +1971,7 @@ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
- nghttp2_nv *nva = NULL;
- size_t nheader;
- size_t i;
-+ size_t header_idx;
- size_t authority_idx;
- char *hdbuf = (char *)mem;
- char *end, *line_end;
-@@ -2010,12 +2077,21 @@ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
- end = memchr(hdbuf, ' ', line_end - hdbuf);
- if(!end || end == hdbuf)
- goto fail;
-- nva[0].name = (unsigned char *)":method";
-- nva[0].namelen = strlen((char *)nva[0].name);
-- nva[0].value = (unsigned char *)hdbuf;
-- nva[0].valuelen = (size_t)(end - hdbuf);
-- nva[0].flags = NGHTTP2_NV_FLAG_NONE;
-- if(HEADER_OVERFLOW(nva[0])) {
-+ /* curl-impersonate: Set the index of ":method" based on libcurl option */
-+ if(http2_pseudo_header_index(data, ":authority", &authority_idx))
-+ goto fail;
-+ if(http2_pseudo_header_index(data, ":method", &header_idx))
-+ goto fail;
-+ /* This is needed to overcome the fact that curl will only move the authority
-+ * header into its place after all other headers have been placed. */
-+ if(header_idx > authority_idx)
-+ header_idx--;
-+ nva[header_idx].name = (unsigned char *)":method";
-+ nva[header_idx].namelen = strlen((char *)nva[header_idx].name);
-+ nva[header_idx].value = (unsigned char *)hdbuf;
-+ nva[header_idx].valuelen = (size_t)(end - hdbuf);
-+ nva[header_idx].flags = NGHTTP2_NV_FLAG_NONE;
-+ if(HEADER_OVERFLOW(nva[header_idx])) {
- failf(data, "Failed sending HTTP request: Header overflow");
- goto fail;
- }
-@@ -2032,25 +2108,35 @@ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
- }
- if(!end || end == hdbuf)
- goto fail;
-- nva[1].name = (unsigned char *)":path";
-- nva[1].namelen = strlen((char *)nva[1].name);
-- nva[1].value = (unsigned char *)hdbuf;
-- nva[1].valuelen = (size_t)(end - hdbuf);
-- nva[1].flags = NGHTTP2_NV_FLAG_NONE;
-- if(HEADER_OVERFLOW(nva[1])) {
-+ /* curl-impersonate: Set the index of ":path" based on libcurl option */
-+ if(http2_pseudo_header_index(data, ":path", &header_idx))
-+ goto fail;
-+ if(header_idx > authority_idx)
-+ header_idx--;
-+ nva[header_idx].name = (unsigned char *)":path";
-+ nva[header_idx].namelen = strlen((char *)nva[header_idx].name);
-+ nva[header_idx].value = (unsigned char *)hdbuf;
-+ nva[header_idx].valuelen = (size_t)(end - hdbuf);
-+ nva[header_idx].flags = NGHTTP2_NV_FLAG_NONE;
-+ if(HEADER_OVERFLOW(nva[header_idx])) {
- failf(data, "Failed sending HTTP request: Header overflow");
- goto fail;
- }
-
-- nva[2].name = (unsigned char *)":scheme";
-- nva[2].namelen = strlen((char *)nva[2].name);
-+ /* curl-impersonate: Set the index of ":scheme" based on libcurl option */
-+ if(http2_pseudo_header_index(data, ":scheme", &header_idx))
-+ goto fail;
-+ if(header_idx > authority_idx)
-+ header_idx--;
-+ nva[header_idx].name = (unsigned char *)":scheme";
-+ nva[header_idx].namelen = strlen((char *)nva[header_idx].name);
- if(conn->handler->flags & PROTOPT_SSL)
-- nva[2].value = (unsigned char *)"https";
-+ nva[header_idx].value = (unsigned char *)"https";
- else
-- nva[2].value = (unsigned char *)"http";
-- nva[2].valuelen = strlen((char *)nva[2].value);
-- nva[2].flags = NGHTTP2_NV_FLAG_NONE;
-- if(HEADER_OVERFLOW(nva[2])) {
-+ nva[header_idx].value = (unsigned char *)"http";
-+ nva[header_idx].valuelen = strlen((char *)nva[header_idx].value);
-+ nva[header_idx].flags = NGHTTP2_NV_FLAG_NONE;
-+ if(HEADER_OVERFLOW(nva[header_idx])) {
- failf(data, "Failed sending HTTP request: Header overflow");
- goto fail;
- }
-@@ -2117,10 +2203,13 @@ static ssize_t http2_send(struct Curl_easy *data, int sockindex,
- ++i;
- }
-
-+ /* curl-impersonate: Set the index of ":authority" based on libcurl option */
-+ if(http2_pseudo_header_index(data, ":authority", &header_idx))
-+ goto fail;
- /* :authority must come before non-pseudo header fields */
-- if(authority_idx && authority_idx != AUTHORITY_DST_IDX) {
-+ if(authority_idx && authority_idx != header_idx) {
- nghttp2_nv authority = nva[authority_idx];
-- for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
-+ for(i = authority_idx; i > header_idx; --i) {
- nva[i] = nva[i - 1];
- }
- nva[i] = authority;
-diff --git a/lib/http2.h b/lib/http2.h
-index d6986d97f..fa5c90e7f 100644
---- a/lib/http2.h
-+++ b/lib/http2.h
-@@ -29,7 +29,8 @@
-
- /* value for MAX_CONCURRENT_STREAMS we use until we get an updated setting
- from the peer */
--#define DEFAULT_MAX_CONCURRENT_STREAMS 100
-+/* curl-impersonate: Use 1000 concurrent streams like Chrome. */
-+#define DEFAULT_MAX_CONCURRENT_STREAMS 1000
-
- /*
- * Store nghttp2 version info in this buffer.
++#endif /* HEADER_CURL_IMPERSONATE_H */
diff --git a/lib/multi.c b/lib/multi.c
index f8dcc63b4..e6b728592 100644
--- a/lib/multi.c