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
26 changes: 26 additions & 0 deletions crates/perry-stdlib/src/fetch/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ pub(crate) unsafe fn body_addr_buffer_bytes(addr: usize) -> Option<Vec<u8>> {
None
}

/// Extract a fetch *request* body as raw bytes for the send paths
/// (`js_fetch_with_options`, `js_fetch_post`). The `body_ptr` codegen hands us is
/// the body value unboxed to its raw heap address (`unbox_to_i64`), so it can be
/// either a binary body — a Buffer / Uint8Array / typed array / ArrayBuffer, data
/// at offset 8 — or a `StringHeader` (a string body, data at offset 20). Probe
/// the buffer / typed-array registry first so a binary body round-trips
/// byte-for-byte; only fall back to the `string_from_header` read when the
/// address isn't a registered binary body.
///
/// Reading a binary body straight through `string_from_header` took the byte
/// length off the right field but the data off the StringHeader data offset (20)
/// instead of the buffer data offset (8), shifting every binary payload left by
/// 12 bytes — `fetch(url, { body: Buffer.from("0123456789ABCDEF") })` arrived as
/// `"CDEF" + zero-fill` (#5757). This is the `fetch(url, init)` twin of
/// #5435/#5483, which fixed the Response body and the `Request` constructor but
/// left the fetch send path reading the body as a string.
pub(crate) unsafe fn fetch_request_body_bytes(body_ptr: *const StringHeader) -> Option<Vec<u8>> {
if body_ptr.is_null() || (body_ptr as usize) < 0x1000 {
return None;
}
if let Some(bytes) = body_addr_buffer_bytes(body_ptr as usize) {
return Some(bytes);
}
super::string_from_header(body_ptr).map(String::into_bytes)
}

lazy_static::lazy_static! {
static ref FORM_DATA_METHOD_VALUE_CACHE: Mutex<HashMap<(usize, &'static str), u64>> =
Mutex::new(HashMap::new());
Expand Down
11 changes: 9 additions & 2 deletions crates/perry-stdlib/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,11 @@ pub unsafe extern "C" fn js_fetch_post(
}
};

let body = string_from_header(body_ptr).unwrap_or_default();
// Probe the buffer/typed-array registry before falling back to a string read
// so a binary body (Buffer / Uint8Array / typed array / ArrayBuffer) is sent
// byte-for-byte instead of being shifted left 12 bytes by the StringHeader
// data offset (#5757). `reqwest::Body` accepts `Vec<u8>` directly.
let body = fetch_request_body_bytes(body_ptr).unwrap_or_default();
let content_type =
string_from_header(content_type_ptr).unwrap_or_else(|| "application/json".to_string());

Expand Down Expand Up @@ -630,7 +634,10 @@ pub unsafe extern "C" fn js_fetch_with_options(
let inputs = match request_handle::resolve_fetch_inputs(
string_from_header(url_ptr),
string_from_header(method_ptr),
string_from_header(body_ptr),
// Read the body as raw bytes (binary bodies probe the buffer/typed-array
// registry first) so a Buffer/Uint8Array body isn't corrupted by a lossy
// StringHeader read (#5757).
fetch_request_body_bytes(body_ptr),
string_from_header(headers_json_ptr),
url_ptr as usize,
) {
Expand Down
9 changes: 5 additions & 4 deletions crates/perry-stdlib/src/fetch/request_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn request_fields_from_handle(maybe_handle: usize) -> Option<RequestFetchFields>
pub(crate) fn resolve_fetch_inputs(
url_from_header: Option<String>,
method_from_header: Option<String>,
body_from_header: Option<String>,
body_bytes: Option<Vec<u8>>,
headers_json: Option<String>,
url_handle: usize,
) -> Result<FetchInputs, u64> {
Expand All @@ -73,9 +73,10 @@ pub(crate) fn resolve_fetch_inputs(
.or_else(|| request_fields.as_ref().map(|rf| rf.method.clone()))
.unwrap_or_else(|| "GET".to_string());

let body = body_from_header
.map(String::into_bytes)
.or_else(|| request_fields.as_ref().and_then(|rf| rf.body.clone()));
// `body_bytes` is already raw bytes (binary bodies preserved byte-for-byte by
// the caller's buffer/typed-array probe; string bodies read as their UTF-8
// bytes), so use it as-is and only fall back to the Request's own body.
let body = body_bytes.or_else(|| request_fields.as_ref().and_then(|rf| rf.body.clone()));

// Start from the `Request`'s own headers (the `fetch(Request)` form), then
// let any `init.headers` override per-key. WHATWG says init headers win, but
Expand Down
Loading