Skip to content
Merged
Changes from 1 commit
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
23 changes: 14 additions & 9 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,22 +217,27 @@ class Parser {
}
}

execute (data) {
/**
* @param {Buffer} chunk
*/
execute (chunk) {
assert(this.ptr != null)
assert(currentParser == null)
assert(!this.paused)

const { socket, llhttp } = this

if (data.length > currentBufferSize) {
// Allocate a new buffer if the current buffer is too small.
if (chunk.length > currentBufferSize) {
if (currentBufferPtr) {
llhttp.free(currentBufferPtr)
}
currentBufferSize = Math.ceil(data.length / 4096) * 4096
// Allocate a buffer that is a multiple of 4096 bytes.
currentBufferSize = Math.ceil(chunk.length / 4096) * 4096
currentBufferPtr = llhttp.malloc(currentBufferSize)
}

new Uint8Array(llhttp.memory.buffer, currentBufferPtr, currentBufferSize).set(data)
new Uint8Array(llhttp.memory.buffer, currentBufferPtr, currentBufferSize).set(chunk)

// Call `execute` on the wasm parser.
// We pass the `llhttp_parser` pointer address, the pointer address of buffer view data,
Expand All @@ -242,9 +247,9 @@ class Parser {
let ret

try {
currentBufferRef = data
currentBufferRef = chunk
currentParser = this
ret = llhttp.llhttp_execute(this.ptr, currentBufferPtr, data.length)
ret = llhttp.llhttp_execute(this.ptr, currentBufferPtr, chunk.length)
/* eslint-disable-next-line no-useless-catch */
} catch (err) {
/* istanbul ignore next: difficult to make a test case for */
Expand All @@ -257,10 +262,10 @@ class Parser {
const offset = llhttp.llhttp_get_error_pos(this.ptr) - currentBufferPtr

if (ret === constants.ERROR.PAUSED_UPGRADE) {
this.onUpgrade(data.slice(offset))
this.onUpgrade(chunk.subarray(offset))
} else if (ret === constants.ERROR.PAUSED) {
this.paused = true
socket.unshift(data.slice(offset))
socket.unshift(chunk.subarray(offset))
} else if (ret !== constants.ERROR.OK) {
const ptr = llhttp.llhttp_get_error_reason(this.ptr)
let message = ''
Expand All @@ -272,7 +277,7 @@ class Parser {
Buffer.from(llhttp.memory.buffer, ptr, len).toString() +
')'
}
throw new HTTPParserError(message, constants.ERROR[ret], data.slice(offset))
throw new HTTPParserError(message, constants.ERROR[ret], chunk.subarray(offset))
}
} catch (err) {
util.destroy(socket, err)
Expand Down