1mod device;
2#[cfg(feature = "emulation-rand")]
3mod rand;
4
5use device::{chrome::*, firefox::*, okhttp::*, safari::*};
6use rquest::{EmulationProvider, EmulationProviderFactory};
7#[cfg(feature = "emulation-serde")]
8use serde::{Deserialize, Serialize};
9#[cfg(feature = "emulation-rand")]
10use strum_macros::VariantArray;
11use typed_builder::TypedBuilder;
12
13macro_rules! define_emulation_enum {
14 ($(#[$meta:meta])* $name:ident, $default_variant:ident, $($variant:ident => $rename:expr),*) => {
15 $(#[$meta])*
16 #[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
17 #[cfg_attr(feature = "emulation-rand", derive(VariantArray))]
18 #[cfg_attr(feature = "emulation-serde", derive(Deserialize, Serialize))]
19 pub enum $name {
20 $(
21 #[cfg_attr(feature = "emulation-serde", serde(rename = $rename))]
22 $variant,
23 )*
24 }
25
26 impl Default for $name {
27 fn default() -> Self {
28 $name::$default_variant
29 }
30 }
31 };
32}
33
34define_emulation_enum!(
35 Emulation, Chrome133,
63 Chrome100 => "chrome_100",
64 Chrome101 => "chrome_101",
65 Chrome104 => "chrome_104",
66 Chrome105 => "chrome_105",
67 Chrome106 => "chrome_106",
68 Chrome107 => "chrome_107",
69 Chrome108 => "chrome_108",
70 Chrome109 => "chrome_109",
71 Chrome114 => "chrome_114",
72 Chrome116 => "chrome_116",
73 Chrome117 => "chrome_117",
74 Chrome118 => "chrome_118",
75 Chrome119 => "chrome_119",
76 Chrome120 => "chrome_120",
77 Chrome123 => "chrome_123",
78 Chrome124 => "chrome_124",
79 Chrome126 => "chrome_126",
80 Chrome127 => "chrome_127",
81 Chrome128 => "chrome_128",
82 Chrome129 => "chrome_129",
83 Chrome130 => "chrome_130",
84 Chrome131 => "chrome_131",
85 Chrome132 => "chrome_132",
86 Chrome133 => "chrome_133",
87 Chrome134 => "chrome_134",
88 SafariIos17_2 => "safari_ios_17.2",
89 SafariIos17_4_1 => "safari_ios_17.4.1",
90 SafariIos16_5 => "safari_ios_16.5",
91 Safari15_3 => "safari_15.3",
92 Safari15_5 => "safari_15.5",
93 Safari15_6_1 => "safari_15.6.1",
94 Safari16 => "safari_16",
95 Safari16_5 => "safari_16.5",
96 Safari17_0 => "safari_17.0",
97 Safari17_2_1 => "safari_17.2.1",
98 Safari17_4_1 => "safari_17.4.1",
99 Safari17_5 => "safari_17.5",
100 Safari18 => "safari_18",
101 SafariIPad18 => "safari_ipad_18",
102 Safari18_2 => "safari_18.2",
103 SafariIos18_1_1 => "safari_ios_18.1.1",
104 Safari18_3 => "safari_18.3",
105 Safari18_3_1 => "safari_18.3.1",
106 OkHttp3_9 => "okhttp_3.9",
107 OkHttp3_11 => "okhttp_3.11",
108 OkHttp3_13 => "okhttp_3.13",
109 OkHttp3_14 => "okhttp_3.14",
110 OkHttp4_9 => "okhttp_4.9",
111 OkHttp4_10 => "okhttp_4.10",
112 OkHttp4_12 => "okhttp_4.12",
113 OkHttp5 => "okhttp_5",
114 Edge101 => "edge_101",
115 Edge122 => "edge_122",
116 Edge127 => "edge_127",
117 Edge131 => "edge_131",
118 Edge134 => "edge_134",
119 Firefox109 => "firefox_109",
120 Firefox117 => "firefox_117",
121 Firefox128 => "firefox_128",
122 Firefox133 => "firefox_133",
123 Firefox135 => "firefox_135",
124 FirefoxPrivate135 => "firefox_private_135",
125 FirefoxAndroid135 => "firefox_android_135",
126 Firefox136 => "firefox_136",
127 FirefoxPrivate136 => "firefox_private_136"
128);
129
130impl EmulationProviderFactory for Emulation {
132 fn emulation(self) -> EmulationProvider {
133 EmulationOption::builder()
134 .emulation(self)
135 .build()
136 .emulation()
137 }
138}
139
140define_emulation_enum!(
141 EmulationOS, MacOS,
167 Windows => "windows",
168 MacOS => "macos",
169 Linux => "linux",
170 Android => "android",
171 IOS => "ios"
172);
173
174impl EmulationOS {
176 #[inline]
177 const fn platform(&self) -> &'static str {
178 match self {
179 EmulationOS::MacOS => "\"macOS\"",
180 EmulationOS::Linux => "\"Linux\"",
181 EmulationOS::Windows => "\"Windows\"",
182 EmulationOS::Android => "\"Android\"",
183 EmulationOS::IOS => "\"iOS\"",
184 }
185 }
186
187 #[inline]
188 const fn is_mobile(&self) -> bool {
189 matches!(self, EmulationOS::Android | EmulationOS::IOS)
190 }
191}
192
193#[derive(Default, TypedBuilder)]
225pub struct EmulationOption {
226 #[builder(default)]
228 emulation: Emulation,
229
230 #[builder(default)]
232 emulation_os: EmulationOS,
233
234 #[builder(default = false)]
236 skip_http2: bool,
237
238 #[builder(default = false)]
240 skip_headers: bool,
241}
242
243macro_rules! emulation_match {
245 ($ver:expr, $opt:expr, $($variant:pat => $path:expr),+) => {
246 match $ver {
247 $(
248 $variant => $path($opt),
249 )+
250 }
251 }
252}
253
254impl EmulationProviderFactory for EmulationOption {
255 fn emulation(self) -> EmulationProvider {
256 emulation_match!(
257 self.emulation,
258 self,
259
260 Emulation::Chrome100 => v100::emulation,
261 Emulation::Chrome101 => v101::emulation,
262 Emulation::Chrome104 => v104::emulation,
263 Emulation::Chrome105 => v105::emulation,
264 Emulation::Chrome106 => v106::emulation,
265 Emulation::Chrome107 => v107::emulation,
266 Emulation::Chrome108 => v108::emulation,
267 Emulation::Chrome109 => v109::emulation,
268 Emulation::Chrome114 => v114::emulation,
269 Emulation::Chrome116 => v116::emulation,
270 Emulation::Chrome117 => v117::emulation,
271 Emulation::Chrome118 => v118::emulation,
272 Emulation::Chrome119 => v119::emulation,
273 Emulation::Chrome120 => v120::emulation,
274 Emulation::Chrome123 => v123::emulation,
275 Emulation::Chrome124 => v124::emulation,
276 Emulation::Chrome126 => v126::emulation,
277 Emulation::Chrome127 => v127::emulation,
278 Emulation::Chrome128 => v128::emulation,
279 Emulation::Chrome129 => v129::emulation,
280 Emulation::Chrome130 => v130::emulation,
281 Emulation::Chrome131 => v131::emulation,
282 Emulation::Chrome132 => v132::emulation,
283 Emulation::Chrome133 => v133::emulation,
284 Emulation::Chrome134 => v134::emulation,
285
286 Emulation::SafariIos17_2 => safari_ios_17_2::emulation,
287 Emulation::SafariIos17_4_1 => safari_ios_17_4_1::emulation,
288 Emulation::SafariIos16_5 => safari_ios_16_5::emulation,
289 Emulation::Safari15_3 => safari15_3::emulation,
290 Emulation::Safari15_5 => safari15_5::emulation,
291 Emulation::Safari15_6_1 => safari15_6_1::emulation,
292 Emulation::Safari16 => safari16::emulation,
293 Emulation::Safari16_5 => safari16_5::emulation,
294 Emulation::Safari17_0 => safari17_0::emulation,
295 Emulation::Safari17_2_1 => safari17_2_1::emulation,
296 Emulation::Safari17_4_1 => safari17_4_1::emulation,
297 Emulation::Safari17_5 => safari17_5::emulation,
298 Emulation::Safari18 => safari18::emulation,
299 Emulation::SafariIPad18 => safari_ipad_18::emulation,
300 Emulation::Safari18_2 => safari18_2::emulation,
301 Emulation::SafariIos18_1_1 => safari_ios_18_1_1::emulation,
302 Emulation::Safari18_3 => safari18_3::emulation,
303 Emulation::Safari18_3_1 => safari18_3_1::emulation,
304
305 Emulation::OkHttp3_9 => okhttp3_9::emulation,
306 Emulation::OkHttp3_11 => okhttp3_11::emulation,
307 Emulation::OkHttp3_13 => okhttp3_13::emulation,
308 Emulation::OkHttp3_14 => okhttp3_14::emulation,
309 Emulation::OkHttp4_9 => okhttp4_9::emulation,
310 Emulation::OkHttp4_10 => okhttp4_10::emulation,
311 Emulation::OkHttp4_12 => okhttp4_12::emulation,
312 Emulation::OkHttp5 => okhttp5::emulation,
313
314 Emulation::Edge101 => edge101::emulation,
315 Emulation::Edge122 => edge122::emulation,
316 Emulation::Edge127 => edge127::emulation,
317 Emulation::Edge131 => edge131::emulation,
318 Emulation::Edge134 => edge134::emulation,
319
320 Emulation::Firefox109 => ff109::emulation,
321 Emulation::Firefox117 => ff117::emulation,
322 Emulation::Firefox128 => ff128::emulation,
323 Emulation::Firefox133 => ff133::emulation,
324 Emulation::Firefox135 => ff135::emulation,
325 Emulation::FirefoxPrivate135 => ff_private_135::emulation,
326 Emulation::FirefoxAndroid135 => ff_android_135::emulation,
327 Emulation::Firefox136 => ff136::emulation,
328 Emulation::FirefoxPrivate136 => ff_private_136::emulation
329 )
330 }
331}