servo/components/devtools/network_handler.rs
Usman Yahaya Baba 152eb63fb3
Remove duplication between request/response properties in NetworkEventActor (#37651)
- Remove request/response fields in `NetworkEventActor` which now stores
minimal request metadata needed : `URL`, `method`, `timestamps`, `raw
headers`, `body`.
- Refactor add_request and add_response: `add_request` takes the
incoming DevtoolsHttpRequest and populates
`request_url`, `request_method`, `request_started`,
`request_time_stamp`, `request_body`, `request_headers_raw`
`request_cookies`, `request_headers`,`total_time` and `event_timing`
(via a new helper that computes Timings)
While `add_response` takes the incoming DevtoolsHttpResponse and
populates `response_headers_raw`, `response_body` ,`response_cookies`,
`response_headers`, `response_start`, `response_content`
- Add a call to `resources_updated` to push initial request info in
`handle_network_event`
Testing: Run and logged servo in devtools mode and now, the request
header info isavailable as soon as the request gets initiated
Fixes: https://github.com/servo/servo/issues/37566

---------

Signed-off-by: Uthman Yahaya Baba <uthmanyahayababa@gmail.com>
2025-06-25 20:18:44 +00:00

81 lines
2.9 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::net::TcpStream;
use std::sync::{Arc, Mutex};
use devtools_traits::NetworkEvent;
use serde::Serialize;
use crate::actor::ActorRegistry;
use crate::actors::browsing_context::BrowsingContextActor;
use crate::actors::network_event::NetworkEventActor;
use crate::resource::{ResourceArrayType, ResourceAvailable};
#[derive(Clone, Serialize)]
pub struct Cause {
#[serde(rename = "type")]
pub type_: String,
#[serde(rename = "loadingDocumentUri")]
pub loading_document_uri: Option<String>,
}
pub(crate) fn handle_network_event(
actors: Arc<Mutex<ActorRegistry>>,
netevent_actor_name: String,
mut connections: Vec<TcpStream>,
network_event: NetworkEvent,
browsing_context_actor_name: String,
) {
let mut actors = actors.lock().unwrap();
match network_event {
NetworkEvent::HttpRequest(httprequest) => {
let (event_actor, resource_updates) = {
let actor = actors.find_mut::<NetworkEventActor>(&netevent_actor_name);
actor.add_request(httprequest);
(actor.event_actor(), actor.resource_updates())
};
let browsing_context_actor =
actors.find::<BrowsingContextActor>(&browsing_context_actor_name);
for stream in &mut connections {
// Notify that a new network event has started
browsing_context_actor.resource_array(
event_actor.clone(),
"network-event".to_string(),
ResourceArrayType::Available,
stream,
);
// Also push initial resource update (request headers, cookies)
browsing_context_actor.resource_array(
resource_updates.clone(),
"network-event".to_string(),
ResourceArrayType::Updated,
stream,
);
}
},
NetworkEvent::HttpResponse(httpresponse) => {
// Scope mutable borrow
let resource = {
let actor = actors.find_mut::<NetworkEventActor>(&netevent_actor_name);
// Store the response information in the actor
actor.add_response(httpresponse);
actor.resource_updates()
};
let browsing_context_actor =
actors.find::<BrowsingContextActor>(&browsing_context_actor_name);
for stream in &mut connections {
browsing_context_actor.resource_array(
resource.clone(),
"network-event".to_string(),
ResourceArrayType::Updated,
stream,
);
}
},
}
}