Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions crates/wreq-util/src/emulate/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,27 @@ macro_rules! header_firefox_sec_fetch {

macro_rules! header_firefox_accept {
($headers:expr) => {
$headers.insert(
ACCEPT,
HeaderValue::from_static(
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
),
);
$headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.5"));
#[cfg(feature = "emulation-compression")]
$headers.insert(
ACCEPT_ENCODING,
HeaderValue::from_static("gzip, deflate, br"),
);
header_firefox_accept!(@build $headers, "en-US,en;q=0.5", "gzip, deflate, br");
};
(zstd, $headers:expr) => {
header_firefox_accept!(@build $headers, "en-US,en;q=0.5", "gzip, deflate, br, zstd");
};
// Firefox 147 changed the generated q-value from dividing 1.0 across the list to
// decrementing by 0.1 per entry, so a two-entry list became q=0.9 (Bugzilla 2000765,
// landed in 0e050ae5116d).
(zstd_q09, $headers:expr) => {
header_firefox_accept!(@build $headers, "en-US,en;q=0.9", "gzip, deflate, br, zstd");
};
(@build $headers:expr, $accept_language:expr, $accept_encoding:expr) => {
$headers.insert(
ACCEPT,
HeaderValue::from_static(
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
),
);
$headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.5"));
$headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static($accept_language));
#[cfg(feature = "emulation-compression")]
$headers.insert(
ACCEPT_ENCODING,
HeaderValue::from_static("gzip, deflate, br, zstd"),
);
$headers.insert(ACCEPT_ENCODING, HeaderValue::from_static($accept_encoding));
};
}

Expand Down
12 changes: 6 additions & 6 deletions crates/wreq-util/src/emulate/profile/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ mod_generator!(
mod_generator!(
ff147,
ff135::build_emulation,
header_initializer_with_zstd,
header_initializer_with_zstd_q09,
[
(
Windows,
Expand Down Expand Up @@ -408,7 +408,7 @@ mod_generator!(
mod_generator!(
ff148,
ff135::build_emulation,
header_initializer_with_zstd,
header_initializer_with_zstd_q09,
[
(
Windows,
Expand Down Expand Up @@ -436,7 +436,7 @@ mod_generator!(
mod_generator!(
ff149,
ff135::build_emulation,
header_initializer_with_zstd,
header_initializer_with_zstd_q09,
[
(
Windows,
Expand Down Expand Up @@ -465,7 +465,7 @@ mod_generator!(
ff150,
tls_options!(4, CIPHER_LIST_3, CURVES_2, KEY_SHARES_2),
http2_options!(1),
header_initializer_with_zstd,
header_initializer_with_zstd_q09,
[
(
Windows,
Expand Down Expand Up @@ -493,7 +493,7 @@ mod_generator!(
mod_generator!(
ff151,
ff150::build_emulation,
header_initializer_with_zstd,
header_initializer_with_zstd_q09,
[
(
Windows,
Expand Down Expand Up @@ -521,7 +521,7 @@ mod_generator!(
mod_generator!(
ff152,
ff150::build_emulation,
header_initializer_with_zstd,
header_initializer_with_zstd_q09,
[
(
Windows,
Expand Down
22 changes: 22 additions & 0 deletions crates/wreq-util/src/emulate/profile/firefox/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ pub fn header_initializer_with_zstd(ua: &'static str) -> HeaderMap {
);
headers
}

/// Firefox 147 and later. Identical to [`header_initializer_with_zstd`] apart from the
/// `Accept-Language` q-value, which Firefox changed in 147 (Bugzilla 2000765).
pub fn header_initializer_with_zstd_q09(ua: &'static str) -> HeaderMap {
let mut headers = HeaderMap::new();
header_firefox_ua!(headers, ua);
header_firefox_accept!(zstd_q09, headers);
headers.insert(
HeaderName::from_static("upgrade-insecure-requests"),
HeaderValue::from_static("1"),
);
header_firefox_sec_fetch!(headers);
headers.insert(
HeaderName::from_static("priority"),
HeaderValue::from_static("u=0, i"),
);
headers.insert(
HeaderName::from_static("te"),
HeaderValue::from_static("trailers"),
);
headers
}
58 changes: 58 additions & 0 deletions crates/wreq-util/tests/emulate_firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,61 @@ test_emulation!(
["t13d1617h2_86a278354501_3cbfd9057e0d"],
"6ea73faa8fc5aac76bded7bd238f6433"
);

/// Firefox 147 changed the generated `Accept-Language` q-value: the list used to divide 1.0
/// across its entries, giving `q=0.5` for two, and now decrements by 0.1 per entry, giving
/// `q=0.9`. See Bugzilla 2000765, landed in mozilla-firefox 0e050ae5116d.
///
/// Every profile is listed rather than a range, so adding one is a deliberate choice about
/// which side of 147 it falls on.
#[test]
fn firefox_accept_language_matches_the_profile_version() {
use wreq::IntoEmulation;
use wreq_util::{Platform, Profile};

const PRE_147: &[(&str, Profile)] = &[
("Firefox109", Emulation::Firefox109),
("Firefox117", Emulation::Firefox117),
("Firefox128", Emulation::Firefox128),
("Firefox133", Emulation::Firefox133),
("Firefox135", Emulation::Firefox135),
("FirefoxPrivate135", Emulation::FirefoxPrivate135),
("FirefoxAndroid135", Emulation::FirefoxAndroid135),
("Firefox136", Emulation::Firefox136),
("FirefoxPrivate136", Emulation::FirefoxPrivate136),
("Firefox139", Emulation::Firefox139),
("Firefox142", Emulation::Firefox142),
("Firefox143", Emulation::Firefox143),
("Firefox144", Emulation::Firefox144),
("Firefox145", Emulation::Firefox145),
("Firefox146", Emulation::Firefox146),
];
const POST_147: &[(&str, Profile)] = &[
("Firefox147", Emulation::Firefox147),
("Firefox148", Emulation::Firefox148),
("Firefox149", Emulation::Firefox149),
("Firefox150", Emulation::Firefox150),
("Firefox151", Emulation::Firefox151),
("Firefox152", Emulation::Firefox152),
];

fn accept_language(profile: Profile) -> String {
Emulation::builder()
.profile(profile)
.platform(Platform::Linux)
.build()
.into_emulation()
.headers
.get("accept-language")
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_owned()
}

for (name, profile) in PRE_147 {
assert_eq!("en-US,en;q=0.5", accept_language(*profile), "{name}");
}
for (name, profile) in POST_147 {
assert_eq!("en-US,en;q=0.9", accept_language(*profile), "{name}");
}
}
Loading