Skip to content

Commit

Permalink
Skip HTTPResponseCompressor logic if response is 204 (no content) (#105)
Browse files Browse the repository at this point in the history
* Skip HTTPResponseCompressor logic if response is 204 (no content)

* update test manifests

* use mayHaveResponseBody
  • Loading branch information
tanner0101 authored Aug 14, 2020
1 parent f700f5b commit 0b9eb87
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/NIOHTTPCompression/HTTPResponseCompressor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public final class HTTPResponseCompressor: ChannelDuplexHandler, RemovableChanne
let httpData = unwrapOutboundIn(data)
switch httpData {
case .head(var responseHead):
guard let algorithm = compressionAlgorithm() else {
guard let algorithm = compressionAlgorithm(), responseHead.status.mayHaveResponseBody else {
context.write(wrapOutboundOut(.head(responseHead)), promise: promise)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extension HTTPResponseCompressorTest {
("testStartsWithSameUnicodeScalarsSaysNoForTheSameStringInDifferentNormalisations", testStartsWithSameUnicodeScalarsSaysNoForTheSameStringInDifferentNormalisations),
("testStartsWithSaysYesForTheSameStringInDifferentNormalisations", testStartsWithSaysYesForTheSameStringInDifferentNormalisations),
("testCanBeRemoved", testCanBeRemoved),
("testBypassCompressionWhenNoContent", testBypassCompressionWhenNoContent),
]
}
}
Expand Down
26 changes: 26 additions & 0 deletions Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,32 @@ class HTTPResponseCompressorTest: XCTestCase {
try sendRequest(acceptEncoding: "deflate", channel: channel)
try assertUncompressedResponse(channel: channel)
}

func testBypassCompressionWhenNoContent() throws {
let channel = EmbeddedChannel()
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseCompressor()).wait())
defer {
XCTAssertNoThrow(try channel.finish())
}

try sendRequest(acceptEncoding: "deflate;q=2.2, gzip;q=0.3", channel: channel)

let head = HTTPResponseHead(version: .init(major: 1, minor: 1),
status: .noContent,
headers: .init())
try channel.writeOutbound(HTTPServerResponsePart.head(head))
try channel.writeOutbound(HTTPServerResponsePart.end(nil))

while let part = try channel.readOutbound(as: HTTPServerResponsePart.self) {
switch part {
case .head(let head):
XCTAssertEqual(head.headers[canonicalForm: "content-encoding"], [])
case .body:
XCTFail("Unexpected body")
case .end: break
}
}
}
}

extension EventLoopFuture {
Expand Down

0 comments on commit 0b9eb87

Please sign in to comment.