mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 21:50:00 -05:00
perf(ext/http): optimize inferrable content-type responses
This commit is contained in:
parent
c77c9b2958
commit
cf6f921df5
3 changed files with 31 additions and 1 deletions
|
@ -122,6 +122,7 @@ function cloneInnerResponse(response) {
|
||||||
body,
|
body,
|
||||||
headerList,
|
headerList,
|
||||||
urlList,
|
urlList,
|
||||||
|
canInferContentType: response.canInferContentType,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusMessage: response.statusMessage,
|
statusMessage: response.statusMessage,
|
||||||
aborted: response.aborted,
|
aborted: response.aborted,
|
||||||
|
@ -142,6 +143,7 @@ function newInnerResponse(status = 200, statusMessage = "") {
|
||||||
headerList: [],
|
headerList: [],
|
||||||
urlList: [],
|
urlList: [],
|
||||||
status,
|
status,
|
||||||
|
canInferContentType: false,
|
||||||
statusMessage,
|
statusMessage,
|
||||||
aborted: false,
|
aborted: false,
|
||||||
url() {
|
url() {
|
||||||
|
@ -178,6 +180,8 @@ function abortedNetworkError() {
|
||||||
* @param {{ body: fetchBody.InnerBody, contentType: string | null } | null} bodyWithType
|
* @param {{ body: fetchBody.InnerBody, contentType: string | null } | null} bodyWithType
|
||||||
*/
|
*/
|
||||||
function initializeAResponse(response, init, bodyWithType) {
|
function initializeAResponse(response, init, bodyWithType) {
|
||||||
|
let canInferContentType = false;
|
||||||
|
|
||||||
// 1.
|
// 1.
|
||||||
if ((init.status < 200 || init.status > 599) && init.status != 101) {
|
if ((init.status < 200 || init.status > 599) && init.status != 101) {
|
||||||
throw new RangeError(
|
throw new RangeError(
|
||||||
|
@ -228,10 +232,14 @@ function initializeAResponse(response, init, bodyWithType) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!hasContentType) {
|
if (!hasContentType) {
|
||||||
|
// keep case in sync with `extractBody`
|
||||||
|
canInferContentType = contentType === "text/plain;charset=UTF-8";
|
||||||
ArrayPrototypePush(list, ["Content-Type", contentType]);
|
ArrayPrototypePush(list, ["Content-Type", contentType]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return canInferContentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Response {
|
class Response {
|
||||||
|
@ -335,7 +343,11 @@ class Response {
|
||||||
if (body !== null) {
|
if (body !== null) {
|
||||||
bodyWithType = extractBody(body);
|
bodyWithType = extractBody(body);
|
||||||
}
|
}
|
||||||
initializeAResponse(this, init, bodyWithType);
|
this[_response].canInferContentType = initializeAResponse(
|
||||||
|
this,
|
||||||
|
init,
|
||||||
|
bodyWithType,
|
||||||
|
);
|
||||||
this[_brand] = _brand;
|
this[_brand] = _brand;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -564,6 +564,12 @@ function mapToCallback(context, callback, onError) {
|
||||||
const headers = inner.headerList;
|
const headers = inner.headerList;
|
||||||
if (headers && headers.length > 0) {
|
if (headers && headers.length > 0) {
|
||||||
if (headers.length == 1) {
|
if (headers.length == 1) {
|
||||||
|
if (
|
||||||
|
inner.canInferContentType &&
|
||||||
|
headers[0][0] === "Content-Type"
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
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, headers);
|
op_http_set_response_headers(req, headers);
|
||||||
|
|
|
@ -739,7 +739,19 @@ pub fn op_http_set_response_body_text(
|
||||||
let http =
|
let http =
|
||||||
// SAFETY: external is deleted before calling this op.
|
// SAFETY: external is deleted before calling this op.
|
||||||
unsafe { take_external!(external, "op_http_set_response_body_text") };
|
unsafe { take_external!(external, "op_http_set_response_body_text") };
|
||||||
|
|
||||||
if !text.is_empty() {
|
if !text.is_empty() {
|
||||||
|
{
|
||||||
|
let mut response_parts = http.response_parts();
|
||||||
|
if !response_parts.headers.contains_key(CONTENT_TYPE) {
|
||||||
|
response_parts.headers.append(
|
||||||
|
CONTENT_TYPE,
|
||||||
|
HeaderValue::from_static("text/plain; charset=utf-8"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set content-type to text/plain; charset=utf-8 if not set
|
||||||
set_response(http, Some(text.len()), status, false, |compression| {
|
set_response(http, Some(text.len()), status, false, |compression| {
|
||||||
ResponseBytesInner::from_vec(compression, text.into_bytes())
|
ResponseBytesInner::from_vec(compression, text.into_bytes())
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue