Skip to content
Open
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
23 changes: 15 additions & 8 deletions src/runtime/server/server_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2478,20 +2478,21 @@ where
);
}

let mut url = URL::parse(temp_url_str);

// `bun.String.cloneUTF8(url.href)` below makes its own copy, so the
// joined buffer only needs to live through this block. The else arm
// borrows `temp_url_str` (kept alive by `url_zig_str`) instead of
// duping it just to satisfy a uniform `defer free` like the Zig did.
let url = URL::parse(temp_url_str);

// `URL::parse` always sets `href` to its full input, so the buffer
// *is* the href — no second parse needed. When the joined arm owns
// an all-ASCII `Vec<u8>` it is adopted into a WTF::ExternalStringImpl
// below (zero-copy); the borrowed arm and any non-ASCII fall back
// to `clone_utf8` (the borrowed arm already saved a dupe by not
// owning `temp_url_str`).
let owned_url_buf: std::borrow::Cow<'_, [u8]> = if url.hostname.is_empty() {
std::borrow::Cow::Owned(
strings::append(&self.base_url_string_for_joining, url.pathname).into_vec(),
)
} else {
std::borrow::Cow::Borrowed(temp_url_str)
};
url = URL::parse(&owned_url_buf);

if arguments.len() >= 2 && arguments[1].is_object() {
let opts = arguments[1];
Expand Down Expand Up @@ -2531,8 +2532,14 @@ where
}
}

let url_string = match owned_url_buf {
std::borrow::Cow::Owned(v) if strings::is_all_ascii(&v) => {
BunString::create_external_globally_allocated_latin1(v)
}
other => BunString::clone_utf8(&other),
};
Box::new(Request::init2(
BunString::clone_utf8(url.href),
url_string,
headers,
// Moves `body` into the per-VM hive pool (ref_count = 1).
crate::webcore::body::hive_alloc(body),
Expand Down
Loading