|
3 | 3 | // Noze.io / Macro |
4 | 4 | // |
5 | 5 | // Created by Helge Heß on 6/2/16. |
6 | | -// Copyright © 2016-2024 ZeeZide GmbH. All rights reserved. |
| 6 | +// Copyright © 2016-2026 ZeeZide GmbH. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
9 | 9 | #if canImport(Foundation) |
10 | 10 | import Foundation |
11 | 11 | #endif |
12 | | -import class http.IncomingMessage |
| 12 | +import http |
13 | 13 | import NIOHTTP1 |
14 | | - |
| 14 | +import NIOCore |
15 | 15 |
|
16 | 16 | public extension IncomingMessage { |
17 | 17 |
|
18 | 18 | typealias Params = ExpressWrappedDictionary<String> |
19 | 19 | typealias Query = ExpressWrappedDictionary<Any> |
20 | 20 |
|
21 | | - // TODO: baseUrl, originalUrl, path |
22 | | - // TODO: hostname, ip, ips, protocol |
| 21 | + /// The hostname from the `Host` header, or |
| 22 | + /// `X-Forwarded-Host` when trust proxy is enabled. |
| 23 | + @inlinable |
| 24 | + var hostname: String? { |
| 25 | + let host: String |
| 26 | + if app?.settings.trustProxy ?? false, |
| 27 | + let fh = headers["X-Forwarded-Host"].first |
| 28 | + { |
| 29 | + if let i = fh.firstIndex(of: ",") { host = String(fh[..<i]) } |
| 30 | + else { host = fh } |
| 31 | + } |
| 32 | + else if let h = headers["Host"].first { host = h } |
| 33 | + else { return nil } |
| 34 | + |
| 35 | + // IPv6 bracket notation: strip port after `]` |
| 36 | + if host.hasPrefix("[") { |
| 37 | + guard let b = host.firstIndex(of: "]") else { return host } |
| 38 | + let a = host.index(after: b) |
| 39 | + if a < host.endIndex, host[a] == ":" { return String(host[..<a]) } |
| 40 | + return String(host[...b]) |
| 41 | + } |
| 42 | + if let i = host.firstIndex(of: ":") { |
| 43 | + return String(host[..<i]) |
| 44 | + } |
| 45 | + return host |
| 46 | + } |
23 | 47 |
|
24 | | - // TODO: originalUrl, path |
25 | | - // TODO: hostname, ip, ips, protocol |
| 48 | + /// The client IP. Uses `X-Forwarded-For` when trust |
| 49 | + /// proxy is enabled. |
| 50 | + @inlinable |
| 51 | + var ip: String? { |
| 52 | + if app?.settings.trustProxy ?? false, |
| 53 | + let xff = headers["X-Forwarded-For"].first |
| 54 | + { |
| 55 | + if let i = xff.firstIndex(of: ",") { return String(xff[..<i]) } |
| 56 | + return xff |
| 57 | + } |
| 58 | + return socket?.remoteAddress?.ipAddress |
| 59 | + } |
| 60 | + |
| 61 | + /// All IPs from `X-Forwarded-For` when trust proxy |
| 62 | + /// is enabled, empty otherwise. |
| 63 | + @inlinable |
| 64 | + var ips: [ String ] { |
| 65 | + guard app?.settings.trustProxy ?? false, |
| 66 | + let xff = headers["X-Forwarded-For"].first else { return [] } |
| 67 | + return xff.split(separator: ",").map { part in |
| 68 | + String(part.drop(while: { $0.isWhitespace })) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * `"https"` when behind a trusted proxy setting |
| 74 | + * `X-Forwarded-Proto`, `"http"` otherwise. |
| 75 | + */ |
| 76 | + @inlinable |
| 77 | + var `protocol`: String { |
| 78 | + if app?.settings.trustProxy ?? false, |
| 79 | + let proto = headers["X-Forwarded-Proto"].first |
| 80 | + { |
| 81 | + if let i = proto.firstIndex(of: ",") { |
| 82 | + return String(proto[..<i]) |
| 83 | + } |
| 84 | + return proto |
| 85 | + } |
| 86 | + return "http" |
| 87 | + } |
| 88 | + |
| 89 | + /// The URL path without the query string. |
| 90 | + @inlinable |
| 91 | + var path: String { |
| 92 | + guard let qIdx = url.firstIndex(of: "?") else { |
| 93 | + return url.isEmpty ? "/" : url |
| 94 | + } |
| 95 | + let p = String(url[..<qIdx]) |
| 96 | + return p.isEmpty ? "/" : p |
| 97 | + } |
26 | 98 |
|
27 | 99 | /// A reference to the active application. Updated when subapps are triggered. |
28 | 100 | var app : Express? { return environment[ExpressExtKey.App.self] } |
@@ -62,12 +134,12 @@ public extension IncomingMessage { |
62 | 134 | // FIXME: improve parser (fragments?!) |
63 | 135 | // TBD: just use Foundation?! |
64 | 136 | guard let idx = url.firstIndex(of: "?") else { |
65 | | - environment[ExpressExtKey.Query.self] = .init([:]) |
| 137 | + environment[ExpressExtKey.Query.self] = Query([:]) |
66 | 138 | return Query([:]) |
67 | 139 | } |
68 | 140 | let q = url[url.index(after: idx)...] |
69 | 141 | let qp = qs.parse(String(q)) |
70 | | - environment[ExpressExtKey.Query.self] = .init(qp) |
| 142 | + environment[ExpressExtKey.Query.self] = Query(qp) |
71 | 143 | return Query(qp) |
72 | 144 | } |
73 | 145 |
|
|
0 commit comments