diff options
author | Wolfy-J <[email protected]> | 2019-05-04 18:47:12 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-05-04 18:47:12 +0300 |
commit | 17c259e8e0a7988b4336643c4d180733663dbaa3 (patch) | |
tree | 157faa2b009e648ba17fbce86fa6ac2a08ade42a /service/http/request.go | |
parent | 5f0fa44cc1b2ad377eb0832ec7e4f2337fc843b4 (diff) |
real ip + better stop sequence
Diffstat (limited to 'service/http/request.go')
-rw-r--r-- | service/http/request.go | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/service/http/request.go b/service/http/request.go index b1ca514a..e56acb2a 100644 --- a/service/http/request.go +++ b/service/http/request.go @@ -34,8 +34,8 @@ type Request struct { // URI contains full request URI with scheme and query. URI string `json:"uri"` - // Headers contains list of request headers. - Headers http.Header `json:"headers"` + // Header contains list of request headers. + Header http.Header `json:"headers"` // Cookies contains list of request cookies. Cookies map[string]string `json:"cookies"` @@ -56,25 +56,28 @@ type Request struct { body interface{} } +func fetchIP(pair string) string { + if !strings.ContainsRune(pair, ':') { + return pair + } + + addr, _, _ := net.SplitHostPort(pair) + return addr +} + // NewRequest creates new PSR7 compatible request using net/http request. func NewRequest(r *http.Request, cfg *UploadsConfig) (req *Request, err error) { req = &Request{ + RemoteAddr: fetchIP(r.RemoteAddr), Protocol: r.Proto, Method: r.Method, URI: uri(r), - Headers: r.Header, + Header: r.Header, Cookies: make(map[string]string), RawQuery: r.URL.RawQuery, Attributes: attributes.All(r), } - // otherwise, return remote address as is - if strings.ContainsRune(r.RemoteAddr, ':') { - req.RemoteAddr, _, _ = net.SplitHostPort(r.RemoteAddr) - } else { - req.RemoteAddr = r.RemoteAddr - } - for _, c := range r.Cookies() { if v, err := url.QueryUnescape(c.Value); err == nil { req.Cookies[c.Name] = v @@ -152,7 +155,7 @@ func (r *Request) contentType() int { return contentNone } - ct := r.Headers.Get("content-type") + ct := r.Header.Get("content-type") if strings.Contains(ct, "application/x-www-form-urlencoded") { return contentFormData } |