1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-01 16:51:13 -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 { listen, TcpConn } from "ext:deno_net/01_net.js";
import { listenTls } from "ext:deno_net/02_tls.js"; import { listenTls } from "ext:deno_net/02_tls.js";
const { const {
ArrayPrototypeFlat,
ArrayPrototypePush, ArrayPrototypePush,
ObjectPrototypeIsPrototypeOf, ObjectPrototypeIsPrototypeOf,
PromisePrototypeCatch, PromisePrototypeCatch,
@ -559,7 +558,7 @@ function mapToCallback(context, callback, onError) {
if (headers.length == 1) { if (headers.length == 1) {
op_http_set_response_header(req, headers[0][0], headers[0][1]); op_http_set_response_header(req, headers[0][0], headers[0][1]);
} else { } 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::futures::TryFutureExt;
use deno_core::op; use deno_core::op;
use deno_core::serde_v8; use deno_core::serde_v8;
use deno_core::serde_v8::from_v8;
use deno_core::task::spawn; use deno_core::task::spawn;
use deno_core::task::JoinHandle; use deno_core::task::JoinHandle;
use deno_core::v8; 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); resp_headers.append(name, value);
} }
#[op] #[op(v8)]
pub fn op_http_set_response_headers(slab_id: SlabId, headers: Vec<ByteString>) { fn op_http_set_response_headers(
scope: &mut v8::HandleScope,
slab_id: SlabId,
headers: serde_v8::Value,
) {
let mut http = slab_get(slab_id); let mut http = slab_get(slab_id);
// TODO(mmastrac): Invalid headers should be handled? // TODO(mmastrac): Invalid headers should be handled?
let resp_headers = http.response().headers_mut(); let resp_headers = http.response().headers_mut();
resp_headers.reserve(headers.len());
for header in headers.chunks_exact(2) { let arr = v8::Local::<v8::Array>::try_from(headers.v8_value).unwrap();
// These are valid latin-1 strings
let name = HeaderName::from_bytes(&header[0]).unwrap(); let len = arr.length();
let value = HeaderValue::from_bytes(&header[1]).unwrap(); let header_len = len * 2;
resp_headers.append(name, value); 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);
} }
} }