Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(network): scope CDP header overrides and preserve BiDi byte values

ldm029283fd86a02
diff --git a/moli-core/src/page/settings_support.rs b/moli-core/src/page/settings_support.rs
index b58f1afc0..6a2845f4f 100644
--- a/moli-core/src/page/settings_support.rs
+++ b/moli-core/src/page/settings_support.rs
@@ -113,10 +113,10 @@ impl Page {
pub async fn set_extra_http_headers_async(
&mut self,
- headers: &[(String, String)],
+ headers: &moli_fetch::RequestHeaders,
) -> Result<()> {
self.dispatch_unit_page_command_async(
- RendererPageCommand::SetExtraHttpHeaders(headers.to_vec()),
+ RendererPageCommand::SetExtraHttpHeaders(headers.clone()),
"set extra HTTP headers",
)
.await
@@ -124,9 +124,9 @@ impl Page {
pub fn start_set_extra_http_headers(
&self,
- headers: &[(String, String)],
+ headers: &moli_fetch::RequestHeaders,
) -> Result<PendingPageCommand> {
- self.start_page_command(RendererPageCommand::SetExtraHttpHeaders(headers.to_vec()))
+ self.start_page_command(RendererPageCommand::SetExtraHttpHeaders(headers.clone()))
}
pub fn finish_set_extra_http_headers(
@@ -144,14 +144,14 @@ impl Page {
pub async fn set_network_request_policy_async(
&mut self,
- extra_http_headers: &[(String, String)],
+ extra_http_headers: &moli_fetch::RequestHeaders,
bypass_service_worker: bool,
cache_disabled: bool,
blocked_url_patterns: &[String],
) -> Result<()> {
self.dispatch_unit_page_command_async(
RendererPageCommand::SetNetworkRequestPolicy {
- extra_http_headers: extra_http_headers.to_vec(),
+ extra_http_headers: extra_http_headers.clone(),
bypass_service_worker,
cache_disabled,
blocked_url_patterns: blocked_url_patterns.to_vec(),
@@ -163,13 +163,13 @@ impl Page {
pub fn start_set_network_request_policy(
&self,
- extra_http_headers: &[(String, String)],
+ extra_http_headers: &moli_fetch::RequestHeaders,
bypass_service_worker: bool,
cache_disabled: bool,
blocked_url_patterns: &[String],
) -> Result<PendingPageCommand> {
self.start_page_command(RendererPageCommand::SetNetworkRequestPolicy {
- extra_http_headers: extra_http_headers.to_vec(),
+ extra_http_headers: extra_http_headers.clone(),
bypass_service_worker,
cache_disabled,
blocked_url_patterns: blocked_url_patterns.to_vec(),
diff --git a/moli-core/src/page/subresource_support.rs b/moli-core/src/page/subresource_support.rs
index db383caa3..c4c04f7db 100644
--- a/moli-core/src/page/subresource_support.rs
+++ b/moli-core/src/page/subresource_support.rs
@@ -30,7 +30,7 @@ impl Page {
url: Option<Url>,
method: Option<String>,
body: Option<Option<String>>,
- headers: Option<moli_fetch::RequestHeaders>,
+ headers: Option<moli_fetch::RequestHeaderOverride>,
intercept_response: bool,
handle_auth_requests: bool,
) -> Result<PendingSubresourceContinueOutcome> {
@@ -52,7 +52,7 @@ impl Page {
url: Option<Url>,
method: Option<String>,
body: Option<Option<String>>,
- headers: Option<moli_fetch::RequestHeaders>,
+ headers: Option<moli_fetch::RequestHeaderOverride>,
intercept_response: bool,
handle_auth_requests: bool,
) -> Result<PendingPageCommand> {
diff --git a/moli-core/src/runtime/mod.rs b/moli-core/src/runtime/mod.rs
index 82c7bd968..765ec5144 100644
--- a/moli-core/src/runtime/mod.rs
+++ b/moli-core/src/runtime/mod.rs
@@ -1163,7 +1163,7 @@ impl Browser {
document_start_scripts,
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
diff --git a/moli-core/src/runtime/navigation_engine.rs b/moli-core/src/runtime/navigation_engine.rs
index a9c8766c6..c4b4779e9 100644
--- a/moli-core/src/runtime/navigation_engine.rs
+++ b/moli-core/src/runtime/navigation_engine.rs
@@ -50,7 +50,7 @@ struct DocumentPageLoadOptions {
pub document_start_scripts: Vec<DocumentStartScript>,
pub runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
pub runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- pub extra_http_headers: Vec<(String, String)>,
+ pub extra_http_headers: moli_fetch::RequestHeaders,
pub script_execution_disabled: bool,
pub bypass_content_security_policy: bool,
pub emulated_media: EmulatedMediaOverrides,
@@ -198,7 +198,7 @@ pub struct PreparedDocumentPageCommitConfiguration {
pub runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
pub runtime_isolated_worlds: Vec<RuntimeIsolatedWorldDefinition>,
pub permission_overrides: Vec<PermissionOverrideRegistration>,
- pub extra_http_headers: Vec<(String, String)>,
+ pub extra_http_headers: moli_fetch::RequestHeaders,
pub script_execution_disabled: bool,
pub bypass_content_security_policy: bool,
pub emulated_media: EmulatedMediaOverrides,
@@ -726,7 +726,7 @@ impl NavigationEngine {
self.page_network_policy.set_cache_disabled(disabled);
}
- pub fn set_extra_http_headers(&mut self, headers: &[(String, String)]) {
+ pub fn set_extra_http_headers(&mut self, headers: &moli_fetch::RequestHeaders) {
self.page_network_policy.set_extra_http_headers(headers);
}
@@ -1163,7 +1163,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
@@ -1223,7 +1223,7 @@ impl NavigationEngine {
response_body: String,
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
@@ -1264,7 +1264,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
@@ -1320,7 +1320,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
bypass_content_security_policy: bool,
emulated_media: EmulatedMediaOverrides,
@@ -1380,7 +1380,7 @@ impl NavigationEngine {
response_body: String,
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
@@ -1432,7 +1432,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
bypass_content_security_policy: bool,
emulated_media: EmulatedMediaOverrides,
@@ -1503,7 +1503,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
bypass_content_security_policy: bool,
emulated_media: EmulatedMediaOverrides,
@@ -1571,7 +1571,7 @@ impl NavigationEngine {
response_body: String,
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
@@ -1626,7 +1626,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
bypass_content_security_policy: bool,
emulated_media: EmulatedMediaOverrides,
@@ -1691,7 +1691,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
@@ -1765,7 +1765,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
bypass_content_security_policy: bool,
emulated_media: EmulatedMediaOverrides,
@@ -1850,7 +1850,7 @@ impl NavigationEngine {
raw_body: ExternalRawDocumentBodyStream,
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
@@ -1906,7 +1906,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
@@ -1973,7 +1973,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
bypass_content_security_policy: bool,
emulated_media: EmulatedMediaOverrides,
@@ -2184,7 +2184,7 @@ impl NavigationEngine {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
bypass_content_security_policy: bool,
emulated_media: EmulatedMediaOverrides,
@@ -2249,7 +2249,7 @@ impl NavigationEngine {
response_body: String,
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::page::RuntimeBindingRegistration>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
script_execution_disabled: bool,
emulated_media: EmulatedMediaOverrides,
viewport_surface: Option<ViewportSurface>,
diff --git a/moli-core/src/runtime/tests.rs b/moli-core/src/runtime/tests.rs
index ce05fed6a..36eae7089 100644
--- a/moli-core/src/runtime/tests.rs
+++ b/moli-core/src/runtime/tests.rs
@@ -216,7 +216,7 @@ async fn page_creation_diagnostics_include_default_runtime_context_event() -> Re
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -307,7 +307,7 @@ async fn page_creation_diagnostics_fence_initial_runtime_observable_output() ->
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -1544,7 +1544,7 @@ async fn navigation_engine_keeps_network_runtime_when_web_storage_changes() -> R
"<!doctype html><html><body>first</body></html>".to_owned(),
Vec::new(),
Vec::new(),
- Vec::new(),
+ Default::default(),
false,
Default::default(),
None,
@@ -1578,7 +1578,7 @@ async fn navigation_engine_keeps_network_runtime_when_web_storage_changes() -> R
"<!doctype html><html><body>second</body></html>".to_owned(),
Vec::new(),
Vec::new(),
- Vec::new(),
+ Default::default(),
false,
Default::default(),
None,
@@ -3570,7 +3570,7 @@ async fn renderer_owner_create_page_command_produces_page() -> Result<()> {
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -3632,7 +3632,7 @@ async fn renderer_owner_created_page_runs_common_page_commands() -> Result<()> {
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -4617,7 +4617,7 @@ async fn renderer_owner_created_page_runs_runtime_protocol_commands() -> Result<
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -4682,7 +4682,7 @@ async fn renderer_owner_created_page_runs_document_start_commands() -> Result<()
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -4862,7 +4862,7 @@ async fn renderer_owner_created_page_runs_input_commands() -> Result<()> {
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -4937,7 +4937,7 @@ async fn renderer_owner_created_page_runs_input_query_commands() -> Result<()> {
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -5033,7 +5033,7 @@ async fn renderer_owner_created_page_reports_real_client_rect_for_positioned_nod
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -5116,7 +5116,7 @@ async fn renderer_owner_created_page_resolves_backend_node_runtime_object() -> R
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -5201,7 +5201,7 @@ async fn runtime_object_resolve_uses_live_backend_node_for_stale_snapshot_path()
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -5336,7 +5336,7 @@ async fn renderer_owner_created_page_runs_inline_stylesheet_commands() -> Result
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
@@ -5450,7 +5450,7 @@ async fn renderer_owner_created_page_runs_subresource_interception_commands() ->
vec![],
vec![],
vec![],
- vec![],
+ Default::default(),
false,
Vec::new(),
false,
diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rs
index ab62bcee1..5557f1af0 100644
--- a/moli-fetch/src/lib.rs
+++ b/moli-fetch/src/lib.rs
@@ -65,9 +65,10 @@ pub use referrer_policy::{
};
pub use request::{
BrowserNavigationRequestKind, BrowserRequestMetadata, FetchPriorityHint, Request, RequestAuth,
- RequestAuthScheme, RequestAuthTarget, RequestCacheMode, RequestCredentialsMode, RequestMode,
- RequestPriorityHints, RequestRedirectMode, RequestResourceType, ResourceLoadPriority,
- ScriptFetchRequestMetadata, ScriptFetchSchedulerPriority, SubresourceRequestMetadata,
+ RequestAuthScheme, RequestAuthTarget, RequestCacheMode, RequestCredentialsMode,
+ RequestHeaderOverride, RequestMode, RequestPriorityHints, RequestRedirectMode,
+ RequestResourceType, ResourceLoadPriority, ScriptFetchRequestMetadata,
+ ScriptFetchSchedulerPriority, SubresourceRequestMetadata,
};
pub use request_policy::{is_bad_port, should_request_be_blocked_due_to_bad_port};
pub use response::{
diff --git a/moli-fetch/src/request.rs b/moli-fetch/src/request.rs
index 0cb46d374..4b6c4df56 100644
--- a/moli-fetch/src/request.rs
+++ b/moli-fetch/src/request.rs
@@ -31,6 +31,7 @@ pub struct Request {
pub body: Option<Vec<u8>>,
/// Wire bytes, with the source encoding resolved before header merging.
pub request_headers: crate::RequestHeaders,
+ redirect_headers: Option<crate::RequestHeaders>,
cache_mode: RequestCacheMode,
pub resource_type: RequestResourceType,
subresource_request_metadata: Option<SubresourceRequestMetadata>,
@@ -54,6 +55,47 @@ pub struct Request {
browser_identity: Option<std::sync::Arc<moli_browser_profile::BrowserIdentityProfile>>,
}
+/// The lifetime of request headers supplied by an interception command.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum RequestHeaderOverride {
+ /// CDP overrides expire when the request follows a redirect.
+ CurrentRequest {
+ headers: crate::RequestHeaders,
+ /// A preceding persistent override, or the renderer's original headers when absent.
+ redirect_headers: Option<crate::RequestHeaders>,
+ },
+ /// BiDi modifies the underlying request header list.
+ RedirectChain(crate::RequestHeaders),
+}
+
+impl RequestHeaderOverride {
+ pub fn current_request(headers: crate::RequestHeaders, previous: Option<&Self>) -> Self {
+ let redirect_headers = match previous {
+ Some(Self::RedirectChain(headers)) => Some(headers.clone()),
+ Some(Self::CurrentRequest {
+ redirect_headers, ..
+ }) => redirect_headers.clone(),
+ None => None,
+ };
+ Self::CurrentRequest {
+ headers,
+ redirect_headers,
+ }
+ }
+
+ pub fn headers(&self) -> &crate::RequestHeaders {
+ match self {
+ Self::CurrentRequest { headers, .. } | Self::RedirectChain(headers) => headers,
+ }
+ }
+}
+
+impl From<crate::RequestHeaders> for RequestHeaderOverride {
+ fn from(headers: crate::RequestHeaders) -> Self {
+ Self::RedirectChain(headers)
+ }
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BrowserRequestMetadata {
Audio,
@@ -387,6 +429,7 @@ impl Request {
method: "GET".to_owned(),
body: None,
request_headers: crate::RequestHeaders::default(),
+ redirect_headers: None,
cache_mode: RequestCacheMode::Default,
resource_type: RequestResourceType::Raw,
subresource_request_metadata: None,
@@ -418,6 +461,7 @@ impl Request {
method: "GET".to_owned(),
body: None,
request_headers: crate::RequestHeaders::default(),
+ redirect_headers: None,
cache_mode: RequestCacheMode::Default,
resource_type: RequestResourceType::Raw,
subresource_request_metadata: None,
@@ -482,6 +526,7 @@ impl Request {
method: method.to_owned(),
body,
request_headers: request_headers.into(),
+ redirect_headers: None,
cache_mode: RequestCacheMode::Default,
resource_type: RequestResourceType::Raw,
subresource_request_metadata: None,
@@ -727,7 +772,11 @@ impl Request {
.context("browser fetch requires an explicit environment origin")
}
+ /// Restore request-scoped interception headers before applying redirect rules.
pub fn apply_redirect_status(&mut self, status: u16) {
+ if let Some(headers) = self.redirect_headers.take() {
+ self.request_headers = headers;
+ }
if redirect_status_rewrites_to_get(status, &self.method) {
self.method = "GET".to_owned();
self.body = None;
@@ -736,6 +785,11 @@ impl Request {
}
}
+ pub fn with_redirect_headers(mut self, headers: Option<crate::RequestHeaders>) -> Self {
+ self.redirect_headers = headers;
+ self
+ }
+
pub fn with_credentials_mode(mut self, credentials_mode: RequestCredentialsMode) -> Self {
self.credentials_mode = credentials_mode;
self
diff --git a/moli-fetch/src/tests/header_bytes.rs b/moli-fetch/src/tests/header_bytes.rs
index c07f00fca..c84094de0 100644
--- a/moli-fetch/src/tests/header_bytes.rs
+++ b/moli-fetch/src/tests/header_bytes.rs
@@ -227,3 +227,78 @@ async fn http_header_bytes_survive_disk_cache_reopen() -> Result<()> {
}
Ok(())
}
+
+#[tokio::test]
+async fn current_hop_headers_expire_before_redirect_method_rewrite_in_all_transports() -> Result<()>
+{
+ for status in [302, 307] {
+ for mode in ["buffered", "html", "raw"] {
+ let listener = TcpListener::bind("127.0.0.1:0").await?;
+ let url = format!("http://{}/start", listener.local_addr()?);
+ let redirect = format!(
+ "HTTP/1.1 {status} Redirect\r\nLocation: /final\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
+ ).into_bytes();
+ let server = tokio::spawn(serve_responses(listener, vec![redirect, response(b"")]));
+ let client =
+ FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store());
+ let original = crate::RequestHeaders::from_bytes(vec![
+ ("X-Raw".into(), vec![0xe9, 0xff]),
+ ("Content-Type".into(), b"text/plain".to_vec()),
+ ]);
+ let request = Request::new(
+ "POST",
+ &url,
+ Some("body".into()),
+ vec![
+ ("X-Raw".into(), "ÿ".into()),
+ ("Content-Type".into(), "application/json".into()),
+ ],
+ )?
+ .with_redirect_headers(Some(original));
+ let (head, body) = match mode {
+ "buffered" => client.fetch(request).await?.into_body(),
+ "html" => client.fetch_html_stream(request).await?.into_body(),
+ "raw" => client
+ .fetch_raw_stream_with_cancel(request, FetchCancelHandle::new())
+ .await?
+ .into_body(),
+ _ => unreachable!(),
+ };
+ body.into_materialized_bytes().await?;
+ assert!(head.redirected, "{mode} {status}");
+ let requests = server.await??;
+ assert_eq!(requests.len(), 2, "{mode} {status}");
+ assert!(
+ requests[0]
+ .windows(b"X-Raw: \xc3\xbf\r\n".len())
+ .any(|value| value == b"X-Raw: \xc3\xbf\r\n"),
+ "{mode} {status}"
+ );
+ assert!(
+ requests[1]
+ .windows(b"X-Raw: \xe9\xff\r\n".len())
+ .any(|value| value == b"X-Raw: \xe9\xff\r\n"),
+ "{mode} {status}"
+ );
+ if status == 302 {
+ assert!(requests[1].starts_with(b"GET /final "), "{mode}");
+ assert!(
+ !requests[1]
+ .windows(b"Content-Type:".len())
+ .any(|value| value.eq_ignore_ascii_case(b"Content-Type:")),
+ "{mode}"
+ );
+ } else {
+ assert!(requests[1].starts_with(b"POST /final "), "{mode}");
+ assert!(
+ requests[1]
+ .windows(b"Content-Type: text/plain\r\n".len())
+ .any(|value| value.eq_ignore_ascii_case(b"Content-Type: text/plain\r\n")),
+ "{mode}"
+ );
+ }
+ assert!(client.shutdown().is_clean());
+ }
+ }
+ Ok(())
+}
diff --git a/moli-protocol-webdriver-bidi/src/commands.rs b/moli-protocol-webdriver-bidi/src/commands.rs
index c0b057394..f1d0a56c8 100644
--- a/moli-protocol-webdriver-bidi/src/commands.rs
+++ b/moli-protocol-webdriver-bidi/src/commands.rs
@@ -983,65 +983,35 @@ fn bidi_network_set_extra_headers_command(
})
}
-fn required_network_extra_headers(params: &Value) -> Result<Vec<(String, String)>, BidiError> {
- let Some(value) = params.get("headers") else {
- return Err(BidiError::new(
- BidiErrorCode::InvalidArgument,
- "headers must be an array",
- ));
- };
- let Some(headers) = value.as_array() else {
- return Err(BidiError::new(
- BidiErrorCode::InvalidArgument,
- "headers must be an array",
- ));
- };
- let mut out = Vec::with_capacity(headers.len());
- for header in headers {
- let (name, value) = network_extra_header_pair(header)?;
- out.retain(|(existing, _)| existing != &name);
+fn required_network_extra_headers(
+ params: &Value,
+) -> Result<moli_header_field::HeaderFields, BidiError> {
+ let headers = optional_network_headers(params, "headers")?.ok_or_else(|| {
+ BidiError::new(BidiErrorCode::InvalidArgument, "headers must be an array")
+ })?;
+ let mut out = moli_header_field::HeaderFields::default();
+ for (name, value) in headers {
+ if value.contains(&0)
+ || value.contains(&b'\n')
+ || value.contains(&b'\r')
+ || value
+ .first()
+ .is_some_and(|byte| matches!(byte, b' ' | b'\t'))
+ || value
+ .last()
+ .is_some_and(|byte| matches!(byte, b' ' | b'\t'))
+ {
+ return Err(BidiError::new(
+ BidiErrorCode::InvalidArgument,
+ "header value is invalid",
+ ));
+ }
+ out.retain(|(existing, _)| !existing.eq_ignore_ascii_case(&name));
out.push((name, value));
}
Ok(out)
}
-fn network_extra_header_pair(value: &Value) -> Result<(String, String), BidiError> {
- let Some(header) = value.as_object() else {
- return Err(BidiError::new(
- BidiErrorCode::InvalidArgument,
- "headers entries must be objects",
- ));
- };
- let name = required_object_string(header, "name")?;
- validate_network_header_name(name, "header name")?;
- let value = required_network_extra_header_value(required_object_value(header, "value")?)?;
- Ok((name.to_owned(), value))
-}
-
-fn required_network_extra_header_value(value: &Value) -> Result<String, BidiError> {
- let Some(value) = value.as_object() else {
- return Err(BidiError::new(
- BidiErrorCode::InvalidArgument,
- "value must be a network bytes value",
- ));
- };
- let type_name = required_object_string(value, "type")?;
- if type_name != "string" {
- return Err(BidiError::new(
- BidiErrorCode::UnsupportedOperation,
- "Only string headers values are supported",
- ));
- }
- let raw = required_object_string(value, "value")?;
- if raw.contains(['\0', '\n', '\r']) || raw.trim() != raw {
- return Err(BidiError::new(
- BidiErrorCode::InvalidArgument,
- "header value is invalid",
- ));
- }
- Ok(raw.to_owned())
-}
-
fn bidi_network_continue_request_command(
command: &BidiCommand,
context: &BidiDevToolsCommandContext,
@@ -1069,10 +1039,10 @@ fn bidi_network_continue_response_command(
let cookie_headers = network_response_cookie_headers(&command.params)?;
let response_headers = match (headers, cookie_headers.is_empty()) {
(Some(mut headers), _) => {
- headers.extend(moli_header_field::HeaderFields::from_utf8(cookie_headers));
+ headers.extend(cookie_headers);
Some(headers)
}
- (None, false) => Some(moli_header_field::HeaderFields::from_utf8(cookie_headers)),
+ (None, false) => Some(cookie_headers),
(None, true) => None,
};
Ok(DevToolsContinueInterceptedResponseCommand {
@@ -1167,9 +1137,7 @@ fn bidi_network_provide_response_command(
) -> Result<DevToolsFulfillInterceptedRequestCommand, BidiError> {
let request = required_network_request_id(&command.params)?;
let mut headers = optional_network_headers(&command.params, "headers")?.unwrap_or_default();
- headers.extend(moli_header_field::HeaderFields::from_utf8(
- network_response_cookie_headers(&command.params)?,
- ));
+ headers.extend(network_response_cookie_headers(&command.params)?);
Ok(DevToolsFulfillInterceptedRequestCommand {
context: context.command_context(None),
request_id: DevToolsRequestId::from(request),
@@ -1284,11 +1252,11 @@ fn apply_network_request_cookies(
};
let headers = headers.get_or_insert_with(Default::default);
headers.retain(|(name, _)| !name.eq_ignore_ascii_case("cookie"));
- headers.push(("Cookie".to_owned(), cookie_header.into_bytes()));
+ headers.push(("Cookie".to_owned(), cookie_header));
Ok(())
}
-fn optional_network_request_cookie_header(params: &Value) -> Result<Option<String>, BidiError> {
+fn optional_network_request_cookie_header(params: &Value) -> Result<Option<Vec<u8>>, BidiError> {
let Some(cookies) = params.get("cookies") else {
return Ok(None);
};
@@ -1298,17 +1266,24 @@ fn optional_network_request_cookie_header(params: &Value) -> Result<Option<Strin
"cookies must be an array",
));
};
- let mut values = Vec::with_capacity(cookies.len());
+ let mut header = Vec::new();
for cookie in cookies {
+ if !header.is_empty() {
+ header.extend_from_slice(b"; ");
+ }
let (name, value) = network_cookie_name_value(cookie)?;
- values.push(format!("{name}={value}"));
+ header.extend_from_slice(name.as_bytes());
+ header.push(b'=');
+ header.extend(value);
}
- Ok(Some(values.join("; ")))
+ Ok(Some(header))
}
-fn network_response_cookie_headers(params: &Value) -> Result<Vec<(String, String)>, BidiError> {
+fn network_response_cookie_headers(
+ params: &Value,
+) -> Result<moli_header_field::HeaderFields, BidiError> {
let Some(cookies) = params.get("cookies") else {
- return Ok(Vec::new());
+ return Ok(Default::default());
};
let Some(cookies) = cookies.as_array() else {
return Err(BidiError::new(
@@ -1320,10 +1295,10 @@ fn network_response_cookie_headers(params: &Value) -> Result<Vec<(String, String
for cookie in cookies {
headers.push(("Set-Cookie".to_owned(), network_set_cookie_header(cookie)?));
}
- Ok(headers)
+ Ok(moli_header_field::HeaderFields::from_bytes(headers))
}
-fn network_cookie_name_value(value: &Value) -> Result<(String, String), BidiError> {
+fn network_cookie_name_value(value: &Value) -> Result<(String, Vec<u8>), BidiError> {
let Some(cookie) = value.as_object() else {
return Err(BidiError::new(
BidiErrorCode::InvalidArgument,
@@ -1332,11 +1307,11 @@ fn network_cookie_name_value(value: &Value) -> Result<(String, String), BidiErro
};
let name = required_object_string(cookie, "name")?;
validate_network_cookie_name(name)?;
- let value = required_network_bytes_value(required_object_value(cookie, "value")?, "value")?;
+ let value = required_network_bytes(required_object_value(cookie, "value")?, "value")?;
Ok((name.to_owned(), value))
}
-fn network_set_cookie_header(value: &Value) -> Result<String, BidiError> {
+fn network_set_cookie_header(value: &Value) -> Result<Vec<u8>, BidiError> {
let Some(cookie) = value.as_object() else {
return Err(BidiError::new(
BidiErrorCode::InvalidArgument,
@@ -1344,7 +1319,9 @@ fn network_set_cookie_header(value: &Value) -> Result<String, BidiError> {
));
};
let (name, value) = network_cookie_name_value(value)?;
- let mut header = format!("{name}={value}");
+ let mut header = name.into_bytes();
+ header.push(b'=');
+ header.extend(value);
for field in ["domain", "expiry", "path"] {
if let Some(value) = optional_object_string(cookie, field)? {
let attribute = match field {
@@ -1353,11 +1330,11 @@ fn network_set_cookie_header(value: &Value) -> Result<String, BidiError> {
"path" => "Path",
_ => unreachable!(),
};
- header.push_str(&format!("; {attribute}={value}"));
+ header.extend_from_slice(format!("; {attribute}={value}").as_bytes());
}
}
if let Some(max_age) = optional_object_uint(cookie, "maxAge")? {
- header.push_str(&format!("; Max-Age={max_age}"));
+ header.extend_from_slice(format!("; Max-Age={max_age}").as_bytes());
}
if let Some(same_site) = optional_object_string(cookie, "sameSite")? {
if !matches!(
@@ -1369,13 +1346,13 @@ fn network_set_cookie_header(value: &Value) -> Result<String, BidiError> {
"sameSite must be strict, lax, or none",
));
}
- header.push_str(&format!("; SameSite={same_site}"));
+ header.extend_from_slice(format!("; SameSite={same_site}").as_bytes());
}
if optional_object_bool(cookie, "httpOnly")?.unwrap_or(false) {
- header.push_str("; HttpOnly");
+ header.extend_from_slice(b"; HttpOnly");
}
if optional_object_bool(cookie, "secure")?.unwrap_or(false) {
- header.push_str("; Secure");
+ header.extend_from_slice(b"; Secure");
}
Ok(header)
}
diff --git a/moli-protocol-webdriver-bidi/src/tests.rs b/moli-protocol-webdriver-bidi/src/tests.rs
index 9e903f63e..602f52616 100644
--- a/moli-protocol-webdriver-bidi/src/tests.rs
+++ b/moli-protocol-webdriver-bidi/src/tests.rs
@@ -8856,7 +8856,7 @@ fn maps_network_set_extra_headers_to_shared_network_command() {
);
assert!(command.browser_context_ids.is_empty());
assert_eq!(
- command.headers,
+ command.headers.to_byte_strings(),
vec![
(
"some_header_name".to_owned(),
@@ -8995,7 +8995,10 @@ fn bound_network_set_extra_headers_outcome_carries_shared_command() {
};
assert!(command.target_ids.is_empty());
assert!(command.browser_context_ids.is_empty());
- assert_eq!(command.headers, vec![("x-test".to_owned(), "1".to_owned())]);
+ assert_eq!(
+ command.headers.to_byte_strings(),
+ vec![("x-test".to_owned(), "1".to_owned())]
+ );
}
#[test]
@@ -9039,20 +9042,85 @@ fn bound_network_add_data_collector_outcome_carries_shared_command() {
}
#[test]
-fn rejects_unsupported_network_set_extra_headers_base64_value() {
+fn maps_network_set_extra_headers_with_opaque_bytes_and_utf8_values() {
let command = super::parse_bidi_command(json!({
- "id": 99,
- "method": "network.setExtraHeaders",
- "params": {
- "headers": [{"name": "x-test", "value": {"type": "base64", "value": "MQ=="}}]
- }
+ "id": 99, "method": "network.setExtraHeaders",
+ "params": {"headers": [
+ {"name": "X-Raw", "value": {"type": "string", "value": "old"}},
+ {"name": "x-raw", "value": {"type": "base64", "value": "6f8="}},
+ {"name": "x-utf8", "value": {"type": "string", "value": "é"}}
+ ]}
}))
.expect("BiDi command");
let context = super::BidiDevToolsCommandContext::new("bidi-session-1");
+ let shared =
+ super::devtools_command_from_bidi_command(&command, &context).expect("byte headers");
+ let moli_protocol::devtools_runtime::DevToolsCommand::SetExtraHeaders(command) = shared else {
+ panic!("expected SetExtraHeaders");
+ };
+ assert_eq!(
+ command.headers,
+ moli_header_field::HeaderFields::from_bytes(vec![
+ ("x-raw".into(), vec![0xe9, 0xff]),
+ ("x-utf8".into(), vec![0xc3, 0xa9]),
+ ])
+ );
+}
- let error = super::devtools_command_from_bidi_command(&command, &context)
- .expect_err("base64 extra header should fail validation");
- assert_eq!(error.code, super::BidiErrorCode::UnsupportedOperation);
+#[test]
+fn bidi_cookie_overrides_preserve_opaque_and_utf8_bytes() {
+ use moli_protocol::devtools_runtime::DevToolsCommand;
+ let context = super::BidiDevToolsCommandContext::new("bidi-session-1");
+ for method in [
+ "network.continueRequest",
+ "network.continueResponse",
+ "network.provideResponse",
+ ] {
+ let mut params = json!({"request": "REQ-raw", "cookies": [
+ {"name": "raw", "value": {"type": "base64", "value": "6f8="}},
+ {"name": "utf8", "value": {"type": "string", "value": "é"}}
+ ]});
+ if method != "network.continueRequest" {
+ params["cookies"][0]["path"] = json!("/");
+ params["cookies"][0]["httpOnly"] = json!(true);
+ params["cookies"][0]["secure"] = json!(true);
+ }
+ let command =
+ super::parse_bidi_command(json!({"id": 100, "method": method, "params": params}))
+ .unwrap();
+ let shared = super::devtools_command_from_bidi_command(&command, &context)
+ .expect("non-UTF8 cookie value must be accepted");
+ let headers = match shared {
+ DevToolsCommand::ContinueInterceptedRequest(command) => command.headers.unwrap(),
+ DevToolsCommand::ContinueInterceptedResponse(command) => {
+ moli_header_field::HeaderFields::from_byte_strings(
+ &command.response_headers.unwrap(),
+ )
+ .unwrap()
+ }
+ DevToolsCommand::FulfillInterceptedRequest(command) => {
+ moli_header_field::HeaderFields::from_byte_strings(&command.response_headers)
+ .unwrap()
+ }
+ _ => panic!("unexpected cookie dispatch"),
+ };
+ let expected = if method == "network.continueRequest" {
+ vec![("Cookie".into(), b"raw=\xe9\xff; utf8=\xc3\xa9".to_vec())]
+ } else {
+ vec![
+ (
+ "Set-Cookie".into(),
+ b"raw=\xe9\xff; Path=/; HttpOnly; Secure".to_vec(),
+ ),
+ ("Set-Cookie".into(), b"utf8=\xc3\xa9".to_vec()),
+ ]
+ };
+ assert_eq!(
+ headers,
+ moli_header_field::HeaderFields::from_bytes(expected),
+ "{method}"
+ );
+ }
}
#[test]
diff --git a/moli-protocol/src/conn.rs b/moli-protocol/src/conn.rs
index ef009261a..590121cad 100644
--- a/moli-protocol/src/conn.rs
+++ b/moli-protocol/src/conn.rs
@@ -1191,7 +1191,7 @@ pub struct CdpConnection {
pub permission_overrides: Vec<PermissionOverride>,
next_global_io_stream_id: u64,
base_browser_identity: moli_browser_profile::BrowserIdentityProfile,
- global_extra_headers: Vec<(String, String)>,
+ global_extra_headers: moli_fetch::RequestHeaders,
global_browser_identity_override: Option<moli_browser_profile::BrowserIdentityProfile>,
global_network_conditions: Option<EmulatedNetworkConditions>,
global_geolocation_override: Option<EmulatedGeolocationOverrideState>,
@@ -1496,7 +1496,7 @@ impl CdpConnection {
pending_runtime_await_jobs: HashMap::new(),
claimed_pending_inspector_await_owners: HashMap::new(),
base_browser_identity,
- global_extra_headers: Vec::new(),
+ global_extra_headers: Default::default(),
global_browser_identity_override: None,
global_network_conditions: None,
global_geolocation_override: None,
diff --git a/moli-protocol/src/conn/browser_context/network_owner.rs b/moli-protocol/src/conn/browser_context/network_owner.rs
index 1470bfa02..b1f421834 100644
--- a/moli-protocol/src/conn/browser_context/network_owner.rs
+++ b/moli-protocol/src/conn/browser_context/network_owner.rs
@@ -197,12 +197,12 @@ impl TargetSessionOwnerMut<'_> {
fn start_set_extra_http_headers(
mut self,
- extra_headers: Vec<(String, String)>,
+ extra_headers: moli_fetch::RequestHeaders,
) -> Result<Option<PendingPageCommand>, String> {
self.mutate_network_policy_session_state(|state| {
state.extra_headers = extra_headers;
});
- let headers = self.effective_policy().extra_headers().to_vec();
+ let headers = self.effective_policy().extra_headers().clone();
let effective_headers = self.effective_extra_headers_for_target_policy(headers);
let Some(page) = self.runtime_slot_mut().loaded_page_mut() else {
return Ok(None);
@@ -214,13 +214,13 @@ impl TargetSessionOwnerMut<'_> {
fn start_set_target_extra_http_headers(
mut self,
- extra_headers: Vec<(String, String)>,
+ extra_headers: moli_fetch::RequestHeaders,
) -> Result<Option<PendingPageCommand>, String> {
let headers = self.mutate_page_state(|state, _session_key| {
state
.network_policy
.replace_base_extra_headers(extra_headers);
- state.effective_policy().extra_headers().to_vec()
+ state.effective_policy().extra_headers().clone()
});
let effective_headers = self.effective_extra_headers_for_target_policy(headers);
let Some(page) = self.runtime_slot_mut().loaded_page_mut() else {
@@ -236,7 +236,7 @@ impl TargetSessionOwnerMut<'_> {
) -> Result<Option<PendingPageCommand>, String> {
let policy = self.effective_policy();
let effective_headers =
- self.effective_extra_headers_for_target_policy(policy.extra_headers().to_vec());
+ self.effective_extra_headers_for_target_policy(policy.extra_headers().clone());
let Some(page) = self.runtime_slot_mut().loaded_page_mut() else {
return Ok(None);
};
@@ -570,7 +570,7 @@ impl CdpConnection {
}
}
- pub(crate) fn set_global_extra_headers(&mut self, extra_headers: Vec<(String, String)>) {
+ pub(crate) fn set_global_extra_headers(&mut self, extra_headers: moli_fetch::RequestHeaders) {
self.global_extra_headers = extra_headers.clone();
if let Some(browser_context) = self.browser_context.as_mut() {
browser_context.global_extra_headers = extra_headers.clone();
@@ -642,7 +642,7 @@ impl CdpConnection {
pub(crate) fn start_set_extra_http_headers_for_session_owner(
&mut self,
session_id: Option<&str>,
- extra_headers: Vec<(String, String)>,
+ extra_headers: moli_fetch::RequestHeaders,
) -> Result<Option<PendingPageCommand>, String> {
let Some(owner) = self.target_session_owner_mut(session_id) else {
return Err("BrowserContextNotLoaded".to_owned());
@@ -653,7 +653,7 @@ impl CdpConnection {
pub(crate) fn start_set_target_extra_http_headers_for_owner(
&mut self,
command_owner: &crate::conn::CommandOwnerScope,
- extra_headers: Vec<(String, String)>,
+ extra_headers: moli_fetch::RequestHeaders,
) -> Result<Option<PendingPageCommand>, String> {
let Some(owner) = self.target_session_owner_mut_for_owner(command_owner) else {
return Err("BrowserContextNotLoaded".to_owned());
@@ -912,7 +912,7 @@ mod tests {
network.cache_disabled = true;
network.bypass_service_worker = true;
network.blocked_url_patterns = vec!["*://blocked.test/*".to_owned()];
- network.extra_headers = vec![("X-Test".to_owned(), "active".to_owned())];
+ network.extra_headers = vec![("X-Test".to_owned(), "active".to_owned())].into();
}
let active_offline = active_session_state_mut(&mut active).set_network_offline(true);
@@ -939,7 +939,8 @@ mod tests {
active
.active_page_target()
.effective_policy()
- .extra_headers(),
+ .extra_headers()
+ .to_byte_strings(),
vec![("X-Test".to_owned(), "active".to_owned())]
);
assert!(active_offline);
@@ -955,7 +956,7 @@ mod tests {
network.cache_disabled = true;
network.bypass_service_worker = true;
network.blocked_url_patterns = vec!["*://background-blocked.test/*".to_owned()];
- network.extra_headers = vec![("X-Test".to_owned(), "background".to_owned())];
+ network.extra_headers = vec![("X-Test".to_owned(), "background".to_owned())].into();
}
let background_offline =
background_session_state_mut(&mut background).set_network_offline(true);
@@ -967,7 +968,10 @@ mod tests {
vec!["*://background-blocked.test/*"]
);
assert_eq!(
- background.effective_policy().extra_headers(),
+ background
+ .effective_policy()
+ .extra_headers()
+ .to_byte_strings(),
vec![("X-Test".to_owned(), "background".to_owned())]
);
assert!(background_offline);
diff --git a/moli-protocol/src/conn/browser_context/target_session_owner.rs b/moli-protocol/src/conn/browser_context/target_session_owner.rs
index acc6e4448..349025927 100644
--- a/moli-protocol/src/conn/browser_context/target_session_owner.rs
+++ b/moli-protocol/src/conn/browser_context/target_session_owner.rs
@@ -145,6 +145,8 @@ impl TargetNavigationStorageHandles {
#[derive(Clone)]
pub(crate) struct TargetNavigationLoadInputs {
+ pub(crate) redirect_chain: Vec<moli_fetch::RedirectInfo>,
+ pub(crate) redirect_headers: Option<moli_fetch::RequestHeaders>,
pub(crate) browser_context_id: Option<String>,
storage_handles: TargetNavigationStorageHandles,
pub(crate) root_frame_id: Option<String>,
@@ -163,7 +165,7 @@ pub(crate) struct TargetNavigationLoadInputs {
pub(crate) runtime_bindings: Vec<RuntimeBindingDefinition>,
pub(crate) runtime_inspector_session_restore_snapshots:
Vec<RendererInspectorSessionRestoreSnapshot>,
- pub(crate) extra_http_headers: Vec<(String, String)>,
+ pub(crate) extra_http_headers: moli_fetch::RequestHeaders,
pub(crate) script_execution_disabled: bool,
pub(crate) bypass_content_security_policy: bool,
pub(crate) emulated_media: moli_core::page::EmulatedMediaOverrides,
@@ -271,6 +273,8 @@ impl TargetNavigationLoadInputs {
.expect("resolved Page target retains document activity");
Self {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
browser_context_id: Some(browser_context.id.clone()),
storage_handles: TargetNavigationStorageHandles::from_page_handles(
browser_context
@@ -367,6 +371,8 @@ impl TargetNavigationLoadInputs {
renderer_runtime: RendererBrowserContextRuntimeOwnerAccess,
) -> Self {
Self {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
browser_context_id: None,
storage_handles: TargetNavigationStorageHandles::from_page_handles(page_handles),
root_frame_id: None,
@@ -382,7 +388,7 @@ impl TargetNavigationLoadInputs {
document_start_scripts: Vec::new(),
runtime_bindings: Vec::new(),
runtime_inspector_session_restore_snapshots: Vec::new(),
- extra_http_headers: Vec::new(),
+ extra_http_headers: Default::default(),
script_execution_disabled: false,
bypass_content_security_policy: false,
emulated_media: Default::default(),
@@ -413,20 +419,20 @@ impl TargetNavigationLoadInputs {
}
}
-fn apply_referrer_header(headers: &mut Vec<(String, String)>, referrer: Option<&str>) {
+fn apply_referrer_header(headers: &mut moli_fetch::RequestHeaders, referrer: Option<&str>) {
let Some(referrer) = referrer else {
return;
};
headers.retain(|(name, _)| !name.eq_ignore_ascii_case("referer"));
- headers.push(("Referer".to_owned(), referrer.to_owned()));
+ headers.push(("Referer".to_owned(), referrer.as_bytes().to_vec()));
}
-fn apply_user_agent_header(headers: &mut Vec<(String, String)>, user_agent: &str) {
+fn apply_user_agent_header(headers: &mut moli_fetch::RequestHeaders, user_agent: &str) {
if !headers
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case("user-agent"))
{
- headers.push(("User-Agent".to_owned(), user_agent.to_owned()));
+ headers.push(("User-Agent".to_owned(), user_agent.as_bytes().to_vec()));
}
}
@@ -907,8 +913,8 @@ impl<'a> TargetSessionOwnerMut<'a> {
pub(super) fn effective_extra_headers_for_target_policy(
&self,
- headers: Vec<(String, String)>,
- ) -> Vec<(String, String)> {
+ headers: moli_fetch::RequestHeaders,
+ ) -> moli_fetch::RequestHeaders {
self.browser_context
.merged_extra_headers_for_target_policy(&headers)
}
@@ -1015,7 +1021,7 @@ impl<'a> TargetSessionOwnerMut<'a> {
document_fetch_event_session_id,
inherited_security_origin,
inherited_secure_context_type,
- request_headers: request_headers.into(),
+ request_headers,
document_fetch_request_stage,
document_fetch_response_stage_candidate,
document_auth_required,
@@ -3179,7 +3185,7 @@ mod tests {
assert!(
inputs
.extra_http_headers
- .contains(&("X-Owner".to_owned(), "background".to_owned()))
+ .contains(&("X-Owner".to_owned(), b"background".to_vec()))
);
assert!(
inputs
diff --git a/moli-protocol/src/conn/dispatch_tests.rs b/moli-protocol/src/conn/dispatch_tests.rs
index 535c0b773..7682cb844 100644
--- a/moli-protocol/src/conn/dispatch_tests.rs
+++ b/moli-protocol/src/conn/dispatch_tests.rs
@@ -717,6 +717,8 @@ async fn bidi_fetch_control_resolves_background_request_owner() {
interception_session_id: Some("bidi-session-1".to_owned()),
document_navigation_token: None,
navigation: NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: None,
owner: CommandOwnerScope::for_session("SID-background"),
result_projection: NavigationResultProjection::WebDriverBidi(json!({})),
diff --git a/moli-protocol/src/conn/downloads.rs b/moli-protocol/src/conn/downloads.rs
index ccddcf6bc..6728c3696 100644
--- a/moli-protocol/src/conn/downloads.rs
+++ b/moli-protocol/src/conn/downloads.rs
@@ -641,7 +641,7 @@ impl CdpConnection {
Some(PendingDownloadOwnerContext {
browser_context_id,
frame_id,
- request_headers: request_headers.into(),
+ request_headers,
initiator_url,
})
}
@@ -1542,9 +1542,9 @@ mod tests {
let mut browser_context = BrowserContext::new("BID-download".to_owned());
browser_context.set_active_target_id("TID-active");
browser_context.default_extra_headers =
- vec![("X-Context-Default".to_owned(), "default".to_owned())];
+ vec![("X-Context-Default".to_owned(), "default".to_owned())].into();
browser_context.global_extra_headers =
- vec![("X-Context-Global".to_owned(), "global".to_owned())];
+ vec![("X-Context-Global".to_owned(), "global".to_owned())].into();
let mut background = PageTargetHost::with_url(
"TID-background".to_owned(),
diff --git a/moli-protocol/src/conn/fetch_support.rs b/moli-protocol/src/conn/fetch_support.rs
index fd969ad9d..88a2366bd 100644
--- a/moli-protocol/src/conn/fetch_support.rs
+++ b/moli-protocol/src/conn/fetch_support.rs
@@ -1810,7 +1810,7 @@ impl PendingSubresourceFetchRequest {
url: Option<Url>,
method: Option<String>,
body: Option<String>,
- headers: Option<moli_fetch::RequestHeaders>,
+ headers: Option<moli_fetch::RequestHeaderOverride>,
) {
let Some(chain) = self.request_stage_chain.as_mut() else {
return;
@@ -1825,7 +1825,8 @@ impl PendingSubresourceFetchRequest {
chain.body = Some(body);
}
if let Some(headers) = headers {
- chain.headers = headers;
+ chain.headers = headers.headers().clone();
+ chain.header_override = Some(headers);
chain.request_cookie_report = None;
}
}
@@ -1836,7 +1837,7 @@ impl PendingSubresourceFetchRequest {
Option<Url>,
Option<String>,
Option<Option<String>>,
- Option<moli_fetch::RequestHeaders>,
+ Option<moli_fetch::RequestHeaderOverride>,
) {
let Some(chain) = self.request_stage_chain.as_ref() else {
return (None, None, None, None);
@@ -1845,7 +1846,7 @@ impl PendingSubresourceFetchRequest {
Some(chain.url.clone()),
Some(chain.method.clone()),
Some(chain.body.clone()),
- Some(chain.headers.clone()),
+ chain.header_override.clone(),
)
}
@@ -1863,6 +1864,7 @@ impl PendingSubresourceFetchRequest {
#[derive(Debug, Clone, PartialEq)]
pub struct PendingSubresourceFetchRequestStageChain {
+ pub header_override: Option<moli_fetch::RequestHeaderOverride>,
pub url: Url,
pub method: String,
pub headers: moli_fetch::RequestHeaders,
@@ -2094,7 +2096,7 @@ impl CdpConnection {
url,
method,
body,
- headers,
+ headers.map(Into::into),
intercept_response,
handle_auth_requests,
)
diff --git a/moli-protocol/src/conn/page_state/fetch_state.rs b/moli-protocol/src/conn/page_state/fetch_state.rs
index ae78028c2..32d41936f 100644
--- a/moli-protocol/src/conn/page_state/fetch_state.rs
+++ b/moli-protocol/src/conn/page_state/fetch_state.rs
@@ -352,6 +352,8 @@ mod tests {
fn navigation_state(url: &Url) -> NavigationDispatchState {
NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: NavigationResultProjection::Cdp(
diff --git a/moli-protocol/src/conn/page_state/surfaces.rs b/moli-protocol/src/conn/page_state/surfaces.rs
index b5ee02ebf..7503c1f0b 100644
--- a/moli-protocol/src/conn/page_state/surfaces.rs
+++ b/moli-protocol/src/conn/page_state/surfaces.rs
@@ -334,16 +334,16 @@ impl BrowserContext {
pub(crate) fn merged_extra_headers_for_target_policy(
&self,
- target_headers: &[(String, String)],
- ) -> Vec<(String, String)> {
+ target_headers: &moli_fetch::RequestHeaders,
+ ) -> moli_fetch::RequestHeaders {
merge_extra_header_layers(&[
- self.global_extra_headers.as_slice(),
- self.default_extra_headers.as_slice(),
+ &self.global_extra_headers,
+ &self.default_extra_headers,
target_headers,
])
}
- pub fn effective_extra_headers(&self) -> Vec<(String, String)> {
+ pub fn effective_extra_headers(&self) -> moli_fetch::RequestHeaders {
let target_headers = self
.page_targets
.active()
@@ -355,7 +355,7 @@ impl BrowserContext {
pub(crate) fn effective_extra_headers_for_target(
&self,
target_id: &str,
- ) -> Vec<(String, String)> {
+ ) -> moli_fetch::RequestHeaders {
let target_headers = self
.page_target(target_id)
.map(PageTargetHost::effective_policy)
@@ -618,11 +618,11 @@ impl CdpConnection {
}
}
-fn merge_extra_header_layers(layers: &[&[(String, String)]]) -> Vec<(String, String)> {
- let mut headers = Vec::new();
+fn merge_extra_header_layers(layers: &[&moli_fetch::RequestHeaders]) -> moli_fetch::RequestHeaders {
+ let mut headers = moli_fetch::RequestHeaders::default();
for layer in layers {
for (name, value) in *layer {
- headers.retain(|(existing, _)| existing != name);
+ headers.retain(|(existing, _)| !existing.eq_ignore_ascii_case(name));
headers.push((name.clone(), value.clone()));
}
}
diff --git a/moli-protocol/src/conn/runtime_load.rs b/moli-protocol/src/conn/runtime_load.rs
index 64aef661b..83f36a44e 100644
--- a/moli-protocol/src/conn/runtime_load.rs
+++ b/moli-protocol/src/conn/runtime_load.rs
@@ -1560,11 +1560,16 @@ impl CdpConnection {
&self,
navigation: &NavigationDispatchState,
) -> TargetNavigationLoadInputs {
- apply_navigation_request_load_policy(
+ let mut inputs = apply_navigation_request_load_policy(
self.navigation_load_inputs_for_owner(&navigation.owner),
navigation.request_load_policy,
)
- .with_main_document_commit_seed(RendererMainDocumentCommitSeed::from_navigation(navigation))
+ .with_main_document_commit_seed(RendererMainDocumentCommitSeed::from_navigation(
+ navigation,
+ ));
+ inputs.redirect_headers = navigation.redirect_headers.clone();
+ inputs.redirect_chain = navigation.redirect_chain.clone();
+ inputs
}
pub(crate) fn prepared_document_commit_configuration_for_owner(
@@ -2215,8 +2220,7 @@ impl CdpConnection {
raw_url: &str,
load_inputs: TargetNavigationLoadInputs,
) -> anyhow::Result<LoadedNavigation> {
- let request_headers =
- moli_fetch::RequestHeaders::from(load_inputs.extra_http_headers.clone());
+ let request_headers = load_inputs.extra_http_headers.clone();
let navigation = self
.load_navigation_request_via_runtime_with_network_events_and_load_inputs_async(
owner,
@@ -3059,20 +3063,16 @@ impl CdpConnection {
.with_context(|| format!("failed to fetch page `{raw_url}`"))
}
- pub(crate) async fn fetch_navigation_auth_raw_response_for_owner_async(
+ pub(crate) async fn fetch_navigation_auth_raw_response_for_navigation_async(
&mut self,
- owner: &CommandOwnerScope,
- request_load_policy: NavigationRequestLoadPolicy,
- method: &str,
- raw_url: &str,
- body: Option<Vec<u8>>,
- request_headers: moli_fetch::RequestHeaders,
+ navigation: &NavigationDispatchState,
auth: SubresourceAuthCredentials,
) -> anyhow::Result<NetworkFetchResult<RawResponse>> {
- let load_inputs = apply_navigation_request_load_policy(
- self.navigation_load_inputs_for_owner(owner),
- request_load_policy,
- );
+ let load_inputs = self.navigation_load_inputs_for_navigation(navigation);
+ let method = &navigation.request_method;
+ let raw_url = navigation.requested_url.as_str();
+ let body = navigation.clone_request_body_bytes();
+ let request_headers = navigation.request_headers.clone();
validate_navigation_network_request(&load_inputs, method, raw_url, &request_headers)?;
let mut request = Request::new_browser_bytes(
@@ -3086,6 +3086,8 @@ impl CdpConnection {
.map_or(moli_url::WebOrigin::Opaque, moli_url::WebOrigin::from_url),
)
.with_context(|| format!("failed to build request for `{raw_url}`"))?
+ .with_redirect_headers(load_inputs.redirect_headers.clone())
+ .with_redirect_chain(load_inputs.redirect_chain.clone())
.with_top_level_navigation_cookie_context()
.with_page_network_policy()
.with_browser_navigation_kind(load_inputs.browser_navigation_kind);
@@ -3128,26 +3130,18 @@ impl CdpConnection {
.await
}
- pub(crate) async fn fetch_navigation_streaming_raw_response_for_owner_async(
+ pub(crate) async fn fetch_navigation_streaming_raw_response_for_navigation_async(
&mut self,
- owner: &CommandOwnerScope,
- request_load_policy: NavigationRequestLoadPolicy,
- method: &str,
- raw_url: &str,
- body: Option<Vec<u8>>,
- request_headers: moli_fetch::RequestHeaders,
+ navigation: &NavigationDispatchState,
auth: Option<SubresourceAuthCredentials>,
) -> anyhow::Result<NetworkFetchResult<StreamingRawResponse>> {
- let load_inputs = apply_navigation_request_load_policy(
- self.navigation_load_inputs_for_owner(owner),
- request_load_policy,
- );
+ let load_inputs = self.navigation_load_inputs_for_navigation(navigation);
self.fetch_navigation_streaming_raw_response_with_load_inputs_async(
&load_inputs,
- method,
- raw_url,
- body,
- request_headers,
+ &navigation.request_method,
+ navigation.requested_url.as_str(),
+ navigation.clone_request_body_bytes(),
+ navigation.request_headers.clone(),
auth,
)
.await
@@ -3175,6 +3169,8 @@ impl CdpConnection {
.map_or(moli_url::WebOrigin::Opaque, moli_url::WebOrigin::from_url),
)
.with_context(|| format!("failed to build request for `{raw_url}`"))?
+ .with_redirect_headers(load_inputs.redirect_headers.clone())
+ .with_redirect_chain(load_inputs.redirect_chain.clone())
.with_top_level_navigation_cookie_context()
.with_page_network_policy()
.with_browser_navigation_kind(load_inputs.browser_navigation_kind);
diff --git a/moli-protocol/src/conn/state/browser_context.rs b/moli-protocol/src/conn/state/browser_context.rs
index eabd0d09b..f4e17235c 100644
--- a/moli-protocol/src/conn/state/browser_context.rs
+++ b/moli-protocol/src/conn/state/browser_context.rs
@@ -61,8 +61,8 @@ pub struct BrowserContext {
pub(crate) dedicated_worker_targets: BTreeMap<u64, DedicatedWorkerTargetState>,
pub(crate) service_worker_targets: BTreeMap<u64, ServiceWorkerTargetState>,
pub(crate) service_worker_domain_sessions: BTreeSet<Option<String>>,
- pub(crate) default_extra_headers: Vec<(String, String)>,
- pub(crate) global_extra_headers: Vec<(String, String)>,
+ pub(crate) default_extra_headers: moli_fetch::RequestHeaders,
+ pub(crate) global_extra_headers: moli_fetch::RequestHeaders,
default_browser_identity: super::BaseBrowserIdentityOverrideState,
environment_owner: moli_core::ProcessEnvironmentOwner,
pub(crate) default_network_conditions: Option<EmulatedNetworkConditions>,
@@ -512,8 +512,8 @@ impl BrowserContext {
dedicated_worker_targets: BTreeMap::new(),
service_worker_targets: BTreeMap::new(),
service_worker_domain_sessions: BTreeSet::new(),
- default_extra_headers: Vec::new(),
- global_extra_headers: Vec::new(),
+ default_extra_headers: Default::default(),
+ global_extra_headers: Default::default(),
default_browser_identity: super::BaseBrowserIdentityOverrideState::default(),
environment_owner: Default::default(),
default_network_conditions: None,
diff --git a/moli-protocol/src/conn/state/devtools_session.rs b/moli-protocol/src/conn/state/devtools_session.rs
index 9822d7ea8..5cf0535df 100644
--- a/moli-protocol/src/conn/state/devtools_session.rs
+++ b/moli-protocol/src/conn/state/devtools_session.rs
@@ -670,7 +670,7 @@ pub(crate) struct DevToolsNetworkSessionState {
pub(crate) cache_disabled: bool,
pub(crate) bypass_service_worker: bool,
pub(crate) blocked_url_patterns: Vec<String>,
- pub(crate) extra_headers: Vec<(String, String)>,
+ pub(crate) extra_headers: moli_fetch::RequestHeaders,
pub(crate) service_worker_fetch_diagnostic_entries: usize,
}
@@ -739,7 +739,7 @@ pub(crate) struct DevToolsNetworkPolicyAggregate {
pub(crate) cache_disabled: bool,
pub(crate) bypass_service_worker: bool,
pub(crate) blocked_url_patterns: Vec<String>,
- pub(crate) extra_headers: Vec<(String, String)>,
+ pub(crate) extra_headers: moli_fetch::RequestHeaders,
}
#[derive(Clone, Debug, Default, PartialEq)]
@@ -1223,7 +1223,8 @@ mod tests {
primary.extra_headers = vec![
("X-Primary".to_owned(), "primary".to_owned()),
("X-Shared".to_owned(), "primary".to_owned()),
- ];
+ ]
+ .into();
let attached_b = &mut sessions.ensure_attached("SID-b").network_session_state;
attached_b.network_enabled = true;
@@ -1231,18 +1232,19 @@ mod tests {
attached_b.extra_headers = vec![
("X-B".to_owned(), "b".to_owned()),
("x-shared".to_owned(), "b".to_owned()),
- ];
+ ]
+ .into();
let attached_a = &mut sessions.ensure_attached("SID-a").network_session_state;
attached_a.network_enabled = false;
attached_a.cache_disabled = true;
- attached_a.extra_headers = vec![("X-Ignored".to_owned(), "a".to_owned())];
+ attached_a.extra_headers = vec![("X-Ignored".to_owned(), "a".to_owned())].into();
let policy = sessions.effective_network_policy();
assert!(policy.cache_disabled);
assert!(policy.bypass_service_worker);
assert_eq!(
- policy.extra_headers,
+ policy.extra_headers.to_byte_strings(),
vec![
("X-Primary".to_owned(), "primary".to_owned()),
("x-shared".to_owned(), "b".to_owned()),
@@ -1255,7 +1257,7 @@ mod tests {
assert!(policy.cache_disabled);
assert!(!policy.bypass_service_worker);
assert_eq!(
- policy.extra_headers,
+ policy.extra_headers.to_byte_strings(),
vec![
("X-Primary".to_owned(), "primary".to_owned()),
("X-Shared".to_owned(), "primary".to_owned()),
diff --git a/moli-protocol/src/conn/state/fetch.rs b/moli-protocol/src/conn/state/fetch.rs
index 4bf968a43..9c086f7e9 100644
--- a/moli-protocol/src/conn/state/fetch.rs
+++ b/moli-protocol/src/conn/state/fetch.rs
@@ -2374,6 +2374,8 @@ mod tests {
response_stage_request_id: request_id.to_owned(),
document_navigation_token: None,
navigation: NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: owner_session_id
.map(crate::conn::CommandOwnerScope::for_session)
@@ -2423,6 +2425,7 @@ mod tests {
let mut pending_b = pending_subresource_fetch(2, Some("SID-B"));
pending_b.request_stage_chain = Some(Box::new(PendingSubresourceFetchRequestStageChain {
+ header_override: None,
url: test_url("api"),
method: "GET".to_owned(),
headers: Vec::new().into(),
@@ -2593,6 +2596,7 @@ mod tests {
);
network_owned_pending.request_stage_chain =
Some(Box::new(PendingSubresourceFetchRequestStageChain {
+ header_override: None,
url: test_url("api"),
method: "GET".to_owned(),
headers: Vec::new().into(),
diff --git a/moli-protocol/src/conn/state/navigation_outcome.rs b/moli-protocol/src/conn/state/navigation_outcome.rs
index 6ebf00708..4bf694ccd 100644
--- a/moli-protocol/src/conn/state/navigation_outcome.rs
+++ b/moli-protocol/src/conn/state/navigation_outcome.rs
@@ -294,6 +294,8 @@ impl NavigationResultProjection {
#[derive(Debug, Clone)]
pub struct NavigationDispatchState {
+ pub(crate) redirect_chain: Vec<moli_fetch::RedirectInfo>,
+ pub(crate) redirect_headers: Option<moli_fetch::RequestHeaders>,
pub navigate_id: Option<u64>,
pub(crate) owner: CommandOwnerScope,
pub(crate) result_projection: NavigationResultProjection,
diff --git a/moli-protocol/src/conn/state/session.rs b/moli-protocol/src/conn/state/session.rs
index 9329fac6c..a85871ba3 100644
--- a/moli-protocol/src/conn/state/session.rs
+++ b/moli-protocol/src/conn/state/session.rs
@@ -204,7 +204,10 @@ impl PageTargetHost {
}
}
-fn overlay_extra_headers(effective: &mut Vec<(String, String)>, layer: &[(String, String)]) {
+fn overlay_extra_headers(
+ effective: &mut moli_fetch::RequestHeaders,
+ layer: &moli_fetch::RequestHeaders,
+) {
for (name, value) in layer {
effective.retain(|(existing, _)| !existing.eq_ignore_ascii_case(name));
effective.push((name.clone(), value.clone()));
@@ -219,7 +222,7 @@ pub(crate) struct EffectiveTargetPolicy {
cache_disabled: bool,
bypass_service_worker: bool,
blocked_url_patterns: Vec<String>,
- extra_headers: Vec<(String, String)>,
+ extra_headers: moli_fetch::RequestHeaders,
browser_identity_override: Option<moli_browser_profile::BrowserIdentityProfile>,
renderer_browser_identity_override: Option<moli_browser_profile::BrowserIdentityProfile>,
}
@@ -249,7 +252,7 @@ impl EffectiveTargetPolicy {
&self.blocked_url_patterns
}
- pub(crate) fn extra_headers(&self) -> &[(String, String)] {
+ pub(crate) fn extra_headers(&self) -> &moli_fetch::RequestHeaders {
&self.extra_headers
}
@@ -545,7 +548,7 @@ pub(crate) struct TargetNetworkPolicyState {
network_offline: bool,
base_browser_identity: super::BaseBrowserIdentityOverrideState,
// Target-scoped headers contributed by WebDriver BiDi.
- base_extra_headers: Vec<(String, String)>,
+ base_extra_headers: moli_fetch::RequestHeaders,
}
impl TargetNetworkPolicyState {
@@ -571,14 +574,14 @@ impl TargetNetworkPolicyState {
self.network_offline = network_offline;
}
- pub(crate) fn replace_base_extra_headers(&mut self, extra_headers: Vec<(String, String)>) {
+ pub(crate) fn replace_base_extra_headers(&mut self, extra_headers: moli_fetch::RequestHeaders) {
self.base_extra_headers = extra_headers;
}
#[cfg(test)]
pub(crate) fn push_extra_header(&mut self, header: (String, String)) {
let mut headers = self.base_extra_headers.clone();
- headers.push(header);
+ headers.push((header.0, header.1.into_bytes()));
self.replace_base_extra_headers(headers);
}
diff --git a/moli-protocol/src/conn/state/tests.rs b/moli-protocol/src/conn/state/tests.rs
index 71d8e9eff..a8d760499 100644
--- a/moli-protocol/src/conn/state/tests.rs
+++ b/moli-protocol/src/conn/state/tests.rs
@@ -25,6 +25,8 @@ use url::Url;
fn test_navigation_dispatch_state(fetch_request_id: &str) -> NavigationDispatchState {
NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: NavigationResultProjection::Cdp(
diff --git a/moli-protocol/src/conn/tests/mod.rs b/moli-protocol/src/conn/tests/mod.rs
index 609eb60f9..e78018eab 100644
--- a/moli-protocol/src/conn/tests/mod.rs
+++ b/moli-protocol/src/conn/tests/mod.rs
@@ -1621,6 +1621,8 @@ fn materialized_navigation_test_state(
requested_url: &str,
) -> NavigationDispatchState {
NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id,
owner: crate::conn::CommandOwnerScope::for_route(crate::conn::CdpSessionRoute::Browser),
result_projection: NavigationResultProjection::Cdp(
diff --git a/moli-protocol/src/conn/tests/navigation_error.rs b/moli-protocol/src/conn/tests/navigation_error.rs
index 977a39f92..574565782 100644
--- a/moli-protocol/src/conn/tests/navigation_error.rs
+++ b/moli-protocol/src/conn/tests/navigation_error.rs
@@ -114,6 +114,8 @@ fn navigation_fixture() -> (TestContext, NavigationDispatchState) {
ctx.conn
.install_browser_context_fixture_for_test(browser_context);
let navigation = NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: CommandOwnerScope::for_session("SID-1"),
result_projection: NavigationResultProjection::Cdp(json!({
@@ -174,13 +176,8 @@ async fn offline_navigation_loaders_preserve_typed_error_causes_through_context(
.await
.expect_err("offline streaming response fetch must fail"),
ctx.conn
- .fetch_navigation_auth_raw_response_for_owner_async(
- &navigation.owner,
- navigation.request_load_policy,
- method,
- url,
- None,
- headers.clone(),
+ .fetch_navigation_auth_raw_response_for_navigation_async(
+ &navigation,
SubresourceAuthCredentials {
target: SubresourceAuthTarget::Server,
scheme: SubresourceAuthScheme::Basic,
diff --git a/moli-protocol/src/conn/tests/resource_runtime.rs b/moli-protocol/src/conn/tests/resource_runtime.rs
index 167ecc07f..246497c3d 100644
--- a/moli-protocol/src/conn/tests/resource_runtime.rs
+++ b/moli-protocol/src/conn/tests/resource_runtime.rs
@@ -103,6 +103,8 @@ async fn buffered_navigation_for_inactive_session_retains_its_target_engine() {
let requested_url = Url::parse("https://target.example/fulfilled").unwrap();
let navigation = NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner,
result_projection: NavigationResultProjection::Cdp(json!({
@@ -3344,7 +3346,8 @@ async fn direct_network_policy_routes_to_inactive_active_owner_without_activatin
inactive
.active_page_target()
.effective_policy()
- .extra_headers(),
+ .extra_headers()
+ .to_byte_strings(),
vec![("X-Test".to_owned(), "direct".to_owned())]
);
assert_eq!(
@@ -3471,7 +3474,7 @@ async fn direct_network_policy_routes_to_inactive_background_owner_without_activ
vec!["*://blocked-background.test/*".to_owned()]
);
assert_eq!(
- staged.effective_policy().extra_headers(),
+ staged.effective_policy().extra_headers().to_byte_strings(),
vec![("X-Background".to_owned(), "direct".to_owned())]
);
assert_eq!(
diff --git a/moli-protocol/src/devtools_runtime.rs b/moli-protocol/src/devtools_runtime.rs
index f2e4e691f..7af70abe6 100644
--- a/moli-protocol/src/devtools_runtime.rs
+++ b/moli-protocol/src/devtools_runtime.rs
@@ -1263,7 +1263,7 @@ pub struct DevToolsSetExtraHeadersCommand {
pub context: DevToolsCommandContext,
pub target_ids: Vec<DevToolsTargetId>,
pub browser_context_ids: Vec<DevToolsBrowserContextId>,
- pub headers: Vec<(String, String)>,
+ pub headers: moli_fetch::RequestHeaders,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
diff --git a/moli-protocol/src/domains/activity/main_document.rs b/moli-protocol/src/domains/activity/main_document.rs
index df72b3b30..b37be62f7 100644
--- a/moli-protocol/src/domains/activity/main_document.rs
+++ b/moli-protocol/src/domains/activity/main_document.rs
@@ -933,6 +933,8 @@ mod tests {
fn navigation_state() -> NavigationDispatchState {
NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(77),
owner: CommandOwnerScope::for_session("SID-nav"),
result_projection: NavigationResultProjection::Cdp(
diff --git a/moli-protocol/src/domains/emulation.rs b/moli-protocol/src/domains/emulation.rs
index bd35fd188..8f80897bf 100644
--- a/moli-protocol/src/domains/emulation.rs
+++ b/moli-protocol/src/domains/emulation.rs
@@ -1465,7 +1465,7 @@ fn start_network_conditions_update_for_current_route(
fn start_extra_headers_for_current_route(
conn: &mut CdpConnection,
route: &CdpSessionRoute,
- headers: Vec<(String, String)>,
+ headers: moli_fetch::RequestHeaders,
) -> Result<Vec<PendingEmulationPageCommand>, DevToolsError> {
let target = pending_emulation_target_for_route(conn, route)?;
let owner = CommandOwnerScope::for_route(route.clone());
diff --git a/moli-protocol/src/domains/emulation/tests.rs b/moli-protocol/src/domains/emulation/tests.rs
index 9ac36bcc0..d8a83d17c 100644
--- a/moli-protocol/src/domains/emulation/tests.rs
+++ b/moli-protocol/src/domains/emulation/tests.rs
@@ -188,7 +188,8 @@ async fn execute_set_extra_headers_for_test(
headers: headers
.into_iter()
.map(|(name, value)| (name.to_owned(), value.to_owned()))
- .collect(),
+ .collect::<Vec<_>>()
+ .into(),
},
))
.await;
@@ -216,7 +217,7 @@ async fn bidi_set_extra_headers_merges_global_user_context_and_context_layers()
.await;
let future_context = ctx.conn.new_browser_context("BID-future".to_owned());
assert_eq!(
- future_context.effective_extra_headers(),
+ future_context.effective_extra_headers().to_byte_strings(),
vec![
("some_header_name".to_owned(), "global".to_owned()),
("global_header".to_owned(), "1".to_owned())
@@ -243,7 +244,8 @@ async fn bidi_set_extra_headers_merges_global_user_context_and_context_layers()
.browser_context
.as_ref()
.expect("active browser context")
- .effective_extra_headers();
+ .effective_extra_headers()
+ .to_byte_strings();
assert_eq!(
headers,
vec![
@@ -260,7 +262,8 @@ async fn bidi_set_extra_headers_merges_global_user_context_and_context_layers()
.browser_context
.as_ref()
.expect("active browser context")
- .effective_extra_headers();
+ .effective_extra_headers()
+ .to_byte_strings();
assert_eq!(
headers,
vec![
diff --git a/moli-protocol/src/domains/fetch/commands.rs b/moli-protocol/src/domains/fetch/commands.rs
index d33434459..05ec3da59 100644
--- a/moli-protocol/src/domains/fetch/commands.rs
+++ b/moli-protocol/src/domains/fetch/commands.rs
@@ -169,11 +169,18 @@ fn build_cdp_continue_intercepted_request_command(
None => None,
};
let headers = params.headers.map(|headers| {
- headers
- .into_iter()
- .map(|header| (header.name, header.value))
- .collect::<Vec<_>>()
- .into()
+ let mut fields = moli_fetch::RequestHeaders::default();
+ for header in headers {
+ if let Some((_, value)) = fields
+ .iter_mut()
+ .find(|(name, _)| name.eq_ignore_ascii_case(&header.name))
+ {
+ *value = header.value.into_bytes();
+ } else {
+ fields.push((header.name, header.value.into_bytes()));
+ }
+ }
+ fields
});
let (browser_context_id, target_id) =
devtools_fetch_owner_identity_for_session(conn, cmd.session_id);
@@ -226,7 +233,21 @@ fn start_devtools_continue_intercepted_request_command(
&request_id,
) {
let mut pending = pending;
- let command_headers = command.headers.clone();
+ let command_headers =
+ command
+ .headers
+ .clone()
+ .map(|headers| match command.context.protocol {
+ DevToolsProtocol::Cdp => moli_fetch::RequestHeaderOverride::current_request(
+ headers,
+ pending
+ .request_stage_pause_state()
+ .and_then(|chain| chain.header_override.as_ref()),
+ ),
+ DevToolsProtocol::WebDriverClassic | DevToolsProtocol::WebDriverBidi => {
+ moli_fetch::RequestHeaderOverride::RedirectChain(headers)
+ }
+ });
if pending
.request_stage_pause_state()
.is_some_and(|chain| !chain.remaining_sessions.is_empty())
@@ -356,6 +377,14 @@ fn start_devtools_continue_intercepted_request_command(
pending.navigation.set_request_body_text(body);
}
if let Some(headers) = command.headers.clone() {
+ if command.context.protocol == DevToolsProtocol::Cdp {
+ pending
+ .navigation
+ .redirect_headers
+ .get_or_insert_with(|| pending.navigation.request_headers.clone());
+ } else {
+ pending.navigation.redirect_headers = None;
+ }
pending.navigation.request_headers = headers;
}
pending.request_cookie_report = page::navigation_cookie_access_report(
@@ -2065,6 +2094,7 @@ mod protocol_neutral_tests {
resource_type: SubresourceResourceType::Fetch,
websocket_socket_id: None,
request_stage_chain: Some(Box::new(PendingSubresourceFetchRequestStageChain {
+ header_override: None,
url: Url::parse("https://example.test/api").unwrap(),
method: "GET".to_owned(),
headers: vec![("x-old".to_owned(), "1".to_owned())].into(),
@@ -2175,7 +2205,7 @@ mod protocol_neutral_tests {
assert_eq!(method.as_deref(), Some("POST"));
assert_eq!(body, Some(Some("body".to_owned())));
assert_eq!(
- headers.map(|headers| headers.to_byte_strings()),
+ headers.map(|headers| headers.headers().to_byte_strings()),
Some(vec![("x-new".to_owned(), "2".to_owned())])
);
}
@@ -2348,6 +2378,7 @@ mod protocol_neutral_tests {
resource_type: SubresourceResourceType::Fetch,
websocket_socket_id: None,
request_stage_chain: Some(Box::new(PendingSubresourceFetchRequestStageChain {
+ header_override: None,
url: Url::parse("https://example.test/api").unwrap(),
method: "GET".to_owned(),
headers: Vec::new().into(),
diff --git a/moli-protocol/src/domains/fetch/navigation.rs b/moli-protocol/src/domains/fetch/navigation.rs
index 6a7667405..9c79f017d 100644
--- a/moli-protocol/src/domains/fetch/navigation.rs
+++ b/moli-protocol/src/domains/fetch/navigation.rs
@@ -70,20 +70,11 @@ pub(crate) async fn load_or_pause_navigation_for_auth_into_buffer_async(
) && pending.navigation.requested_url.scheme() != "data";
if should_handle_auth {
- let method = pending.navigation.request_method.clone();
- let raw_url = pending.navigation.requested_url.to_string();
- let body = pending.navigation.clone_request_body_bytes();
- let request_headers = pending.navigation.request_headers.clone();
let has_auth_credentials = auth.is_some();
if pending.intercept_response && navigation_response_stage_auth_can_stream(auth.as_ref()) {
match conn
- .fetch_navigation_streaming_raw_response_for_owner_async(
- &navigate_owner,
- pending.navigation.request_load_policy,
- &method,
- &raw_url,
- body,
- request_headers,
+ .fetch_navigation_streaming_raw_response_for_navigation_async(
+ &pending.navigation,
auth,
)
.await
@@ -134,25 +125,12 @@ pub(crate) async fn load_or_pause_navigation_for_auth_into_buffer_async(
// before libcurl has completed the credential retry, so keep
// credential replay on the buffered path until the fetch runtime
// can mark intermediate auth responses separately.
- conn.fetch_navigation_auth_raw_response_for_owner_async(
- &navigate_owner,
- pending.navigation.request_load_policy,
- &method,
- &raw_url,
- body,
- request_headers,
- auth,
- )
- .await
+ conn.fetch_navigation_auth_raw_response_for_navigation_async(&pending.navigation, auth)
+ .await
} else {
match conn
- .fetch_navigation_streaming_raw_response_for_owner_async(
- &navigate_owner,
- pending.navigation.request_load_policy,
- &method,
- &raw_url,
- body,
- request_headers,
+ .fetch_navigation_streaming_raw_response_for_navigation_async(
+ &pending.navigation,
auth,
)
.await
@@ -217,15 +195,7 @@ pub(crate) async fn load_or_pause_navigation_for_auth_into_buffer_async(
if pending.intercept_response && pending.navigation.requested_url.scheme() != "data" {
match conn
- .fetch_navigation_streaming_raw_response_for_owner_async(
- &navigate_owner,
- pending.navigation.request_load_policy,
- &pending.navigation.request_method,
- pending.navigation.requested_url.as_str(),
- pending.navigation.clone_request_body_bytes(),
- pending.navigation.request_headers.clone(),
- None,
- )
+ .fetch_navigation_streaming_raw_response_for_navigation_async(&pending.navigation, None)
.await
{
Ok(response) => {
@@ -845,6 +815,26 @@ fn register_navigation_auth_required_event(
request_cookie_report: Option<StoredCookieQueryReport>,
response: NetworkFetchResult<RawResponse>,
) -> crate::conn::BackgroundProtocolEvent {
+ let mut navigation = pending.navigation.clone();
+ let head = response.response();
+ if !head.redirect_chain.is_empty() {
+ let mut request = moli_fetch::Request::get_with_url(navigation.requested_url.clone())
+ .with_redirect_headers(navigation.redirect_headers.take());
+ request.method = navigation.request_method.clone();
+ request.body = navigation.clone_request_body_bytes();
+ request.request_headers = navigation.request_headers.clone();
+ for redirect in &head.redirect_chain {
+ request.apply_redirect_status(redirect.status);
+ }
+ navigation.requested_url = head.final_url.clone();
+ navigation.request_method = request.method;
+ navigation.request_headers = request.request_headers;
+ navigation.request_body_bytes = request.body;
+ if navigation.request_body_bytes.is_none() {
+ navigation.request_body = None;
+ }
+ }
+ navigation.redirect_chain = head.redirect_chain.clone();
let blocked_intercepts = navigation_auth_required_blocked_intercepts(conn, pending);
let mut pending_auth = crate::conn::PendingFetchAuthNavigation {
owner_session_id: pending.navigation.owner.session_id().map(str::to_owned),
@@ -854,7 +844,7 @@ fn register_navigation_auth_required_event(
fetch_request_id: pending.fetch_request_id.clone(),
response_stage_request_id: pending.fetch_request_id.clone(),
document_navigation_token: pending.document_navigation_token.clone(),
- navigation: pending.navigation.clone(),
+ navigation,
request_cookie_report,
auth_response: std::sync::Arc::new(response),
challenge,
diff --git a/moli-protocol/src/domains/fetch/subresource.rs b/moli-protocol/src/domains/fetch/subresource.rs
index 0aa29c598..319e76fb1 100644
--- a/moli-protocol/src/domains/fetch/subresource.rs
+++ b/moli-protocol/src/domains/fetch/subresource.rs
@@ -314,6 +314,7 @@ async fn prepare_subresource_fetch_pause_sources_async(
if !remaining_sessions.is_empty() {
pending.request_stage_chain =
Some(Box::new(PendingSubresourceFetchRequestStageChain {
+ header_override: None,
url: info.url.clone(),
method: info.method.clone(),
headers: info.request_headers.clone(),
diff --git a/moli-protocol/src/domains/fetch/tests/basics.rs b/moli-protocol/src/domains/fetch/tests/basics.rs
index 4f6ce893e..a49f8bf8b 100644
--- a/moli-protocol/src/domains/fetch/tests/basics.rs
+++ b/moli-protocol/src/domains/fetch/tests/basics.rs
@@ -1062,6 +1062,8 @@ async fn disable_clears_fetch_state() {
interception_session_id: Some("SID-1".to_owned()),
document_navigation_token: None,
navigation: crate::conn::NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_route(
crate::conn::CdpSessionRoute::Browser,
@@ -1102,6 +1104,8 @@ async fn disable_clears_fetch_state() {
response_stage_request_id: "INT-1".to_owned(),
document_navigation_token: None,
navigation: crate::conn::NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_route(
crate::conn::CdpSessionRoute::Browser,
@@ -1328,6 +1332,8 @@ async fn continue_with_auth_rejects_invalid_response_without_consuming_pending_a
response_stage_request_id: "INT-8".to_owned(),
document_navigation_token: None,
navigation: crate::conn::NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: crate::conn::NavigationResultProjection::Cdp(
@@ -1403,6 +1409,8 @@ async fn continue_with_auth_unsupported_challenge_preserves_pending_auth_navigat
response_stage_request_id: "INT-9".to_owned(),
document_navigation_token: None,
navigation: crate::conn::NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: crate::conn::NavigationResultProjection::Cdp(
@@ -1575,6 +1583,8 @@ fn emit_auth_required_preserves_request_headers_and_post_data_shape() {
interception_session_id: Some("SID-1".to_owned()),
document_navigation_token: None,
navigation: crate::conn::NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: crate::conn::NavigationResultProjection::Cdp(
diff --git a/moli-protocol/src/domains/fetch/tests/header_bytes.rs b/moli-protocol/src/domains/fetch/tests/header_bytes.rs
index 416c408b6..181d305ef 100644
--- a/moli-protocol/src/domains/fetch/tests/header_bytes.rs
+++ b/moli-protocol/src/domains/fetch/tests/header_bytes.rs
@@ -38,7 +38,7 @@ async fn capture_headers(
}
}
-async fn check_intercepted_header_bytes(script: &str, override_headers: bool) {
+async fn check_intercepted_header_bytes(script: &str, override_headers: bool, request_path: &str) {
let captured = CapturedHeaders::default();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
@@ -61,15 +61,18 @@ async fn check_intercepted_header_bytes(script: &str, override_headers: bool) {
let mut ctx = TestContext::new();
with_loaded_http_document(&mut ctx, &format!("http://{addr}/page"), "SID-1", "TID-1").await;
enable_runtime_async(&mut ctx, "SID-1", 70100).await;
+ ctx.process_async(json!({"id": 70107, "sessionId": "SID-1", "method": "Network.enable"}))
+ .await;
+ ctx.expect_result(70107, json!({}), Some("SID-1"));
ctx.process_async(json!({
"id": 70101, "sessionId": "SID-1", "method": "Fetch.enable",
- "params": {"handleAuthRequests": true, "patterns": [{"urlPattern": "*/start"}]}
+ "params": {"handleAuthRequests": true, "patterns": [{"urlPattern": format!("*{request_path}")}]}
}))
.await;
ctx.expect_result(70101, json!({}), Some("SID-1"));
ctx.process_async(json!({
"id": 70102, "sessionId": "SID-1", "method": "Runtime.evaluate",
- "params": {"expression": script}
+ "params": {"expression": script.replace("/start", request_path)}
}))
.await;
let _ = take_response_by_id(&mut ctx, 70102);
@@ -81,6 +84,7 @@ async fn check_intercepted_header_bytes(script: &str, override_headers: bool) {
message["method"] == "Fetch.requestPaused"
});
let request_id = paused["params"]["requestId"].as_str().unwrap().to_owned();
+ let network_id = paused["params"]["networkId"].clone();
let mut params = json!({"requestId": request_id});
if override_headers {
params["headers"] = json!([
@@ -117,13 +121,51 @@ async fn check_intercepted_header_bytes(script: &str, override_headers: bool) {
"request completion",
)
.await;
- let expected = if override_headers {
- vec![vec![0xc3, 0xa9], vec![0xc3, 0xbf]]
+ if script == XHR {
+ evaluate_until_value_async(
+ &mut ctx,
+ "SID-1",
+ 70106,
+ "globalThis.__headerBytesFinalPath",
+ &json!("/protected"),
+ "authenticated response URL",
+ )
+ .await;
} else {
- vec![vec![0xe9, 0xff]]
+ evaluate_until_value_async(
+ &mut ctx,
+ "SID-1",
+ 70106,
+ "globalThis.__headerBytesRedirected",
+ &json!(request_path == "/start"),
+ "redirect history survives authentication",
+ )
+ .await;
+ }
+ let request_urls = ctx
+ .sent
+ .iter()
+ .filter(|message| {
+ message["method"] == "Network.requestWillBeSent"
+ && message["params"]["requestId"] == network_id
+ })
+ .map(|message| message["params"]["request"]["url"].clone())
+ .collect::<Vec<_>>();
+ let expected_urls = if request_path == "/start" {
+ vec![
+ json!(format!("http://{addr}/start")),
+ json!(format!("http://{addr}/protected")),
+ ]
+ } else {
+ vec![json!(format!("http://{addr}/protected"))]
};
+ assert_eq!(
+ request_urls, expected_urls,
+ "auth retries preserve Network request identity"
+ );
let captured = captured.lock();
- assert!(captured.iter().any(|request| request.path == "/start"));
+ assert_eq!(captured.len(), if request_path == "/start" { 3 } else { 2 });
+ assert!(captured.iter().any(|request| request.path == request_path));
assert!(
captured
.iter()
@@ -135,6 +177,11 @@ async fn check_intercepted_header_bytes(script: &str, override_headers: bool) {
.any(|request| request.path == "/protected" && request.authenticated)
);
for request in captured.iter() {
+ let expected = if override_headers && request.path == request_path {
+ vec![vec![0xc3, 0xbf]]
+ } else {
+ vec![vec![0xe9, 0xff]]
+ };
assert_eq!(
request.headers, expected,
"{}, authenticated={}",
@@ -145,36 +192,36 @@ async fn check_intercepted_header_bytes(script: &str, override_headers: bool) {
}
const FETCH: &str = r#"globalThis.__headerBytesResult = 'pending';
-fetch('/start', {headers: {'x-raw': '\u00e9\u00ff'}}).then(r => r.text()).then(value => {globalThis.__headerBytesResult = value});"#;
+fetch('/start', {headers: {'x-raw': '\u00e9\u00ff'}}).then(r => {globalThis.__headerBytesRedirected = r.redirected; return r.text()}).then(value => {globalThis.__headerBytesResult = value});"#;
const XHR: &str = r#"globalThis.__headerBytesResult = 'pending';
var xhr = new XMLHttpRequest(); xhr.open('GET', '/start'); xhr.setRequestHeader('x-raw', '\u00e9\u00ff');
-xhr.onload = () => {globalThis.__headerBytesResult = xhr.responseText}; xhr.send();"#;
+xhr.onload = () => {globalThis.__headerBytesFinalPath = new URL(xhr.responseURL).pathname; globalThis.__headerBytesResult = xhr.responseText}; xhr.send();"#;
const WORKER: &str = r#"globalThis.__headerBytesResult = 'pending';
var worker = new Worker(URL.createObjectURL(new Blob([
- "self.onmessage = event => fetch(event.data, {headers: {'x-raw': '\\u00e9\\u00ff'}}).then(r => r.text()).then(value => postMessage(value))"
+ "self.onmessage = event => fetch(event.data, {headers: {'x-raw': '\\u00e9\\u00ff'}}).then(async r => postMessage({value: await r.text(), redirected: r.redirected}))"
], {type: 'text/javascript'})));
-worker.onmessage = event => {globalThis.__headerBytesResult = event.data};
+worker.onmessage = event => {globalThis.__headerBytesResult = event.data.value; globalThis.__headerBytesRedirected = event.data.redirected};
worker.postMessage(new URL('/start', location.href).href);"#;
#[tokio::test(flavor = "multi_thread")]
async fn fetch_header_bytes_survive_continue_redirect_and_auth() {
- check_intercepted_header_bytes(FETCH, false).await;
+ check_intercepted_header_bytes(FETCH, false, "/start").await;
}
#[tokio::test(flavor = "multi_thread")]
async fn xhr_header_bytes_survive_continue_redirect_and_auth() {
- check_intercepted_header_bytes(XHR, false).await;
+ check_intercepted_header_bytes(XHR, false, "/start").await;
}
#[tokio::test(flavor = "multi_thread")]
-async fn cdp_fetch_header_overrides_use_utf8_and_preserve_duplicates() {
- check_intercepted_header_bytes(FETCH, true).await;
+async fn cdp_fetch_header_overrides_use_utf8_last_value_and_expire_on_redirect() {
+ check_intercepted_header_bytes(FETCH, true, "/start").await;
}
#[tokio::test(flavor = "multi_thread")]
-async fn cdp_xhr_header_overrides_use_utf8_and_preserve_duplicates() {
- check_intercepted_header_bytes(XHR, true).await;
+async fn cdp_xhr_header_overrides_use_utf8_last_value_and_expire_on_redirect() {
+ check_intercepted_header_bytes(XHR, true, "/start").await;
}
#[tokio::test(flavor = "multi_thread")]
async fn worker_header_bytes_survive_continue_redirect_and_auth() {
- check_intercepted_header_bytes(WORKER, false).await;
+ check_intercepted_header_bytes(WORKER, false, "/start").await;
}
async fn check_response_header_bytes(continue_response: bool, binary: bool) {
@@ -266,3 +313,167 @@ async fn continued_binary_response_header_bytes_reach_fetch_headers() {
async fn fulfilled_text_response_header_bytes_are_utf8() {
check_response_header_bytes(false, false).await;
}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn cdp_worker_header_overrides_expire_on_redirect_before_auth_retry() {
+ check_intercepted_header_bytes(WORKER, true, "/start").await;
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn cdp_fetch_header_override_survives_same_hop_auth_retry() {
+ check_intercepted_header_bytes(FETCH, true, "/protected").await;
+}
+#[tokio::test(flavor = "multi_thread")]
+async fn cdp_xhr_header_override_survives_same_hop_auth_retry() {
+ check_intercepted_header_bytes(XHR, true, "/protected").await;
+}
+#[tokio::test(flavor = "multi_thread")]
+async fn cdp_worker_header_override_survives_same_hop_auth_retry() {
+ check_intercepted_header_bytes(WORKER, true, "/protected").await;
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn navigation_header_override_expires_on_redirect_before_auth_retry() {
+ let captured = CapturedHeaders::default();
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ let app = Router::new()
+ .route("/start", get(capture_headers))
+ .route("/protected", get(capture_headers))
+ .with_state(captured.clone());
+ let server = tokio::spawn(async move {
+ axum::serve(listener, app).await.unwrap();
+ });
+ let mut ctx = TestContext::new();
+ ctx.conn
+ .install_browser_context_fixture_for_test(attached_browser_context());
+ ctx.enable_page_events_for_test(Some("SID-1"));
+ ctx.conn
+ .set_global_extra_headers(moli_fetch::RequestHeaders::from_bytes(vec![(
+ "x-raw".into(),
+ vec![0xe9, 0xff],
+ )]));
+ ctx.process_async(json!({"id": 70300, "sessionId": "SID-1", "method": "Fetch.enable", "params": {"handleAuthRequests": true}})).await;
+ ctx.expect_result(70300, json!({}), Some("SID-1"));
+ ctx.process_async(json!({"id": 70301, "sessionId": "SID-1", "method": "Page.navigate", "params": {"url": format!("http://{addr}/start")}})).await;
+ let paused = take_main_document_request_pause(&mut ctx).await;
+ ctx.process_async(
+ json!({"id": 70302, "sessionId": "SID-1", "method": "Fetch.continueRequest", "params": {
+ "requestId": paused["params"]["requestId"], "headers": [{"name": "x-raw", "value": "ÿ"}]
+ }}),
+ )
+ .await;
+ ctx.expect_result(70302, json!({}), Some("SID-1"));
+ wait_until_message(&mut ctx, "SID-1", "redirected navigation auth", |message| {
+ message["method"] == "Fetch.authRequired"
+ })
+ .await;
+ let auth = ctx.take_first_matching("auth", |message| message["method"] == "Fetch.authRequired");
+ ctx.process_async(json!({"id": 70303, "sessionId": "SID-1", "method": "Fetch.continueWithAuth", "params": {
+ "requestId": auth["params"]["requestId"], "authChallengeResponse": {"response": "ProvideCredentials", "username": "user", "password": "pass"}
+ }})).await;
+ ctx.expect_result(70303, json!({}), Some("SID-1"));
+ wait_until_frame_stopped_loading(&mut ctx, "TID-1").await;
+ let captured = captured.lock();
+ assert_eq!(captured.len(), 3);
+ assert_eq!(captured[0].path, "/start");
+ assert_eq!(captured[0].headers, vec![vec![0xc3, 0xbf]]);
+ for request in &captured[1..] {
+ assert_eq!(request.path, "/protected");
+ assert_eq!(request.headers, vec![vec![0xe9, 0xff]]);
+ }
+ assert!(!captured[1].authenticated);
+ assert!(captured[2].authenticated);
+ server.abort();
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn bidi_extra_header_bytes_reach_fetch_xhr_worker_and_navigation() {
+ use crate::devtools_runtime::{
+ DevToolsCommand, DevToolsCommandContext, DevToolsCommandResult, DevToolsProtocol,
+ DevToolsSessionId, DevToolsSetExtraHeadersCommand, DevToolsTargetId,
+ };
+ async fn echo_headers(headers: HeaderMap) -> String {
+ ["x-extra", "x-utf8"]
+ .into_iter()
+ .map(|name| {
+ headers
+ .get(name)
+ .expect("extra header")
+ .as_bytes()
+ .iter()
+ .map(|byte| format!("{byte:02x}"))
+ .collect::<String>()
+ })
+ .collect::<Vec<_>>()
+ .join("/")
+ }
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ let app = Router::new()
+ .route(
+ "/page",
+ get(|| async {
+ (
+ [(CONTENT_TYPE.as_str(), "text/html")],
+ "<!doctype html><body>ready",
+ )
+ }),
+ )
+ .route("/echo", get(echo_headers));
+ let server = tokio::spawn(async move {
+ axum::serve(listener, app).await.unwrap();
+ });
+ let mut ctx = TestContext::new();
+ with_loaded_http_document(&mut ctx, &format!("http://{addr}/page"), "SID-1", "TID-1").await;
+ enable_runtime_async(&mut ctx, "SID-1", 70400).await;
+ let outcome = ctx
+ .conn
+ .execute_devtools_command(DevToolsCommand::SetExtraHeaders(
+ DevToolsSetExtraHeadersCommand {
+ context: DevToolsCommandContext {
+ protocol: DevToolsProtocol::WebDriverBidi,
+ session_id: Some(DevToolsSessionId::from("bidi-session-1")),
+ target_id: None,
+ browser_context_id: None,
+ },
+ target_ids: vec![DevToolsTargetId::from("TID-1")],
+ browser_context_ids: Vec::new(),
+ headers: moli_fetch::RequestHeaders::from_bytes(vec![
+ ("x-extra".into(), vec![0xe9, 0xff]),
+ ("x-utf8".into(), vec![0xc3, 0xa9]),
+ ]),
+ },
+ ))
+ .await;
+ assert!(matches!(
+ outcome.into_complete_parts().0,
+ Ok(DevToolsCommandResult::Empty)
+ ));
+ for script in [FETCH, XHR, WORKER] {
+ ctx.process_async(json!({"id": 70401, "sessionId": "SID-1", "method": "Runtime.evaluate", "params": {"expression": script.replace("/start", "/echo")}})).await;
+ let _ = take_response_by_id(&mut ctx, 70401);
+ evaluate_until_value_async(
+ &mut ctx,
+ "SID-1",
+ 70402,
+ "globalThis.__headerBytesResult",
+ &json!("e9ff/c3a9"),
+ "extra headers on the wire",
+ )
+ .await;
+ }
+ ctx.enable_page_events_for_test(Some("SID-1"));
+ ctx.process_async(json!({"id": 70403, "sessionId": "SID-1", "method": "Page.navigate", "params": {"url": format!("http://{addr}/echo")}})).await;
+ wait_until_frame_stopped_loading(&mut ctx, "TID-1").await;
+ evaluate_until_value_async(
+ &mut ctx,
+ "SID-1",
+ 70404,
+ "document.body.textContent",
+ &json!("e9ff/c3a9"),
+ "navigation extra headers on the wire",
+ )
+ .await;
+ server.abort();
+}
diff --git a/moli-protocol/src/domains/network/settings.rs b/moli-protocol/src/domains/network/settings.rs
index eacece250..61ab1d127 100644
--- a/moli-protocol/src/domains/network/settings.rs
+++ b/moli-protocol/src/domains/network/settings.rs
@@ -103,7 +103,7 @@ pub(super) fn network_offline_for_emulation_command(
pub(super) fn extra_http_headers_for_command(
cmd: &Cmd<'_>,
-) -> Result<Vec<(String, String)>, CommandOutputPlan> {
+) -> Result<moli_fetch::RequestHeaders, CommandOutputPlan> {
let params: SetExtraHttpHeadersParams = match cmd.get_params() {
Ok(Some(params)) => params,
_ => return Err(CommandOutputPlan::error(-32602, "InvalidParams")),
@@ -134,11 +134,12 @@ pub(crate) fn user_agent_override_for_command(
fn extra_http_headers_from_params(
params: SetExtraHttpHeadersParams,
-) -> Option<Vec<(String, String)>> {
+) -> Option<moli_fetch::RequestHeaders> {
params.headers.inner().as_object().map(|headers| {
headers
.iter()
.filter_map(|(name, value)| value.as_str().map(|v| (name.clone(), v.to_owned())))
.collect::<Vec<_>>()
+ .into()
})
}
diff --git a/moli-protocol/src/domains/network/tests/cache.rs b/moli-protocol/src/domains/network/tests/cache.rs
index 664664d04..268c93f99 100644
--- a/moli-protocol/src/domains/network/tests/cache.rs
+++ b/moli-protocol/src/domains/network/tests/cache.rs
@@ -648,6 +648,8 @@ async fn clear_browser_cache_keeps_pending_response_navigation_transfer() {
"INT-1".to_owned(),
None,
NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: crate::conn::NavigationResultProjection::Cdp(
diff --git a/moli-protocol/src/domains/network/tests/cookies.rs b/moli-protocol/src/domains/network/tests/cookies.rs
index 16e358bf0..2384f1409 100644
--- a/moli-protocol/src/domains/network/tests/cookies.rs
+++ b/moli-protocol/src/domains/network/tests/cookies.rs
@@ -83,11 +83,15 @@ async fn set_extra_http_headers_replaces_previous_headers() {
bc.active_page_target()
.effective_policy()
.extra_headers()
+ .to_byte_strings()
.len(),
1
);
assert_eq!(
- bc.active_page_target().effective_policy().extra_headers()[0],
+ bc.active_page_target()
+ .effective_policy()
+ .extra_headers()
+ .to_byte_strings()[0],
("food".to_owned(), "bars".to_owned())
);
}
diff --git a/moli-protocol/src/domains/network/tests/response_body.rs b/moli-protocol/src/domains/network/tests/response_body.rs
index abf74f1ed..f31eee160 100644
--- a/moli-protocol/src/domains/network/tests/response_body.rs
+++ b/moli-protocol/src/domains/network/tests/response_body.rs
@@ -507,6 +507,8 @@ async fn get_request_post_data_returns_main_document_navigation_post_body() {
let requested_url = Url::parse("http://127.0.0.1:1/post").unwrap();
let navigation_state = NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: crate::conn::NavigationResultProjection::Cdp(
@@ -596,6 +598,8 @@ async fn get_request_post_data_uses_text_projection_while_bidi_collector_keeps_t
let requested_url = Url::parse("http://127.0.0.1:1/upload").unwrap();
let navigation_state = NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: Some(1),
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: crate::conn::NavigationResultProjection::Cdp(
diff --git a/moli-protocol/src/domains/network/tests/session.rs b/moli-protocol/src/domains/network/tests/session.rs
index 47194f488..7c498e9f8 100644
--- a/moli-protocol/src/domains/network/tests/session.rs
+++ b/moli-protocol/src/domains/network/tests/session.rs
@@ -134,7 +134,7 @@ async fn network_configuration_commands_succeed_while_the_target_is_changing_doc
assert!(configuration.bypass_service_worker);
assert!(configuration.network_offline);
assert_eq!(
- configuration.extra_http_headers,
+ configuration.extra_http_headers.to_byte_strings(),
[("X-During-Navigation".to_owned(), "current".to_owned())]
);
assert_eq!(
@@ -376,7 +376,7 @@ async fn page_network_policy_aggregates_enabled_sessions_like_chromium_handlers(
assert!(policy.cache_disabled());
assert!(policy.bypass_service_worker());
assert_eq!(
- policy.extra_headers(),
+ policy.extra_headers().to_byte_strings(),
[
("X-Primary".to_owned(), "primary".to_owned()),
("x-shared".to_owned(), "aux".to_owned()),
@@ -411,7 +411,7 @@ async fn page_network_policy_aggregates_enabled_sessions_like_chromium_handlers(
assert!(!policy.cache_disabled());
assert!(!policy.bypass_service_worker());
assert_eq!(
- policy.extra_headers(),
+ policy.extra_headers().to_byte_strings(),
[
("X-Aux".to_owned(), "aux".to_owned()),
("x-shared".to_owned(), "aux".to_owned()),
@@ -441,7 +441,7 @@ async fn page_network_policy_aggregates_enabled_sessions_like_chromium_handlers(
assert!(!policy.cache_disabled());
assert!(!policy.bypass_service_worker());
assert_eq!(
- policy.extra_headers(),
+ policy.extra_headers().to_byte_strings(),
[
("X-Aux".to_owned(), "aux".to_owned()),
("x-shared".to_owned(), "aux".to_owned()),
diff --git a/moli-protocol/src/domains/page/navigation.rs b/moli-protocol/src/domains/page/navigation.rs
index f6ba3de0a..b7ff9a6b9 100644
--- a/moli-protocol/src/domains/page/navigation.rs
+++ b/moli-protocol/src/domains/page/navigation.rs
@@ -2723,6 +2723,8 @@ fn start_navigate_to_url_command_with_background_policy_and_request(
.map(|preflight| preflight.inherited_secure_context_type.clone())
.unwrap_or_else(|| "Secure".to_owned());
let mut navigation_state = NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: command_id,
owner: owner.clone(),
result_projection,
diff --git a/moli-protocol/src/domains/page/tests/lifecycle.rs b/moli-protocol/src/domains/page/tests/lifecycle.rs
index c18718dcf..450912689 100644
--- a/moli-protocol/src/domains/page/tests/lifecycle.rs
+++ b/moli-protocol/src/domains/page/tests/lifecycle.rs
@@ -2974,7 +2974,7 @@ async fn close_clears_loaded_page_state_and_emits_detached_events() {
network.network_enabled = true;
network.cache_disabled = true;
network.bypass_service_worker = true;
- network.extra_headers = vec![("X-Test".into(), "1".into())];
+ network.extra_headers = vec![("X-Test".into(), "1".into())].into();
},
);
bc.active_page_target_mut().css_enabled = true;
diff --git a/moli-protocol/src/domains/runtime/tests/evaluate.rs b/moli-protocol/src/domains/runtime/tests/evaluate.rs
index 31264c7a1..c2924a689 100644
--- a/moli-protocol/src/domains/runtime/tests/evaluate.rs
+++ b/moli-protocol/src/domains/runtime/tests/evaluate.rs
@@ -2459,6 +2459,8 @@ async fn page_navigate_network_failure_commits_error_document() {
let stale_body_completion = BackgroundNavigationCompletion::main_document_body(
old_document_token.clone(),
crate::conn::NavigationDispatchState {
+ redirect_chain: Vec::new(),
+ redirect_headers: None,
navigate_id: None,
owner: crate::conn::CommandOwnerScope::for_session("SID-1"),
result_projection: crate::conn::NavigationResultProjection::Cdp(json!({})),
diff --git a/moli-protocol/src/domains/target/tests/tests_background_staging.rs b/moli-protocol/src/domains/target/tests/tests_background_staging.rs
index 18586eb89..e52b86f54 100644
--- a/moli-protocol/src/domains/target/tests/tests_background_staging.rs
+++ b/moli-protocol/src/domains/target/tests/tests_background_staging.rs
@@ -2100,7 +2100,8 @@ async fn same_context_background_session_can_stage_its_own_extra_headers_before_
active
.active_page_target()
.effective_policy()
- .extra_headers(),
+ .extra_headers()
+ .to_byte_strings(),
vec![("X-Target".into(), "A".into())]
);
let staged = active
@@ -2108,7 +2109,7 @@ async fn same_context_background_session_can_stage_its_own_extra_headers_before_
.filter(|target| target.has_non_default_session_state())
.expect("second target should have staged background page session state");
assert_eq!(
- staged.effective_policy().extra_headers(),
+ staged.effective_policy().extra_headers().to_byte_strings(),
vec![("X-Target".into(), "B".into())]
);
}
@@ -2309,7 +2310,8 @@ async fn same_context_background_session_can_clear_its_own_extra_headers_before_
active
.active_page_target()
.effective_policy()
- .extra_headers(),
+ .extra_headers()
+ .to_byte_strings(),
vec![("X-Target".into(), "A".into())]
);
let staged = active
diff --git a/moli-protocol/src/domains/target/tests/tests_same_context_overrides.rs b/moli-protocol/src/domains/target/tests/tests_same_context_overrides.rs
index efaf1ea06..876b01f60 100644
--- a/moli-protocol/src/domains/target/tests/tests_same_context_overrides.rs
+++ b/moli-protocol/src/domains/target/tests/tests_same_context_overrides.rs
@@ -138,7 +138,10 @@ async fn same_context_targets_keep_local_network_policy_but_share_process_locale
.expect("active browser context");
assert_eq!(bc.active_target_id(), Some("TID-000000000A"));
assert_eq!(
- bc.active_page_target().effective_policy().extra_headers(),
+ bc.active_page_target()
+ .effective_policy()
+ .extra_headers()
+ .to_byte_strings(),
vec![("X-Target".into(), "A".into())]
);
assert_eq!(
@@ -202,7 +205,10 @@ async fn same_context_targets_keep_local_network_policy_but_share_process_locale
.expect("active browser context");
assert_eq!(bc.active_target_id(), Some(second_target_id.as_str()));
assert_eq!(
- bc.active_page_target().effective_policy().extra_headers(),
+ bc.active_page_target()
+ .effective_policy()
+ .extra_headers()
+ .to_byte_strings(),
vec![("X-Target".into(), "B".into())]
);
assert_eq!(
diff --git a/moli-protocol/src/domains/target/tests/tests_target_attachment.rs b/moli-protocol/src/domains/target/tests/tests_target_attachment.rs
index 762b17679..a45d38b2a 100644
--- a/moli-protocol/src/domains/target/tests/tests_target_attachment.rs
+++ b/moli-protocol/src/domains/target/tests/tests_target_attachment.rs
@@ -1605,7 +1605,7 @@ async fn detach_from_target() {
network.network_enabled = true;
network.cache_disabled = true;
network.bypass_service_worker = true;
- network.extra_headers = vec![("X-Test".into(), "1".into())];
+ network.extra_headers = vec![("X-Test".into(), "1".into())].into();
},
);
bc.active_page_target_mut().css_enabled = true;
@@ -2039,7 +2039,8 @@ async fn detach_attached_page_session_removes_its_network_policy_contribution()
.unwrap()
.active_page_target()
.effective_policy()
- .extra_headers(),
+ .extra_headers()
+ .to_byte_strings(),
&[
("X-Primary".to_owned(), "primary".to_owned()),
("X-Attached".to_owned(), "attached".to_owned()),
@@ -2111,7 +2112,8 @@ async fn detach_attached_page_session_removes_its_network_policy_contribution()
.unwrap()
.active_page_target()
.effective_policy()
- .extra_headers(),
+ .extra_headers()
+ .to_byte_strings(),
&[("X-Primary".to_owned(), "primary".to_owned())]
);
assert_eq!(
@@ -2887,7 +2889,7 @@ async fn set_auto_attach_false_detaches_existing_target() {
network.network_enabled = true;
network.cache_disabled = true;
network.bypass_service_worker = true;
- network.extra_headers = vec![("X-Test".into(), "1".into())];
+ network.extra_headers = vec![("X-Test".into(), "1".into())].into();
},
);
bc.active_page_target_mut().css_enabled = true;
diff --git a/moli-protocol/src/domains/target/tests/tests_target_management.rs b/moli-protocol/src/domains/target/tests/tests_target_management.rs
index 9314bd184..3707ef57b 100644
--- a/moli-protocol/src/domains/target/tests/tests_target_management.rs
+++ b/moli-protocol/src/domains/target/tests/tests_target_management.rs
@@ -57,7 +57,7 @@ async fn close_target_success() {
network.network_enabled = true;
network.cache_disabled = true;
network.bypass_service_worker = true;
- network.extra_headers = vec![("X-Test".into(), "1".into())];
+ network.extra_headers = vec![("X-Test".into(), "1".into())].into();
},
);
bc.active_page_target_mut().css_enabled = true;
diff --git a/moli-renderer-v8/src/context_bootstrap/shared_worker_host.rs b/moli-renderer-v8/src/context_bootstrap/shared_worker_host.rs
index 2d1d19db6..de48fd509 100644
--- a/moli-renderer-v8/src/context_bootstrap/shared_worker_host.rs
+++ b/moli-renderer-v8/src/context_bootstrap/shared_worker_host.rs
@@ -515,7 +515,7 @@ fn shared_worker_constructor_context(
network_policy: WorkerNetworkPolicy {
secure_context: creator_secure_context,
permission_overrides: host.permission_overrides().to_vec(),
- extra_http_headers: host.extra_http_headers().to_vec(),
+ extra_http_headers: host.extra_http_headers().clone(),
network_offline: host.network_offline(),
blocked_url_patterns: host.blocked_url_patterns().to_vec(),
network_partition_key,
diff --git a/moli-renderer-v8/src/context_bootstrap/worker_host/constructor.rs b/moli-renderer-v8/src/context_bootstrap/worker_host/constructor.rs
index a74f59aa4..0f2c968b1 100644
--- a/moli-renderer-v8/src/context_bootstrap/worker_host/constructor.rs
+++ b/moli-renderer-v8/src/context_bootstrap/worker_host/constructor.rs
@@ -295,7 +295,7 @@ pub(in crate::context_bootstrap) fn worker_constructor_callback<'s>(
let network_policy = WorkerNetworkPolicy {
secure_context,
permission_overrides: host.permission_overrides().to_vec(),
- extra_http_headers: host.extra_http_headers().to_vec(),
+ extra_http_headers: host.extra_http_headers().clone(),
network_offline: host.network_offline(),
blocked_url_patterns: host.blocked_url_patterns().to_vec(),
network_partition_key: network_partition_key.clone(),
diff --git a/moli-renderer-v8/src/native_bridge/context_host/core.rs b/moli-renderer-v8/src/native_bridge/context_host/core.rs
index ec1240965..8c84656bf 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/core.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/core.rs
@@ -261,7 +261,7 @@ impl JsContextHost {
stored_document_start_scripts: Vec::new(),
stored_runtime_bindings: Vec::new(),
app_manifest_link_change_epoch: 0,
- extra_http_headers: Vec::new(),
+ extra_http_headers: Default::default(),
permission_overrides: Vec::new(),
idle_override: None,
protocol_user_gesture_activation_depth: 0,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
index 070c5855b..e8dd2865a 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
@@ -697,8 +697,8 @@ impl JsContextHost {
}
}
- pub(crate) fn set_extra_http_headers(&mut self, headers: &[(String, String)]) {
- self.extra_http_headers = headers.to_vec();
+ pub(crate) fn set_extra_http_headers(&mut self, headers: &moli_fetch::RequestHeaders) {
+ self.extra_http_headers = headers.clone();
if let Some(resource_loader) = self.current_main_document_resource_loader() {
resource_loader
.request_client()
@@ -711,7 +711,7 @@ impl JsContextHost {
}
}
- pub(crate) fn extra_http_headers(&self) -> &[(String, String)] {
+ pub(crate) fn extra_http_headers(&self) -> &moli_fetch::RequestHeaders {
&self.extra_http_headers
}
diff --git a/moli-renderer-v8/src/native_bridge/context_host/mod.rs b/moli-renderer-v8/src/native_bridge/context_host/mod.rs
index 2dc9b06df..0534a20d0 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/mod.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/mod.rs
@@ -865,7 +865,7 @@ pub(crate) struct JsContextHost {
stored_document_start_scripts: Vec<crate::DocumentStartScript>,
stored_runtime_bindings: Vec<crate::protocol_types::RuntimeBindingRegistration>,
app_manifest_link_change_epoch: u64,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
permission_overrides: Vec<crate::protocol_types::PermissionOverrideRegistration>,
idle_override: Option<crate::protocol_types::EmulatedIdleOverride>,
protocol_user_gesture_activation_depth: usize,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/resource_loading.rs b/moli-renderer-v8/src/native_bridge/context_host/resource_loading.rs
index af6f38258..ef473fb31 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/resource_loading.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/resource_loading.rs
@@ -587,6 +587,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
info.internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -625,6 +626,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
info.internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&info.document_url),
info,
load,
@@ -661,6 +663,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
info.internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&info.document_url),
info,
load,
@@ -695,6 +698,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
info.internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&info.document_url),
info,
load,
@@ -732,6 +736,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
info.internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&info.document_url),
info,
load,
@@ -768,6 +773,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
info.internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&info.document_url),
info,
load,
@@ -805,6 +811,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
info.internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&info.document_url),
info,
load,
@@ -861,6 +868,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -915,6 +923,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load: load.clone(),
@@ -968,6 +977,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1038,6 +1048,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1154,6 +1165,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1209,6 +1221,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1248,6 +1261,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1288,6 +1302,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1324,6 +1339,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1364,6 +1380,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1403,6 +1420,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1438,6 +1456,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
info.internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -1476,6 +1495,7 @@ impl JsContextHost {
self.pending_subresource_fetches.insert(
internal_id,
PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -2873,10 +2893,10 @@ impl JsContextHost {
{
self.record_pending_subresource_auth(PendingSubresourceAuthState {
pending: in_flight.pending,
- request_url: in_flight.request_url,
- request_method: in_flight.request_method,
- request_headers: in_flight.request_headers,
- request_body: in_flight.request_body,
+ request_url: info.url.clone(),
+ request_method: info.method.clone(),
+ request_headers: info.request_headers.clone(),
+ request_body: info.request_body.clone(),
intercept_response: info.intercept_response,
initial_network_request_headers: info.network_request_headers.clone(),
response: navigation_response_from_subresource_body(
diff --git a/moli-renderer-v8/src/native_bridge/context_host/service_workers.rs b/moli-renderer-v8/src/native_bridge/context_host/service_workers.rs
index 6ea4e8be6..afded7177 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/service_workers.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/service_workers.rs
@@ -637,7 +637,7 @@ impl JsContextHost {
creator_secure_context,
),
permission_overrides: self.permission_overrides().to_vec(),
- extra_http_headers: self.extra_http_headers().to_vec(),
+ extra_http_headers: self.extra_http_headers().clone(),
network_offline: self.network_offline(),
blocked_url_patterns: self.blocked_url_patterns().to_vec(),
network_partition_key: None,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/websockets/registration.rs b/moli-renderer-v8/src/native_bridge/context_host/websockets/registration.rs
index cb0337293..dcda3e9fe 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/websockets/registration.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/websockets/registration.rs
@@ -56,7 +56,7 @@ impl JsContextHost {
user_agent: loader
.map(|loader| loader.user_agent().to_owned())
.unwrap_or_else(|| moli_fetch::FetchConfig::DEFAULT_USER_AGENT.to_owned()),
- extra_headers: moli_fetch::RequestHeaders::from_utf8(self.extra_http_headers.clone()),
+ extra_headers: self.extra_http_headers.clone(),
http_proxy: loader.and_then(|loader| loader.http_proxy().map(ToOwned::to_owned)),
http_no_proxy: loader.and_then(|loader| loader.http_no_proxy().map(ToOwned::to_owned)),
http_host_resolve: loader
diff --git a/moli-renderer-v8/src/native_bridge/context_host/workers/registration.rs b/moli-renderer-v8/src/native_bridge/context_host/workers/registration.rs
index b4665ca55..270052da3 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/workers/registration.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/workers/registration.rs
@@ -507,7 +507,7 @@ impl JsContextHost {
let network_policy = WorkerNetworkPolicy {
secure_context,
permission_overrides: self.permission_overrides().to_vec(),
- extra_http_headers: self.extra_http_headers().to_vec(),
+ extra_http_headers: self.extra_http_headers().clone(),
network_offline: self.network_offline(),
blocked_url_patterns: self.blocked_url_patterns().to_vec(),
network_partition_key,
diff --git a/moli-renderer-v8/src/network/context/tests.rs b/moli-renderer-v8/src/network/context/tests.rs
index dea733c0e..1d0092a28 100644
--- a/moli-renderer-v8/src/network/context/tests.rs
+++ b/moli-renderer-v8/src/network/context/tests.rs
@@ -162,7 +162,9 @@ async fn registry_rejects_retired_generation_without_hiding_current_generation()
#[tokio::test(flavor = "current_thread")]
async fn registry_replaces_future_transport_without_rebinding_existing_loads() {
let original = ResourceRequestClient::new(&FetchConfig::default()).expect("original transport");
- original.set_extra_http_headers(&[("x-request-policy".to_owned(), "original".to_owned())]);
+ original.set_extra_http_headers(
+ &vec![("x-request-policy".to_owned(), "original".to_owned())].into(),
+ );
let original_runtime = original.browser_resource_runtime();
let authority = document_loader(original.handle(), 1, "https://example.test/one");
let existing_load = authority
@@ -178,9 +180,9 @@ async fn registry_replaces_future_transport_without_rebinding_existing_loads() {
ResourceRequestClient::new(&replacement_config).expect("replacement resource transport");
let replacement_runtime = replacement.browser_resource_runtime();
let replacement_view = authority.with_replacement_transport(replacement.handle());
- replacement_view
- .request_client()
- .set_extra_http_headers(&[("x-request-policy".to_owned(), "replacement".to_owned())]);
+ replacement_view.request_client().set_extra_http_headers(
+ &vec![("x-request-policy".to_owned(), "replacement".to_owned())].into(),
+ );
let registry = DocumentResourceLoaderRegistry::default();
let document_owner = WindowDocumentOwner::Frame(owner(1));
registry.register(document_owner, authority.clone());
diff --git a/moli-renderer-v8/src/network/policy.rs b/moli-renderer-v8/src/network/policy.rs
index ac93404fb..45a11ec30 100644
--- a/moli-renderer-v8/src/network/policy.rs
+++ b/moli-renderer-v8/src/network/policy.rs
@@ -4,8 +4,6 @@ use std::sync::{
};
use anyhow::{Result, anyhow};
-use http::HeaderName;
-use indexmap::IndexMap;
use moli_fetch::{Request, RequestCacheMode, url_pattern_matches};
use parking_lot::Mutex;
@@ -21,7 +19,7 @@ fn next_memory_cache_partition_id() -> u64 {
.expect("Page memory-cache partition id exhausted")
}
-type SharedHeaderList = Arc<[(Box<str>, Box<str>)]>;
+type SharedHeaderList = Arc<[(Box<str>, Box<[u8]>)]>;
type SharedPatternList = Arc<[Box<str>]>;
#[derive(Debug, Clone)]
@@ -237,13 +235,13 @@ impl PageNetworkPolicy {
self.snapshot().revision()
}
- pub fn set_extra_http_headers(&self, headers: &[(String, String)]) {
+ pub fn set_extra_http_headers(&self, headers: &moli_fetch::RequestHeaders) {
let headers = headers
.iter()
.map(|(name, value)| {
(
name.clone().into_boxed_str(),
- value.clone().into_boxed_str(),
+ value.clone().into_boxed_slice(),
)
})
.collect::<Vec<_>>();
@@ -478,27 +476,17 @@ impl PageNetworkPolicySnapshot {
}
fn merge_page_network_policy_headers(
- context_headers: &[(Box<str>, Box<str>)],
+ context_headers: &[(Box<str>, Box<[u8]>)],
request_headers: &moli_fetch::RequestHeaders,
) -> moli_fetch::RequestHeaders {
- let mut merged = IndexMap::<String, (String, Vec<u8>)>::new();
- for (name, value) in context_headers {
- merged
- .entry(header_name_key(name))
- .or_insert_with(|| (name.to_string(), value.as_bytes().to_vec()));
- }
- for (name, value) in request_headers.iter() {
- let key = header_name_key(name);
- merged.shift_remove(&key);
- merged.insert(key, (name.clone(), value.clone()));
- }
- moli_fetch::RequestHeaders::from_bytes(merged.into_values().collect())
-}
-
-fn header_name_key(name: &str) -> String {
- HeaderName::from_bytes(name.as_bytes())
- .map(|name| name.as_str().to_owned())
- .unwrap_or_else(|_| name.to_ascii_lowercase())
+ let mut headers = moli_fetch::RequestHeaders::from_bytes(
+ context_headers
+ .iter()
+ .map(|(name, value)| (name.to_string(), value.to_vec()))
+ .collect(),
+ );
+ headers.overlay(request_headers.clone());
+ headers
}
#[cfg(test)]
@@ -508,7 +496,7 @@ mod tests {
#[test]
fn isolated_policy_copy_preserves_values_without_sharing_mutations() {
let policy = PageNetworkPolicy::new(OptionalResourceFetchMask::IMAGE, false, true);
- policy.set_extra_http_headers(&[("x-owner".to_owned(), "first".to_owned())]);
+ policy.set_extra_http_headers(&vec![("x-owner".to_owned(), "first".to_owned())].into());
let isolated = policy.isolated_copy();
assert!(!policy.shares_state_with(&isolated));
@@ -520,7 +508,7 @@ mod tests {
assert!(isolated.author_styles_disabled());
isolated.set_network_offline(true);
- isolated.set_extra_http_headers(&[("x-owner".to_owned(), "second".to_owned())]);
+ isolated.set_extra_http_headers(&vec![("x-owner".to_owned(), "second".to_owned())].into());
assert!(!policy.snapshot().network_offline());
let original = policy
@@ -575,10 +563,14 @@ mod tests {
#[test]
fn request_snapshot_does_not_observe_later_policy_mutation() {
let policy = PageNetworkPolicy::default();
- policy.set_extra_http_headers(&[("x-policy-revision".to_owned(), "one".to_owned())]);
+ policy.set_extra_http_headers(
+ &vec![("x-policy-revision".to_owned(), "one".to_owned())].into(),
+ );
let snapshot = policy.snapshot();
- policy.set_extra_http_headers(&[("x-policy-revision".to_owned(), "two".to_owned())]);
+ policy.set_extra_http_headers(
+ &vec![("x-policy-revision".to_owned(), "two".to_owned())].into(),
+ );
policy.set_network_offline(true);
let request = snapshot
@@ -619,10 +611,14 @@ mod tests {
#[test]
fn frozen_request_view_keeps_configuration_but_observes_live_network_conditions() {
let policy = PageNetworkPolicy::default();
- policy.set_extra_http_headers(&[("x-policy-revision".to_owned(), "one".to_owned())]);
+ policy.set_extra_http_headers(
+ &vec![("x-policy-revision".to_owned(), "one".to_owned())].into(),
+ );
let request_view = policy.frozen_request_view();
- policy.set_extra_http_headers(&[("x-policy-revision".to_owned(), "two".to_owned())]);
+ policy.set_extra_http_headers(
+ &vec![("x-policy-revision".to_owned(), "two".to_owned())].into(),
+ );
let request = request_view
.snapshot()
.apply_to_request(
diff --git a/moli-renderer-v8/src/network/request_client.rs b/moli-renderer-v8/src/network/request_client.rs
index 68b5d5030..8a34f9234 100644
--- a/moli-renderer-v8/src/network/request_client.rs
+++ b/moli-renderer-v8/src/network/request_client.rs
@@ -995,7 +995,7 @@ impl ResourceRequestClient {
self.resource_runtime.client().cookie_store()
}
- pub fn set_extra_http_headers(&self, headers: &[(String, String)]) {
+ pub fn set_extra_http_headers(&self, headers: &moli_fetch::RequestHeaders) {
self.page_network_policy.set_extra_http_headers(headers);
}
diff --git a/moli-renderer-v8/src/network/request_client/tests.rs b/moli-renderer-v8/src/network/request_client/tests.rs
index d044fc321..9884cc279 100644
--- a/moli-renderer-v8/src/network/request_client/tests.rs
+++ b/moli-renderer-v8/src/network/request_client/tests.rs
@@ -72,7 +72,7 @@ fn page_loader_fork_shares_backend_but_isolates_mutable_policy() {
.with_site_for_cookies_url(&Url::parse("https://parent.test/").unwrap())
.with_top_frame_origin_url(&Url::parse("https://parent.test/").unwrap()),
);
- parent.set_extra_http_headers(&[("x-page".to_owned(), "parent".to_owned())]);
+ parent.set_extra_http_headers(&vec![("x-page".to_owned(), "parent".to_owned())].into());
let page = parent.fork_with_isolated_page_network_policy();
assert!(parent.shares_resource_runtime_with(&page));
@@ -85,7 +85,7 @@ fn page_loader_fork_shares_backend_but_isolates_mutable_policy() {
assert!(page.browser_site_context().is_none());
page.set_network_offline(true);
- page.set_extra_http_headers(&[("x-page".to_owned(), "child".to_owned())]);
+ page.set_extra_http_headers(&vec![("x-page".to_owned(), "child".to_owned())].into());
assert!(!parent.page_network_policy().snapshot().network_offline());
assert!(page.page_network_policy().snapshot().network_offline());
@@ -360,10 +360,13 @@ async fn read_http_request_text(stream: &mut tokio::net::TcpStream) -> std::io::
#[test]
fn merge_loader_network_policy_headers_uses_header_name_keys_and_request_order() {
let policy = super::PageNetworkPolicy::default();
- policy.set_extra_http_headers(&[
- ("X-Test".to_owned(), "context".to_owned()),
- ("Accept".to_owned(), "text/html".to_owned()),
- ]);
+ policy.set_extra_http_headers(
+ &vec![
+ ("X-Test".to_owned(), "context".to_owned()),
+ ("Accept".to_owned(), "text/html".to_owned()),
+ ]
+ .into(),
+ );
let mut request = browser_navigation_request("https://example.test/headers")
.unwrap()
.with_page_network_policy();
@@ -1552,7 +1555,9 @@ async fn loader_applies_network_policy_only_to_opt_in_requests() -> Result<()> {
});
let loader = ResourceRequestClient::new(&FetchConfig::default())?;
- loader.set_extra_http_headers(&[("x-cdp-test".to_owned(), "loader-policy".to_owned())]);
+ loader.set_extra_http_headers(
+ &vec![("x-cdp-test".to_owned(), "loader-policy".to_owned())].into(),
+ );
let plain = loader
.fetch(browser_navigation_request(&format!("http://{addr}/plain"))?)
diff --git a/moli-renderer-v8/src/network_host/beacon.rs b/moli-renderer-v8/src/network_host/beacon.rs
index 41e6a30b5..a6ee6299c 100644
--- a/moli-renderer-v8/src/network_host/beacon.rs
+++ b/moli-renderer-v8/src/network_host/beacon.rs
@@ -71,10 +71,8 @@ pub(crate) fn navigator_send_beacon_callback<'s>(
let mut request_headers = Vec::new();
append_default_body_content_type(&mut request_headers, body_content_type.as_deref());
request_headers = filter_headers_for_guard(&request_headers, HeadersGuard::RequestNoCors);
- let request_headers = moli_fetch::RequestHeaders::from_utf8(merge_subresource_request_headers(
- host.extra_http_headers(),
- &request_headers,
- ));
+ let request_headers =
+ merge_subresource_request_headers(host.extra_http_headers(), &request_headers);
let request_body_text = request_body_text(&body);
let request_cookie_report = observe_subresource_request_cookie_report(
resource_loader.request_client(),
@@ -209,10 +207,8 @@ pub(crate) fn send_link_audit_ping(
if document_url.scheme() == "http" || moli_url::same_origin(&document_url, &ping_url) {
request_headers.push(("Ping-From".to_owned(), document_url.as_str().to_owned()));
}
- let request_headers = moli_fetch::RequestHeaders::from_utf8(merge_subresource_request_headers(
- host.extra_http_headers(),
- &request_headers,
- ));
+ let request_headers =
+ merge_subresource_request_headers(host.extra_http_headers(), &request_headers);
let request_body = Some("PING".to_owned());
let request_cookie_report = observe_subresource_request_cookie_report(
resource_loader.request_client(),
diff --git a/moli-renderer-v8/src/network_host/event_source/request.rs b/moli-renderer-v8/src/network_host/event_source/request.rs
index 302a0ea52..e87b3d3b1 100644
--- a/moli-renderer-v8/src/network_host/event_source/request.rs
+++ b/moli-renderer-v8/src/network_host/event_source/request.rs
@@ -342,10 +342,8 @@ fn prepare_event_source_request<'s>(
if !last_event_id.is_empty() {
request_headers.push(("Last-Event-ID".to_owned(), last_event_id));
}
- let request_headers = moli_fetch::RequestHeaders::from_utf8(merge_subresource_request_headers(
- host.extra_http_headers(),
- &request_headers,
- ));
+ let request_headers =
+ merge_subresource_request_headers(host.extra_http_headers(), &request_headers);
let credentials_mode = if event_source_with_credentials(scope, event_source) {
RequestCredentialsMode::Include
} else {
@@ -368,7 +366,7 @@ fn prepare_event_source_request<'s>(
request_origin,
resolved_url,
request_headers,
- cors_preflight_request_headers: host.extra_http_headers().to_vec(),
+ cors_preflight_request_headers: host.extra_http_headers().to_byte_strings(),
credentials_mode,
request_cookie_report,
network_partition_key: active_subresource_network_partition_key(host, owner),
diff --git a/moli-renderer-v8/src/network_host/image.rs b/moli-renderer-v8/src/network_host/image.rs
index dadae3261..a3732097c 100644
--- a/moli-renderer-v8/src/network_host/image.rs
+++ b/moli-renderer-v8/src/network_host/image.rs
@@ -79,10 +79,7 @@ pub(crate) fn start_scanned_image_preload(
return ScannedImagePreloadStart::Disabled;
}
};
- let request_headers = moli_fetch::RequestHeaders::from_utf8(merge_subresource_request_headers(
- host.extra_http_headers(),
- &[],
- ));
+ let request_headers = merge_subresource_request_headers(host.extra_http_headers(), &[]);
let policy_context = effective_subresource_policy_context(scope, host, OwnerDispatchScope::Top);
let request = match Request::new("GET", request_url.as_str(), None, request_headers) {
Ok(request) => request
@@ -249,10 +246,7 @@ pub(crate) fn start_image_element_resource_fetch(
"GET",
credentials_mode,
);
- let request_headers = moli_fetch::RequestHeaders::from_utf8(merge_subresource_request_headers(
- host.extra_http_headers(),
- &[],
- ));
+ let request_headers = merge_subresource_request_headers(host.extra_http_headers(), &[]);
let info = PendingSubresourceFetchInfo {
internal_id: 0,
network_request_handle: None,
diff --git a/moli-renderer-v8/src/network_host/url_helpers.rs b/moli-renderer-v8/src/network_host/url_helpers.rs
index 4906fe5f5..bb8d26827 100644
--- a/moli-renderer-v8/src/network_host/url_helpers.rs
+++ b/moli-renderer-v8/src/network_host/url_helpers.rs
@@ -1,6 +1,3 @@
-use http::HeaderName;
-use indexmap::IndexMap;
-
pub(crate) fn resolve_context_url(
document_url: &url::Url,
input: &str,
@@ -19,29 +16,22 @@ pub(crate) fn resolve_context_url(
}
pub(in crate::network_host) fn merge_subresource_request_headers(
- context_headers: &[(String, String)],
+ context_headers: &moli_fetch::RequestHeaders,
request_headers: &[(String, String)],
-) -> Vec<(String, String)> {
- let mut merged = IndexMap::<String, (String, String)>::new();
- for (name, value) in context_headers {
- merged
- .entry(header_name_key(name))
- .or_insert_with(|| (name.clone(), value.clone()));
- }
- for (name, value) in request_headers {
- let key = header_name_key(name);
- merged.shift_remove(&key);
- merged.insert(key, (name.clone(), value.clone()));
- }
- merged.into_values().collect()
+) -> moli_fetch::RequestHeaders {
+ let mut headers = context_headers.clone();
+ headers.overlay(moli_fetch::RequestHeaders::from_utf8(
+ request_headers.to_vec(),
+ ));
+ headers
}
-/// Encode validated WebIDL headers once, then overlay them on UTF-8 defaults.
+/// Encode validated WebIDL headers once, then overlay them on encoded defaults.
pub(crate) fn merge_byte_string_request_headers(
- context_headers: &[(String, String)],
+ context_headers: &moli_fetch::RequestHeaders,
request_headers: &[(String, String)],
) -> moli_fetch::RequestHeaders {
- let mut headers = moli_fetch::RequestHeaders::from_utf8(context_headers.to_vec());
+ let mut headers = context_headers.clone();
headers.overlay(
moli_fetch::RequestHeaders::from_byte_strings(request_headers)
.expect("validated Fetch/XHR headers are ByteStrings"),
@@ -49,12 +39,6 @@ pub(crate) fn merge_byte_string_request_headers(
headers
}
-fn header_name_key(name: &str) -> String {
- HeaderName::from_bytes(name.as_bytes())
- .map(|name| name.as_str().to_owned())
- .unwrap_or_else(|_| name.to_ascii_lowercase())
-}
-
#[cfg(test)]
mod tests {
use super::merge_subresource_request_headers;
@@ -62,10 +46,11 @@ mod tests {
#[test]
fn merge_subresource_request_headers_uses_header_name_keys_and_request_order() {
let merged = merge_subresource_request_headers(
- &[
+ &vec![
("X-Test".to_owned(), "context".to_owned()),
("Accept".to_owned(), "text/html".to_owned()),
- ],
+ ]
+ .into(),
&[
("x-test".to_owned(), "request".to_owned()),
("X-New".to_owned(), "new".to_owned()),
@@ -73,7 +58,7 @@ mod tests {
);
assert_eq!(
- merged,
+ merged.to_byte_strings(),
vec![
("Accept".to_owned(), "text/html".to_owned()),
("x-test".to_owned(), "request".to_owned()),
diff --git a/moli-renderer-v8/src/runtime/owner.rs b/moli-renderer-v8/src/runtime/owner.rs
index 2361abe65..031f692f1 100644
--- a/moli-renderer-v8/src/runtime/owner.rs
+++ b/moli-renderer-v8/src/runtime/owner.rs
@@ -105,7 +105,7 @@ pub struct RendererPreparedDocumentCommitConfiguration {
pub runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
pub runtime_isolated_worlds: Vec<crate::protocol_types::RuntimeIsolatedWorldDefinition>,
pub permission_overrides: Vec<crate::protocol_types::PermissionOverrideRegistration>,
- pub extra_http_headers: Vec<(String, String)>,
+ pub extra_http_headers: moli_fetch::RequestHeaders,
pub script_execution_disabled: bool,
pub bypass_content_security_policy: bool,
pub emulated_media: crate::protocol_types::EmulatedMediaOverrides,
@@ -150,7 +150,7 @@ pub struct RendererCreateHtmlPageRequest {
pub runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
pub runtime_isolated_worlds: Vec<crate::protocol_types::RuntimeIsolatedWorldDefinition>,
pub permission_overrides: Vec<crate::protocol_types::PermissionOverrideRegistration>,
- pub extra_http_headers: Vec<(String, String)>,
+ pub extra_http_headers: moli_fetch::RequestHeaders,
pub script_execution_disabled: bool,
pub bypass_content_security_policy: bool,
pub network_offline: bool,
@@ -198,7 +198,7 @@ pub struct RendererCreateStreamingRawPageRequest {
pub runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
pub runtime_isolated_worlds: Vec<crate::protocol_types::RuntimeIsolatedWorldDefinition>,
pub permission_overrides: Vec<crate::protocol_types::PermissionOverrideRegistration>,
- pub extra_http_headers: Vec<(String, String)>,
+ pub extra_http_headers: moli_fetch::RequestHeaders,
pub script_execution_disabled: bool,
pub bypass_content_security_policy: bool,
pub network_offline: bool,
@@ -1152,7 +1152,7 @@ fn page_turn_trigger_log_label(trigger: PageTurnTrigger) -> &'static str {
fn loader_for_new_document(
loader: &ResourceRequestClient,
- extra_http_headers: &[(String, String)],
+ extra_http_headers: &moli_fetch::RequestHeaders,
network_offline: bool,
blocked_url_patterns: &[String],
) -> ResourceRequestClient {
@@ -2060,7 +2060,7 @@ impl RendererOwnerHandle {
document_start_scripts: Vec<DocumentStartScript>,
runtime_bindings: Vec<crate::protocol_types::RuntimeBindingRegistration>,
runtime_inspector_session_restore_snapshots: Vec<RendererInspectorSessionRestoreSnapshot>,
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
network_offline: bool,
blocked_url_patterns: Vec<String>,
fetch_subresource_interception_enabled: bool,
diff --git a/moli-renderer-v8/src/runtime/page.rs b/moli-renderer-v8/src/runtime/page.rs
index d3cc3d9af..60648e82b 100644
--- a/moli-renderer-v8/src/runtime/page.rs
+++ b/moli-renderer-v8/src/runtime/page.rs
@@ -32,7 +32,7 @@ pub struct RendererDocumentOptions {
pub storage_bucket_store: Option<crate::context_bootstrap::SharedStorageBucketStore>,
pub document_start_scripts: Vec<DocumentStartScript>,
pub runtime_bindings: Vec<crate::protocol_types::RuntimeBindingRegistration>,
- pub extra_http_headers: Vec<(String, String)>,
+ pub extra_http_headers: moli_fetch::RequestHeaders,
pub script_execution_disabled: bool,
pub bypass_content_security_policy: bool,
pub emulated_media: crate::protocol_types::EmulatedMediaOverrides,
diff --git a/moli-renderer-v8/src/runtime/page_network.rs b/moli-renderer-v8/src/runtime/page_network.rs
index be43169a5..18da2f1a9 100644
--- a/moli-renderer-v8/src/runtime/page_network.rs
+++ b/moli-renderer-v8/src/runtime/page_network.rs
@@ -136,8 +136,8 @@ impl PageVm {
.set_fetch_subresource_interception(enabled, resource_type);
}
- pub(crate) fn set_extra_http_headers(&mut self, headers: &[(String, String)]) {
- self.extra_http_headers = headers.to_vec();
+ pub(crate) fn set_extra_http_headers(&mut self, headers: &moli_fetch::RequestHeaders) {
+ self.extra_http_headers = headers.clone();
self.vm_mut().set_extra_http_headers(headers);
}
@@ -286,7 +286,7 @@ impl PageVm {
url: Option<Url>,
method: Option<String>,
body: Option<Option<String>>,
- headers: Option<moli_fetch::RequestHeaders>,
+ headers: Option<moli_fetch::RequestHeaderOverride>,
intercept_response: bool,
handle_auth_requests: bool,
) -> Result<crate::PendingSubresourceContinueOutcome> {
diff --git a/moli-renderer-v8/src/runtime/page_surface.rs b/moli-renderer-v8/src/runtime/page_surface.rs
index 2c0ca3388..41fb05e37 100644
--- a/moli-renderer-v8/src/runtime/page_surface.rs
+++ b/moli-renderer-v8/src/runtime/page_surface.rs
@@ -5222,7 +5222,7 @@ pub enum RendererPageCommand {
url: Option<Url>,
method: Option<String>,
body: Option<Option<String>>,
- headers: Option<moli_fetch::RequestHeaders>,
+ headers: Option<moli_fetch::RequestHeaderOverride>,
intercept_response: bool,
handle_auth_requests: bool,
},
@@ -5298,9 +5298,9 @@ pub enum RendererPageCommand {
},
PrepareAppManifestLoad,
PublishAppManifestLoad(Box<crate::RendererAppManifestLoadPublication>),
- SetExtraHttpHeaders(Vec<(String, String)>),
+ SetExtraHttpHeaders(moli_fetch::RequestHeaders),
SetNetworkRequestPolicy {
- extra_http_headers: Vec<(String, String)>,
+ extra_http_headers: moli_fetch::RequestHeaders,
bypass_service_worker: bool,
cache_disabled: bool,
blocked_url_patterns: Vec<String>,
diff --git a/moli-renderer-v8/src/runtime/page_vm/mod.rs b/moli-renderer-v8/src/runtime/page_vm/mod.rs
index 4de6720f2..18b82a747 100644
--- a/moli-renderer-v8/src/runtime/page_vm/mod.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/mod.rs
@@ -1076,7 +1076,7 @@ pub(crate) struct PageVmEnvConfig {
Vec<RendererInspectorSessionRestoreSnapshot>,
pub(crate) runtime_isolated_worlds: Vec<crate::protocol_types::RuntimeIsolatedWorldDefinition>,
pub(crate) permission_overrides: Vec<crate::protocol_types::PermissionOverrideRegistration>,
- pub(crate) extra_http_headers: Vec<(String, String)>,
+ pub(crate) extra_http_headers: moli_fetch::RequestHeaders,
pub(crate) navigator_identity: moli_browser_profile::BrowserIdentityProfile,
pub(crate) document_policy_container: crate::document_runtime::DocumentPolicyContainer,
pub(crate) document_default_language: Option<String>,
@@ -1602,7 +1602,7 @@ pub(crate) struct PageVm {
pub(super) runtime_bindings: Vec<crate::protocol_types::RuntimeBindingRegistration>,
pub(super) runtime_inspector_protocol_configurations:
BTreeMap<DevToolsSessionKey, RendererInspectorProtocolConfiguration>,
- pub(super) extra_http_headers: Vec<(String, String)>,
+ pub(super) extra_http_headers: moli_fetch::RequestHeaders,
pub(super) bypass_content_security_policy: bool,
pub(super) emulated_media: crate::protocol_types::EmulatedMediaOverrides,
pub(super) idle_override: Option<crate::protocol_types::EmulatedIdleOverride>,
diff --git a/moli-renderer-v8/src/runtime/page_vm/test_support.rs b/moli-renderer-v8/src/runtime/page_vm/test_support.rs
index 2e8583a77..359d7d853 100644
--- a/moli-renderer-v8/src/runtime/page_vm/test_support.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/test_support.rs
@@ -678,7 +678,7 @@ fn minimal_test_page_vm_env_config() -> PageVmEnvConfig {
runtime_inspector_session_restore_snapshots: Vec::new(),
runtime_isolated_worlds: Vec::new(),
permission_overrides: Vec::new(),
- extra_http_headers: Vec::new(),
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/mod.rs b/moli-renderer-v8/src/runtime/page_vm/tests/mod.rs
index 028e610bc..471ba3769 100644
--- a/moli-renderer-v8/src/runtime/page_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/tests/mod.rs
@@ -1797,7 +1797,7 @@ fn test_page_vm_with_loader_dom_host_hooks_and_response_referrer_policy(
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers,
+ extra_http_headers: extra_http_headers.into(),
navigator_identity: loader.browser_identity().clone(),
document_policy_container: crate::document_runtime::DocumentPolicyContainer {
referrer_policy: response_referrer_policy,
@@ -3195,7 +3195,7 @@ fn default_runtime_hooks_reject_direct_no_owner_page_vm_construction() {
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: Vec::new(),
+ extra_http_headers: Default::default(),
navigator_identity: loader.browser_identity().clone(),
document_policy_container: Default::default(),
document_default_language: None,
diff --git a/moli-renderer-v8/src/runtime/phase_one/mod.rs b/moli-renderer-v8/src/runtime/phase_one/mod.rs
index a56c834dd..d03d23169 100644
--- a/moli-renderer-v8/src/runtime/phase_one/mod.rs
+++ b/moli-renderer-v8/src/runtime/phase_one/mod.rs
@@ -2537,7 +2537,7 @@ document.body.setAttribute('data-error-state', [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -6309,7 +6309,7 @@ globalThis.__outerDocumentWriteScriptContinued = true;
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -14713,7 +14713,7 @@ document.body.setAttribute('data-result', [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -14893,7 +14893,7 @@ document.body.setAttribute('data-result', [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -15122,7 +15122,7 @@ document.body.setAttribute('data-result', [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -17121,7 +17121,7 @@ document.body.setAttribute("data-range", [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -17249,7 +17249,7 @@ document.body.setAttribute("data-range", [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -17387,7 +17387,7 @@ document.body.setAttribute("data-range", [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -17560,7 +17560,7 @@ document.body.setAttribute("data-range", [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -17883,7 +17883,7 @@ document.body.setAttribute("data-range", [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -18053,7 +18053,7 @@ document.body.setAttribute("data-range", [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
@@ -18141,7 +18141,7 @@ document.body.setAttribute("data-range", [
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
diff --git a/moli-renderer-v8/src/runtime/phase_one/streaming.rs b/moli-renderer-v8/src/runtime/phase_one/streaming.rs
index c51387820..d3862df63 100644
--- a/moli-renderer-v8/src/runtime/phase_one/streaming.rs
+++ b/moli-renderer-v8/src/runtime/phase_one/streaming.rs
@@ -939,7 +939,7 @@ mod tests {
runtime_inspector_session_restore_snapshots: vec![],
runtime_isolated_worlds: vec![],
permission_overrides: vec![],
- extra_http_headers: vec![],
+ extra_http_headers: Default::default(),
navigator_identity: Default::default(),
document_policy_container: Default::default(),
document_default_language: None,
diff --git a/moli-renderer-v8/src/runtime/tests.rs b/moli-renderer-v8/src/runtime/tests.rs
index 376226991..7f08b8783 100644
--- a/moli-renderer-v8/src/runtime/tests.rs
+++ b/moli-renderer-v8/src/runtime/tests.rs
@@ -1513,7 +1513,7 @@ async fn streaming_unstyled_xml_converts_live_document_before_domcontentloaded()
runtime_inspector_session_restore_snapshots: Vec::new(),
runtime_isolated_worlds: Vec::new(),
permission_overrides: Vec::new(),
- extra_http_headers: Vec::new(),
+ extra_http_headers: Default::default(),
script_execution_disabled: false,
bypass_content_security_policy: false,
emulated_media: Default::default(),
@@ -1649,7 +1649,7 @@ async fn prepared_streaming_xml_document_waits_for_permit_and_uses_latest_config
grant_universal_access: false,
}],
permission_overrides: Vec::new(),
- extra_http_headers: Vec::new(),
+ extra_http_headers: Default::default(),
script_execution_disabled: false,
bypass_content_security_policy: false,
emulated_media: Default::default(),
@@ -2576,7 +2576,7 @@ globalThis.__preparedCommitObserved = JSON.stringify([
origin: None,
embedded_origin: None,
}],
- extra_http_headers: Vec::new(),
+ extra_http_headers: Default::default(),
script_execution_disabled: false,
bypass_content_security_policy: false,
emulated_media: Default::default(),
diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rs
index 2265a42cc..e0830df64 100644
--- a/moli-renderer-v8/src/script_vm.rs
+++ b/moli-renderer-v8/src/script_vm.rs
@@ -4926,7 +4926,7 @@ impl ScriptVm {
self.document_runtime.clear_cookie_store();
}
- pub(super) fn set_extra_http_headers(&mut self, headers: &[(String, String)]) {
+ pub(super) fn set_extra_http_headers(&mut self, headers: &moli_fetch::RequestHeaders) {
self._context_host
.borrow_mut()
.set_extra_http_headers(headers);
diff --git a/moli-renderer-v8/src/script_vm/app_manifest.rs b/moli-renderer-v8/src/script_vm/app_manifest.rs
index 0483607d8..463f1e9d0 100644
--- a/moli-renderer-v8/src/script_vm/app_manifest.rs
+++ b/moli-renderer-v8/src/script_vm/app_manifest.rs
@@ -62,7 +62,7 @@ impl ScriptVm {
} else {
RequestCredentialsMode::Omit
};
- let request_headers = host.extra_http_headers().to_vec();
+ let request_headers = host.extra_http_headers().clone();
let cancel_handle = FetchCancelHandle::new();
let Some(load) = resource_loader.register_load(
ResourceLoadKind::Manifest,
@@ -92,7 +92,7 @@ impl ScriptVm {
),
RendererAppManifestNetworkObservation::new(
self.root_frame_id.clone(),
- moli_fetch::RequestHeaders::from_utf8(request_headers),
+ request_headers,
credentials_mode,
load,
cancel_handle,
diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch.rs b/moli-renderer-v8/src/script_vm/subresource_fetch.rs
index 00ecb51e9..c0ddce0d4 100644
--- a/moli-renderer-v8/src/script_vm/subresource_fetch.rs
+++ b/moli-renderer-v8/src/script_vm/subresource_fetch.rs
@@ -1106,16 +1106,31 @@ impl ScriptVm {
url: Option<Url>,
method: Option<String>,
body: Option<Option<String>>,
- headers: Option<moli_fetch::RequestHeaders>,
+ headers: Option<moli_fetch::RequestHeaderOverride>,
intercept_response: bool,
handle_auth_requests: bool,
) -> Result<AsyncSubresourceCommandExecution<PendingSubresourceContinueOutcome>> {
- let pending = self
+ let mut pending = self
._context_host
.borrow_mut()
.take_pending_subresource_fetch(internal_id)
.ok_or_else(|| anyhow!("unknown pending subresource fetch `{internal_id}`"))?;
+ let headers = headers.map(|override_headers| match override_headers {
+ moli_fetch::RequestHeaderOverride::CurrentRequest {
+ headers,
+ redirect_headers,
+ } => {
+ pending.redirect_headers =
+ Some(redirect_headers.unwrap_or_else(|| pending.info.request_headers.clone()));
+ headers
+ }
+ moli_fetch::RequestHeaderOverride::RedirectChain(headers) => {
+ pending.redirect_headers = None;
+ headers
+ }
+ });
let PendingSubresourceFetchState {
+ redirect_headers,
request_origin,
info,
load,
@@ -1158,6 +1173,7 @@ impl ScriptVm {
let continued = self.continue_worker_owned_fetch(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: redirect_headers.clone(),
fetch_id,
internal_id,
network_request_handle: info.network_request_handle,
@@ -1179,6 +1195,7 @@ impl ScriptVm {
.record_in_flight_worker_subresource_fetch(
crate::types::InFlightWorkerSubresourceFetchState {
pending: PendingSubresourceFetchState {
+ redirect_headers: redirect_headers.clone(),
request_origin,
info,
load,
@@ -1213,6 +1230,7 @@ impl ScriptVm {
let continued = self.continue_worker_owned_xhr(
target,
crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: redirect_headers.clone(),
xhr_id,
internal_id,
network_request_handle: info.network_request_handle,
@@ -1234,6 +1252,7 @@ impl ScriptVm {
.record_in_flight_worker_subresource_fetch(
crate::types::InFlightWorkerSubresourceFetchState {
pending: PendingSubresourceFetchState {
+ redirect_headers: redirect_headers.clone(),
request_origin,
info,
load,
@@ -1268,6 +1287,7 @@ impl ScriptVm {
let continued = self.continue_worker_owned_csp_report(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: redirect_headers.clone(),
fetch_id: report_id,
internal_id,
network_request_handle: info.network_request_handle,
@@ -1298,6 +1318,7 @@ impl ScriptVm {
let request_body = body.unwrap_or_else(|| info.request_body.clone());
let request_headers = headers.unwrap_or_else(|| info.request_headers.clone());
let pending = PendingSubresourceFetchState {
+ redirect_headers: redirect_headers.clone(),
request_origin,
info,
load,
@@ -1345,6 +1366,7 @@ impl ScriptVm {
);
}
continuation => PendingSubresourceFetchState {
+ redirect_headers: redirect_headers.clone(),
request_origin,
info,
load,
@@ -1415,6 +1437,7 @@ impl ScriptVm {
request_body.clone(),
request_headers.clone(),
)?
+ .with_redirect_headers(pending.redirect_headers.clone())
.with_initiator_url(&pending.info.document_url)
.with_request_origin(pending.request_origin.clone())
.with_request_mode(pending.request_mode)
@@ -1605,7 +1628,7 @@ impl ScriptVm {
request_body,
intercept_response,
initial_network_request_headers,
- response: _,
+ response,
} = pending;
if let Some(target) = WorkerOwnedFetchTarget::from_continuation(&pending_fetch.continuation)
{
@@ -1613,6 +1636,7 @@ impl ScriptVm {
let continued = self.continue_worker_owned_fetch(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: pending_fetch.redirect_headers.clone(),
fetch_id: target.fetch_id(),
internal_id,
network_request_handle: pending_fetch.info.network_request_handle,
@@ -1648,6 +1672,7 @@ impl ScriptVm {
let continued = self.continue_worker_owned_xhr(
target,
crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: pending_fetch.redirect_headers.clone(),
xhr_id: target.xhr_id(),
internal_id,
network_request_handle: pending_fetch.info.network_request_handle,
@@ -1685,12 +1710,24 @@ impl ScriptVm {
request_body.clone(),
original_request_headers.clone(),
)?
+ .with_redirect_headers(pending_fetch.redirect_headers.clone())
.with_initiator_url(&pending_fetch.info.document_url)
.with_request_origin(pending_fetch.request_origin.clone())
.with_request_mode(pending_fetch.request_mode)
.with_credentials_mode(pending_fetch.credentials_mode)
.with_auth(auth.into())
.with_subframe_context(pending_fetch.info.frame_id.is_some());
+ for redirect in &response.redirect_chain {
+ request.apply_redirect_status(redirect.status);
+ }
+ request.url = response.final_url.clone();
+ request = request.with_redirect_chain(
+ response
+ .redirect_chain
+ .into_iter()
+ .map(Into::into)
+ .collect(),
+ );
request = match pending_fetch.info.resource_type {
SubresourceResourceType::Script
| SubresourceResourceType::Stylesheet
@@ -1796,6 +1833,7 @@ impl ScriptVm {
let failed = self.fail_worker_owned_fetch_auth(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.fetch_id(),
internal_id,
network_request_handle: pending.pending.info.network_request_handle,
@@ -1828,6 +1866,7 @@ impl ScriptVm {
let failed = self.fail_worker_owned_xhr_auth(
target,
crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: None,
xhr_id: target.xhr_id(),
internal_id,
network_request_handle: pending.pending.info.network_request_handle,
@@ -1933,6 +1972,7 @@ impl ScriptVm {
.take_pending_subresource_fetch(internal_id)
.ok_or_else(|| anyhow!("unknown pending subresource fetch `{internal_id}`"))?;
let PendingSubresourceFetchState {
+ redirect_headers: _,
request_origin,
info,
load,
@@ -1959,6 +1999,7 @@ impl ScriptVm {
let failed = self.fail_worker_owned_fetch(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.fetch_id(),
internal_id: 0,
network_request_handle: info.network_request_handle,
@@ -2003,6 +2044,7 @@ impl ScriptVm {
let failed = self.fail_worker_owned_xhr(
target,
crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: None,
xhr_id: target.xhr_id(),
internal_id: 0,
network_request_handle: info.network_request_handle,
@@ -2047,6 +2089,7 @@ impl ScriptVm {
let failed = self.fail_worker_owned_csp_report(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.report_id(),
internal_id: 0,
network_request_handle: info.network_request_handle,
@@ -2085,6 +2128,7 @@ impl ScriptVm {
return Ok(AsyncSubresourceCommandExecution::without_window_realm(()));
}
continuation => PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -2126,6 +2170,7 @@ impl ScriptVm {
.take_pending_subresource_fetch(internal_id)
.ok_or_else(|| anyhow!("unknown pending subresource fetch `{internal_id}`"))?;
let PendingSubresourceFetchState {
+ redirect_headers: _,
request_origin,
info,
load,
@@ -2201,6 +2246,7 @@ impl ScriptVm {
let fulfilled = self.fulfill_worker_owned_fetch(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.fetch_id(),
internal_id: 0,
network_request_handle: info.network_request_handle,
@@ -2249,6 +2295,7 @@ impl ScriptVm {
let failed = self.fail_worker_owned_fetch(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.fetch_id(),
internal_id: 0,
network_request_handle: info.network_request_handle,
@@ -2306,6 +2353,7 @@ impl ScriptVm {
let fulfilled = self.fulfill_worker_owned_xhr(
target,
crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: None,
xhr_id: target.xhr_id(),
internal_id: 0,
network_request_handle: info.network_request_handle,
@@ -2354,6 +2402,7 @@ impl ScriptVm {
let failed = self.fail_worker_owned_xhr(
target,
crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: None,
xhr_id: target.xhr_id(),
internal_id: 0,
network_request_handle: info.network_request_handle,
@@ -2404,6 +2453,7 @@ impl ScriptVm {
let fulfilled = self.fulfill_worker_owned_csp_report(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.report_id(),
internal_id: 0,
network_request_handle: info.network_request_handle,
@@ -2450,6 +2500,7 @@ impl ScriptVm {
return Ok(AsyncSubresourceCommandExecution::without_window_realm(()));
}
continuation => PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin,
info,
load,
@@ -2620,6 +2671,7 @@ impl ScriptVm {
&result,
);
let request = crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.fetch_id(),
internal_id,
network_request_handle: pending.pending.info.network_request_handle,
@@ -2680,6 +2732,7 @@ impl ScriptVm {
&result,
);
let request = crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: None,
xhr_id: target.xhr_id(),
internal_id,
network_request_handle: pending.pending.info.network_request_handle,
@@ -2769,6 +2822,7 @@ impl ScriptVm {
let failed = self.fail_worker_owned_fetch_response(
target,
crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.fetch_id(),
internal_id,
network_request_handle: pending.pending.info.network_request_handle,
@@ -2799,6 +2853,7 @@ impl ScriptVm {
&result,
);
let request = crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: None,
xhr_id: target.xhr_id(),
internal_id,
network_request_handle: pending.pending.info.network_request_handle,
@@ -2907,6 +2962,7 @@ impl ScriptVm {
&result,
);
let request = crate::worker::WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id: target.fetch_id(),
internal_id,
network_request_handle: pending.pending.info.network_request_handle,
@@ -2969,6 +3025,7 @@ impl ScriptVm {
&result,
);
let request = crate::worker::WorkerPendingXhrContinue {
+ redirect_headers: None,
xhr_id: target.xhr_id(),
internal_id,
network_request_handle: pending.pending.info.network_request_handle,
@@ -3035,7 +3092,7 @@ impl ScriptVm {
url: Option<Url>,
method: Option<String>,
body: Option<Option<String>>,
- headers: Option<moli_fetch::RequestHeaders>,
+ headers: Option<moli_fetch::RequestHeaderOverride>,
intercept_response: bool,
handle_auth_requests: bool,
) -> Result<PendingSubresourceContinueOutcome> {
@@ -4082,12 +4139,26 @@ impl ScriptVm {
&& let Some(challenge) =
crate::network_host::extract_subresource_auth_challenge(&response.headers)
{
+ // Keep the original request for Network event replay; only the
+ // challenged hop is used by Fetch.authRequired and auth retries.
+ let mut challenged_request = moli_fetch::Request::new(
+ &request_method,
+ request_url.as_str(),
+ request_body.clone(),
+ request_headers.clone(),
+ )?
+ .with_redirect_headers(pending.redirect_headers.clone());
+ for redirect in &response.redirect_chain {
+ challenged_request.apply_redirect_status(redirect.status);
+ }
+ let challenged_body =
+ challenged_request.body.as_ref().and(request_body.clone());
let info = PendingSubresourceAuthInfo {
internal_id,
- url: request_url.clone(),
- method: request_method.clone(),
- request_headers: request_headers.clone(),
- request_body: request_body.clone(),
+ url: response.final_url.clone(),
+ method: challenged_request.method,
+ request_headers: challenged_request.request_headers,
+ request_body: challenged_body,
resource_type,
request_cookie_report: response.request_cookie_report.clone(),
network_request_headers: response
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/images.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/images.rs
index 5b304cfaa..68a13102b 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/images.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/images.rs
@@ -791,7 +791,9 @@ async fn image_request_interception_fulfills_through_image_continuation() {
"https://example.test/page.html",
&loader,
);
- vm.set_extra_http_headers(&[("X-Image-Request".to_owned(), "intercepted".to_owned())]);
+ vm.set_extra_http_headers(
+ &vec![("X-Image-Request".to_owned(), "intercepted".to_owned())].into(),
+ );
vm.set_fetch_subresource_interception(true, Some(crate::types::SubresourceResourceType::Image));
vm.eval(
r#"
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
index d23df0672..620cee54b 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
@@ -2018,6 +2018,7 @@ async fn streaming_subresource_finish_preserves_response_head_cache_state() {
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
response_filter: None,
pending: super::PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&document_url),
info: crate::types::PendingSubresourceFetchInfo {
internal_id,
@@ -2167,6 +2168,7 @@ async fn async_subresource_failure_network_error_override_preserves_fetch_reject
context_host.borrow_mut().restore_pending_subresource_fetch(
super::PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&document_url),
info: crate::types::PendingSubresourceFetchInfo {
internal_id,
@@ -2302,6 +2304,7 @@ async fn streaming_fetch_body_error_records_response_started_then_body_failed()
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
response_filter: None,
pending: super::PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(
&(Url::parse("https://streaming-fetch-body-error.test/")
.expect("document URL should parse")),
@@ -2462,6 +2465,7 @@ fn install_streaming_fetch_response_fixture(
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
response_filter: None,
pending: super::PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(&document_url),
info: crate::types::PendingSubresourceFetchInfo {
internal_id,
@@ -2564,6 +2568,7 @@ async fn streaming_fetch_body_cancel_aborts_streaming_subresource() {
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
response_filter: None,
pending: super::PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(
&(Url::parse("https://streaming-fetch-body-cancel.test/")
.expect("document URL should parse")),
@@ -3192,6 +3197,7 @@ async fn streaming_xhr_materialization_failure_errors_body_source_before_close()
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
response_filter: None,
pending: super::PendingSubresourceFetchState {
+ redirect_headers: None,
request_origin: moli_url::WebOrigin::from_url(
&(Url::parse("https://xhr-streaming-materialize-error.test/")
.expect("document URL should parse")),
diff --git a/moli-renderer-v8/src/shared_worker_runtime/resource_routing.rs b/moli-renderer-v8/src/shared_worker_runtime/resource_routing.rs
index 860f1779e..90b653e36 100644
--- a/moli-renderer-v8/src/shared_worker_runtime/resource_routing.rs
+++ b/moli-renderer-v8/src/shared_worker_runtime/resource_routing.rs
@@ -474,6 +474,7 @@ mod tests {
fn pending_fetch(fetch_id: u32) -> WorkerPendingFetchContinue {
WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id,
internal_id: u64::from(fetch_id),
network_request_handle: None,
diff --git a/moli-renderer-v8/src/types.rs b/moli-renderer-v8/src/types.rs
index 07090a9f1..e1546efb6 100644
--- a/moli-renderer-v8/src/types.rs
+++ b/moli-renderer-v8/src/types.rs
@@ -505,6 +505,7 @@ impl PendingSubresourceExecutionContext {
}
pub(super) struct PendingSubresourceFetchState {
+ pub(super) redirect_headers: Option<moli_fetch::RequestHeaders>,
pub(super) request_origin: moli_url::WebOrigin,
pub(super) info: PendingSubresourceFetchInfo,
pub(super) load: crate::network::loads::ResourceLoadLease,
diff --git a/moli-renderer-v8/src/worker/global_scope/content_security_policy.rs b/moli-renderer-v8/src/worker/global_scope/content_security_policy.rs
index 5d61d25b1..6ff817a54 100644
--- a/moli-renderer-v8/src/worker/global_scope/content_security_policy.rs
+++ b/moli-renderer-v8/src/worker/global_scope/content_security_policy.rs
@@ -549,6 +549,7 @@ pub(in crate::worker) fn continue_pending_worker_csp_report(
continuation.headers.clone(),
) {
Ok(request) => request
+ .with_redirect_headers(continuation.redirect_headers)
.with_initiator_url(&pending.document_url)
.with_request_origin(moli_url::WebOrigin::from_url(&pending.document_url))
.with_resource_type(RequestResourceType::CspReport)
diff --git a/moli-renderer-v8/src/worker/global_scope/fetch.rs b/moli-renderer-v8/src/worker/global_scope/fetch.rs
index 893d23fae..56df94812 100644
--- a/moli-renderer-v8/src/worker/global_scope/fetch.rs
+++ b/moli-renderer-v8/src/worker/global_scope/fetch.rs
@@ -158,6 +158,8 @@ pub(in crate::worker) fn spawn_worker_fetch_network(
auth: Option<crate::protocol_types::SubresourceAuthCredentials>,
suppress_default_content_type: bool,
allow_headers_first: bool,
+ redirect_headers: Option<moli_fetch::RequestHeaders>,
+ redirect_chain: Vec<moli_fetch::RedirectInfo>,
) {
tokio::task::spawn_local(async move {
let loader = load.request_client();
@@ -189,6 +191,8 @@ pub(in crate::worker) fn spawn_worker_fetch_network(
) {
Ok(request) => {
let mut request = request
+ .with_redirect_headers(redirect_headers)
+ .with_redirect_chain(redirect_chain)
.with_initiator_url(&document_url)
.with_request_mode(request_mode)
.with_credentials_mode(credentials_mode)
@@ -463,6 +467,8 @@ fn spawn_worker_fetch_service_worker(
None,
suppress_default_content_type,
true,
+ None,
+ Vec::new(),
),
Ok(ServiceWorkerDirectFetchResult::Response(response)) => {
let _ = completion_tx.send(WorkerFetchEvent::Completion(Box::new(
@@ -511,6 +517,8 @@ pub(in crate::worker) fn spawn_worker_xhr_network(
headers: moli_fetch::RequestHeaders,
credentials_mode: RequestCredentialsMode,
auth: Option<crate::protocol_types::SubresourceAuthCredentials>,
+ redirect_headers: Option<moli_fetch::RequestHeaders>,
+ redirect_chain: Vec<moli_fetch::RedirectInfo>,
) {
tokio::task::spawn_local(async move {
let loader = load.request_client();
@@ -524,6 +532,8 @@ pub(in crate::worker) fn spawn_worker_xhr_network(
)
.map(|request| {
let mut request = request
+ .with_redirect_headers(redirect_headers)
+ .with_redirect_chain(redirect_chain)
.with_initiator_url(&document_url)
.with_credentials_mode(credentials_mode)
.with_network_partition_key(network_partition_key.clone())
@@ -613,6 +623,29 @@ pub(in crate::worker) fn continue_pending_worker_fetch(
state: &Rc<RefCell<WorkerGlobalState>>,
request: WorkerPendingFetchContinue,
) {
+ let redirect_chain = if request.auth.is_some() {
+ state
+ .borrow()
+ .pending_fetches
+ .get(&request.fetch_id)
+ .and_then(|pending| pending.paused_response.as_ref())
+ .map(|response| response.head.redirect_chain.clone())
+ .unwrap_or_default()
+ } else {
+ Vec::new()
+ };
+ let redirect_headers = if !redirect_chain.is_empty() {
+ None
+ } else if request.auth.is_some() {
+ state
+ .borrow()
+ .pending_fetches
+ .get(&request.fetch_id)
+ .and_then(|pending| pending.network_record.as_ref())
+ .and_then(|record| record.redirect_headers.clone())
+ } else {
+ request.redirect_headers.clone()
+ };
let allow_headers_first = !request.intercept_response && !request.handle_auth_requests;
let (
load,
@@ -645,16 +678,21 @@ pub(in crate::worker) fn continue_pending_worker_fetch(
.network_record
.as_ref()
.and_then(|record| record.initial_network_request_headers.clone());
- pending.network_record = Some(PendingWorkerFetchNetworkRecord {
- internal_id: request.internal_id,
- url: request.url.clone(),
- method: request.method.clone(),
- request_headers: request.headers.clone(),
- request_body: request.body.clone(),
- initial_network_request_headers,
- intercept_response: request.intercept_response,
- handle_auth_requests: request.handle_auth_requests,
- });
+ // Authentication retries use the challenged URL, but Network events
+ // still describe the original request and its complete redirect chain.
+ if request.auth.is_none() || pending.network_record.is_none() {
+ pending.network_record = Some(PendingWorkerFetchNetworkRecord {
+ redirect_headers: redirect_headers.clone(),
+ internal_id: request.internal_id,
+ url: request.url.clone(),
+ method: request.method.clone(),
+ request_headers: request.headers.clone(),
+ request_body: request.body.clone(),
+ initial_network_request_headers,
+ intercept_response: request.intercept_response,
+ handle_auth_requests: request.handle_auth_requests,
+ });
+ }
(
pending.load.clone(),
completion_tx,
@@ -695,6 +733,8 @@ pub(in crate::worker) fn continue_pending_worker_fetch(
auth,
false,
allow_headers_first,
+ redirect_headers,
+ redirect_chain,
);
}
@@ -712,6 +752,7 @@ pub(in crate::worker) fn fail_pending_worker_fetch(
{
pending.network_request_handle = request.network_request_handle;
pending.network_record = Some(PendingWorkerFetchNetworkRecord {
+ redirect_headers: None,
internal_id: request.internal_id,
url: request.url,
method: request.method,
@@ -775,6 +816,7 @@ pub(in crate::worker) fn fulfill_pending_worker_fetch(
pending.network_request_handle = request.network_request_handle;
if request.internal_id != 0 {
pending.network_record = Some(PendingWorkerFetchNetworkRecord {
+ redirect_headers: None,
internal_id: request.internal_id,
url: request.url.clone(),
method: request.method.clone(),
@@ -905,6 +947,29 @@ pub(in crate::worker) fn continue_pending_worker_xhr(
state: &Rc<RefCell<WorkerGlobalState>>,
request: WorkerPendingXhrContinue,
) {
+ let redirect_chain = if request.auth.is_some() {
+ state
+ .borrow()
+ .pending_xhrs
+ .get(&request.xhr_id)
+ .and_then(|pending| pending.paused_response.as_ref())
+ .map(|response| response.head.redirect_chain.clone())
+ .unwrap_or_default()
+ } else {
+ Vec::new()
+ };
+ let redirect_headers = if !redirect_chain.is_empty() {
+ None
+ } else if request.auth.is_some() {
+ state
+ .borrow()
+ .pending_xhrs
+ .get(&request.xhr_id)
+ .and_then(|pending| pending.network_record.as_ref())
+ .and_then(|record| record.redirect_headers.clone())
+ } else {
+ request.redirect_headers.clone()
+ };
let (
load,
completion_tx,
@@ -933,16 +998,21 @@ pub(in crate::worker) fn continue_pending_worker_xhr(
.network_record
.as_ref()
.and_then(|record| record.initial_network_request_headers.clone());
- pending.network_record = Some(PendingWorkerFetchNetworkRecord {
- internal_id: request.internal_id,
- url: request.url.clone(),
- method: request.method.clone(),
- request_headers: request.headers.clone(),
- request_body: request.body.clone(),
- initial_network_request_headers,
- intercept_response: request.intercept_response,
- handle_auth_requests: request.handle_auth_requests,
- });
+ // Authentication retries use the challenged URL, but Network events
+ // still describe the original request and its complete redirect chain.
+ if request.auth.is_none() || pending.network_record.is_none() {
+ pending.network_record = Some(PendingWorkerFetchNetworkRecord {
+ redirect_headers: redirect_headers.clone(),
+ internal_id: request.internal_id,
+ url: request.url.clone(),
+ method: request.method.clone(),
+ request_headers: request.headers.clone(),
+ request_body: request.body.clone(),
+ initial_network_request_headers,
+ intercept_response: request.intercept_response,
+ handle_auth_requests: request.handle_auth_requests,
+ });
+ }
(
pending.load.clone(),
completion_tx,
@@ -973,6 +1043,8 @@ pub(in crate::worker) fn continue_pending_worker_xhr(
headers,
credentials_mode,
auth,
+ redirect_headers,
+ redirect_chain,
);
}
@@ -991,6 +1063,7 @@ pub(in crate::worker) fn fail_pending_worker_xhr(
pending.request_paused = false;
pending.network_request_handle = request.network_request_handle;
pending.network_record = Some(PendingWorkerFetchNetworkRecord {
+ redirect_headers: None,
internal_id: request.internal_id,
url: request.url,
method: request.method,
@@ -1052,6 +1125,7 @@ pub(in crate::worker) fn fulfill_pending_worker_xhr(
pending.network_request_handle = request.network_request_handle;
if request.internal_id != 0 {
pending.network_record = Some(PendingWorkerFetchNetworkRecord {
+ redirect_headers: None,
internal_id: request.internal_id,
url: request.url.clone(),
method: request.method.clone(),
@@ -1296,7 +1370,7 @@ pub(crate) fn register_worker_websocket<'s>(
.effective_browser_identity()
.user_agent()
.to_owned(),
- extra_headers: moli_fetch::RequestHeaders::from_utf8(extra_http_headers),
+ extra_headers: extra_http_headers,
http_proxy: loader.request_client().http_proxy().map(ToOwned::to_owned),
http_no_proxy: loader
.request_client()
@@ -2296,6 +2370,8 @@ pub(in crate::worker) fn worker_fetch_callback<'s>(
None,
suppress_default_content_type,
true,
+ None,
+ Vec::new(),
);
}
@@ -2747,12 +2823,13 @@ pub(in crate::worker) fn drain_worker_fetch_completion_result(
let Some(pending) = state_ref.pending_fetches.get_mut(&completion.fetch_id) else {
return;
};
- pending.network_record.clone().and_then(|record| {
+ pending.network_record.clone().and_then(|mut record| {
if record.handle_auth_requests
&& matches!(response_head.status, 401 | 407)
&& let Some(challenge) =
extract_subresource_auth_challenge(&response_head.headers)
{
+ record.follow_redirects(&response_head);
let response_body = response.subresource_response_body();
pending.paused_response = Some(PausedWorkerSubresourceResponse {
head: response_head.clone(),
diff --git a/moli-renderer-v8/src/worker/global_scope/mod.rs b/moli-renderer-v8/src/worker/global_scope/mod.rs
index bc4dc9732..e2dcf7d44 100644
--- a/moli-renderer-v8/src/worker/global_scope/mod.rs
+++ b/moli-renderer-v8/src/worker/global_scope/mod.rs
@@ -1193,6 +1193,7 @@ enum WorkerFetchResponseParts {
#[derive(Clone)]
pub(super) struct PendingWorkerFetchNetworkRecord {
+ pub(crate) redirect_headers: Option<moli_fetch::RequestHeaders>,
pub(super) internal_id: u64,
pub(super) url: Url,
pub(super) method: String,
@@ -1203,6 +1204,28 @@ pub(super) struct PendingWorkerFetchNetworkRecord {
pub(super) handle_auth_requests: bool,
}
+impl PendingWorkerFetchNetworkRecord {
+ fn follow_redirects(&mut self, head: &ResponseHead) {
+ if head.redirect_chain.is_empty() {
+ return;
+ }
+ let mut request = Request::get_with_url(self.url.clone())
+ .with_redirect_headers(self.redirect_headers.take());
+ request.method = std::mem::take(&mut self.method);
+ request.body = self.request_body.clone().map(String::into_bytes);
+ request.request_headers = std::mem::take(&mut self.request_headers);
+ for redirect in &head.redirect_chain {
+ request.apply_redirect_status(redirect.status);
+ }
+ self.url = head.final_url.clone();
+ self.method = request.method;
+ self.request_headers = request.request_headers;
+ if request.body.is_none() {
+ self.request_body = None;
+ }
+ }
+}
+
pub(super) struct PendingWorkerXhr {
pub(super) xhr: v8::Global<v8::Object>,
pub(super) document_url: Url,
@@ -1519,7 +1542,7 @@ pub(crate) struct WorkerGlobalState {
/// Whether this worker global is a secure context for `[SecureContext]` APIs.
pub(super) secure_context: bool,
/// Network.setExtraHTTPHeaders headers inherited from the owning page/CDP session.
- pub(super) extra_http_headers: Vec<(String, String)>,
+ pub(super) extra_http_headers: moli_fetch::RequestHeaders,
/// Permission overrides inherited from the owning page/CDP session.
pub(super) permission_overrides: Vec<crate::protocol_types::PermissionOverrideRegistration>,
/// Network.emulateNetworkConditions offline state inherited from the owning page/CDP session.
@@ -6583,10 +6606,10 @@ fn worker_url_blocked(patterns: &[String], url: &Url) -> bool {
}
fn merge_worker_request_headers(
- context_headers: &[(String, String)],
+ context_headers: &moli_fetch::RequestHeaders,
request_headers: &moli_fetch::RequestHeaders,
) -> moli_fetch::RequestHeaders {
- let mut headers = moli_fetch::RequestHeaders::from_utf8(context_headers.to_vec());
+ let mut headers = context_headers.clone();
headers.overlay(request_headers.clone());
headers
}
diff --git a/moli-renderer-v8/src/worker/global_scope/xhr.rs b/moli-renderer-v8/src/worker/global_scope/xhr.rs
index 1f8cf87ca..8735918d3 100644
--- a/moli-renderer-v8/src/worker/global_scope/xhr.rs
+++ b/moli-renderer-v8/src/worker/global_scope/xhr.rs
@@ -549,6 +549,8 @@ pub(crate) fn try_worker_xhr_send_callback<'s>(
prepared.request_headers,
prepared.credentials_mode,
None,
+ None,
+ Vec::new(),
);
true
@@ -1008,12 +1010,13 @@ pub(in crate::worker) fn drain_worker_xhr_completion(
return;
};
let response_head = response.head();
- pending.network_record.clone().and_then(|record| {
+ pending.network_record.clone().and_then(|mut record| {
if record.handle_auth_requests
&& matches!(response_head.status, 401 | 407)
&& let Some(challenge) =
extract_subresource_auth_challenge(&response_head.headers)
{
+ record.follow_redirects(&response_head);
let response_body = response.subresource_response_body();
pending.paused_response = Some(PausedWorkerSubresourceResponse {
head: response_head.clone(),
diff --git a/moli-renderer-v8/src/worker/handle.rs b/moli-renderer-v8/src/worker/handle.rs
index 1f5c519ec..91cf5b366 100644
--- a/moli-renderer-v8/src/worker/handle.rs
+++ b/moli-renderer-v8/src/worker/handle.rs
@@ -154,7 +154,7 @@ pub(crate) enum WorkerMessage {
response_tx: oneshot::Sender<Result<WorkerResourceOwnerSlotDiagnostics, String>>,
},
/// Update the Network.setExtraHTTPHeaders state visible to worker-owned fetch/XHR.
- SetExtraHttpHeaders(Vec<(String, String)>),
+ SetExtraHttpHeaders(moli_fetch::RequestHeaders),
/// Update the Network.emulateNetworkConditions offline state visible to worker-owned fetch/XHR.
SetNetworkOffline(bool),
/// Update the effective Network.setCacheDisabled state visible to worker-owned fetch/XHR.
@@ -561,6 +561,7 @@ pub(crate) struct WorkerPendingSubresourceFetch {
#[derive(Debug, Clone)]
pub(crate) struct WorkerPendingFetchContinue {
+ pub(crate) redirect_headers: Option<moli_fetch::RequestHeaders>,
pub(crate) fetch_id: u32,
pub(crate) internal_id: u64,
pub(crate) network_request_handle: Option<SubresourceNetworkRequestHandle>,
@@ -575,6 +576,7 @@ pub(crate) struct WorkerPendingFetchContinue {
#[derive(Debug, Clone)]
pub(crate) struct WorkerPendingXhrContinue {
+ pub(crate) redirect_headers: Option<moli_fetch::RequestHeaders>,
pub(crate) xhr_id: u32,
pub(crate) internal_id: u64,
pub(crate) network_request_handle: Option<SubresourceNetworkRequestHandle>,
@@ -644,7 +646,7 @@ pub(crate) struct WorkerNetworkPolicy {
/// blob/data workers inherit it from the creator context instead.
pub(crate) secure_context: bool,
pub(crate) permission_overrides: Vec<crate::protocol_types::PermissionOverrideRegistration>,
- pub(crate) extra_http_headers: Vec<(String, String)>,
+ pub(crate) extra_http_headers: moli_fetch::RequestHeaders,
pub(crate) network_offline: bool,
pub(crate) blocked_url_patterns: Vec<String>,
pub(crate) network_partition_key: Option<String>,
@@ -885,10 +887,10 @@ impl WorkerHandle {
self.devtools.clone()
}
- pub(crate) fn set_extra_http_headers(&self, headers: &[(String, String)]) {
+ pub(crate) fn set_extra_http_headers(&self, headers: &moli_fetch::RequestHeaders) {
let _ = self
.tx
- .send(WorkerMessage::SetExtraHttpHeaders(headers.to_vec()));
+ .send(WorkerMessage::SetExtraHttpHeaders(headers.clone()));
}
pub(crate) fn set_network_offline(&self, offline: bool) {
diff --git a/moli-renderer-v8/src/worker/thread/tests/mod.rs b/moli-renderer-v8/src/worker/thread/tests/mod.rs
index a7159df6b..da57d3007 100644
--- a/moli-renderer-v8/src/worker/thread/tests/mod.rs
+++ b/moli-renderer-v8/src/worker/thread/tests/mod.rs
@@ -63,9 +63,9 @@ const TIMEOUT: Duration = Duration::from_secs(5);
#[tokio::test(flavor = "current_thread")]
async fn worker_loader_shares_backend_without_sharing_page_policy() {
let creator = ResourceRequestClient::new(&FetchConfig::default()).expect("creator loader");
- creator.set_extra_http_headers(&[("x-owner".to_owned(), "page".to_owned())]);
+ creator.set_extra_http_headers(&vec![("x-owner".to_owned(), "page".to_owned())].into());
let policy = WorkerNetworkPolicy {
- extra_http_headers: vec![("x-owner".to_owned(), "worker".to_owned())],
+ extra_http_headers: vec![("x-owner".to_owned(), "worker".to_owned())].into(),
network_offline: true,
..WorkerNetworkPolicy::default()
};
@@ -847,6 +847,7 @@ fn pending_worker_fetch_continue(
intercept_response: bool,
) -> WorkerPendingFetchContinue {
WorkerPendingFetchContinue {
+ redirect_headers: None,
fetch_id,
internal_id,
network_request_handle: Some(owner_assigned_request_handle(internal_id)),
@@ -867,6 +868,7 @@ fn pending_worker_xhr_continue(
intercept_response: bool,
) -> WorkerPendingXhrContinue {
WorkerPendingXhrContinue {
+ redirect_headers: None,
xhr_id,
internal_id,
network_request_handle: Some(owner_assigned_request_handle(internal_id)),
diff --git a/moli-renderer-v8/src/worker/thread/tests/network.rs b/moli-renderer-v8/src/worker/thread/tests/network.rs
index b90ba1add..bebf48c01 100644
--- a/moli-renderer-v8/src/worker/thread/tests/network.rs
+++ b/moli-renderer-v8/src/worker/thread/tests/network.rs
@@ -3884,7 +3884,7 @@ async fn worker_fetch_applies_network_policy_extra_http_headers() {
format!("http://{addr}/worker/main.js"),
loader,
WorkerNetworkPolicy {
- extra_http_headers: vec![("x-cdp-test".to_owned(), "works-worker".to_owned())],
+ extra_http_headers: vec![("x-cdp-test".to_owned(), "works-worker".to_owned())].into(),
..WorkerNetworkPolicy::default()
},
);
@@ -4527,7 +4527,8 @@ async fn worker_importscripts_applies_loader_network_policy_extra_http_headers()
extra_http_headers: vec![(
"x-cdp-test".to_owned(),
"works-worker-importscripts".to_owned(),
- )],
+ )]
+ .into(),
..WorkerNetworkPolicy::default()
},
);
@@ -4594,7 +4595,8 @@ async fn module_worker_dependency_applies_loader_network_policy_extra_http_heade
extra_http_headers: vec![(
"x-cdp-test".to_owned(),
"works-module-worker-dependency".to_owned(),
- )],
+ )]
+ .into(),
..WorkerNetworkPolicy::default()
},
);
@@ -6018,7 +6020,7 @@ async fn worker_classic_websocket_applies_network_policy_extra_http_headers() {
"http://127.0.0.1/worker/main.js".into(),
loader,
WorkerNetworkPolicy {
- extra_http_headers: vec![("x-cdp-test".to_owned(), "worker-ws".to_owned())],
+ extra_http_headers: vec![("x-cdp-test".to_owned(), "worker-ws".to_owned())].into(),
..WorkerNetworkPolicy::default()
},
);