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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion moli-renderer-v8/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod reading;
mod stream;
pub(crate) use parts::{BlobParts, BlobPropertyBag, required_blob_parts_argument};
use reading::BlobReadMode;
pub(crate) use reading::BlobReadTask;
pub(crate) use reading::{BlobReadTask, stream_blob};

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "Blob")]
Expand Down
2 changes: 1 addition & 1 deletion moli-renderer-v8/src/blob/reading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub(super) fn read_blob<'s>(
Some(promise)
}

pub(super) fn stream_blob<'s>(
pub(crate) fn stream_blob<'s>(
scope: &mut v8::PinScope<'s, '_>,
blob: v8::Local<'s, v8::Object>,
) -> Option<v8::Local<'s, v8::Object>> {
Expand Down
3 changes: 2 additions & 1 deletion moli-renderer-v8/src/network_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub(crate) use self::beacon::{navigator_send_beacon_callback, send_link_audit_pi
pub(super) use self::bindings::install_window_network_bindings;
pub(crate) use self::body::append_default_body_content_type;
pub(in crate::network_host) use self::body::{
PreparedBodyInit, body_init, body_stream_object, readable_body_stream_unusable,
PreparedBodyInit, PreparedBodySource, body_init, body_stream_object,
readable_body_stream_unusable,
};
#[cfg(test)]
pub(crate) use self::body_source::pending_network_body_source_buffered_len_for_test;
Expand Down
15 changes: 11 additions & 4 deletions moli-renderer-v8/src/network_host/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,17 @@ fn navigator_beacon_body<'s>(
data,
webidl::Context::argument("Navigator.sendBeacon", 2),
) {
Ok(body) => Some((
body.as_ref().map(|body| body.bytes.clone()),
body.and_then(|body| body.content_type),
)),
Ok(Some(body)) => {
let bytes = match body.source.into_buffered_bytes(scope) {
Ok(bytes) => bytes,
Err(error) => {
crate::util::throw_type_error(scope, &error.to_string());
return None;
}
};
Some((Some(bytes), body.content_type))
}
Ok(None) => Some((None, None)),
Err(error) => {
crate::util::throw_type_error(scope, &error.to_string());
None
Expand Down
70 changes: 67 additions & 3 deletions moli-renderer-v8/src/network_host/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,63 @@ pub(in crate::network_host) fn readable_body_stream_unusable<'s>(

#[derive(Debug, Clone)]
pub(in crate::network_host) struct PreparedBodyInit {
pub(in crate::network_host) bytes: bytes::Bytes,
pub(in crate::network_host) source: PreparedBodySource,
pub(in crate::network_host) content_type: Option<String>,
}

#[derive(Debug, Clone)]
pub(in crate::network_host) enum PreparedBodySource {
Bytes(bytes::Bytes),
Blob(v8::Global<v8::Object>),
Stream(v8::Global<v8::Object>),
}

impl PreparedBodySource {
pub(in crate::network_host) fn into_buffered_bytes(
self,
scope: &mut v8::PinScope<'_, '_>,
) -> Result<bytes::Bytes, webidl::WebIdlError> {
match self {
Self::Bytes(bytes) => Ok(bytes),
Self::Blob(blob) => {
let blob = v8::Local::new(scope, blob);
blob::blob_data_from_object(scope, blob)
.ok_or_else(|| webidl::WebIdlError::custom_message("Failed to read Blob body"))?
.read_blocking()
.map(bytes::Bytes::from)
.map_err(|_| webidl::WebIdlError::custom_message("Failed to read Blob body"))
}
Self::Stream(_) => Err(webidl::WebIdlError::custom_message(
"Body cannot be buffered synchronously",
)),
}
}

pub(in crate::network_host) fn into_body_stream<'s>(
self,
scope: &mut v8::PinScope<'s, '_>,
owner: v8::Local<'s, v8::Object>,
) -> Option<v8::Local<'s, v8::Object>> {
match self {
Self::Bytes(bytes) => {
let buffer = set_network_body_owned_bytes(scope, owner, bytes)?;
new_readable_stream_from_array_buffer(scope, buffer, buffer.byte_length())
}
Self::Blob(blob) => {
let blob = v8::Local::new(scope, blob);
let stream = blob::stream_blob(scope, blob)?;
body_source::set_network_body_blob_source(scope, owner, blob);
Some(stream)
}
Self::Stream(stream) => Some(v8::Local::new(scope, stream)),
}
}
}

