@marcel91200 / caddy Mirror · read-only / commits / fe36d26b
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddyhttp: Add RemoteAddr placeholders (#2801)

* Ignore build artifacts * Add RemoteAddr placeholders

Pascal committed Oct 10, 2019 at 21:37 UTC fe36d26b63b6398592e46604d1795f84ce0477d4
3 files changed +89 -3
.gitignore
+4
@@ -6,6 +6,10 @@ Caddyfile
6 *.prof
7 *.test
8
9 +# build artifacts
10 +cmd/caddy/caddy
11 +cmd/caddy/caddy.exe
12 +
13 # mac specific
14 .DS_Store
15
modules/caddyhttp/replacer.go
+14 -3
@@ -65,13 +65,24 @@ func addHTTPVarsToReplacer(repl caddy.Replacer, req *http.Request, w http.Respon
65 return req.Host, true // OK; there probably was no port
66 }
67 return host, true
68 + case "http.request.port":
69 + _, port, _ := net.SplitHostPort(req.Host)
70 + return port, true
71 case "http.request.hostport":
72 return req.Host, true
73 + case "http.request.remote":
74 + return req.RemoteAddr, true
75 + case "http.request.remote.host":
76 + host, _, err := net.SplitHostPort(req.RemoteAddr)
77 + if err != nil {
78 + return req.RemoteAddr, true
79 + }
80 + return host, true
81 + case "http.request.remote.port":
82 + _, port, _ := net.SplitHostPort(req.RemoteAddr)
83 + return port, true
84 case "http.request.method":
85 return req.Method, true
72 - case "http.request.port":
73 - _, port, _ := net.SplitHostPort(req.Host)
74 - return port, true
86 case "http.request.scheme":
87 if req.TLS != nil {
88 return "https", true
modules/caddyhttp/replacer_test.go new
+71
@@ -0,0 +1,71 @@
1 +// Copyright 2015 Matthew Holt and The Caddy Authors
2 +//
3 +// Licensed under the Apache License, Version 2.0 (the "License");
4 +// you may not use this file except in compliance with the License.
5 +// You may obtain a copy of the License at
6 +//
7 +// http://www.apache.org/licenses/LICENSE-2.0
8 +//
9 +// Unless required by applicable law or agreed to in writing, software
10 +// distributed under the License is distributed on an "AS IS" BASIS,
11 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 +// See the License for the specific language governing permissions and
13 +// limitations under the License.
14 +
15 +package caddyhttp
16 +
17 +import (
18 + "context"
19 + "net/http"
20 + "net/http/httptest"
21 + "testing"
22 +
23 + "github.com/caddyserver/caddy/v2"
24 +)
25 +
26 +func TestHTTPVarReplacement(t *testing.T) {
27 + req, _ := http.NewRequest("GET", "/", nil)
28 + repl := caddy.NewReplacer()
29 + ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
30 + req = req.WithContext(ctx)
31 + req.Host = "example.com:80"
32 + req.RemoteAddr = "localhost:1234"
33 + res := httptest.NewRecorder()
34 + addHTTPVarsToReplacer(repl, req, res)
35 +
36 + for i, tc := range []struct {
37 + input string
38 + expect string
39 + }{
40 + {
41 + input: "{http.request.scheme}",
42 + expect: "http",
43 + },
44 + {
45 + input: "{http.request.host}",
46 + expect: "example.com",
47 + },
48 + {
49 + input: "{http.request.port}",
50 + expect: "80",
51 + },
52 + {
53 + input: "{http.request.hostport}",
54 + expect: "example.com:80",
55 + },
56 + {
57 + input: "{http.request.remote.host}",
58 + expect: "localhost",
59 + },
60 + {
61 + input: "{http.request.remote.port}",
62 + expect: "1234",
63 + },
64 + } {
65 + actual := repl.ReplaceAll(tc.input, "<empty>")
66 + if actual != tc.expect {
67 + t.Errorf("Test %d: Expected placeholder %s to be '%s' but got '%s'",
68 + i, tc.input, tc.expect, actual)
69 + }
70 + }
71 +}