Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
da016f8d
Commit
da016f8d
authored
Apr 18, 2016
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #760 from Burmudar/hostname-placeholder
Add hostname placeholder. Headers use replacer
parents
2f2d357f
a762bec0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
1 deletion
+26
-1
middleware/headers/headers.go
middleware/headers/headers.go
+2
-1
middleware/headers/headers_test.go
middleware/headers/headers_test.go
+7
-0
middleware/replacer.go
middleware/replacer.go
+8
-0
middleware/replacer_test.go
middleware/replacer_test.go
+9
-0
No files found.
middleware/headers/headers.go
View file @
da016f8d
...
...
@@ -20,13 +20,14 @@ type Headers struct {
// ServeHTTP implements the middleware.Handler interface and serves requests,
// setting headers on the response according to the configured rules.
func
(
h
Headers
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
replacer
:=
middleware
.
NewReplacer
(
r
,
nil
,
""
)
for
_
,
rule
:=
range
h
.
Rules
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
Path
)
{
for
_
,
header
:=
range
rule
.
Headers
{
if
strings
.
HasPrefix
(
header
.
Name
,
"-"
)
{
w
.
Header
()
.
Del
(
strings
.
TrimLeft
(
header
.
Name
,
"-"
))
}
else
{
w
.
Header
()
.
Set
(
header
.
Name
,
header
.
Value
)
w
.
Header
()
.
Set
(
header
.
Name
,
replacer
.
Replace
(
header
.
Value
)
)
}
}
}
...
...
middleware/headers/headers_test.go
View file @
da016f8d
...
...
@@ -3,12 +3,17 @@ package headers
import
(
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/mholt/caddy/middleware"
)
func
TestHeaders
(
t
*
testing
.
T
)
{
hostname
,
err
:=
os
.
Hostname
()
if
err
!=
nil
{
t
.
Fatalf
(
"Could not determine hostname: %v"
,
err
)
}
for
i
,
test
:=
range
[]
struct
{
from
string
name
string
...
...
@@ -17,6 +22,7 @@ func TestHeaders(t *testing.T) {
{
"/a"
,
"Foo"
,
"Bar"
},
{
"/a"
,
"Bar"
,
""
},
{
"/a"
,
"Baz"
,
""
},
{
"/a"
,
"ServerName"
,
hostname
},
{
"/b"
,
"Foo"
,
""
},
{
"/b"
,
"Bar"
,
"Removed in /a"
},
}
{
...
...
@@ -27,6 +33,7 @@ func TestHeaders(t *testing.T) {
Rules
:
[]
Rule
{
{
Path
:
"/a"
,
Headers
:
[]
Header
{
{
Name
:
"Foo"
,
Value
:
"Bar"
},
{
Name
:
"ServerName"
,
Value
:
"{hostname}"
},
{
Name
:
"-Bar"
},
}},
},
...
...
middleware/replacer.go
View file @
da016f8d
...
...
@@ -4,6 +4,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
...
...
@@ -52,6 +53,13 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
}
return
"http"
}(),
"{hostname}"
:
func
()
string
{
name
,
err
:=
os
.
Hostname
()
if
err
!=
nil
{
return
""
}
return
name
}(),
"{host}"
:
r
.
Host
,
"{path}"
:
r
.
URL
.
Path
,
"{path_escaped}"
:
url
.
QueryEscape
(
r
.
URL
.
Path
),
...
...
middleware/replacer_test.go
View file @
da016f8d
...
...
@@ -3,6 +3,7 @@ package middleware
import
(
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
...
...
@@ -53,6 +54,14 @@ func TestReplace(t *testing.T) {
request
.
Header
.
Set
(
"ShorterVal"
,
"1"
)
repl
:=
NewReplacer
(
request
,
recordRequest
,
"-"
)
hostname
,
err
:=
os
.
Hostname
()
if
err
!=
nil
{
t
.
Fatal
(
"Failed to determine hostname
\n
"
)
}
if
expected
,
actual
:=
"This hostname is "
+
hostname
,
repl
.
Replace
(
"This hostname is {hostname}"
);
expected
!=
actual
{
t
.
Errorf
(
"{hostname} replacement: expected '%s', got '%s'"
,
expected
,
actual
)
}
if
expected
,
actual
:=
"This host is localhost."
,
repl
.
Replace
(
"This host is {host}."
);
expected
!=
actual
{
t
.
Errorf
(
"{host} replacement: expected '%s', got '%s'"
,
expected
,
actual
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment