Skip to content

Commit c55c13d

Browse files
Jake ChampionJakeChampion
authored andcommitted
rename to manualFramingHeaders and add a method to enable or disable manualFramingHeaders
1 parent da3eba0 commit c55c13d

File tree

5 files changed

+148
-56
lines changed

5 files changed

+148
-56
lines changed

integration-tests/js-compute/fixtures/app/src/override-content-length.js

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { routes, isRunningLocally } from "./routes.js";
77

88
let error;
99

10-
async function requestInitObjectLiteral(overrideContentLength) {
10+
async function requestInitObjectLiteral(manualFramingHeaders) {
1111
let request = new Request("https://http-me.glitch.me/anything", {
1212
backend: "httpme",
1313
method: "POST",
1414
body: "meow",
15-
overrideContentLength,
15+
manualFramingHeaders,
1616
headers: {
1717
"content-length": "1"
1818
}
@@ -22,12 +22,27 @@ async function requestInitObjectLiteral(overrideContentLength) {
2222
return body?.headers?.["content-length"];
2323
}
2424

25-
async function requestClone(overrideContentLength) {
25+
async function requestMethod(manualFramingHeaders) {
2626
let request = new Request("https://http-me.glitch.me/anything", {
2727
backend: "httpme",
2828
method: "POST",
2929
body: "meow",
30-
overrideContentLength,
30+
headers: {
31+
"content-length": "1"
32+
}
33+
});
34+
request.setManualFramingHeaders(manualFramingHeaders);
35+
let response = await fetch(request);
36+
let body = await response.json()
37+
return body?.headers?.["content-length"];
38+
}
39+
40+
async function requestClone(manualFramingHeaders) {
41+
let request = new Request("https://http-me.glitch.me/anything", {
42+
backend: "httpme",
43+
method: "POST",
44+
body: "meow",
45+
manualFramingHeaders,
3146
headers: {
3247
"content-length": "1"
3348
}
@@ -37,12 +52,12 @@ async function requestClone(overrideContentLength) {
3752
return body?.headers?.["content-length"];
3853
}
3954

40-
async function fetchInitObjectLiteral(overrideContentLength) {
55+
async function fetchInitObjectLiteral(manualFramingHeaders) {
4156
let response = await fetch("https://http-me.glitch.me/anything", {
4257
backend: "httpme",
4358
method: "POST",
4459
body: "meow",
45-
overrideContentLength,
60+
manualFramingHeaders,
4661
headers: {
4762
"content-length": "1"
4863
}
@@ -74,6 +89,29 @@ routes.set("/override-content-length/request/init/object-literal/false", async (
7489
return pass("ok")
7590
});
7691

92+
routes.set("/override-content-length/request/method/object-literal/true", async () => {
93+
if (isRunningLocally()) {
94+
error = await assertRejects(() => requestMethod(true))
95+
if (error) { return error }
96+
} else {
97+
let actual = await requestMethod(true);
98+
let expected = "1"
99+
error = assert(actual, expected, `await requestMethod(true)`)
100+
if (error) { return error }
101+
}
102+
103+
return pass("ok")
104+
});
105+
106+
routes.set("/override-content-length/request/method/object-literal/false", async () => {
107+
let actual = await requestMethod(false);
108+
let expected = "4"
109+
error = assert(actual, expected, `await requestMethod(false)`)
110+
if (error) { return error }
111+
112+
return pass("ok")
113+
});
114+
77115
routes.set("/override-content-length/request/clone/true", async () => {
78116
if (isRunningLocally()) {
79117
error = await assertRejects(() => requestClone(true))
@@ -120,29 +158,29 @@ routes.set("/override-content-length/fetch/init/object-literal/false", async ()
120158
return pass("ok")
121159
});
122160

123-
async function responseInitObjectLiteral(overrideContentLength) {
161+
async function responseInitObjectLiteral(manualFramingHeaders) {
124162
let response = new Response(new ReadableStream({
125163
start(controller) {
126164
controller.enqueue(new TextEncoder().encode("meow"));
127165
controller.close();
128166
},
129167
}), {
130-
overrideContentLength,
168+
manualFramingHeaders,
131169
headers: {
132170
"content-length": "4"
133171
}
134172
});
135173
return response;
136174
}
137175

138-
async function responseInitresponseInstance(overrideContentLength) {
176+
async function responseInitresponseInstance(manualFramingHeaders) {
139177
let response = new Response(new ReadableStream({
140178
start(controller) {
141179
controller.enqueue(new TextEncoder().encode("meow"));
142180
controller.close();
143181
},
144182
}), {
145-
overrideContentLength,
183+
manualFramingHeaders,
146184
headers: {
147185
"content-length": "4"
148186
}
@@ -172,32 +210,33 @@ routes.set("/override-content-length/response/init/response-instance/false", asy
172210
return responseInitresponseInstance(false);
173211
});
174212

175-
async function responseInitfetch(overrideContentLength) {
213+
async function responseMethod(setManualFramingHeaders) {
176214
const response = await fetch("https://httpbin.org/stream-bytes/11", {
177215
backend: "httpbin",
178-
overrideContentLength,
179216
cacheOverride: new CacheOverride('pass')
180217
});
218+
response.setManualFramingHeaders(setManualFramingHeaders);
181219
response.headers.set("content-length", "11")
220+
response.headers.delete("transfer-encoding")
182221
return response;
183222
}
184223

185-
routes.set("/override-content-length/response/init/fetch/false", async () => {
186-
return responseInitfetch(false);
224+
routes.set("/override-content-length/response/method/false", async () => {
225+
return responseMethod(false);
187226
});
188227

189-
routes.set("/override-content-length/response/init/fetch/true", async () => {
190-
return responseInitfetch(true);
228+
routes.set("/override-content-length/response/method/true", async () => {
229+
return responseMethod(true);
191230
});
192231

193232

194233
// TODO: uncomment when we have an implementation of Response.prototype.clone
195-
// async function responseClone(overrideContentLength) {
234+
// async function responseClone(setManualFramingHeaders) {
196235
// let response = new Response("meow", {
197236
// backend: "httpme",
198237
// method: "POST",
199238
// body: "meow",
200-
// overrideContentLength,
239+
// setManualFramingHeaders,
201240
// headers: {
202241
// "transfer-encoding": "chunked"
203242
// }

integration-tests/js-compute/fixtures/app/tests.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4704,22 +4704,22 @@
47044704
}
47054705
},
47064706

4707-
"GET /override-content-length/response/init/fetch/false": {
4707+
"GET /override-content-length/response/method/false": {
47084708
"environments": ["compute"],
47094709
"downstream_request": {
47104710
"method": "GET",
4711-
"pathname": "/override-content-length/response/init/fetch/false"
4711+
"pathname": "/override-content-length/response/method/false"
47124712
},
47134713
"downstream_response": {
47144714
"status": 200
47154715
}
47164716
},
47174717

4718-
"GET /override-content-length/response/init/fetch/true": {
4718+
"GET /override-content-length/response/method/true": {
47194719
"environments": ["compute"],
47204720
"downstream_request": {
47214721
"method": "GET",
4722-
"pathname": "/override-content-length/response/init/fetch/true"
4722+
"pathname": "/override-content-length/response/method/true"
47234723
},
47244724
"downstream_response": {
47254725
"status": 200,

runtime/js-compute-runtime/builtins/request-response.cpp

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,29 @@ bool Request::setCacheKey(JSContext *cx, unsigned argc, JS::Value *vp) {
13041304
return true;
13051305
}
13061306

1307+
bool Request::setManualFramingHeaders(JSContext *cx, unsigned argc, JS::Value *vp) {
1308+
METHOD_HEADER(1)
1309+
1310+
bool manualFramingHeaders = JS::ToBoolean(args.get(0));
1311+
JS::SetReservedSlot(self, static_cast<uint32_t>(Slots::ManualFramingHeaders), JS::BooleanValue(manualFramingHeaders));
1312+
auto handle = request_handle(self);
1313+
host_api::Result<host_api::Void> res;
1314+
if (manualFramingHeaders) {
1315+
res =
1316+
handle.set_framing_headers_mode(host_api::FramingHeadersMode::ManuallyFromHeaders);
1317+
} else {
1318+
res =
1319+
handle.set_framing_headers_mode(host_api::FramingHeadersMode::Automatic);
1320+
}
1321+
if (auto *err = res.to_err()) {
1322+
HANDLE_ERROR(cx, *err);
1323+
return false;
1324+
}
1325+
1326+
args.rval().setUndefined();
1327+
return true;
1328+
}
1329+
13071330
JSString *GET_atom;
13081331

13091332
bool Request::clone(JSContext *cx, unsigned argc, JS::Value *vp) {
@@ -1325,13 +1348,13 @@ bool Request::clone(JSContext *cx, unsigned argc, JS::Value *vp) {
13251348
JS::GetReservedSlot(self, static_cast<uint32_t>(Slots::URL)));
13261349
JS::SetReservedSlot(requestInstance, static_cast<uint32_t>(Slots::IsDownstream),
13271350
JS::GetReservedSlot(self, static_cast<uint32_t>(Slots::IsDownstream)));
1328-
JS::RootedValue overrideContentLength(
1351+
JS::RootedValue manualFramingHeaders(
13291352
cx,
1330-
JS::GetReservedSlot(self, static_cast<uint32_t>(Slots::FramingHeadersManuallyFromHeaders)));
1353+
JS::GetReservedSlot(self, static_cast<uint32_t>(Slots::ManualFramingHeaders)));
13311354
JS::SetReservedSlot(requestInstance,
1332-
static_cast<uint32_t>(Slots::FramingHeadersManuallyFromHeaders),
1333-
overrideContentLength);
1334-
if (JS::ToBoolean(overrideContentLength)) {
1355+
static_cast<uint32_t>(Slots::ManualFramingHeaders),
1356+
manualFramingHeaders);
1357+
if (JS::ToBoolean(manualFramingHeaders)) {
13351358
auto res =
13361359
request_handle.set_framing_headers_mode(host_api::FramingHeadersMode::ManuallyFromHeaders);
13371360
if (auto *err = res.to_err()) {
@@ -1473,6 +1496,7 @@ const JSFunctionSpec Request::methods[] = {
14731496
JS_FN("text", Request::bodyAll<RequestOrResponse::BodyReadResult::Text>, 0, JSPROP_ENUMERATE),
14741497
JS_FN("setCacheOverride", Request::setCacheOverride, 3, JSPROP_ENUMERATE),
14751498
JS_FN("setCacheKey", Request::setCacheKey, 0, JSPROP_ENUMERATE),
1499+
JS_FN("setManualFramingHeaders", Request::setManualFramingHeaders, 1, JSPROP_ENUMERATE),
14761500
JS_FN("clone", Request::clone, 0, JSPROP_ENUMERATE),
14771501
JS_FS_END,
14781502
};
@@ -1686,8 +1710,8 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
16861710
JS::RootedValue backend_val(cx);
16871711
JS::RootedValue cache_override(cx);
16881712
JS::RootedValue fastly_val(cx);
1689-
JS::RootedValue overrideContentLength(cx);
1690-
bool hasOverrideContentLength;
1713+
JS::RootedValue manualFramingHeaders(cx);
1714+
bool hasmanualFramingHeaders;
16911715
if (init_val.isObject()) {
16921716
JS::RootedObject init(cx, init_val.toObjectOrNull());
16931717
if (!JS_GetProperty(cx, init, "method", &method_val) ||
@@ -1696,8 +1720,8 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
16961720
!JS_GetProperty(cx, init, "backend", &backend_val) ||
16971721
!JS_GetProperty(cx, init, "cacheOverride", &cache_override) ||
16981722
!JS_GetProperty(cx, init, "fastly", &fastly_val) ||
1699-
!JS_HasOwnProperty(cx, init, "overrideContentLength", &hasOverrideContentLength) ||
1700-
!JS_GetProperty(cx, init, "overrideContentLength", &overrideContentLength)) {
1723+
!JS_HasOwnProperty(cx, init, "manualFramingHeaders", &hasmanualFramingHeaders) ||
1724+
!JS_GetProperty(cx, init, "manualFramingHeaders", &manualFramingHeaders)) {
17011725
return nullptr;
17021726
}
17031727
} else if (!init_val.isNullOrUndefined()) {
@@ -2009,18 +2033,18 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
20092033
JS::BooleanValue(false));
20102034
}
20112035

2012-
if (!hasOverrideContentLength) {
2036+
if (!hasmanualFramingHeaders) {
20132037
if (input_request) {
2014-
overrideContentLength.set(JS::GetReservedSlot(
2015-
input_request, static_cast<uint32_t>(Slots::FramingHeadersManuallyFromHeaders)));
2038+
manualFramingHeaders.set(JS::GetReservedSlot(
2039+
input_request, static_cast<uint32_t>(Slots::ManualFramingHeaders)));
20162040
} else {
2017-
overrideContentLength.setBoolean(false);
2041+
manualFramingHeaders.setBoolean(false);
20182042
}
20192043
}
2020-
JS::SetReservedSlot(request, static_cast<uint32_t>(Slots::FramingHeadersManuallyFromHeaders),
2021-
JS::BooleanValue(JS::ToBoolean(overrideContentLength)));
2044+
JS::SetReservedSlot(request, static_cast<uint32_t>(Slots::ManualFramingHeaders),
2045+
JS::BooleanValue(JS::ToBoolean(manualFramingHeaders)));
20222046

2023-
if (JS::ToBoolean(overrideContentLength)) {
2047+
if (JS::ToBoolean(manualFramingHeaders)) {
20242048
auto res =
20252049
request_handle.set_framing_headers_mode(host_api::FramingHeadersMode::ManuallyFromHeaders);
20262050
if (auto *err = res.to_err()) {
@@ -2650,6 +2674,28 @@ bool Response::json(JSContext *cx, unsigned argc, JS::Value *vp) {
26502674
return true;
26512675
}
26522676

2677+
bool Response::setManualFramingHeaders(JSContext *cx, unsigned argc, JS::Value *vp) {
2678+
METHOD_HEADER(1)
2679+
2680+
bool manualFramingHeaders = JS::ToBoolean(args.get(0));
2681+
JS::SetReservedSlot(self, static_cast<uint32_t>(Slots::ManualFramingHeaders), JS::BooleanValue(manualFramingHeaders));
2682+
auto handle = response_handle(self);
2683+
host_api::Result<host_api::Void> res;
2684+
if (manualFramingHeaders) {
2685+
res = handle.set_framing_headers_mode(host_api::FramingHeadersMode::ManuallyFromHeaders);
2686+
} else {
2687+
res = handle.set_framing_headers_mode(host_api::FramingHeadersMode::Automatic);
2688+
}
2689+
if (auto *err = res.to_err()) {
2690+
HANDLE_ERROR(cx, *err);
2691+
return false;
2692+
}
2693+
2694+
args.rval().setUndefined();
2695+
return true;
2696+
}
2697+
2698+
26532699
const JSFunctionSpec Response::static_methods[] = {
26542700
JS_FN("redirect", redirect, 1, JSPROP_ENUMERATE),
26552701
JS_FN("json", json, 1, JSPROP_ENUMERATE),
@@ -2665,6 +2711,7 @@ const JSFunctionSpec Response::methods[] = {
26652711
JSPROP_ENUMERATE),
26662712
JS_FN("json", bodyAll<RequestOrResponse::BodyReadResult::JSON>, 0, JSPROP_ENUMERATE),
26672713
JS_FN("text", bodyAll<RequestOrResponse::BodyReadResult::Text>, 0, JSPROP_ENUMERATE),
2714+
JS_FN("setManualFramingHeaders", Response::setManualFramingHeaders, 1, JSPROP_ENUMERATE),
26682715
JS_FS_END,
26692716
};
26702717

@@ -2700,8 +2747,8 @@ bool Response::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
27002747
JS::RootedValue statusText_val(cx);
27012748
JS::RootedString statusText(cx, JS_GetEmptyString(cx));
27022749
JS::RootedValue headers_val(cx);
2703-
bool hasOverrideContentLength;
2704-
JS::RootedValue overrideContentLength(cx);
2750+
bool hasmanualFramingHeaders;
2751+
JS::RootedValue manualFramingHeaders(cx);
27052752
host_api::FramingHeadersMode mode{host_api::FramingHeadersMode::Automatic};
27062753

27072754
if (init_val.isObject()) {
@@ -2747,8 +2794,8 @@ bool Response::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
27472794
statusText = JS_NewStringCopyZ(cx, status_text.c_str());
27482795
}
27492796

2750-
if (!JS_HasOwnProperty(cx, init, "overrideContentLength", &hasOverrideContentLength) ||
2751-
!JS_GetProperty(cx, init, "overrideContentLength", &overrideContentLength)) {
2797+
if (!JS_HasOwnProperty(cx, init, "manualFramingHeaders", &hasmanualFramingHeaders) ||
2798+
!JS_GetProperty(cx, init, "manualFramingHeaders", &manualFramingHeaders)) {
27522799
return false;
27532800
}
27542801

@@ -2801,20 +2848,20 @@ bool Response::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
28012848
return false;
28022849
}
28032850

2804-
if (!hasOverrideContentLength) {
2851+
if (!hasmanualFramingHeaders) {
28052852
if (is_instance(init_val)) {
2806-
overrideContentLength.set(
2853+
manualFramingHeaders.set(
28072854
JS::GetReservedSlot(init_val.toObjectOrNull(),
2808-
static_cast<uint32_t>(Slots::FramingHeadersManuallyFromHeaders)));
2855+
static_cast<uint32_t>(Slots::ManualFramingHeaders)));
28092856
} else {
2810-
overrideContentLength.setBoolean(false);
2857+
manualFramingHeaders.setBoolean(false);
28112858
}
28122859
}
2813-
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::FramingHeadersManuallyFromHeaders),
2814-
JS::BooleanValue(JS::ToBoolean(overrideContentLength)));
2860+
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::ManualFramingHeaders),
2861+
JS::BooleanValue(JS::ToBoolean(manualFramingHeaders)));
28152862

2816-
// `overrideContentLength: true` indicates that we want to set the framing mode manually.
2817-
if (JS::ToBoolean(overrideContentLength)) {
2863+
// `manualFramingHeaders: true` indicates that we want to set the framing mode manually.
2864+
if (JS::ToBoolean(manualFramingHeaders)) {
28182865
mode = host_api::FramingHeadersMode::ManuallyFromHeaders;
28192866
}
28202867
if (mode != host_api::FramingHeadersMode::Automatic) {

0 commit comments

Comments
 (0)