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/http_example_test.go | |
| 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/http_example_test.go')
| -rw-r--r-- | lib/http/http_example_test.go | 29 |
1 files changed, 11 insertions, 18 deletions
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() { |
