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};
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    /// Represents different browser versions for impersonation.
36    ///
37    /// The `Emulation` enum provides variants for different browser versions that can be used
38    /// to emulation HTTP requests. Each variant corresponds to a specific browser version.
39    ///
40    /// # Naming Convention
41    ///
42    /// The naming convention for the variants follows the pattern `browser_version`, where
43    /// `browser` is the name of the browser (e.g., `chrome`, `firefox`, `safari`) and `version`
44    /// is the version number. For example, `Chrome100` represents Chrome version 100.
45    ///
46    /// The serialized names of the variants use underscores to separate the browser name and
47    /// version number, following the pattern `browser_version`. For example, `Chrome100` is
48    /// serialized as `"chrome_100"`.
49    ///
50    /// # Examples
51    ///
52    /// ```rust
53    /// use rquest_util::Emulation;
54    ///
55    /// let emulation = Emulation::Chrome100;
56    /// let serialized = serde_json::to_string(&emulation).unwrap();
57    /// assert_eq!(serialized, "\"chrome_100\"");
58    ///
59    /// let deserialized: Emulation = serde_json::from_str(&serialized).unwrap();
60    /// assert_eq!(deserialized, Emulation::Chrome100);
61    /// ```
62    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    Chrome110 => "chrome_110",
72    Chrome114 => "chrome_114",
73    Chrome116 => "chrome_116",
74    Chrome117 => "chrome_117",
75    Chrome118 => "chrome_118",
76    Chrome119 => "chrome_119",
77    Chrome120 => "chrome_120",
78    Chrome123 => "chrome_123",
79    Chrome124 => "chrome_124",
80    Chrome126 => "chrome_126",
81    Chrome127 => "chrome_127",
82    Chrome128 => "chrome_128",
83    Chrome129 => "chrome_129",
84    Chrome130 => "chrome_130",
85    Chrome131 => "chrome_131",
86    Chrome132 => "chrome_132",
87    Chrome133 => "chrome_133",
88    Chrome134 => "chrome_134",
89    Chrome135 => "chrome_135",
90
91    SafariIos17_2 => "safari_ios_17.2",
92    SafariIos17_4_1 => "safari_ios_17.4.1",
93    SafariIos16_5 => "safari_ios_16.5",
94    Safari15_3 => "safari_15.3",
95    Safari15_5 => "safari_15.5",
96    Safari15_6_1 => "safari_15.6.1",
97    Safari16 => "safari_16",
98    Safari16_5 => "safari_16.5",
99    Safari17_0 => "safari_17.0",
100    Safari17_2_1 => "safari_17.2.1",
101    Safari17_4_1 => "safari_17.4.1",
102    Safari17_5 => "safari_17.5",
103    Safari18 => "safari_18",
104    SafariIPad18 => "safari_ipad_18",
105    Safari18_2 => "safari_18.2",
106    SafariIos18_1_1 => "safari_ios_18.1.1",
107    Safari18_3 => "safari_18.3",
108    Safari18_3_1 => "safari_18.3.1",
109    OkHttp3_9 => "okhttp_3.9",
110    OkHttp3_11 => "okhttp_3.11",
111    OkHttp3_13 => "okhttp_3.13",
112    OkHttp3_14 => "okhttp_3.14",
113    OkHttp4_9 => "okhttp_4.9",
114    OkHttp4_10 => "okhttp_4.10",
115    OkHttp4_12 => "okhttp_4.12",
116    OkHttp5 => "okhttp_5",
117    Edge101 => "edge_101",
118    Edge122 => "edge_122",
119    Edge127 => "edge_127",
120    Edge131 => "edge_131",
121    Edge134 => "edge_134",
122    Firefox109 => "firefox_109",
123    Firefox117 => "firefox_117",
124    Firefox128 => "firefox_128",
125    Firefox133 => "firefox_133",
126    Firefox135 => "firefox_135",
127    FirefoxPrivate135 => "firefox_private_135",
128    FirefoxAndroid135 => "firefox_android_135",
129    Firefox136 => "firefox_136",
130    FirefoxPrivate136 => "firefox_private_136"
131);
132
133/// ======== Emulation impls ========
134impl EmulationProviderFactory for Emulation {
135    fn emulation(self) -> EmulationProvider {
136        EmulationOption::builder()
137            .emulation(self)
138            .build()
139            .emulation()
140    }
141}
142
143define_emulation_enum!(
144    /// Represents different operating systems for impersonation.
145    ///
146    /// The `EmulationOS` enum provides variants for different operating systems that can be used
147    /// to emulation HTTP requests. Each variant corresponds to a specific operating system.
148    ///
149    /// # Naming Convention
150    ///
151    /// The naming convention for the variants follows the pattern `os_name`, where
152    /// `os_name` is the name of the operating system (e.g., `windows`, `macos`, `linux`, `android`, `ios`).
153    ///
154    /// The serialized names of the variants use lowercase letters to represent the operating system names.
155    /// For example, `Windows` is serialized as `"windows"`.
156    ///
157    /// # Examples
158    ///
159    /// ```rust
160    /// use rquest::EmulationOS;
161    ///
162    /// let emulation_os = EmulationOS::Windows;
163    /// let serialized = serde_json::to_string(&emulation_os).unwrap();
164    /// assert_eq!(serialized, "\"windows\"");
165    ///
166    /// let deserialized: EmulationOS = serde_json::from_str(&serialized).unwrap();
167    /// assert_eq!(deserialized, EmulationOS::Windows);
168    /// ```
169    EmulationOS, MacOS,
170    Windows => "windows",
171    MacOS => "macos",
172    Linux => "linux",
173    Android => "android",
174    IOS => "ios"
175);
176
177/// ======== EmulationOS impls ========
178impl EmulationOS {
179    #[inline]
180    const 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    const fn is_mobile(&self) -> bool {
192        matches!(self, EmulationOS::Android | EmulationOS::IOS)
193    }
194}
195
196/// Represents the configuration options for emulating a browser and operating system.
197///
198/// The `EmulationOption` struct allows you to configure various aspects of browser and OS emulation,
199/// including the browser version, operating system, and whether to skip certain features like HTTP/2
200/// or headers.
201///
202/// This struct is typically used to build an `EmulationProvider` that can be applied to HTTP clients
203/// for making requests that mimic specific browser and OS configurations.
204///
205/// # Fields
206///
207/// - `emulation`: The browser version to emulate. Defaults to `Emulation::default()`.
208/// - `emulation_os`: The operating system to emulate. Defaults to `EmulationOS::default()`.
209/// - `skip_http2`: Whether to skip HTTP/2 support. Defaults to `false`.
210/// - `skip_headers`: Whether to skip adding default headers. Defaults to `false`.
211///
212/// # Examples
213///
214/// ```rust
215/// use rquest_util::{Emulation, EmulationOS, EmulationOption};
216///
217/// let emulation_option = EmulationOption::builder()
218///     .emulation(Emulation::Chrome134)
219///     .emulation_os(EmulationOS::MacOS)
220///     .skip_http2(true)
221///     .skip_headers(false)
222///     .build();
223///
224/// // Use `emulation_option` to create an EmulationProvider
225/// let emulation_provider = emulation_option.emulation();
226/// ```
227#[derive(Default, TypedBuilder)]
228pub struct EmulationOption {
229    /// The browser version to emulation.
230    #[builder(default)]
231    emulation: Emulation,
232
233    /// The operating system.
234    #[builder(default)]
235    emulation_os: EmulationOS,
236
237    /// Whether to skip HTTP/2.
238    #[builder(default = false)]
239    skip_http2: bool,
240
241    /// Whether to skip headers.
242    #[builder(default = false)]
243    skip_headers: bool,
244}
245
246/// ======== EmulationOption impls ========
247macro_rules! emulation_match {
248    ($ver:expr, $opt:expr, $($variant:pat => $path:expr),+) => {
249        match $ver {
250            $(
251                $variant => $path($opt),
252            )+
253        }
254    }
255}
256
257impl EmulationProviderFactory for EmulationOption {
258    fn emulation(self) -> EmulationProvider {
259        emulation_match!(
260            self.emulation,
261            self,
262
263            Emulation::Chrome100 => v100::emulation,
264            Emulation::Chrome101 => v101::emulation,
265            Emulation::Chrome104 => v104::emulation,
266            Emulation::Chrome105 => v105::emulation,
267            Emulation::Chrome106 => v106::emulation,
268            Emulation::Chrome107 => v107::emulation,
269            Emulation::Chrome108 => v108::emulation,
270            Emulation::Chrome109 => v109::emulation,
271            Emulation::Chrome110 => v110::emulation,
272            Emulation::Chrome114 => v114::emulation,
273            Emulation::Chrome116 => v116::emulation,
274            Emulation::Chrome117 => v117::emulation,
275            Emulation::Chrome118 => v118::emulation,
276            Emulation::Chrome119 => v119::emulation,
277            Emulation::Chrome120 => v120::emulation,
278            Emulation::Chrome123 => v123::emulation,
279            Emulation::Chrome124 => v124::emulation,
280            Emulation::Chrome126 => v126::emulation,
281            Emulation::Chrome127 => v127::emulation,
282            Emulation::Chrome128 => v128::emulation,
283            Emulation::Chrome129 => v129::emulation,
284            Emulation::Chrome130 => v130::emulation,
285            Emulation::Chrome131 => v131::emulation,
286            Emulation::Chrome132 => v132::emulation,
287            Emulation::Chrome133 => v133::emulation,
288            Emulation::Chrome134 => v134::emulation,
289            Emulation::Chrome135 => v135::emulation,
290
291            Emulation::SafariIos17_2 => safari_ios_17_2::emulation,
292            Emulation::SafariIos17_4_1 => safari_ios_17_4_1::emulation,
293            Emulation::SafariIos16_5 => safari_ios_16_5::emulation,
294            Emulation::Safari15_3 => safari15_3::emulation,
295            Emulation::Safari15_5 => safari15_5::emulation,
296            Emulation::Safari15_6_1 => safari15_6_1::emulation,
297            Emulation::Safari16 => safari16::emulation,
298            Emulation::Safari16_5 => safari16_5::emulation,
299            Emulation::Safari17_0 => safari17_0::emulation,
300            Emulation::Safari17_2_1 => safari17_2_1::emulation,
301            Emulation::Safari17_4_1 => safari17_4_1::emulation,
302            Emulation::Safari17_5 => safari17_5::emulation,
303            Emulation::Safari18 => safari18::emulation,
304            Emulation::SafariIPad18 => safari_ipad_18::emulation,
305            Emulation::Safari18_2 => safari18_2::emulation,
306            Emulation::SafariIos18_1_1 => safari_ios_18_1_1::emulation,
307            Emulation::Safari18_3 => safari18_3::emulation,
308            Emulation::Safari18_3_1 => safari18_3_1::emulation,
309
310            Emulation::OkHttp3_9 => okhttp3_9::emulation,
311            Emulation::OkHttp3_11 => okhttp3_11::emulation,
312            Emulation::OkHttp3_13 => okhttp3_13::emulation,
313            Emulation::OkHttp3_14 => okhttp3_14::emulation,
314            Emulation::OkHttp4_9 => okhttp4_9::emulation,
315            Emulation::OkHttp4_10 => okhttp4_10::emulation,
316            Emulation::OkHttp4_12 => okhttp4_12::emulation,
317            Emulation::OkHttp5 => okhttp5::emulation,
318
319            Emulation::Edge101 => edge101::emulation,
320            Emulation::Edge122 => edge122::emulation,
321            Emulation::Edge127 => edge127::emulation,
322            Emulation::Edge131 => edge131::emulation,
323            Emulation::Edge134 => edge134::emulation,
324
325            Emulation::Firefox109 => ff109::emulation,
326            Emulation::Firefox117 => ff117::emulation,
327            Emulation::Firefox128 => ff128::emulation,
328            Emulation::Firefox133 => ff133::emulation,
329            Emulation::Firefox135 => ff135::emulation,
330            Emulation::FirefoxPrivate135 => ff_private_135::emulation,
331            Emulation::FirefoxAndroid135 => ff_android_135::emulation,
332            Emulation::Firefox136 => ff136::emulation,
333            Emulation::FirefoxPrivate136 => ff_private_136::emulation
334        )
335    }
336}