-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
http2: properly handle already closed stream error #17942
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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,129 @@ | ||
/* eslint-disable required-modules */ | ||
'use strict'; | ||
|
||
// An HTTP/2 testing tool used to create mock frames for direct testing | ||
// of HTTP/2 endpoints. | ||
|
||
const kFrameData = Symbol('frame-data'); | ||
const FLAG_EOS = 0x1; | ||
const FLAG_ACK = 0x1; | ||
const FLAG_EOH = 0x4; | ||
const FLAG_PADDED = 0x8; | ||
const PADDING = Buffer.alloc(255); | ||
|
||
const kClientMagic = Buffer.from('505249202a20485454502f322' + | ||
'e300d0a0d0a534d0d0a0d0a', 'hex'); | ||
|
||
const kFakeRequestHeaders = Buffer.from('828684410f7777772e65' + | ||
'78616d706c652e636f6d', 'hex'); | ||
|
||
|
||
const kFakeResponseHeaders = Buffer.from('4803333032580770726976617465611d' + | ||
'4d6f6e2c203231204f63742032303133' + | ||
'2032303a31333a323120474d546e1768' + | ||
'747470733a2f2f7777772e6578616d70' + | ||
'6c652e636f6d', 'hex'); | ||
|
||
function isUint32(val) { | ||
return val >>> 0 === val; | ||
} | ||
|
||
function isUint24(val) { | ||
return val >>> 0 === val && val <= 0xFFFFFF; | ||
} | ||
|
||
function isUint8(val) { | ||
return val >>> 0 === val && val <= 0xFF; | ||
} | ||
|
||
function write32BE(array, pos, val) { | ||
if (!isUint32(val)) | ||
throw new RangeError('val is not a 32-bit number'); | ||
array[pos++] = (val >> 24) & 0xff; | ||
array[pos++] = (val >> 16) & 0xff; | ||
array[pos++] = (val >> 8) & 0xff; | ||
array[pos++] = val & 0xff; | ||
} | ||
|
||
function write24BE(array, pos, val) { | ||
if (!isUint24(val)) | ||
throw new RangeError('val is not a 24-bit number'); | ||
array[pos++] = (val >> 16) & 0xff; | ||
array[pos++] = (val >> 8) & 0xff; | ||
array[pos++] = val & 0xff; | ||
} | ||
|
||
function write8(array, pos, val) { | ||
if (!isUint8(val)) | ||
throw new RangeError('val is not an 8-bit number'); | ||
array[pos] = val; | ||
} | ||
|
||
class Frame { | ||
constructor(length, type, flags, id) { | ||
this[kFrameData] = Buffer.alloc(9); | ||
write24BE(this[kFrameData], 0, length); | ||
write8(this[kFrameData], 3, type); | ||
write8(this[kFrameData], 4, flags); | ||
write32BE(this[kFrameData], 5, id); | ||
} | ||
|
||
get data() { | ||
return this[kFrameData]; | ||
} | ||
} | ||
|
||
class SettingsFrame extends Frame { | ||
constructor(ack = false) { | ||
let flags = 0; | ||
if (ack) | ||
flags |= FLAG_ACK; | ||
super(0, 4, flags, 0); | ||
} | ||
} | ||
|
||
class DataFrame extends Frame { | ||
constructor(id, payload, padlen = 0, final = false) { | ||
let len = payload.length; | ||
let flags = 0; | ||
if (final) flags |= FLAG_EOS; | ||
const buffers = [payload]; | ||
if (padlen > 0) { | ||
buffers.unshift(Buffer.from([padlen])); | ||
buffers.push(PADDING.slice(0, padlen)); | ||
len += padlen + 1; | ||
flags |= FLAG_PADDED; | ||
} | ||
super(len, 0, flags, id); | ||
buffers.unshift(this[kFrameData]); | ||
this[kFrameData] = Buffer.concat(buffers); | ||
} | ||
} | ||
|
||
class HeadersFrame extends Frame { | ||
constructor(id, payload, padlen = 0, final = false) { | ||
let len = payload.length; | ||
let flags = FLAG_EOH; | ||
if (final) flags |= FLAG_EOS; | ||
const buffers = [payload]; | ||
if (padlen > 0) { | ||
buffers.unshift(Buffer.from([padlen])); | ||
buffers.push(PADDING.slice(0, padlen)); | ||
len += padlen + 1; | ||
flags |= FLAG_PADDED; | ||
} | ||
super(len, 1, flags, id); | ||
buffers.unshift(this[kFrameData]); | ||
this[kFrameData] = Buffer.concat(buffers); | ||
} | ||
} | ||
|
||
module.exports = { | ||
Frame, | ||
DataFrame, | ||
HeadersFrame, | ||
SettingsFrame, | ||
kFakeRequestHeaders, | ||
kFakeResponseHeaders, | ||
kClientMagic | ||
}; |
This file contains 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,59 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const h2 = require('http2'); | ||
const net = require('net'); | ||
const h2test = require('../common/http2'); | ||
let client; | ||
|
||
const server = h2.createServer(); | ||
server.on('stream', common.mustCall((stream) => { | ||
stream.respond(); | ||
stream.end('ok'); | ||
}, 2)); | ||
server.on('session', common.mustCall((session) => { | ||
session.on('error', common.expectsError({ | ||
code: 'ERR_HTTP2_ERROR', | ||
type: Error, | ||
message: 'Stream was already closed or invalid' | ||
})); | ||
})); | ||
|
||
const settings = new h2test.SettingsFrame(); | ||
const settingsAck = new h2test.SettingsFrame(true); | ||
const head1 = new h2test.HeadersFrame(1, h2test.kFakeRequestHeaders, 0, true); | ||
const head2 = new h2test.HeadersFrame(3, h2test.kFakeRequestHeaders, 0, true); | ||
const head3 = new h2test.HeadersFrame(1, h2test.kFakeRequestHeaders, 0, true); | ||
const head4 = new h2test.HeadersFrame(5, h2test.kFakeRequestHeaders, 0, true); | ||
|
||
server.listen(0, () => { | ||
client = net.connect(server.address().port, () => { | ||
client.write(h2test.kClientMagic, () => { | ||
client.write(settings.data, () => { | ||
client.write(settingsAck.data); | ||
// This will make it ok. | ||
client.write(head1.data, () => { | ||
// This will make it ok. | ||
client.write(head2.data, () => { | ||
// This will cause an error to occur because the client is | ||
// attempting to reuse an already closed stream. This must | ||
// cause the server session to be torn down. | ||
client.write(head3.data, () => { | ||
// This won't ever make it to the server | ||
client.write(head4.data); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
// An error may or may not be emitted on the client side, we don't care | ||
// either way if it is, but we don't want to die if it is. | ||
client.on('error', () => {}); | ||
client.on('close', common.mustCall(() => server.close())); | ||
}); |
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.
can you add some comments on how to use these?
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.
Done