| @@ -1,12 +1,13 @@ |
| 1 | 1 | import std/bytes |
| 2 | 2 | import std/http.{type Request, Request} |
| 3 | 3 | import std/io |
| 4 | | −import std/net.{SocketAddress} |
| 4 | +import std/net.{type SocketAddress, SocketAddress} |
| 5 | 5 | |
| 6 | 6 | // The client's address (issue #308), on requests built by hand: the |
| 7 | | −// server puts the peer first, as the `:peer` pseudo-header. |
| 8 | | −fn request(peer: String, headers: List<(String, String)>) -> Request { |
| 9 | | − Request("GET", "/", [(":peer", peer), ..headers], bytes.empty()) |
| 7 | +// server puts the peer first, as the `:peer` pseudo-header; `peer` |
| 8 | +// finds it anywhere in the list (issue #313). |
| 9 | +fn request(peer: SocketAddress, headers: List<(String, String)>) -> Request { |
| 10 | + http.with_peer(Request("GET", "/", headers, bytes.empty()), peer) |
| 10 | 11 | } |
| 11 | 12 | |
| 12 | 13 | fn shown(o: Option<String>) -> String { |
| @@ -21,19 +22,32 @@ pub fn main() -> () / <io> { |
| 21 | 22 | let plain = Request("GET", "/", [("Host", "x")], bytes.empty()) |
| 22 | 23 | io.println(shown(http.client_address(plain, []))) |
| 23 | 24 | //> none |
| 24 | | − match http.peer(request("[::1]:4242", [])) { |
| 25 | + match http.peer(request(SocketAddress("::1", 4242), [])) { |
| 25 | 26 | Some(SocketAddress(host, port)) -> io.println(host <> " " <> show(port)) |
| 26 | 27 | , None -> io.println("no peer") |
| 27 | 28 | } |
| 28 | 29 | //> ::1 4242 |
| 30 | + // `:peer` not first (issue #313): still found, anywhere in the list. |
| 31 | + let buried = |
| 32 | + Request("GET", "/", [("Host", "x"), (":peer", "10.0.0.2:1")], bytes.empty()) |
| 33 | + match http.peer(buried) { |
| 34 | + Some(SocketAddress(host, port)) -> io.println(host <> " " <> show(port)) |
| 35 | + , None -> io.println("no peer") |
| 36 | + } |
| 37 | + //> 10.0.0.2 1 |
| 38 | + io.println(shown(http.client_address(buried, []))) |
| 39 | + //> 10.0.0.2 |
| 29 | 40 | // A direct client: its X-Forwarded-For, which anyone can send, is |
| 30 | 41 | // ignored. |
| 31 | | − let spoofed = request("198.51.100.4:5000", [("X-Forwarded-For", "1.2.3.4")]) |
| 42 | + let spoofed = |
| 43 | + request( SocketAddress("198.51.100.4", 5000) |
| 44 | + , [("X-Forwarded-For", "1.2.3.4")] |
| 45 | + ) |
| 32 | 46 | io.println(shown(http.client_address(spoofed, ["10.0.0.2"]))) |
| 33 | 47 | //> 198.51.100.4 |
| 34 | 48 | // Behind a trusted proxy: the right-most untrusted entry. |
| 35 | 49 | let proxied = |
| 36 | | − request( "10.0.0.2:5000" |
| 50 | + request( SocketAddress("10.0.0.2", 5000) |
| 37 | 51 | , [ ("X-Forwarded-For", "1.2.3.4, 203.0.113.7") |
| 38 | 52 | , ("X-Forwarded-For", "10.0.0.3") |
| 39 | 53 | ] |
| @@ -48,7 +62,9 @@ pub fn main() -> () / <io> { |
| 48 | 62 | ) |
| 49 | 63 | //> 1.2.3.4 |
| 50 | 64 | // A trusted proxy without X-Forwarded-For: the proxy itself. |
| 51 | | − io.println( shown( http.client_address( request("10.0.0.2:1", []) |
| 65 | + io.println( shown( http.client_address( request( SocketAddress("10.0.0.2", 1) |
| 66 | + , [] |
| 67 | + ) |
| 52 | 68 | , ["10.0.0.2"] |
| 53 | 69 | ) |
| 54 | 70 | ) |