servo/components/devtools/actors/long_string.rs
Usman Yahaya Baba 02dca0fb21
net: Send ResponseContentObj to Devtools (#38625)
Currently, the response tab for a request's detail in devtools does not
show the available data, this was due to how the content is being
structured (not the way firefox's devtools client expects it) and also
the body being discarded and not stored in the actor.
This PR stores the body in the actor , which is then retrieved in
`getResponseContent` and then use it to instantiate the new struct
`ResponseContentObj` which matches the format firefox's expects

Fixes: https://github.com/servo/servo/issues/38128

---------

Signed-off-by: uthmaniv <uthmanyahayababa@gmail.com>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Co-authored-by: Josh Matthews <josh@joshmatthews.net>
2025-08-15 08:26:24 +00:00

87 lines
2.4 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 serde::Serialize;
use serde_json::{Map, Value};
use crate::StreamId;
use crate::actor::{Actor, ActorError, ActorRegistry};
use crate::protocol::ClientRequest;
const INITIAL_LENGTH: usize = 500;
pub struct LongStringActor {
name: String,
full_string: String,
}
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LongStringObj {
#[serde(rename = "type")]
type_: String,
actor: String,
length: usize,
initial: String,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SubstringReply {
from: String,
substring: String,
}
impl Actor for LongStringActor {
fn name(&self) -> String {
self.name.clone()
}
fn handle_message(
&self,
request: ClientRequest,
_registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
_id: StreamId,
) -> Result<(), ActorError> {
match msg_type {
"substring" => {
let start = msg.get("start").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
let end = msg
.get("end")
.and_then(|v| v.as_u64())
.unwrap_or(self.full_string.len() as u64) as usize;
let substring: String = self
.full_string
.chars()
.skip(start)
.take(end - start)
.collect();
let reply = SubstringReply {
from: self.name(),
substring,
};
request.reply_final(&reply)?
},
_ => return Err(ActorError::UnrecognizedPacketType),
}
Ok(())
}
}
impl LongStringActor {
pub fn new(registry: &ActorRegistry, full_string: String) -> Self {
let name = registry.new_name("longStringActor");
LongStringActor { name, full_string }
}
pub fn long_string_obj(&self) -> LongStringObj {
LongStringObj {
type_: "longString".to_string(),
actor: self.name.clone(),
length: self.full_string.len(),
initial: self.full_string.chars().take(INITIAL_LENGTH).collect(),
}
}
}