mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
Strip Transfer-Encoding when Content-Length is present in HTTPEncoder (#3583)
Strip Transfer-Encoding when Content-Length is present in HTTPEncoder ### Motivation When `correctlyFrameTransportHeaders()` detected a `Content-Length` header, it returned `.contentLength` without removing any existing `Transfer-Encoding` header. ### Changes - Remove `Transfer-Encoding` header, if a `Content-Length` header is present ### Result NIO sends either a `Content-Length` header or a `Transfer-Encoding` header, but not both.
This commit is contained in:
1 parent
1c8b29ce10
commit
37e191d35b
10 files changed
+91
-135
No files matched your search
@@ -133,6 +133,7 @@ private func correctlyFrameTransportHeaders(
|
||||
return .neither
|
||||
case .yes:
|
||||
if headers.contains(name: "content-length") {
|
||||
headers.remove(name: "transfer-encoding")
|
||||
return .contentLength
|
||||
}
|
||||
if version.major == 1 && version.minor >= 1 {
|
||||
@@ -143,6 +144,7 @@ private func correctlyFrameTransportHeaders(
|
||||
}
|
||||
case .unlikely:
|
||||
if headers.contains(name: "content-length") {
|
||||
headers.remove(name: "transfer-encoding")
|
||||
return .contentLength
|
||||
}
|
||||
if version.major == 1 && version.minor >= 1 {
|
||||
@@ -204,11 +206,6 @@ public final class HTTPRequestEncoder: ChannelOutboundHandler, RemovableChannelH
|
||||
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
switch HTTPRequestEncoder.unwrapOutboundIn(data) {
|
||||
case .head(var request):
|
||||
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"
|
||||
)
|
||||
if self.configuration.automaticallySetFramingHeaders {
|
||||
self.isChunked =
|
||||
correctlyFrameTransportHeaders(
|
||||
@@ -294,12 +291,6 @@ public final class HTTPResponseEncoder: ChannelOutboundHandler, RemovableChannel
|
||||
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
switch HTTPResponseEncoder.unwrapOutboundIn(data) {
|
||||
case .head(var response):
|
||||
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"
|
||||
)
|
||||
|
||||
if self.configuration.automaticallySetFramingHeaders {
|
||||
self.isChunked =
|
||||
correctlyFrameTransportHeaders(
|
||||
|
||||
Reference in new issue
Block a user