1use crate::profile::ChromeProfile;
2use http::header::{HeaderMap, HeaderValue, ACCEPT, ACCEPT_ENCODING, ACCEPT_LANGUAGE, USER_AGENT};
3
4#[allow(dead_code)]
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum RequestContext {
8 Navigate,
10 Xhr,
12 Form,
14 Iframe,
16 NoCorsScript,
18 NoCorsStyle,
20 NoCorsImage,
22 NoCorsFont,
24 NoCorsMedia,
26 Worker,
28 ServiceWorker,
30 Prefetch,
32}
33
34pub fn inject_chrome_headers(
50 headers: &mut HeaderMap,
51 profile: &ChromeProfile,
52 sec_fetch_site: &str,
53 is_initial_navigation: bool,
54 context: RequestContext,
55 accept_ch: bool,
56 referer: Option<&str>,
57) {
58 if let Ok(val) = HeaderValue::from_str(&profile.headers.sec_ch_ua) {
61 headers.insert("sec-ch-ua", val);
62 }
63 headers.insert("sec-ch-ua-mobile", HeaderValue::from_static("?0"));
64 if let Ok(val) = HeaderValue::from_str(&profile.headers.sec_ch_ua_platform) {
65 headers.insert("sec-ch-ua-platform", val);
66 }
67 if accept_ch {
69 if let Ok(val) = HeaderValue::from_str(&profile.headers.sec_ch_ua_platform_version) {
70 headers.insert("sec-ch-ua-platform-version", val);
71 }
72 }
73
74 headers.insert("upgrade-insecure-requests", HeaderValue::from_static("1"));
76 if let Ok(val) = HeaderValue::from_str(&profile.headers.user_agent) {
77 headers.insert(USER_AGENT, val);
78 }
79
80 if let Ok(val) = HeaderValue::from_str(sec_fetch_site) {
82 headers.insert("sec-fetch-site", val);
83 }
84 let (mode, dest, accept_val) = match context {
85 RequestContext::Navigate | RequestContext::Form => ("navigate", "document", "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.7"),
86 RequestContext::Iframe => ("navigate", "iframe", "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.7"),
87 RequestContext::Xhr => ("cors", "empty", "*/*"),
88 RequestContext::NoCorsScript => ("no-cors", "script", "*/*"),
89 RequestContext::NoCorsStyle => ("no-cors", "style", "text/css,*/*;q=0.1"),
90 RequestContext::NoCorsImage => ("no-cors", "image", "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8"),
91 RequestContext::NoCorsFont => ("no-cors", "font", "*/*"),
92 RequestContext::NoCorsMedia => ("no-cors", "video", "*/*"),
93 RequestContext::Worker => ("same-origin", "worker", "*/*"),
94 RequestContext::ServiceWorker => ("same-origin", "serviceworker", "*/*"),
95 RequestContext::Prefetch => ("no-cors", "empty", "*/*"),
96 };
97 headers.insert(ACCEPT, HeaderValue::from_static(accept_val));
98 headers.insert("sec-fetch-mode", HeaderValue::from_static(mode));
99
100 if is_initial_navigation && (context == RequestContext::Navigate || context == RequestContext::Form || context == RequestContext::Iframe) {
102 headers.insert("sec-fetch-user", HeaderValue::from_static("?1"));
103 }
104
105 headers.insert("sec-fetch-dest", HeaderValue::from_static(dest));
106
107 if let Some(r) = referer {
108 if let Ok(val) = HeaderValue::from_str(r) {
109 headers.insert(http::header::REFERER, val);
110 }
111 }
112
113 let encoding = if profile.headers.zstd_encoding {
115 "gzip, deflate, br, zstd"
116 } else {
117 "gzip, deflate, br"
118 };
119 headers.insert(ACCEPT_ENCODING, HeaderValue::from_static(encoding));
120 if let Ok(val) = HeaderValue::from_str(&profile.headers.accept_language) {
121 headers.insert(ACCEPT_LANGUAGE, val);
122 }
123
124 if profile.headers.include_priority_header {
126 headers.insert("priority", HeaderValue::from_static("u=0, i"));
127 }
128
129 for (name, value) in headers.iter_mut() {
134 if name == "cookie" || name == "authorization" {
135 value.set_sensitive(true);
136 }
137 }
138
139 }