impl PreparedBodyInit {
fn new(bytes: Vec<u8>, content_type: Option<String>) -> Self {
Self {
bytes: bytes.into(),
source: PreparedBodySource::Bytes(bytes.into()),
content_type,
}
}
Expand Down Expand Up @@ -78,11 +127,26 @@ pub(in crate::network_host) fn body_init<'s>(
return Ok(None);
}
if let Ok(object) = v8::Local::<v8::Object>::try_from(value) {
if web_api_interfaces::Blob::is_instance(scope, object) {
let mime_type = blob::blob_mime_type_from_object(scope, object).unwrap_or_default();
return Ok(Some(PreparedBodyInit {
source: PreparedBodySource::Blob(v8::Global::new(scope, object)),
content_type: (!mime_type.is_empty()).then_some(mime_type),
}));
}
if let Some(body) = object_body_init(scope, object)? {
return Ok(Some(body));
}
if web_api_interfaces::ReadableStream::is_instance(scope, object) {
return Ok(Some(PreparedBodyInit::new(Vec::new(), None)));
if readable_body_stream_unusable(scope, object) {
return Err(webidl::WebIdlError::custom_message(
"BodyInit ReadableStream is locked or disturbed",
));
}
return Ok(Some(PreparedBodyInit {
source: PreparedBodySource::Stream(v8::Global::new(scope, object)),
content_type: None,
}));
}
}
if blob::buffer_source_has_shared_or_resizable_backing_store(value) {
Expand Down
50 changes: 47 additions & 3 deletions moli-renderer-v8/src/network_host/body_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ use parking_lot::Mutex;
use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt};

const BODY_SOURCE_KIND_BYTES: &str = "bytes";
const BODY_SOURCE_KIND_BLOB: &str = "blob";
const BODY_SOURCE_BLOB_SLOT: &str = "__lmNetworkBodySourceBlob";
const BODY_SOURCE_KIND_REGISTRY_BYTES: &str = "registry-bytes";
const BODY_SOURCE_KIND_SUBRESOURCE_BODY: &str = "subresource-body";
const BODY_SOURCE_KIND_PENDING_STREAM: &str = "pending-stream";
Expand All @@ -58,6 +60,15 @@ struct OwnedNetworkBodySourceDeclaration<'scope> {
bytes: v8::Local<'scope, v8::ArrayBuffer>,
}

#[derive(WebApiObject)]
#[webapi(plain)]
struct BlobNetworkBodySourceDeclaration<'scope> {
#[webapi(slot = NETWORK_BODY_SOURCE_KIND_SLOT)]
kind: &'static str,
#[webapi(slot = BODY_SOURCE_BLOB_SLOT)]
blob: v8::Local<'scope, v8::Object>,
}

#[derive(WebApiObject)]
#[webapi(plain)]
struct RegisteredNetworkBodySourceDeclaration<'scope> {
Expand Down Expand Up @@ -531,6 +542,26 @@ pub(in crate::network_host) fn set_network_body_owned_bytes<'s>(
Some(buffer)
}

pub(in crate::network_host) fn set_network_body_blob_source<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
blob: v8::Local<'s, v8::Object>,
) {
let source = BlobNetworkBodySourceDeclaration::new(BODY_SOURCE_KIND_BLOB, blob)
.bind(scope)
.expect("Blob body source declaration should bind");
set_network_body_source_object(scope, object, source);
}

pub(in crate::network_host) fn body_uses_blob_stream<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
) -> bool {
network_body_source_from_object(scope, object).is_some_and(|source| {
network_body_source_kind(scope, source).as_deref() == Some(BODY_SOURCE_KIND_BLOB)
})
}

fn set_network_body_response_body<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
Expand All @@ -552,7 +583,7 @@ pub(in crate::network_host) fn network_body_source_from_object<'s>(
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
}

