Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 9009cfb

Browse files
Merge pull request #22 from NeedleInAJayStack/feature/forwardedHeader
Adds hostname extraction for ‘forwarded’ header
2 parents 1bf4692 + f5a52f6 commit 9009cfb

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Sources/Gatekeeper/Request+Hostname.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import Vapor
22

33
extension Request {
44
var hostname: String? {
5-
headers.first(name: .xForwardedFor) ?? remoteAddress?.hostname
5+
headers.forwarded.first?.for ?? headers.first(name: .xForwardedFor) ?? remoteAddress?.hostname
66
}
77
}

Tests/GatekeeperTests/GatekeeperTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ class GatekeeperTests: XCTestCase {
3737
})
3838
}
3939

40+
func testGateKeeperForwardedSupported() throws {
41+
let app = Application(.testing)
42+
defer { app.shutdown() }
43+
app.gatekeeper.config = .init(maxRequests: 10, per: .second)
44+
45+
app.grouped(GatekeeperMiddleware()).get("test") { req -> HTTPStatus in
46+
return .ok
47+
}
48+
49+
try app.test(
50+
.GET,
51+
"test",
52+
beforeRequest: { req in
53+
req.headers.forwarded = [HTTPHeaders.Forwarded(for: "\"[::1]\"")]
54+
},
55+
afterResponse: { res in
56+
XCTAssertEqual(res.status, .ok)
57+
}
58+
)
59+
}
60+
4061
func testGateKeeperCountRefresh() throws {
4162
let app = Application(.testing)
4263
defer { app.shutdown() }

0 commit comments

Comments
 (0)