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
eafbf0b2
Commit
eafbf0b2
authored
Mar 07, 2017
by
Matt Holt
Committed by
GitHub
Mar 07, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1474 from jtyr/jtyr-local_ip
Adding ServerIP context
parents
c8514ad7
73d52490
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
0 deletions
+62
-0
caddyhttp/httpserver/context.go
caddyhttp/httpserver/context.go
+23
-0
caddyhttp/httpserver/context_test.go
caddyhttp/httpserver/context_test.go
+39
-0
No files found.
caddyhttp/httpserver/context.go
View file @
eafbf0b2
...
...
@@ -84,6 +84,29 @@ func (c Context) IP() string {
return
ip
}
// To mock the net.InterfaceAddrs from the test.
var
networkInterfacesFn
=
net
.
InterfaceAddrs
// ServerIP gets the (local) IP address of the server.
// TODO: The bind directive should be honored in this method (see PR #1474).
func
(
c
Context
)
ServerIP
()
string
{
addrs
,
err
:=
networkInterfacesFn
()
if
err
!=
nil
{
return
""
}
for
_
,
address
:=
range
addrs
{
// Validate the address and check if it's not a loopback
if
ipnet
,
ok
:=
address
.
(
*
net
.
IPNet
);
ok
&&
!
ipnet
.
IP
.
IsLoopback
()
{
if
ipnet
.
IP
.
To4
()
!=
nil
||
ipnet
.
IP
.
To16
()
!=
nil
{
return
ipnet
.
IP
.
String
()
}
}
}
return
""
}
// URI returns the raw, unprocessed request URI (including query
// string and hash) obtained directly from the Request-Line of
// the HTTP request.
...
...
caddyhttp/httpserver/context_test.go
View file @
eafbf0b2
...
...
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
...
...
@@ -302,6 +303,44 @@ func TestIP(t *testing.T) {
}
}
type
myIP
string
func
(
ip
myIP
)
mockInterfaces
()
([]
net
.
Addr
,
error
)
{
a
:=
net
.
ParseIP
(
string
(
ip
))
return
[]
net
.
Addr
{
&
net
.
IPNet
{
IP
:
a
,
Mask
:
nil
},
},
nil
}
func
TestServerIP
(
t
*
testing
.
T
)
{
context
:=
getContextOrFail
(
t
)
tests
:=
[]
string
{
// Test 0 - ipv4
"1.1.1.1"
,
// Test 1 - ipv6
"2001:db8:a0b:12f0::1"
,
}
for
i
,
expectedIP
:=
range
tests
{
testPrefix
:=
getTestPrefix
(
i
)
// Mock the network interface
ip
:=
myIP
(
expectedIP
)
networkInterfacesFn
=
ip
.
mockInterfaces
defer
func
()
{
networkInterfacesFn
=
net
.
InterfaceAddrs
}()
actualIP
:=
context
.
ServerIP
()
if
actualIP
!=
expectedIP
{
t
.
Errorf
(
"%sExpected IP
\"
%s
\"
, found
\"
%s
\"
."
,
testPrefix
,
expectedIP
,
actualIP
)
}
}
}
func
TestURL
(
t
*
testing
.
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