HTTPEncoder: fix 0 length chunks (#1524)

Motivation:

Previously, if the user sent us a 0 length chunk, we would encode it as
`0\r\n\r\n` which means the _end_ of the body in chunked encoding. But
the end in NIO is send by sending `.end(...)`.

Modifications:

Don't write anything for 0 length writes.

Result:

More correct behaviour with 0 length body chunks.
This commit is contained in:
Johannes Weiss authored and GitHub committed 2020-05-18 18:50:43 +01:00
1 parent b174051b81
commit c1d86289ed
3 files changed
+134 -1

No files matched your search

+6 -1
View File
@@ -143,13 +143,18 @@ public final class HTTPRequestEncoder: ChannelOutboundHandler, RemovableChannelH
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
switch self.unwrapOutboundIn(data) {
case .head(var request):
self.isChunked = sanitizeTransportHeaders(hasBody: request.method.hasRequestBody, headers: &request.headers, version: request.version) == .chunked
writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in
buffer.write(request: request)
}, context: context, headers: request.headers, promise: promise)
case .body(let bodyPart):
guard bodyPart.readableBytes > 0 else {
// Empty writes shouldn't send any bytes in chunked or identity encoding.
context.write(self.wrapOutboundOut(bodyPart), promise: promise)
return
}
writeChunk(wrapOutboundOut: self.wrapOutboundOut, context: context, isChunked: self.isChunked, chunk: bodyPart, promise: promise)
case .end(let trailers):
writeTrailers(wrapOutboundOut: self.wrapOutboundOut, context: context, isChunked: self.isChunked, trailers: trailers, promise: promise)