Skip to content
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
12 changes: 11 additions & 1 deletion lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ let currentBufferRef = null
*/
let currentBufferSize = 0
let currentBufferPtr = null
let currentBuffer = null

const USE_NATIVE_TIMER = 0
const USE_FAST_TIMER = 1
Expand Down Expand Up @@ -322,7 +323,16 @@ class Parser {
currentBufferPtr = llhttp.malloc(currentBufferSize)
}

new Uint8Array(llhttp.memory.buffer, currentBufferPtr, currentBufferSize).set(chunk)
if (
currentBuffer === null ||
currentBuffer.buffer !== llhttp.memory.buffer ||
currentBuffer.byteOffset !== currentBufferPtr ||
currentBuffer.byteLength !== currentBufferSize
) {
currentBuffer = new Uint8Array(llhttp.memory.buffer, currentBufferPtr, currentBufferSize)
}

currentBuffer.set(chunk)

// Call `execute` on the wasm parser.
// We pass the `llhttp_parser` pointer address, the pointer address of buffer view data,
Expand Down
66 changes: 66 additions & 0 deletions test/parser-issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,69 @@ test('split header value', async (t) => {

await t.completed
})

test('refreshes wasm input view after reallocating parser buffer', async (t) => {
t = tspl(t, { plan: 4 })

const smallBody = Buffer.from('ok')
const largeBody = Buffer.alloc(8192, 'a')
const responses = [
Buffer.concat([
Buffer.from(`HTTP/1.1 200 OK\r\nContent-Length: ${smallBody.length}\r\n\r\n`),
smallBody
]),
Buffer.concat([
Buffer.from(`HTTP/1.1 200 OK\r\nContent-Length: ${largeBody.length}\r\n\r\n`),
largeBody
])
]

const server = net.createServer(socket => {
let responsesSent = 0

socket.on('data', () => {
socket.write(responses[responsesSent++])
})
})
after(() => server.close())

await new Promise(resolve => server.listen(0, resolve))

const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.destroy())

function request () {
return new Promise((resolve, reject) => {
client.request({
method: 'GET',
path: '/'
}, (err, { statusCode, body } = {}) => {
if (err) {
reject(err)
return
}

const bufs = []

body.on('data', buf => {
bufs.push(buf)
})
body.on('end', () => {
resolve({
statusCode,
body: Buffer.concat(bufs)
})
})
body.on('error', reject)
})
})
}

const smallResponse = await request()
t.strictEqual(smallResponse.statusCode, 200)
t.strictEqual(smallResponse.body.toString(), smallBody.toString())

const largeResponse = await request()
t.strictEqual(largeResponse.statusCode, 200)
t.strictEqual(largeResponse.body.toString(), largeBody.toString())
})
Loading