rquest_util/emulation/device/
macros.rs

1#[macro_export]
2macro_rules! conditional_headers {
3    ($skip_headers:expr, $initializer:expr) => {
4        if $skip_headers {
5            None
6        } else {
7            Some($initializer())
8        }
9    };
10    ($skip_headers:expr, $initializer:expr, $ua:expr) => {
11        if $skip_headers {
12            None
13        } else {
14            Some($initializer($ua))
15        }
16    };
17}
18
19#[macro_export]
20macro_rules! conditional_http2 {
21    ($skip_http2:expr, $http2:expr) => {
22        if $skip_http2 { None } else { Some($http2) }
23    };
24}
25
26#[macro_export]
27macro_rules! header_chrome_sec_ch_ua {
28    ($headers:expr, $ua:expr, $platform:expr, $is_mobile:expr) => {
29        let mobile = if $is_mobile { "?1" } else { "?0" };
30        $headers.insert("sec-ch-ua", HeaderValue::from_static($ua));
31        $headers.insert("sec-ch-ua-mobile", HeaderValue::from_static(mobile));
32        $headers.insert("sec-ch-ua-platform", HeaderValue::from_static($platform));
33    };
34}
35
36#[macro_export]
37macro_rules! header_chrome_sec_fetch {
38    ($headers:expr) => {
39        $headers.insert("sec-fetch-dest", HeaderValue::from_static("document"));
40        $headers.insert("sec-fetch-mode", HeaderValue::from_static("navigate"));
41        $headers.insert("sec-fetch-site", HeaderValue::from_static("none"));
42    };
43}
44
45#[macro_export]
46macro_rules! header_chrome_ua {
47    ($headers:expr, $ua:expr) => {
48        $headers.insert(UPGRADE_INSECURE_REQUESTS, HeaderValue::from_static("1"));
49        $headers.insert(USER_AGENT, HeaderValue::from_static($ua));
50    };
51}
52
53#[macro_export]
54macro_rules! header_chrome_accpet {
55    ($headers:expr) => {
56        $headers.insert(ACCEPT, HeaderValue::from_static("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"));
57        #[cfg(all(feature = "gzip", feature = "deflate", feature = "brotli"))]
58        $headers.insert(
59            ACCEPT_ENCODING,
60            HeaderValue::from_static("gzip, deflate, br"),
61        );
62        $headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.9"));
63    };
64    (zstd, $headers:expr) => {
65        $headers.insert(ACCEPT, HeaderValue::from_static("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"));
66        #[cfg(all(
67            feature = "gzip",
68            feature = "deflate",
69            feature = "brotli",
70            feature = "zstd"
71        ))]
72        $headers.insert(
73            ACCEPT_ENCODING,
74            HeaderValue::from_static("gzip, deflate, br, zstd"),
75        );
76        $headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.9"));
77    }
78}
79
80#[macro_export]
81macro_rules! header_firefox_sec_fetch {
82    ($headers:expr) => {
83        $headers.insert("sec-fetch-dest", HeaderValue::from_static("document"));
84        $headers.insert("sec-fetch-mode", HeaderValue::from_static("navigate"));
85        $headers.insert("sec-fetch-site", HeaderValue::from_static("none"));
86    };
87}
88
89#[macro_export]
90macro_rules! header_firefox_accept {
91    ($headers:expr) => {
92        $headers.insert(
93            ACCEPT,
94            HeaderValue::from_static(
95                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
96            ),
97        );
98        #[cfg(all(feature = "gzip", feature = "deflate", feature = "brotli"))]
99        $headers.insert(
100            ACCEPT_ENCODING,
101            HeaderValue::from_static("gzip, deflate, br"),
102        );
103        $headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.5"));
104    };
105    (zstd, $headers:expr) => {
106        $headers.insert(
107            ACCEPT,
108            HeaderValue::from_static(
109                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
110            ),
111        );
112        #[cfg(all(
113            feature = "gzip",
114            feature = "deflate",
115            feature = "brotli",
116            feature = "zstd"
117        ))]
118        $headers.insert(
119            ACCEPT_ENCODING,
120            HeaderValue::from_static("gzip, deflate, br, zstd"),
121        );
122        $headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.5"));
123    };
124}
125
126#[macro_export]
127macro_rules! header_firefox_ua {
128    ($headers:expr, $ua:expr) => {
129        $headers.insert(
130            HeaderName::from_static("te"),
131            HeaderValue::from_static("trailers"),
132        );
133        $headers.insert(UPGRADE_INSECURE_REQUESTS, HeaderValue::from_static("1"));
134        $headers.insert(USER_AGENT, HeaderValue::from_static($ua));
135    };
136}
137
138#[macro_export]
139macro_rules! join {
140    ($sep:expr, $first:expr $(, $rest:expr)*) => {
141        concat!($first $(, $sep, $rest)*)
142    };
143}
144
145macro_rules! emulation_match {
146    ($ver:expr, $opt:expr, $($variant:pat => $path:expr),+) => {
147        match $ver {
148            $(
149                $variant => $path($opt),
150            )+
151        }
152    }
153}