Skip to content

Commit

Permalink
Correctly mark 304 as not having a response body (#2737)
Browse files Browse the repository at this point in the history
* Fixed an issue where HTTP/2 connections would disconnect if response compression were used and a 304 Not Modified response was returned

* Added HTTP encoder tests for 304 Not Modified responses
  • Loading branch information
dimitribouniol committed Jun 12, 2024
1 parent 2b5d277 commit 72cab7d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/NIOHTTP1/HTTPTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ public enum HTTPResponseStatus: Sendable {
.switchingProtocols,
.processing,
.noContent,
.notModified,
.custom where (code < 200) && (code >= 100):
return false
default:
Expand Down
12 changes: 12 additions & 0 deletions Tests/NIOHTTP1Tests/HTTPResponseEncoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ class HTTPResponseEncoderTests: XCTestCase {
writtenData.assertContainsOnly("HTTP/1.1 204 No Content\r\ncontent-length: 0\r\n\r\n")
}

func testNoContentLengthHeadersFor304() throws {
let headers = HTTPHeaders([("content-length", "0")])
let writtenData = sendResponse(withStatus: .notModified, andHeaders: headers)
writtenData.assertContainsOnly("HTTP/1.1 304 Not Modified\r\n\r\n")
}

func testNoTransferEncodingHeadersFor101() throws {
let headers = HTTPHeaders([("transfer-encoding", "chunked")])
let writtenData = sendResponse(withStatus: .switchingProtocols, andHeaders: headers)
Expand Down Expand Up @@ -153,6 +159,12 @@ class HTTPResponseEncoderTests: XCTestCase {
writtenData.assertContainsOnly("HTTP/1.1 204 No Content\r\ntransfer-encoding: chunked\r\n\r\n")
}

func testNoTransferEncodingHeadersFor304() throws {
let headers = HTTPHeaders([("transfer-encoding", "chunked")])
let writtenData = sendResponse(withStatus: .notModified, andHeaders: headers)
writtenData.assertContainsOnly("HTTP/1.1 304 Not Modified\r\n\r\n")
}

func testNoChunkedEncodingForHTTP10() throws {
let channel = EmbeddedChannel()
defer {
Expand Down

0 comments on commit 72cab7d

Please sign in to comment.