Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
ef914ab8
Commit
ef914ab8
authored
3 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Update /ip endpoint to return single origin IP
parent
b30cb3a2
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
httpbin/handlers_test.go
+51
-13
51 additions, 13 deletions
httpbin/handlers_test.go
httpbin/helpers.go
+5
-4
5 additions, 4 deletions
httpbin/helpers.go
with
56 additions
and
17 deletions
httpbin/handlers_test.go
+
51
−
13
View file @
ef914ab8
...
...
@@ -318,8 +318,44 @@ func TestCORS(t *testing.T) {
}
func
TestIP
(
t
*
testing
.
T
)
{
t
.
Parallel
()
testCases
:=
map
[
string
]
struct
{
remoteAddr
string
headers
map
[
string
]
string
wantOrigin
string
}{
"remote addr used if no x-forwarded-for"
:
{
remoteAddr
:
"192.168.0.100"
,
wantOrigin
:
"192.168.0.100"
,
},
"remote addr used if x-forwarded-for empty"
:
{
remoteAddr
:
"192.168.0.100"
,
headers
:
map
[
string
]
string
{
"X-Forwarded-For"
:
""
},
wantOrigin
:
"192.168.0.100"
,
},
"first entry in x-forwarded-for used if present"
:
{
remoteAddr
:
"192.168.0.100"
,
headers
:
map
[
string
]
string
{
"X-Forwarded-For"
:
"10.1.1.1, 10.2.2.2, 10.3.3.3"
},
wantOrigin
:
"10.1.1.1"
,
},
"single entry x-forwarded-for ok"
:
{
remoteAddr
:
"192.168.0.100"
,
headers
:
map
[
string
]
string
{
"X-Forwarded-For"
:
"10.1.1.1"
},
wantOrigin
:
"10.1.1.1"
,
},
}
for
name
,
tc
:=
range
testCases
{
tc
:=
tc
t
.
Run
(
name
,
func
(
t
*
testing
.
T
)
{
t
.
Parallel
()
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/ip"
,
nil
)
r
.
RemoteAddr
=
"192.168.0.100"
r
.
RemoteAddr
=
tc
.
remoteAddr
for
k
,
v
:=
range
tc
.
headers
{
r
.
Header
.
Set
(
k
,
v
)
}
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
...
...
@@ -332,8 +368,10 @@ func TestIP(t *testing.T) {
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
if
resp
.
Origin
!=
r
.
RemoteAddr
{
t
.
Fatalf
(
"%#v != %#v"
,
resp
.
Origin
,
r
.
RemoteAddr
)
if
resp
.
Origin
!=
tc
.
wantOrigin
{
t
.
Fatalf
(
"got %q, want %q"
,
resp
.
Origin
,
tc
.
wantOrigin
)
}
})
}
}
...
...
This diff is collapsed.
Click to expand it.
httpbin/helpers.go
+
5
−
4
View file @
ef914ab8
...
...
@@ -33,11 +33,12 @@ func getRequestHeaders(r *http.Request) http.Header {
}
func
getOrigin
(
r
*
http
.
Request
)
string
{
or
igin
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
)
if
or
igin
==
""
{
origin
=
r
.
RemoteAddr
f
or
wardedFor
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
)
if
f
or
wardedFor
==
""
{
return
r
.
RemoteAddr
}
return
origin
// take the first entry in a comma-separated list of IP addrs
return
strings
.
TrimSpace
(
strings
.
SplitN
(
forwardedFor
,
","
,
2
)[
0
])
}
func
getURL
(
r
*
http
.
Request
)
*
url
.
URL
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment