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

perf(http): avoid flattening http headers (#19384)

This commit is contained in:
Marvin Hagemeister 2023-06-06 16:55:37 +02:00 committed by Bartek Iwańczuk
parent b34d95667a
commit f8a0cfa5fd
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
2 changed files with 26 additions and 10 deletions

View file

@ -37,7 +37,6 @@ import {
import { listen, TcpConn } from "ext:deno_net/01_net.js";
import { listenTls } from "ext:deno_net/02_tls.js";
const {
ArrayPrototypeFlat,
ArrayPrototypePush,
ObjectPrototypeIsPrototypeOf,
PromisePrototypeCatch,
@ -559,7 +558,7 @@ function mapToCallback(context, callback, onError) {
if (headers.length == 1) {
op_http_set_response_header(req, headers[0][0], headers[0][1]);
} else {
op_http_set_response_headers(req, ArrayPrototypeFlat(headers));
op_http_set_response_headers(req, headers);
}
}

View file

@ -21,6 +21,7 @@ use deno_core::error::AnyError;
use deno_core::futures::TryFutureExt;
use deno_core::op;
use deno_core::serde_v8;
use deno_core::serde_v8::from_v8;
use deno_core::task::spawn;
use deno_core::task::JoinHandle;
use deno_core::v8;
@ -384,17 +385,33 @@ pub fn op_http_set_response_header(slab_id: SlabId, name: &str, value: &str) {
resp_headers.append(name, value);
}
#[op]
pub fn op_http_set_response_headers(slab_id: SlabId, headers: Vec<ByteString>) {
#[op(v8)]
fn op_http_set_response_headers(
scope: &mut v8::HandleScope,
slab_id: SlabId,
headers: serde_v8::Value,
) {
let mut http = slab_get(slab_id);
// TODO(mmastrac): Invalid headers should be handled?
let resp_headers = http.response().headers_mut();
resp_headers.reserve(headers.len());
for header in headers.chunks_exact(2) {
// These are valid latin-1 strings
let name = HeaderName::from_bytes(&header[0]).unwrap();
let value = HeaderValue::from_bytes(&header[1]).unwrap();
resp_headers.append(name, value);
let arr = v8::Local::<v8::Array>::try_from(headers.v8_value).unwrap();
let len = arr.length();
let header_len = len * 2;
resp_headers.reserve(header_len.try_into().unwrap());
for i in 0..len {
let item = arr.get_index(scope, i).unwrap();
let pair = v8::Local::<v8::Array>::try_from(item).unwrap();
let name = pair.get_index(scope, 0).unwrap();
let value = pair.get_index(scope, 1).unwrap();
let v8_name: ByteString = from_v8(scope, name).unwrap();
let v8_value: ByteString = from_v8(scope, value).unwrap();
let header_name = HeaderName::from_bytes(&v8_name).unwrap();
let header_value = HeaderValue::from_bytes(&v8_value).unwrap();
resp_headers.append(header_name, header_value);
}
}