mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-30 05:28:45 +00:00
Be emphatic about remote peers making mistakes. (#65)
Motivation: Receiving a CONTINUATION frame at the wrong stage of a connection is a PROTOCOL_ERROR, but out of an abundance of caution we were categorising it as an INTERNAL_ERROR. We should be more confident about our error categories. Modifications: - Changed INTERNAL_ERROR to PROTOCOL_ERROR. - Added a test for this error case. Result: More confidence, more style, more pizazz.
This commit is contained in:
1 parent
9e07639b44
commit
d9f1131749
4 files changed
+25
-1
No files matched your search
@@ -201,4 +201,6 @@ internal enum InternalError: Error {
|
||||
case codecError(code: HTTP2ErrorCode)
|
||||
}
|
||||
|
||||
extension InternalError: Hashable { }
|
||||
|
||||
|
||||
@@ -390,7 +390,7 @@ struct HTTP2FrameDecoder {
|
||||
}
|
||||
guard state.header.type != 9 else {
|
||||
// we shouldn't see any CONTINUATION frames in this state
|
||||
throw InternalError.codecError(code: .internalError)
|
||||
throw InternalError.codecError(code: .protocolError)
|
||||
}
|
||||
guard state.accumulatedBytes.readableBytes >= state.header.length else {
|
||||
return .needMoreData
|
||||
|
||||
@@ -78,6 +78,7 @@ extension HTTP2FrameParserTests {
|
||||
("testWindowUpdateFrameEncoding", testWindowUpdateFrameEncoding),
|
||||
("testHeadersContinuationFrameDecoding", testHeadersContinuationFrameDecoding),
|
||||
("testPushPromiseContinuationFrameDecoding", testPushPromiseContinuationFrameDecoding),
|
||||
("testUnsolicitedContinuationFrame", testUnsolicitedContinuationFrame),
|
||||
("testAltServiceFrameDecoding", testAltServiceFrameDecoding),
|
||||
("testAltServiceFrameDecodingFailure", testAltServiceFrameDecodingFailure),
|
||||
("testAltServiceFrameEncoding", testAltServiceFrameEncoding),
|
||||
|
||||
@@ -1460,6 +1460,27 @@ class HTTP2FrameParserTests: XCTestCase {
|
||||
|
||||
self.assertEqualFrames(frame, expectedFrame)
|
||||
}
|
||||
|
||||
func testUnsolicitedContinuationFrame() throws {
|
||||
var headers = self.byteBuffer(withBytes: self.simpleHeadersEncoded)
|
||||
|
||||
let frameBytes: [UInt8] = [
|
||||
0x00, 0x00, 0x0a, // 3-byte payload length (17 bytes)
|
||||
0x09, // 1-byte frame type (CONTINUATION)
|
||||
0x00, // 1-byte flags (none)
|
||||
0x00, 0x00, 0x00, 0x01, // 4-byte stream identifier
|
||||
]
|
||||
var buf = byteBuffer(withBytes: frameBytes, extraCapacity: 10)
|
||||
buf.writeBuffer(&headers)
|
||||
|
||||
var decoder = HTTP2FrameDecoder(allocator: self.allocator, expectClientMagic: false)
|
||||
|
||||
// should throw
|
||||
decoder.append(bytes: &buf)
|
||||
XCTAssertThrowsError(try decoder.nextFrame()) { error in
|
||||
XCTAssertEqual(error as? InternalError, .codecError(code: .protocolError))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ALTSVC frames
|
||||
|
||||
|
||||
Reference in new issue
Block a user