From 6a97259a5dfd9825d7024405363d083771a38f8b Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Fri, 10 May 2019 16:56:14 +0100 Subject: [PATCH] Avoid double-counting of flow controlled length of synthesised data frames. (#114) Motivation: When synthesising data frames we try to emit the flow controlled length of the entire real data frame on the first synthetic frame. This allows us to correctly police the window management of that frame, while still synthesising frames. However, @glbrntt spotted that we were incorrectly repeatedly counting the flow controlled length of a partial frame. A patch that had been previously added to ensure we only counted it once was not working correctly. Modifications: - Adjusted the location of the call to flowControlledLength. Result: Correct accounting of flow controlled length --- Sources/NIOHTTP2/HTTP2FrameParser.swift | 7 ++++- .../HTTP2FrameParserTests+XCTest.swift | 1 + .../NIOHTTP2Tests/HTTP2FrameParserTests.swift | 31 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Sources/NIOHTTP2/HTTP2FrameParser.swift b/Sources/NIOHTTP2/HTTP2FrameParser.swift index 20cfd4c..2fcfc26 100644 --- a/Sources/NIOHTTP2/HTTP2FrameParser.swift +++ b/Sources/NIOHTTP2/HTTP2FrameParser.swift @@ -481,6 +481,11 @@ struct HTTP2FrameDecoder { var frameBytes: ByteBuffer var nextState: ParserState var flags: FrameFlags = state.header.flags + + // We extract the flow controlled length early because we only ever emit it once for a given frame. + // This operation mutates the flow controlled length and sets it to zero, so it will always give an appropriate result. + let flowControlledLength = state.flowControlledLength() + if state.payload.readableBytes >= state.remainingByteCount { // read all the bytes for this last frame frameBytes = state.payload.readSlice(length: state.remainingByteCount - Int(state.expectedPadding))! @@ -510,7 +515,7 @@ struct HTTP2FrameDecoder { let dataPayload = HTTP2Frame.FramePayload.Data(data: .byteBuffer(frameBytes), endStream: flags.contains(.endStream), paddingBytes: nil) let outputFrame = HTTP2Frame(streamID: streamID, payload: .data(dataPayload)) self.state = nextState - return .frame(outputFrame, flowControlledLength: state.flowControlledLength()) + return .frame(outputFrame, flowControlledLength: flowControlledLength) case .accumulatingContinuationPayload(var state): guard state.continuationHeader.length <= state.continuationPayload.readableBytes else { diff --git a/Tests/NIOHTTP2Tests/HTTP2FrameParserTests+XCTest.swift b/Tests/NIOHTTP2Tests/HTTP2FrameParserTests+XCTest.swift index b30244d..6eae445 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FrameParserTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FrameParserTests+XCTest.swift @@ -93,6 +93,7 @@ extension HTTP2FrameParserTests { ("testHeaderAndContinuationsInOneBuffer", testHeaderAndContinuationsInOneBuffer), ("testPushPromiseAndContinuationsInOneBuffer", testPushPromiseAndContinuationsInOneBuffer), ("testMultipleFramesInOneBuffer", testMultipleFramesInOneBuffer), + ("testCorrectlyAccountForFlowControlledLength", testCorrectlyAccountForFlowControlledLength), ] } } diff --git a/Tests/NIOHTTP2Tests/HTTP2FrameParserTests.swift b/Tests/NIOHTTP2Tests/HTTP2FrameParserTests.swift index 01638d7..b587e7d 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FrameParserTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FrameParserTests.swift @@ -1962,4 +1962,35 @@ class HTTP2FrameParserTests: XCTestCase { XCTAssertNil(noFrame) } + func testCorrectlyAccountForFlowControlledLength() throws { + var payload = ByteBufferAllocator().buffer(capacity: 2) + payload.writeBytes(repeatElement(UInt8(0), count: 2)) + + let frameBytes: [UInt8] = [ + 0x00, 0x00, 0x02, // 3-byte payload length (2 bytes) + 0x00, // 1-byte frame type (DATA) + 0x00, // 0-byte flags + 0x00, 0x00, 0x00, 0x01, // 4-byte stream identifier + ] + var buf = byteBuffer(withBytes: frameBytes, extraCapacity: payload.readableBytes) + buf.writeBytes(payload.readableBytesView) + + var decoder = HTTP2FrameDecoder(allocator: self.allocator, expectClientMagic: false) + var slice = buf.readSlice(length: 10)! + decoder.append(bytes: &slice) + guard let result = try assertNoThrowWithValue(decoder.nextFrame()) else { + XCTFail("Failed to parse frame") + return + } + XCTAssertEqual(result.flowControlledLength, 2) + + slice = buf.readSlice(length: 1)! + decoder.append(bytes: &slice) + guard let result2 = try decoder.nextFrame() else { + XCTFail("Failed to parse frame") + return + } + XCTAssertEqual(result2.flowControlledLength, 0) + } + }