aboutsummaryrefslogtreecommitdiffhomepage
path: root/http
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-11-11 16:42:30 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-11-11 16:42:30 -0800
commit9f85f6703116c3d01d3d1b768df22b466d5d3613 (patch)
tree9456e1c31efc4315d7ebd4c25251846905603943 /http
parent1315282c7ff46f1ac048022f7eeb0b4d09eedb57 (diff)
Make sure the remote address is populated even when using unix socket
Diffstat (limited to 'http')
-rw-r--r--http/request/client_ip.go5
-rw-r--r--http/request/client_ip_test.go20
2 files changed, 25 insertions, 0 deletions
diff --git a/http/request/client_ip.go b/http/request/client_ip.go
index 52fc05c..27fb64e 100644
--- a/http/request/client_ip.go
+++ b/http/request/client_ip.go
@@ -34,5 +34,10 @@ func FindClientIP(r *http.Request) string {
remoteIP = r.RemoteAddr
}
+ // When listening on a Unix socket, RemoteAddr is empty.
+ if remoteIP == "" {
+ remoteIP = "127.0.0.1"
+ }
+
return remoteIP
}
diff --git a/http/request/client_ip_test.go b/http/request/client_ip_test.go
index 12d6e16..82f7dd6 100644
--- a/http/request/client_ip_test.go
+++ b/http/request/client_ip_test.go
@@ -80,3 +80,23 @@ func TestClientIPWithBothHeaders(t *testing.T) {
t.Fatalf(`Unexpected result, got: %q`, ip)
}
}
+
+func TestClientIPWithNoRemoteAddress(t *testing.T) {
+ r := &http.Request{}
+
+ if ip := FindClientIP(r); ip != "127.0.0.1" {
+ t.Fatalf(`Unexpected result, got: %q`, ip)
+ }
+}
+
+func TestClientIPWithoutRemoteAddrAndBothHeaders(t *testing.T) {
+ headers := http.Header{}
+ headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
+ headers.Set("X-Real-Ip", "192.168.122.1")
+
+ r := &http.Request{RemoteAddr: "", Header: headers}
+
+ if ip := FindClientIP(r); ip != "203.0.113.195" {
+ t.Fatalf(`Unexpected result, got: %q`, ip)
+ }
+}