diff options
| author | Shulhan <m.shulhan@gmail.com> | 2021-02-10 15:18:01 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2021-02-10 15:18:01 +0700 |
| commit | f8f4fddec8ea2cb4905a6d9d466a4d9e99e2cb99 (patch) | |
| tree | 25cb2e7abae10896ce59189c17d45483acd6f8b3 /lib/http | |
| parent | 215e4b60542a6cddbcf201b3cd793714d45b65da (diff) | |
| download | pakakeh.go-f8f4fddec8ea2cb4905a6d9d466a4d9e99e2cb99.tar.xz | |
http: change signature of IPAddressOfRequest
Previously, the IPAddressOfRequest accept only one parameter, the
http.Request. This make the function only works on HTTP server from
standard library, not from other library (for example websocket) or
other protocol that use HTTP header too.
This commit changes the parameter to http.Header and default address.
If no headers present, then the default address will be used.
Diffstat (limited to 'lib/http')
| -rw-r--r-- | lib/http/http.go | 12 | ||||
| -rw-r--r-- | lib/http/http_example_test.go | 29 |
2 files changed, 17 insertions, 24 deletions
diff --git a/lib/http/http.go b/lib/http/http.go index 7c289479..175cf84b 100644 --- a/lib/http/http.go +++ b/lib/http/http.go @@ -251,15 +251,15 @@ var ( // // IPAddressOfRequest get the client IP address from HTTP request header -// "X-Real-IP" or "X-Forwarded-For" or from Request.RemoteAddr, which ever -// non-empty first. +// "X-Real-IP" or "X-Forwarded-For", which ever non-empty first. +// If no headers present, use the default address. // -func IPAddressOfRequest(req *http.Request) (addr string) { - addr = req.Header.Get(HeaderXRealIp) +func IPAddressOfRequest(headers http.Header, defAddr string) (addr string) { + addr = headers.Get(HeaderXRealIp) if len(addr) == 0 { - addr, _ = ParseXForwardedFor(req.Header.Get(HeaderXForwardedFor)) + addr, _ = ParseXForwardedFor(headers.Get(HeaderXForwardedFor)) if len(addr) == 0 { - addr = req.RemoteAddr + addr = defAddr } } addr, _, _ = libnet.ParseIPPort(addr, 0) diff --git a/lib/http/http_example_test.go b/lib/http/http_example_test.go index 6271d97c..fd9db431 100644 --- a/lib/http/http_example_test.go +++ b/lib/http/http_example_test.go @@ -6,32 +6,25 @@ import ( ) func ExampleIPAddressOfRequest() { - reqWithXRealIP := &http.Request{ - Header: http.Header{ - "X-Real-Ip": []string{"127.0.0.1"}, - }, - RemoteAddr: "192.168.100.1", - } - fmt.Println("Request with X-Real-IP:", IPAddressOfRequest(reqWithXRealIP)) + defAddress := "192.168.100.1" - reqWithXForwardedFor := &http.Request{ - Header: http.Header{ - "X-Forwarded-For": []string{"127.0.0.2, 192.168.100.1"}, - }, - RemoteAddr: "192.168.100.1", + headers := http.Header{ + "X-Real-Ip": []string{"127.0.0.1"}, } - fmt.Println("Request with X-Forwarded-For:", IPAddressOfRequest(reqWithXForwardedFor)) + fmt.Println("Request with X-Real-IP:", IPAddressOfRequest(headers, defAddress)) - reqWithRemoteAddr := &http.Request{ - Header: http.Header{}, - RemoteAddr: "127.0.0.3", + headers = http.Header{ + "X-Forwarded-For": []string{"127.0.0.2, 192.168.100.1"}, } - fmt.Println("Request without X-* headers:", IPAddressOfRequest(reqWithRemoteAddr)) + fmt.Println("Request with X-Forwarded-For:", IPAddressOfRequest(headers, defAddress)) + + headers = http.Header{} + fmt.Println("Request without X-* headers:", IPAddressOfRequest(headers, defAddress)) // Output: // Request with X-Real-IP: 127.0.0.1 // Request with X-Forwarded-For: 127.0.0.2 - // Request without X-* headers: 127.0.0.3 + // Request without X-* headers: 192.168.100.1 } func ExampleParseXForwardedFor() { |
