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
Łukasz Nowak
caddy
Commits
b37da039
Commit
b37da039
authored
Apr 19, 2017
by
Matt Holt
Committed by
GitHub
Apr 19, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1595 from jannickfahlbusch/feature/hostnameInTemplateActions
templates: Add Hostname template action
parents
729e4f02
92af3ee4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
0 deletions
+43
-0
caddyhttp/httpserver/context.go
caddyhttp/httpserver/context.go
+12
-0
caddyhttp/httpserver/context_test.go
caddyhttp/httpserver/context_test.go
+31
-0
No files found.
caddyhttp/httpserver/context.go
View file @
b37da039
...
@@ -63,6 +63,18 @@ func (c Context) Header(name string) string {
...
@@ -63,6 +63,18 @@ func (c Context) Header(name string) string {
return
c
.
Req
.
Header
.
Get
(
name
)
return
c
.
Req
.
Header
.
Get
(
name
)
}
}
// Hostname gets the (remote) hostname of the client making the request.
func
(
c
Context
)
Hostname
()
string
{
ip
:=
c
.
IP
()
hostnameList
,
err
:=
net
.
LookupAddr
(
ip
)
if
err
!=
nil
||
len
(
hostnameList
)
==
0
{
return
c
.
Req
.
RemoteAddr
}
return
hostnameList
[
0
]
}
// Env gets a map of the environment variables.
// Env gets a map of the environment variables.
func
(
c
Context
)
Env
()
map
[
string
]
string
{
func
(
c
Context
)
Env
()
map
[
string
]
string
{
osEnv
:=
os
.
Environ
()
osEnv
:=
os
.
Environ
()
...
...
caddyhttp/httpserver/context_test.go
View file @
b37da039
...
@@ -244,6 +244,37 @@ func TestHeader(t *testing.T) {
...
@@ -244,6 +244,37 @@ func TestHeader(t *testing.T) {
}
}
}
}
func
TestHostname
(
t
*
testing
.
T
)
{
context
:=
getContextOrFail
(
t
)
tests
:=
[]
struct
{
inputRemoteAddr
string
expectedHostname
string
}{
// Test 0 - ipv4 with port
{
"8.8.8.8:1111"
,
"google-public-dns-a.google.com."
},
// Test 1 - ipv4 without port
{
"8.8.8.8"
,
"google-public-dns-a.google.com."
},
// Test 2 - ipv6 with port
{
"[2001:4860:4860::8888]:11"
,
"google-public-dns-a.google.com."
},
// Test 3 - ipv6 without port and brackets
{
"2001:4860:4860::8888"
,
"google-public-dns-a.google.com."
},
// Test 4 - no hostname available
{
"1.1.1.1"
,
"1.1.1.1"
},
}
for
i
,
test
:=
range
tests
{
testPrefix
:=
getTestPrefix
(
i
)
context
.
Req
.
RemoteAddr
=
test
.
inputRemoteAddr
actualHostname
:=
context
.
Hostname
()
if
actualHostname
!=
test
.
expectedHostname
{
t
.
Errorf
(
testPrefix
+
"Expected hostname %s, found %s"
,
test
.
expectedHostname
,
actualHostname
)
}
}
}
func
TestEnv
(
t
*
testing
.
T
)
{
func
TestEnv
(
t
*
testing
.
T
)
{
context
:=
getContextOrFail
(
t
)
context
:=
getContextOrFail
(
t
)
...
...
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