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    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
130/// ======== Emulation impls ========
131impl 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    /// Represents different operating systems for impersonation.
142    ///
143    /// The `EmulationOS` enum provides variants for different operating systems that can be used
144    /// to emulation HTTP requests. Each variant corresponds to a specific operating system.
145    ///
146    /// # Naming Convention
147    ///
148    /// The naming convention for the variants follows the pattern `os_name`, where
149    /// `os_name` is the name of the operating system (e.g., `windows`, `macos`, `linux`, `android`, `ios`).
150    ///
151    /// The serialized names of the variants use lowercase letters to represent the operating system names.
152    /// For example, `Windows` is serialized as `"windows"`.
153    ///
154    /// # Examples
155    ///
156    /// ```rust
157    /// use rquest::EmulationOS;
158    ///
159    /// let emulation_os = EmulationOS::Windows;
160    /// let serialized = serde_json::to_string(&emulation_os).unwrap();
161    /// assert_eq!(serialized, "\"windows\"");
162    ///
163    /// let deserialized: EmulationOS = serde_json::from_str(&serialized).unwrap();
164    /// assert_eq!(deserialized, EmulationOS::Windows);
165    /// ```
166    EmulationOS, MacOS,
167    Windows => "windows",
168    MacOS => "macos",
169    Linux => "linux",
170    Android => "android",
171    IOS => "ios"
172);
173
174/// ======== EmulationOS impls ========
175impl 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/// Represents the configuration options for emulating a browser and operating system.
194///
195/// The `EmulationOption` struct allows you to configure various aspects of browser and OS emulation,
196/// including the browser version, operating system, and whether to skip certain features like HTTP/2
197/// or headers.
198///
199/// This struct is typically used to build an `EmulationProvider` that can be applied to HTTP clients
200/// for making requests that mimic specific browser and OS configurations.
201///
202/// # Fields
203///
204/// - `emulation`: The browser version to emulate. Defaults to `Emulation::default()`.
205/// - `emulation_os`: The operating system to emulate. Defaults to `EmulationOS::default()`.
206/// - `skip_http2`: Whether to skip HTTP/2 support. Defaults to `false`.
207/// - `skip_headers`: Whether to skip adding default headers. Defaults to `false`.
208///
209/// # Examples
210///
211/// ```rust
212/// use rquest_util::{Emulation, EmulationOS, EmulationOption};
213///
214/// let emulation_option = EmulationOption::builder()
215///     .emulation(Emulation::Chrome134)
216///     .emulation_os(EmulationOS::MacOS)
217///     .skip_http2(true)
218///     .skip_headers(false)
219///     .build();
220///
221/// // Use `emulation_option` to create an EmulationProvider
222/// let emulation_provider = emulation_option.emulation();
223/// ```
224#[derive(Default, TypedBuilder)]
225pub struct EmulationOption {
226    /// The browser version to emulation.
227    #[builder(default)]
228    emulation: Emulation,
229
230    /// The operating system.
231    #[builder(default)]
232    emulation_os: EmulationOS,
233
234    /// Whether to skip HTTP/2.
235    #[builder(default = false)]
236    skip_http2: bool,
237
238    /// Whether to skip headers.
239    #[builder(default = false)]
240    skip_headers: bool,
241}
242
243/// ======== EmulationOption impls ========
244macro_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}