HTTP: assert that user doesn't set CL & TE:chunked (#1556)

Motivation:

NIO users need to understand the semantic of the underlying protocol.
Therefore NIO does not (and in many case cannot) check all the
invariants. Generally however, we try to be helpful and assert a bunch
of things that a user has (hopefully) checked. That doesn't affect
performance and simplifies development for users.

Modifications:

- assert that request/response heads don't set content-length +
  transfer-encoding: chunked

Result:

Developers of libraries like AHC will have an easier time.
This commit is contained in:
Johannes Weiss authored and GitHub committed 2020-06-15 12:36:19 +01:00
1 parent 5268726898
commit 7b93c22836
4 files changed
+57 -4

No files matched your search

+10 -3
View File
@@ -143,7 +143,11 @@ 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
assert(!(request.headers.contains(name: "content-length") &&
request.headers[canonicalForm: "transfer-encoding"].contains("chunked"[...])),
"illegal HTTP sent: \(request) contains both a content-length and transfer-encoding:chunked")
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)
@@ -177,8 +181,11 @@ public final class HTTPResponseEncoder: ChannelOutboundHandler, RemovableChannel
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
switch self.unwrapOutboundIn(data) {
case .head(var response):
self.isChunked = sanitizeTransportHeaders(hasBody: response.status.mayHaveResponseBody ? .yes : .no, headers: &response.headers, version: response.version) == .chunked
assert(!(response.headers.contains(name: "content-length") &&
response.headers[canonicalForm: "transfer-encoding"].contains("chunked"[...])),
"illegal HTTP sent: \(response) contains both a content-length and transfer-encoding:chunked")
self.isChunked = sanitizeTransportHeaders(hasBody: response.status.mayHaveResponseBody ? .yes : .no,
headers: &response.headers, version: response.version) == .chunked
writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in
buffer.write(response: response)