Repository navigation
http: compute body chunk length from decoded bytes #67615
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
Open
ShogunPanda
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
ShogunPanda:http-encoded-chunk-length
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
| const net = require('net'); | ||
|
|
||
| // The length announced for a chunk (or in Content-Length) must match the | ||
| // number of bytes actually written, also for encodings where the decoded size | ||
| // is smaller than the one estimated by `Buffer.byteLength()`. | ||
|
|
||
| const cases = [ | ||
| // [string, encoding, expected decoded bytes] | ||
| ['QUJD REVG', 'base64', 'ABCDEF'], | ||
| ['QUJD\r\n\r\nREVG', 'BASE64', 'ABCDEF'], | ||
| ['QUJD REVG', 'base64url', 'ABCDEF'], | ||
| ['41424 3', 'hex', 'AB'], | ||
| ['4142zz4344', 'hex', 'AB'], | ||
| ['414', 'hex', 'A'], | ||
| ]; | ||
|
|
||
| // Reads the raw request, returning the body bytes de-framed according to | ||
| // the framing used. Fails if the framing is not exactly consumed. | ||
| function parseChunked(raw) { | ||
| let rest = raw; | ||
| let body = ''; | ||
| for (;;) { | ||
| const eol = rest.indexOf('\r\n'); | ||
| assert.notStrictEqual(eol, -1); | ||
| const size = parseInt(rest.slice(0, eol), 16); | ||
| rest = rest.slice(eol + 2); | ||
| if (size === 0) { | ||
| assert.strictEqual(rest, '\r\n'); | ||
| return body; | ||
| } | ||
| body += rest.slice(0, size); | ||
| assert.strictEqual(rest.slice(size, size + 2), '\r\n'); | ||
| rest = rest.slice(size + 2); | ||
| } | ||
| } | ||
|
|
||
| function run(send, isComplete, verify) { | ||
| const server = net.createServer(common.mustCall((socket) => { | ||
| let data = ''; | ||
| socket.setEncoding('latin1'); | ||
| socket.on('data', (chunk) => { | ||
| data += chunk; | ||
| const idx = data.indexOf('\r\n\r\n'); | ||
| if (idx === -1) return; | ||
| const head = data.slice(0, idx); | ||
| const raw = data.slice(idx + 4); | ||
| if (isComplete(head, raw)) { | ||
| verify(head, raw); | ||
| socket.end('HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n'); | ||
| } | ||
| }); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| const req = http.request({ port: server.address().port, method: 'POST', agent: false }, | ||
| common.mustCall((res) => { | ||
| res.resume(); | ||
| res.on('end', common.mustCall(() => server.close())); | ||
| })); | ||
| send(req); | ||
| })); | ||
| } | ||
|
|
||
| for (const [str, encoding, expected] of cases) { | ||
| // Chunked, write() then end() | ||
| run((req) => { | ||
| req.write(str, encoding); | ||
| req.end(); | ||
| }, (head, raw) => raw.endsWith('0\r\n\r\n'), common.mustCall((head, raw) => { | ||
| assert.match(head, /Transfer-Encoding: chunked/i); | ||
| assert.strictEqual(parseChunked(raw), expected); | ||
| })); | ||
|
|
||
| // Chunked, write() twice so a following chunk would be mis-framed | ||
| run((req) => { | ||
| req.write(str, encoding); | ||
| req.write('tail'); | ||
| req.end(); | ||
| }, (head, raw) => raw.endsWith('0\r\n\r\n'), common.mustCall((head, raw) => { | ||
| assert.strictEqual(parseChunked(raw), `${expected}tail`); | ||
| })); | ||
|
|
||
| // Single final chunk: Content-Length is computed | ||
| run((req) => { | ||
| req.end(str, encoding); | ||
| }, (head, raw) => raw.length >= expected.length, common.mustCall((head, raw) => { | ||
| const match = /Content-Length: (\d+)/i.exec(head); | ||
| assert.ok(match); | ||
| assert.strictEqual(Number(match[1]), expected.length); | ||
| assert.strictEqual(raw, expected); | ||
| })); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a
normalizeEncodingmethod ininternal/util. Would that be better?