Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing Content-Length #10548

Merged
merged 8 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions fetch/content-length/parsing.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
promise_test(() => {
return fetch("resources/content-lengths.json").then(res => res.json()).then(runTests);
}, "Loading JSON…");

function runTests(testUnits) {
testUnits.forEach(testUnit => {
const input = encodeURIComponent(testUnit.input);
promise_test(t => {
const result = fetch("resources/content-length.py?length=" + input);
if (testUnit.output === null) {
return promise_rejects_js(t, TypeError, result);
} else {
return result.then(res => res.text()).then(text => {
assert_equals(text.length, testUnit.output);
});
}
}, input);
});
}
9 changes: 9 additions & 0 deletions fetch/content-length/resources/content-length.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def main(request, response):
response.add_required_headers = False
output = b"HTTP/1.1 200 OK\r\n"
output += b"Content-Type: text/plain;charset=UTF-8\r\n"
output += request.GET.first(b"length") + b"\r\n"
output += b"\r\n"
output += b"Fact: this is really forty-two bytes long."
response.writer.write(output)
response.close_connection = True
126 changes: 126 additions & 0 deletions fetch/content-length/resources/content-lengths.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
[
{
"input": "Content-Length: 42",
"output": 42
},
{
"input": "Content-Length: 42,42",
"output": 42
},
{
"input": "Content-Length: 42\r\nContent-Length: 42",
"output": 42
},
{
"input": "Content-Length: 42\r\nContent-Length: 42,42",
"output": 42
},
{
"input": "Content-Length: 30",
"output": 30
},
{
"input": "Content-Length: 30,30",
"output": 30
},
{
"input": "Content-Length: 30\r\nContent-Length: 30",
"output": 30
},
{
"input": "Content-Length: 30\r\nContent-Length: 30,30",
"output": 30
},
{
"input": "Content-Length: 42,30",
"output": null
},
{
"input": "Content-Length: 30,42",
"output": null
},
{
"input": "Content-Length: 42\r\nContent-Length: 30",
"output": null
},
{
"input": "Content-Length: 30\r\nContent-Length: 42",
"output": null
},
{
"input": "Content-Length: 30,",
"output": null
},
{
"input": "Content-Length: ,30",
"output": null
},
{
"input": "Content-Length: 30\r\nContent-Length: \t",
"output": null
},
{
"input": "Content-Length: \r\nContent-Length: 30",
"output": null
},
{
"input": "Content-Length: aaaah\r\nContent-Length: nah",
"output": null
},
{
"input": "Content-Length: aaaah, nah",
"output": null
},
{
"input": "Content-Length: aaaah\r\nContent-Length: aaaah",
"output": 42
},
{
"input": "Content-Length: aaaah, aaaah",
"output": 42
},
{
"input": "Content-Length: aaaah",
"output": 42
},
{
"input": "Content-Length: 42s",
"output": 42
},
{
"input": "Content-Length: 30s",
"output": 42
},
{
"input": "Content-Length: -1",
"output": 42
},
{
"input": "Content-Length: 0x20",
"output": 42
},
{
"input": "Content-Length: 030",
"output": 30
},
{
"input": "Content-Length: 030\r\nContent-Length: 30",
"output": null
},
{
"input": "Content-Length: 030, 30",
"output": null
},
{
"input": "Content-Length: \"30\"",
"output": 42
},
{
"input": "Content-Length:30\r\nContent-Length:,\r\nContent-Length:30",
"output": null
},
{
"input": "Content-Length: ",
"output": 42
}
]
4 changes: 4 additions & 0 deletions fetch/content-length/too-long.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
promise_test(async t => {
const result = await fetch(`resources/content-length.py?length=${encodeURIComponent("Content-Length: 50")}`);
await promise_rejects_js(t, TypeError, result.text());
}, "Content-Length header value of network response exceeds response body");