mirror of
https://github.com/labstack/echo.git
synced 2024-12-26 20:54:00 +02:00
New implements engine.Request#RemoteIP
function.
This commit is contained in:
parent
e918eacd9d
commit
2f1bd0225f
@ -68,6 +68,9 @@ type (
|
||||
// RemoteAddress returns the client's network address.
|
||||
RemoteAddress() string
|
||||
|
||||
// RemoteIP returns the client's network ip address.
|
||||
RemoteIP() string
|
||||
|
||||
// Method returns the request's HTTP function.
|
||||
Method() string
|
||||
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"bytes"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/engine"
|
||||
@ -78,6 +79,19 @@ func (r *Request) RemoteAddress() string {
|
||||
return r.RemoteAddr().String()
|
||||
}
|
||||
|
||||
// RemoteIP implements `engine.Request#RemoteIP` function.
|
||||
func (r *Request) RemoteIP() string {
|
||||
ra := r.RemoteAddress()
|
||||
if ip := r.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
||||
ra = ip
|
||||
} else if ip := r.Header().Get(echo.HeaderXRealIP); ip != "" {
|
||||
ra = ip
|
||||
} else {
|
||||
ra, _, _ = net.SplitHostPort(ra)
|
||||
}
|
||||
return ra
|
||||
}
|
||||
|
||||
// Method implements `engine.Request#Method` function.
|
||||
func (r *Request) Method() string {
|
||||
return string(r.RequestCtx.Method())
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@ -98,6 +99,19 @@ func (r *Request) RemoteAddress() string {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
// RemoteIP implements `engine.Request#RemoteIP` function.
|
||||
func (r *Request) RemoteIP() string {
|
||||
ra := r.RemoteAddress()
|
||||
if ip := r.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
||||
ra = ip
|
||||
} else if ip := r.Header().Get(echo.HeaderXRealIP); ip != "" {
|
||||
ra = ip
|
||||
} else {
|
||||
ra, _, _ = net.SplitHostPort(ra)
|
||||
}
|
||||
return ra
|
||||
}
|
||||
|
||||
// Method implements `engine.Request#Method` function.
|
||||
func (r *Request) Method() string {
|
||||
return r.Request.Method
|
||||
|
@ -21,7 +21,7 @@ Cache-Control: no-cache
|
||||
Accept-Language: de,en;q=0.7,en-us;q=0.3
|
||||
Referer: https://github.com/
|
||||
Cookie: session=securetoken; user=123
|
||||
X-Real-IP: 127.0.0.1
|
||||
X-Real-IP: 192.168.1.1
|
||||
|
||||
--Asrf456BGe4h
|
||||
Content-Disposition: form-data; name="foo"
|
||||
@ -47,10 +47,11 @@ func RequestTest(t *testing.T, request engine.Request) {
|
||||
|
||||
assert.Equal(t, "/labstack/echo", request.URL().Path())
|
||||
assert.Equal(t, "https://github.com/", request.Referer())
|
||||
assert.Equal(t, "127.0.0.1", request.Header().Get("X-Real-IP"))
|
||||
assert.Equal(t, "192.168.1.1", request.Header().Get("X-Real-IP"))
|
||||
assert.Equal(t, "http", request.Scheme())
|
||||
assert.Equal(t, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10", request.UserAgent())
|
||||
assert.Equal(t, "127.0.0.1", request.RemoteAddress())
|
||||
assert.Equal(t, "192.168.1.1", request.RemoteIP())
|
||||
assert.Equal(t, "POST", request.Method())
|
||||
|
||||
assert.Equal(t, int64(261), request.ContentLength())
|
||||
|
Loading…
Reference in New Issue
Block a user