aboutsummaryrefslogtreecommitdiffhomepage
path: root/http
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-09-09 15:15:14 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-09-09 15:15:14 -0700
commitc9f9dd3262b9f8437981f92fac100e508e3c3bcd (patch)
tree16a08ddab87276c6cad972311aa2cfb78843557e /http
parentc1e1506720e3c8b4e8782da9ff73ce1c57ce71a5 (diff)
Store client IP address in request context
Diffstat (limited to 'http')
-rw-r--r--http/request/context.go6
-rw-r--r--http/request/request.go4
-rw-r--r--http/request/request_test.go16
3 files changed, 16 insertions, 10 deletions
diff --git a/http/request/context.go b/http/request/context.go
index 78014e4..b77365d 100644
--- a/http/request/context.go
+++ b/http/request/context.go
@@ -24,6 +24,7 @@ const (
FlashMessageContextKey
FlashErrorMessageContextKey
PocketRequestTokenContextKey
+ ClientIPContextKey
)
// IsAdminUser checks if the logged user is administrator.
@@ -103,6 +104,11 @@ func PocketRequestToken(r *http.Request) string {
return getContextStringValue(r, PocketRequestTokenContextKey)
}
+// ClientIP returns the client IP address stored in the context.
+func ClientIP(r *http.Request) string {
+ return getContextStringValue(r, ClientIPContextKey)
+}
+
func getContextStringValue(r *http.Request, key ContextKey) string {
if v := r.Context().Value(key); v != nil {
return v.(string)
diff --git a/http/request/request.go b/http/request/request.go
index 802b1d0..d27137b 100644
--- a/http/request/request.go
+++ b/http/request/request.go
@@ -100,8 +100,8 @@ func HasQueryParam(r *http.Request, param string) bool {
return ok
}
-// RealIP returns client's real IP address.
-func RealIP(r *http.Request) string {
+// FindClientIP returns client's real IP address.
+func FindClientIP(r *http.Request) string {
headers := []string{"X-Forwarded-For", "X-Real-Ip"}
for _, header := range headers {
value := r.Header.Get(header)
diff --git a/http/request/request_test.go b/http/request/request_test.go
index fa2c4b1..946b132 100644
--- a/http/request/request_test.go
+++ b/http/request/request_test.go
@@ -11,12 +11,12 @@ import (
func TestRealIPWithoutHeaders(t *testing.T) {
r := &http.Request{RemoteAddr: "192.168.0.1:4242"}
- if ip := RealIP(r); ip != "192.168.0.1" {
+ if ip := FindClientIP(r); ip != "192.168.0.1" {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
r = &http.Request{RemoteAddr: "192.168.0.1"}
- if ip := RealIP(r); ip != "192.168.0.1" {
+ if ip := FindClientIP(r); ip != "192.168.0.1" {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
}
@@ -27,7 +27,7 @@ func TestRealIPWithXFFHeader(t *testing.T) {
headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
- if ip := RealIP(r); ip != "203.0.113.195" {
+ if ip := FindClientIP(r); ip != "203.0.113.195" {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
@@ -36,7 +36,7 @@ func TestRealIPWithXFFHeader(t *testing.T) {
headers.Set("X-Forwarded-For", "2001:db8:85a3:8d3:1319:8a2e:370:7348")
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
- if ip := RealIP(r); ip != "2001:db8:85a3:8d3:1319:8a2e:370:7348" {
+ if ip := FindClientIP(r); ip != "2001:db8:85a3:8d3:1319:8a2e:370:7348" {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
@@ -45,7 +45,7 @@ func TestRealIPWithXFFHeader(t *testing.T) {
headers.Set("X-Forwarded-For", "70.41.3.18")
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
- if ip := RealIP(r); ip != "70.41.3.18" {
+ if ip := FindClientIP(r); ip != "70.41.3.18" {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
@@ -54,7 +54,7 @@ func TestRealIPWithXFFHeader(t *testing.T) {
headers.Set("X-Forwarded-For", "fake IP")
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
- if ip := RealIP(r); ip != "192.168.0.1" {
+ if ip := FindClientIP(r); ip != "192.168.0.1" {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
}
@@ -64,7 +64,7 @@ func TestRealIPWithXRealIPHeader(t *testing.T) {
headers.Set("X-Real-Ip", "192.168.122.1")
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
- if ip := RealIP(r); ip != "192.168.122.1" {
+ if ip := FindClientIP(r); ip != "192.168.122.1" {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
}
@@ -76,7 +76,7 @@ func TestRealIPWithBothHeaders(t *testing.T) {
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
- if ip := RealIP(r); ip != "203.0.113.195" {
+ if ip := FindClientIP(r); ip != "203.0.113.195" {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
}