fn set_network_body_source_object<'s>(
pub(in crate::network_host) fn set_network_body_source_object<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
source: v8::Local<'s, v8::Object>,
Expand Down Expand Up @@ -1154,6 +1185,17 @@ fn try_network_body_bytes_from_storage<'s>(
) -> Result<Option<Vec<u8>>, String> {
let source_kind = network_body_source_kind(scope, object);
match source_kind.as_deref() {
Some(BODY_SOURCE_KIND_BLOB) => {
let blob = network_body_source_slot_value(scope, object, BODY_SOURCE_BLOB_SLOT)
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
.ok_or_else(|| "Missing Blob body source".to_owned())?;
let data = blob::blob_data_from_object(scope, blob)
.ok_or_else(|| "Failed to read Blob body".to_owned())?;
return data
.read_blocking()
.map(Some)
.map_err(|error| format!("Failed to read Blob body: {error}"));
}
Some(BODY_SOURCE_KIND_REGISTRY_BYTES) => {
let Some(id) = registry_body_source_id(scope, object) else {
return Ok(None);
Expand Down Expand Up @@ -1529,7 +1571,9 @@ fn consume_network_body_value_from_source_inner<'s>(
let source = explicit_source.unwrap_or(object);
let pending_stream =
network_body_source_kind(scope, source).as_deref() == Some(BODY_SOURCE_KIND_PENDING_STREAM);
let uses_reader = explicit_source.is_none() || pending_stream && chunk_callback.is_some();
let uses_reader = explicit_source.is_none()
|| body_uses_blob_stream(scope, object)
|| pending_stream && chunk_callback.is_some();
if !uses_reader
&& let Some(stream) = body_stream_object(scope, object)
&& !crate::context_bootstrap::begin_readable_stream_body_consumption(scope, stream)
Expand Down Expand Up @@ -1624,7 +1668,7 @@ fn consume_network_body_value_from_source_inner<'s>(
return (NetworkBodyConsumption::Pending(promise), None);
}

if explicit_source.is_none()
if (explicit_source.is_none() || body_uses_blob_stream(scope, object))
&& let Some(stream) = readable_body_stream_from_object(scope, object)
{
return consume_readable_body_stream(
Expand Down
3 changes: 2 additions & 1 deletion moli-renderer-v8/src/network_host/fetch/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub(super) fn parse_window_fetch_input<'s>(
);
}
let args = webidl::try_parse_args::<crate::network_host::FetchArgs>(scope, args)?;
let (url, inherited, source) = args.input.materialize(scope)?;
let inherit_body = !args.init.body_is_present();
let (url, inherited, source) = args.input.materialize_for_fetch(scope, inherit_body)?;
if let Some(inherited) = inherited {
let req_obj = source.expect("request input source");
let init = parse_fetch_init(scope, args.init)?;
Expand Down
54 changes: 51 additions & 3 deletions moli-renderer-v8/src/network_host/fetch_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,8 @@ fn request_clone_callback<'s>(
);
return;
}
if network_body_source_from_object(scope, this).is_none()
if (network_body_source_from_object(scope, this).is_none()
|| body_source::body_uses_blob_stream(scope, this))
&& let Some(stream) = body_stream_object(scope, this)
{
let clone = RequestCloneShellDeclaration::new()
Expand Down Expand Up @@ -860,6 +861,9 @@ fn request_clone_callback<'s>(
};
set_request_slot_value(scope, this, REQUEST_BODY_SLOT, original_body);
set_request_slot_value(scope, clone, REQUEST_BODY_SLOT, clone_body);
if let Some(source) = network_body_source_from_object(scope, this) {
body_source::set_network_body_source_object(scope, clone, source);
}
mark_request_object(scope, clone);
rv.set(clone.into());
return;
Expand Down Expand Up @@ -919,6 +923,31 @@ fn response_clone_callback<'s>(
.bind(scope)
.expect("Response clone init declaration should bind");

if body_source::body_uses_blob_stream(scope, this)
&& let Some(stream) = body_stream_object(scope, this)
{
let clone = ResponseCloneShellDeclaration::new(None)
.bind(scope)
.expect("Response clone shell declaration should bind");
copy_response_surface_slots(scope, this, clone);
let Some(branches) = crate::context_bootstrap::tee_fetch_body_stream(scope, stream) else {
return;
};
let Some(original_body) = branches.get_index(scope, 0) else {
return;
};
let Some(clone_body) = branches.get_index(scope, 1) else {
return;
};
set_response_slot_value(scope, this, RESPONSE_BODY_SLOT, original_body);
set_response_slot_value(scope, clone, RESPONSE_BODY_SLOT, clone_body);
if let Some(source) = network_body_source_from_object(scope, this) {
body_source::set_network_body_source_object(scope, clone, source);
}
mark_response_object(scope, clone);
rv.set(clone.into());
return;
}
if let Some(source) = network_body_source_from_object(scope, this) {
let clone = ResponseCloneShellDeclaration::new(None)
.bind(scope)
Expand Down Expand Up @@ -984,15 +1013,34 @@ fn copy_response_surface_slots<'s>(
RESPONSE_INTERNAL_URL_SLOT,
RESPONSE_INTERNAL_STATUS_SLOT,
RESPONSE_INTERNAL_STATUS_TEXT_SLOT,
RESPONSE_INTERNAL_HEADERS_SLOT,
RESPONSE_REDIRECTED_SLOT,
RESPONSE_TYPE_SLOT,
RESPONSE_HEADERS_SLOT,
] {
if let Some(value) = response_slot_value(scope, from, slot) {
set_response_slot_value(scope, to, slot, value);
}
}
let headers = response_slot_object(scope, from, RESPONSE_HEADERS_SLOT);
let cloned_headers =
headers.map(|headers| super::headers::clone_headers_object(scope, headers));
if let Some(cloned_headers) = cloned_headers {
set_response_slot_value(scope, to, RESPONSE_HEADERS_SLOT, cloned_headers.into());
}
if let Some(internal_headers) =
response_slot_object(scope, from, RESPONSE_INTERNAL_HEADERS_SLOT)
{
let cloned_internal = if Some(internal_headers) == headers {
cloned_headers.expect("public response headers were cloned")
} else {
super::headers::clone_headers_object(scope, internal_headers)
};
set_response_slot_value(
scope,
to,
RESPONSE_INTERNAL_HEADERS_SLOT,
cloned_internal.into(),
);
}
}

fn clone_response_readable_stream_body<'s>(
Expand Down
10 changes: 10 additions & 0 deletions moli-renderer-v8/src/network_host/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ pub(super) use self::store::{
build_headers_object, build_headers_object_with_state, headers_entries_from_init, headers_list,
mark_headers_immutable, normalized_headers_entries,
};

pub(super) fn clone_headers_object<'s>(
scope: &mut v8::PinScope<'s, '_>,
original: v8::Local<'s, v8::Object>,
) -> v8::Local<'s, v8::Object> {
let entries = headers_list(scope, original);
let guard = self::store::headers_guard(scope, original);
let immutable = self::store::headers_are_immutable(scope, original);
build_headers_object_with_state(scope, &entries, guard, immutable)
}
16 changes: 7 additions & 9 deletions moli-renderer-v8/src/network_host/request/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ pub(crate) fn request_constructor_callback<'s>(
}

append_default_body_content_type(&mut state.headers, state.body_content_type.as_deref());
let body_buffer = state
.body
.take()
.and_then(|body| set_network_body_owned_bytes(scope, obj, body));

let headers_obj = build_headers_object_with_state(
scope,
&state.headers,
Expand Down Expand Up @@ -131,12 +126,15 @@ pub(crate) fn request_constructor_callback<'s>(
} else {
stream
};
if let Some(source) = state.inherited_body_source {
let source = v8::Local::new(scope, source);
body_source::set_network_body_source_object(scope, obj, source);
}
stream.into()
} else {
body_buffer
.and_then(|buffer| {
new_readable_stream_from_array_buffer(scope, buffer, buffer.byte_length())
})
state
.body
.and_then(|source| source.into_body_stream(scope, obj))
.map(|stream| stream.into())
.unwrap_or_else(|| v8::null(scope).into())
};
Expand Down
Loading
Loading