mirror of
https://github.com/apple/swift-nio
synced 2026-11-27 05:04:22 +00:00
Don't require casing to match in the HTTP upgrade header. (#1)
Motivation: The HTTP upgrade handler should not require that we case-match with the mandatory headers. Modifications: Treat all headers as lowercase. Result: Upgrade is easier.
This commit is contained in:
1 parent
de0bc1ed83
commit
c8c8dbf2a2
3 files changed
+33
-1
No files matched your search
@@ -125,7 +125,7 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler {
|
||||
continue
|
||||
}
|
||||
|
||||
let requiredHeaders = Set(upgrader.requiredUpgradeHeaders)
|
||||
let requiredHeaders = Set(upgrader.requiredUpgradeHeaders.map { $0.lowercased() })
|
||||
guard requiredHeaders.isSubset(of: allHeaderNames) && requiredHeaders.isSubset(of: connectionHeader) else {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ extension HTTPUpgradeTestCase {
|
||||
("testUpgradeRespectsClientPreference", testUpgradeRespectsClientPreference),
|
||||
("testUpgradeFiresUserEvent", testUpgradeFiresUserEvent),
|
||||
("testUpgraderCanRejectUpgradeForPersonalReasons", testUpgraderCanRejectUpgradeForPersonalReasons),
|
||||
("testUpgradeIsCaseInsensitive", testUpgradeIsCaseInsensitive),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -504,4 +504,35 @@ class HTTPUpgradeTestCase: XCTestCase {
|
||||
XCTFail("Unexpected error: \(errorCatcher.errors[0])")
|
||||
}
|
||||
}
|
||||
|
||||
func testUpgradeIsCaseInsensitive() throws {
|
||||
let upgrader = SuccessfulUpgrader(forProtocol: "myproto", requiringHeaders: ["WeIrDcAsE"]) { req in }
|
||||
let handler = HTTPServerUpgradeHandler(upgraders: [upgrader]) { ctx in
|
||||
ctx.close(promise: nil)
|
||||
}
|
||||
let (group, server, client) = try setUpTest(withHandlers: [handler])
|
||||
defer {
|
||||
XCTAssertNoThrow(try group.syncShutdownGracefully())
|
||||
}
|
||||
|
||||
let completePromise: EventLoopPromise<Void> = group.next().newPromise()
|
||||
let clientHandler = ArrayAccumulationHandler<ByteBuffer> { buffers in
|
||||
let resultString = buffers.map { $0.getString(at: $0.readerIndex, length: $0.readableBytes)! }.joined(separator: "")
|
||||
assertResponseIs(response: resultString,
|
||||
expectedResponseLine: "HTTP/1.1 101 Switching Protocols",
|
||||
expectedResponseHeaders: ["x-upgrade-complete: true", "upgrade: myproto", "connection: upgrade"])
|
||||
completePromise.succeed(result: ())
|
||||
}
|
||||
XCTAssertNoThrow(try client.pipeline.add(handler: clientHandler).wait())
|
||||
|
||||
// This request is safe to upgrade.
|
||||
let request = "OPTIONS * HTTP/1.1\r\nHost: localhost\r\nUpgrade: myproto\r\nWeirdcase: yup\r\nConnection: upgrade,weirdcase\r\n\r\n"
|
||||
XCTAssertNoThrow(try client.writeAndFlush(ByteBuffer.forString(request)).wait())
|
||||
|
||||
// Let the machinery do its thing.
|
||||
XCTAssertNoThrow(try completePromise.futureResult.wait())
|
||||
|
||||
// We also want to confirm that the upgrade handler is no longer in the pipeline.
|
||||
try client.pipeline.assertDoesNotContain(handler: handler)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user