Skip to content

Commit

Permalink
Handle EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
normanmaurer committed May 3, 2018
1 parent 0e864a6 commit e3d2f5e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Sources/NIOHTTP1/HTTPDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public class HTTPDecoder<HTTPMessageT>: 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
Expand All @@ -421,6 +421,12 @@ public class HTTPDecoder<HTTPMessageT>: 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
Expand Down Expand Up @@ -457,7 +463,7 @@ public class HTTPDecoder<HTTPMessageT>: 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.
Expand Down Expand Up @@ -544,6 +550,10 @@ public class HTTPDecoder<HTTPMessageT>: 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)

Expand Down

0 comments on commit e3d2f5e

Please sign in to comment.