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

Adds hostname extraction for ‘forwarded’ header #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Gatekeeper/Request+Hostname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Vapor

extension Request {
var hostname: String? {
headers.first(name: .xForwardedFor) ?? remoteAddress?.hostname
headers.forwarded.first?.for ?? headers.first(name: .xForwardedFor) ?? remoteAddress?.hostname
}
}
21 changes: 21 additions & 0 deletions Tests/GatekeeperTests/GatekeeperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ class GatekeeperTests: XCTestCase {
})
}

func testGateKeeperForwardedSupported() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.gatekeeper.config = .init(maxRequests: 10, per: .second)

app.grouped(GatekeeperMiddleware()).get("test") { req -> HTTPStatus in
return .ok
}

try app.test(
.GET,
"test",
beforeRequest: { req in
req.headers.forwarded = [HTTPHeaders.Forwarded(for: "\"[::1]\"")]
},
afterResponse: { res in
XCTAssertEqual(res.status, .ok)
}
)
}

func testGateKeeperCountRefresh() throws {
let app = Application(.testing)
defer { app.shutdown() }
Expand Down