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

Commit

Permalink
Adds hostname extraction for ‘forwarded’ header
Browse files Browse the repository at this point in the history
  • Loading branch information
NeedleInAJayStack committed Aug 24, 2021
1 parent 02ec165 commit f5a52f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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

0 comments on commit f5a52f6

Please sign in to comment.