rquest_util/emulation/
mod.rs

1mod device;
2#[cfg(feature = "emulation-rand")]
3mod rand;
4
5use device::{chrome::*, firefox::*, okhttp::*, safari::*};
6use rquest::{EmulationProvider, EmulationProviderFactory};
7use serde::{Deserialize, Serialize};
8#[cfg(feature = "emulation-rand")]
9use strum_macros::VariantArray;
10use typed_builder::TypedBuilder;
11
12macro_rules! define_emulation_enum {
13    ($(#[$meta:meta])* $name:ident, $default_variant:ident, $($variant:ident => $rename:expr),*) => {
14        $(#[$meta])*
15        #[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
16        #[cfg_attr(feature = "emulation-rand", derive(Serialize, Deserialize, VariantArray))]
17        pub enum $name {
18            $(
19                #[cfg_attr(feature = "emulation-rand", serde(rename = $rename))]
20                $variant,
21            )*
22        }
23
24        impl Default for $name {
25            fn default() -> Self {
26                $name::$default_variant
27            }
28        }
29    };
30}
31
32define_emulation_enum!(
33    /// Represents different browser versions for impersonation.
34    ///
35    /// The `Emulation` enum provides variants for different browser versions that can be used
36    /// to emulation HTTP requests. Each variant corresponds to a specific browser version.
37    ///
38    /// # Naming Convention
39    ///
40    /// The naming convention for the variants follows the pattern `browser_version`, where
41    /// `browser` is the name of the browser (e.g., `chrome`, `firefox`, `safari`) and `version`
42    /// is the version number. For example, `Chrome100` represents Chrome version 100.
43    ///
44    /// The serialized names of the variants use underscores to separate the browser name and
45    /// version number, following the pattern `browser_version`. For example, `Chrome100` is
46    /// serialized as `"chrome_100"`.
47    ///
48    /// # Examples
49    ///
50    /// ```rust
51    /// use rquest_util::Emulation;
52    ///
53    /// let emulation = Emulation::Chrome100;
54    /// let serialized = serde_json::to_string(&emulation).unwrap();
55    /// assert_eq!(serialized, "\"chrome_100\"");
56    ///
57    /// let deserialized: Emulation = serde_json::from_str(&serialized).unwrap();
58    /// assert_eq!(deserialized, Emulation::Chrome100);
59    /// ```
60    Emulation, Chrome133,
61    Chrome100 => "chrome_100",
62    Chrome101 => "chrome_101",
63    Chrome104 => "chrome_104",
64    Chrome105 => "chrome_105",
65    Chrome106 => "chrome_106",
66    Chrome107 => "chrome_107",
67    Chrome108 => "chrome_108",
68    Chrome109 => "chrome_109",
69    Chrome114 => "chrome_114",
70    Chrome116 => "chrome_116",
71    Chrome117 => "chrome_117",
72    Chrome118 => "chrome_118",
73    Chrome119 => "chrome_119",
74    Chrome120 => "chrome_120",
75    Chrome123 => "chrome_123",
76    Chrome124 => "chrome_124",
77    Chrome126 => "chrome_126",
78    Chrome127 => "chrome_127",
79    Chrome128 => "chrome_128",
80    Chrome129 => "chrome_129",
81    Chrome130 => "chrome_130",
82    Chrome131 => "chrome_131",
83    Chrome132 => "chrome_132",
84    Chrome133 => "chrome_133",
85    Chrome134 => "chrome_134",
86    SafariIos17_2 => "safari_ios_17.2",
87    SafariIos17_4_1 => "safari_ios_17.4.1",
88    SafariIos16_5 => "safari_ios_16.5",
89    Safari15_3 => "safari_15.3",
90    Safari15_5 => "safari_15.5",
91    Safari15_6_1 => "safari_15.6.1",
92    Safari16 => "safari_16",
93    Safari16_5 => "safari_16.5",
94    Safari17_0 => "safari_17.0",
95    Safari17_2_1 => "safari_17.2.1",
96    Safari17_4_1 => "safari_17.4.1",
97    Safari17_5 => "safari_17.5",
98    Safari18 => "safari_18",
99    SafariIPad18 => "safari_ipad_18",
100    Safari18_2 => "safari_18.2",
101    SafariIos18_1_1 => "safari_ios_18.1.1",
102    Safari18_3 => "safari_18.3",
103    OkHttp3_9 => "okhttp_3.9",
104    OkHttp3_11 => "okhttp_3.11",
105    OkHttp3_13 => "okhttp_3.13",
106    OkHttp3_14 => "okhttp_3.14",
107    OkHttp4_9 => "okhttp_4.9",
108    OkHttp4_10 => "okhttp_4.10",
109    OkHttp4_12 => "okhttp_4.12",
110    OkHttp5 => "okhttp_5",
111    Edge101 => "edge_101",
112    Edge122 => "edge_122",
113    Edge127 => "edge_127",
114    Edge131 => "edge_131",
115    Edge134 => "edge_134",
116    Firefox109 => "firefox_109",
117    Firefox117 => "firefox_117",
118    Firefox128 => "firefox_128",
119    Firefox133 => "firefox_133",
120    Firefox135 => "firefox_135",
121    FirefoxPrivate135 => "firefox_private_135",
122    FirefoxAndroid135 => "firefox_android_135",
123    Firefox136 => "firefox_136",
124    FirefoxPrivate136 => "firefox_private_136"
125);
126
127/// ======== Emulation impls ========
128impl EmulationProviderFactory for Emulation {
129    fn emulation(self) -> EmulationProvider {
130        EmulationOption::builder()
131            .emulation(self)
132            .build()
133            .emulation()
134    }
135}
136
137/// Represents different operating systems for impersonation.
138///
139/// The `EmulationOS` enum provides variants for different operating systems that can be used
140/// to emulation HTTP requests. Each variant corresponds to a specific operating system.
141///
142/// # Naming Convention
143///
144/// The naming convention for the variants follows the pattern `os_name`, where
145/// `os_name` is the name of the operating system (e.g., `windows`, `macos`, `linux`, `android`, `ios`).
146///
147/// The serialized names of the variants use lowercase letters to represent the operating system names.
148/// For example, `Windows` is serialized as `"windows"`.
149///
150/// # Examples
151///
152/// ```rust
153/// use rquest::EmulationOS;
154///
155/// let emulation_os = EmulationOS::Windows;
156/// let serialized = serde_json::to_string(&emulation_os).unwrap();
157/// assert_eq!(serialized, "\"windows\"");
158///
159/// let deserialized: EmulationOS = serde_json::from_str(&serialized).unwrap();
160/// assert_eq!(deserialized, EmulationOS::Windows);
161/// ```
162#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
163pub enum EmulationOS {
164    #[serde(rename = "windows")]
165    Windows,
166    #[serde(rename = "macos")]
167    #[default]
168    MacOS,
169    #[serde(rename = "linux")]
170    Linux,
171    #[serde(rename = "android")]
172    Android,
173    #[serde(rename = "ios")]
174    IOS,
175}
176
177/// ======== EmulationOS impls ========
178impl EmulationOS {
179    #[inline]
180    fn platform(&self) -> &'static str {
181        match self {
182            EmulationOS::MacOS => "\"macOS\"",
183            EmulationOS::Linux => "\"Linux\"",
184            EmulationOS::Windows => "\"Windows\"",
185            EmulationOS::Android => "\"Android\"",
186            EmulationOS::IOS => "\"iOS\"",
187        }
188    }
189
190    #[inline]
191    fn is_mobile(&self) -> bool {
192        matches!(self, EmulationOS::Android | EmulationOS::IOS)
193    }
194}
195
196#[derive(Default, TypedBuilder)]
197pub struct EmulationOption {
198    /// The browser version to emulation.
199    #[builder(default)]
200    emulation: Emulation,
201
202    /// The operating system.
203    #[builder(default)]
204    emulation_os: EmulationOS,
205
206    /// Whether to skip HTTP/2.
207    #[builder(default = false)]
208    skip_http2: bool,
209
210    /// Whether to skip headers.
211    #[builder(default = false)]
212    skip_headers: bool,
213}
214
215/// ======== EmulationOption impls ========
216macro_rules! emulation_match {
217    ($ver:expr, $opt:expr, $($variant:pat => $path:expr),+) => {
218        match $ver {
219            $(
220                $variant => $path($opt),
221            )+
222        }
223    }
224}
225
226impl EmulationProviderFactory for EmulationOption {
227    fn emulation(self) -> EmulationProvider {
228        emulation_match!(
229            self.emulation,
230            self,
231
232            Emulation::Chrome100 => v100::emulation,
233            Emulation::Chrome101 => v101::emulation,
234            Emulation::Chrome104 => v104::emulation,
235            Emulation::Chrome105 => v105::emulation,
236            Emulation::Chrome106 => v106::emulation,
237            Emulation::Chrome107 => v107::emulation,
238            Emulation::Chrome108 => v108::emulation,
239            Emulation::Chrome109 => v109::emulation,
240            Emulation::Chrome114 => v114::emulation,
241            Emulation::Chrome116 => v116::emulation,
242            Emulation::Chrome117 => v117::emulation,
243            Emulation::Chrome118 => v118::emulation,
244            Emulation::Chrome119 => v119::emulation,
245            Emulation::Chrome120 => v120::emulation,
246            Emulation::Chrome123 => v123::emulation,
247            Emulation::Chrome124 => v124::emulation,
248            Emulation::Chrome126 => v126::emulation,
249            Emulation::Chrome127 => v127::emulation,
250            Emulation::Chrome128 => v128::emulation,
251            Emulation::Chrome129 => v129::emulation,
252            Emulation::Chrome130 => v130::emulation,
253            Emulation::Chrome131 => v131::emulation,
254            Emulation::Chrome132 => v132::emulation,
255            Emulation::Chrome133 => v133::emulation,
256            Emulation::Chrome134 => v134::emulation,
257
258            Emulation::SafariIos17_2 => safari_ios_17_2::emulation,
259            Emulation::SafariIos17_4_1 => safari_ios_17_4_1::emulation,
260            Emulation::SafariIos16_5 => safari_ios_16_5::emulation,
261            Emulation::Safari15_3 => safari15_3::emulation,
262            Emulation::Safari15_5 => safari15_5::emulation,
263            Emulation::Safari15_6_1 => safari15_6_1::emulation,
264            Emulation::Safari16 => safari16::emulation,
265            Emulation::Safari16_5 => safari16_5::emulation,
266            Emulation::Safari17_0 => safari17_0::emulation,
267            Emulation::Safari17_2_1 => safari17_2_1::emulation,
268            Emulation::Safari17_4_1 => safari17_4_1::emulation,
269            Emulation::Safari17_5 => safari17_5::emulation,
270            Emulation::Safari18 => safari18::emulation,
271            Emulation::SafariIPad18 => safari_ipad_18::emulation,
272            Emulation::Safari18_2 => safari18_2::emulation,
273            Emulation::SafariIos18_1_1 => safari_ios_18_1_1::emulation,
274            Emulation::Safari18_3 => safari18_3::emulation,
275
276            Emulation::OkHttp3_9 => okhttp3_9::emulation,
277            Emulation::OkHttp3_11 => okhttp3_11::emulation,
278            Emulation::OkHttp3_13 => okhttp3_13::emulation,
279            Emulation::OkHttp3_14 => okhttp3_14::emulation,
280            Emulation::OkHttp4_9 => okhttp4_9::emulation,
281            Emulation::OkHttp4_10 => okhttp4_10::emulation,
282            Emulation::OkHttp4_12 => okhttp4_12::emulation,
283            Emulation::OkHttp5 => okhttp5::emulation,
284
285            Emulation::Edge101 => edge101::emulation,
286            Emulation::Edge122 => edge122::emulation,
287            Emulation::Edge127 => edge127::emulation,
288            Emulation::Edge131 => edge131::emulation,
289            Emulation::Edge134 => edge134::emulation,
290
291            Emulation::Firefox109 => ff109::emulation,
292            Emulation::Firefox117 => ff117::emulation,
293            Emulation::Firefox128 => ff128::emulation,
294            Emulation::Firefox133 => ff133::emulation,
295            Emulation::Firefox135 => ff135::emulation,
296            Emulation::FirefoxPrivate135 => ff_private_135::emulation,
297            Emulation::FirefoxAndroid135 => ff_android_135::emulation,
298            Emulation::Firefox136 => ff136::emulation,
299            Emulation::FirefoxPrivate136 => ff_private_136::emulation
300        )
301    }
302}