mirror of
https://github.com/apple/swift-nio
synced 2026-11-26 04:57:49 +00:00
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:
1 parent
b174051b81
commit
c1d86289ed
3 files changed
+134
-1
No files matched your search
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user