1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

perf(http): use Cow<[u8]> for setting header (#20112)

This commit is contained in:
Bartek Iwańczuk 2023-08-10 23:35:01 +02:00 committed by Divy Srivastava
parent 5515cee539
commit d70c43f3eb

View file

@ -385,18 +385,23 @@ pub fn op_http_read_request_body(
state.resource_table.add_rc(body_resource) state.resource_table.add_rc(body_resource)
} }
#[op2] #[op2(fast)]
pub fn op_http_set_response_header( pub fn op_http_set_response_header(
#[smi] slab_id: SlabId, #[smi] slab_id: SlabId,
#[serde] name: ByteString, #[string(onebyte)] name: Cow<[u8]>,
#[serde] value: ByteString, #[string(onebyte)] value: Cow<[u8]>,
) { ) {
let mut http = slab_get(slab_id); let mut http = slab_get(slab_id);
let resp_headers = http.response().headers_mut(); let resp_headers = http.response().headers_mut();
// These are valid latin-1 strings // These are valid latin-1 strings
let name = HeaderName::from_bytes(&name).unwrap(); let name = HeaderName::from_bytes(&name).unwrap();
// SAFETY: These are valid latin-1 strings let value = match value {
let value = unsafe { HeaderValue::from_maybe_shared_unchecked(value) }; Cow::Borrowed(bytes) => HeaderValue::from_bytes(bytes).unwrap(),
// SAFETY: These are valid latin-1 strings
Cow::Owned(bytes_vec) => unsafe {
HeaderValue::from_maybe_shared_unchecked(bytes::Bytes::from(bytes_vec))
},
};
resp_headers.append(name, value); resp_headers.append(name, value);
} }