-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
32 lines (27 loc) · 877 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package httputil
import (
"io"
"net/http"
"strings"
)
const (
headerXForwardedFor = "X-Forwarded-For"
)
// CloseResponse closes the response's body. But reads at least some of the body so if it's
// small the underlying TCP connection will be re-used. No need to check for errors: if it
// fails, the Transport won't reuse it anyway.
func CloseResponse(resp *http.Response) {
if resp != nil && resp.Body != nil {
const maxBodySlurpSize = 2 << 10 // 2KB
_, _ = io.CopyN(io.Discard, resp.Body, maxBodySlurpSize)
resp.Body.Close()
}
}
func GetRequestIP(req *http.Request) string {
addresses := strings.Split(req.Header.Get(headerXForwardedFor), ",")
if addresses[0] == "" {
splits := strings.Split(req.RemoteAddr, ":")
return strings.Join(splits[:len(splits)-1], ":") // When there is no load-balancer
}
return strings.ReplaceAll(addresses[0], " ", "")
}