From e3d2f5e3c54104194b6a5f7a76355907a336ad5e Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 3 May 2018 10:19:00 +0200 Subject: [PATCH] Handle EOF --- Sources/NIOHTTP1/HTTPDecoder.swift | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sources/NIOHTTP1/HTTPDecoder.swift b/Sources/NIOHTTP1/HTTPDecoder.swift index 577016f102..f72e883f39 100644 --- a/Sources/NIOHTTP1/HTTPDecoder.swift +++ b/Sources/NIOHTTP1/HTTPDecoder.swift @@ -397,7 +397,7 @@ public class HTTPDecoder: ByteToMessageDecoder, AnyHTTPDecoder { } // Decode HTTP until there is nothing more to decode. - private func decodeHTTP() throws { + private func decodeHTTP(ctx: ChannelHandlerContext) throws { // We need to refetch the cumulationBuffer on each loop as it may has changed due re-entrance calls of channelRead(...) while let bufferSlice = self.cumulationBuffer, bufferSlice.readableBytes > 0 { let result = try bufferSlice.withVeryUnsafeBytes { (pointer) -> size_t in @@ -421,6 +421,12 @@ public class HTTPDecoder: ByteToMessageDecoder, AnyHTTPDecoder { return result } + guard !self.state.seenEOF else { + // We need to notify the parser about the EOF as we received it while in http_parser_excecute. + self.notifyParserEOF(ctx: ctx) + break + } + guard self.cumulationBuffer != nil else { // Guard against the case of closing the channel while still in the decode loop. In this case the cumulationBuffer will be nil. break @@ -457,7 +463,7 @@ public class HTTPDecoder: ByteToMessageDecoder, AnyHTTPDecoder { } do { - try self.decodeHTTP() + try self.decodeHTTP(ctx: ctx) guard self.cumulationBuffer != nil else { // Guard against the case of closing the channel. In this case the cumulationBuffer will be nil. @@ -544,6 +550,10 @@ public class HTTPDecoder: ByteToMessageDecoder, AnyHTTPDecoder { return } + self.notifyParserEOF(ctx: ctx) + } + + private func notifyParserEOF(ctx: ChannelHandlerContext) { self.state.baseAddress = nil _ = c_nio_http_parser_execute(&self.parser, &self.settings, nil, 0)