diff --git a/Sources/NIOHTTP1/HTTPEncoder.swift b/Sources/NIOHTTP1/HTTPEncoder.swift index f0e9d0350..87205fca5 100644 --- a/Sources/NIOHTTP1/HTTPEncoder.swift +++ b/Sources/NIOHTTP1/HTTPEncoder.swift @@ -387,7 +387,7 @@ extension ByteBuffer { case (1, 0, .useProxy): self.writeStaticString("HTTP/1.0 305 Use Proxy\r\n") case (1, 0, .temporaryRedirect): - self.writeStaticString("HTTP/1.0 307 Tempory Redirect\r\n") + self.writeStaticString("HTTP/1.0 307 Temporary Redirect\r\n") case (1, 0, .permanentRedirect): self.writeStaticString("HTTP/1.0 308 Permanent Redirect\r\n") case (1, 0, .badRequest): @@ -465,7 +465,7 @@ extension ByteBuffer { case (1, 0, .notExtended): self.writeStaticString("HTTP/1.0 510 Not Extended\r\n") case (1, 0, .networkAuthenticationRequired): - self.writeStaticString("HTTP/1.1 511 Network Authentication Required\r\n") + self.writeStaticString("HTTP/1.0 511 Network Authentication Required\r\n") // Optimization for HTTP/1.1 case (1, 1, .custom(_, _)): @@ -511,7 +511,7 @@ extension ByteBuffer { case (1, 1, .useProxy): self.writeStaticString("HTTP/1.1 305 Use Proxy\r\n") case (1, 1, .temporaryRedirect): - self.writeStaticString("HTTP/1.1 307 Tempory Redirect\r\n") + self.writeStaticString("HTTP/1.1 307 Temporary Redirect\r\n") case (1, 1, .permanentRedirect): self.writeStaticString("HTTP/1.1 308 Permanent Redirect\r\n") case (1, 1, .badRequest): @@ -547,7 +547,7 @@ extension ByteBuffer { case (1, 1, .unsupportedMediaType): self.writeStaticString("HTTP/1.1 415 Unsupported Media Type\r\n") case (1, 1, .rangeNotSatisfiable): - self.writeStaticString("HTTP/1.1 416 Request Range Not Satisified\r\n") + self.writeStaticString("HTTP/1.1 416 Range Not Satisfiable\r\n") case (1, 1, .expectationFailed): self.writeStaticString("HTTP/1.1 417 Expectation Failed\r\n") case (1, 1, .misdirectedRequest): @@ -565,7 +565,7 @@ extension ByteBuffer { case (1, 1, .tooManyRequests): self.writeStaticString("HTTP/1.1 429 Too Many Requests\r\n") case (1, 1, .requestHeaderFieldsTooLarge): - self.writeStaticString("HTTP/1.1 431 Range Not Satisfiable\r\n") + self.writeStaticString("HTTP/1.1 431 Request Header Fields Too Large\r\n") case (1, 1, .unavailableForLegalReasons): self.writeStaticString("HTTP/1.1 451 Unavailable For Legal Reasons\r\n") case (1, 1, .internalServerError): diff --git a/Tests/NIOHTTP1Tests/HTTPResponseEncoderTest.swift b/Tests/NIOHTTP1Tests/HTTPResponseEncoderTest.swift index e7dab98ef..7a2496726 100644 --- a/Tests/NIOHTTP1Tests/HTTPResponseEncoderTest.swift +++ b/Tests/NIOHTTP1Tests/HTTPResponseEncoderTest.swift @@ -37,6 +37,7 @@ class HTTPResponseEncoderTests: XCTestCase { private func sendResponse( withStatus status: HTTPResponseStatus, andHeaders headers: HTTPHeaders, + version: HTTPVersion = .http1_1, configuration: HTTPResponseEncoder.Configuration = .init() ) -> ByteBuffer { let channel = EmbeddedChannel() @@ -47,7 +48,7 @@ class HTTPResponseEncoderTests: XCTestCase { XCTAssertNoThrow( try channel.pipeline.syncOperations.addHandler(HTTPResponseEncoder(configuration: configuration)) ) - var switchingResponse = HTTPResponseHead(version: .http1_1, status: status) + var switchingResponse = HTTPResponseHead(version: version, status: status) switchingResponse.headers = headers XCTAssertNoThrow(try channel.writeOutbound(HTTPServerResponsePart.head(switchingResponse))) do { @@ -64,6 +65,47 @@ class HTTPResponseEncoderTests: XCTestCase { } } + /// The encoder writes hand-optimised status lines for the well-known statuses (rather than + /// going through the general `default` path that uses `HTTPResponseStatus.reasonPhrase`). Those + /// fast paths must stay in lockstep with `reasonPhrase` and must carry the correct HTTP version, + /// otherwise the bytes on the wire depend on whether a status happens to have a fast path. + func testStatusLineFastPathsMatchCanonicalReasonPhraseAndVersion() throws { + let statuses: [HTTPResponseStatus] = [ + .continue, .switchingProtocols, .processing, + .ok, .created, .accepted, .nonAuthoritativeInformation, .noContent, .resetContent, + .partialContent, .multiStatus, .alreadyReported, .imUsed, + .multipleChoices, .movedPermanently, .found, .seeOther, .notModified, .useProxy, + .temporaryRedirect, .permanentRedirect, + .badRequest, .unauthorized, .paymentRequired, .forbidden, .notFound, .methodNotAllowed, + .notAcceptable, .proxyAuthenticationRequired, .requestTimeout, .conflict, .gone, + .lengthRequired, .preconditionFailed, .payloadTooLarge, .uriTooLong, .unsupportedMediaType, + .rangeNotSatisfiable, .expectationFailed, .imATeapot, .misdirectedRequest, + .unprocessableEntity, .locked, .failedDependency, .upgradeRequired, .preconditionRequired, + .tooManyRequests, .requestHeaderFieldsTooLarge, .unavailableForLegalReasons, + .internalServerError, .notImplemented, .badGateway, .serviceUnavailable, .gatewayTimeout, + .httpVersionNotSupported, .variantAlsoNegotiates, .insufficientStorage, .loopDetected, + .notExtended, .networkAuthenticationRequired, + ] + + for version in [HTTPVersion.http1_0, .http1_1] { + for status in statuses { + let written = sendResponse( + withStatus: status, + andHeaders: HTTPHeaders(), + version: version + ) + let bytes = String(buffer: written) + let statusLine = bytes.components(separatedBy: "\r\n").first ?? "" + let expected = "HTTP/\(version.major).\(version.minor) \(status.code) \(status.reasonPhrase)" + XCTAssertEqual( + statusLine, + expected, + "status line for \(status) on HTTP/\(version.major).\(version.minor) did not match canonical form" + ) + } + } + } + func testNoAutoHeadersFor101() throws { let writtenData = sendResponse(withStatus: .switchingProtocols, andHeaders: HTTPHeaders()) writtenData.assertContainsOnly("HTTP/1.1 101 Switching Protocols\r\n\r\n")