This commit is contained in:
Javier Provecho Fernandez 2016-12-05 11:21:59 +01:00
parent 18392f589f
commit 7e58c80a7c
4 changed files with 30 additions and 0 deletions

View File

@ -353,9 +353,17 @@ func (c *Context) ClientIP() string {
return clientIP return clientIP
} }
} }
if c.engine.AppEngine {
if addr := c.Request.Header.Get("X-Appengine-Remote-Addr"); addr != "" {
return addr
}
}
if ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr)); err == nil { if ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr)); err == nil {
return ip return ip
} }
return "" return ""
} }

7
context_appengine.go Normal file
View File

@ -0,0 +1,7 @@
// +build appengine
package gin
func init() {
defaultAppEngine = true
}

View File

@ -650,6 +650,7 @@ func TestContextClientIP(t *testing.T) {
c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ") c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ")
c.Request.Header.Set("X-Forwarded-For", " 20.20.20.20, 30.30.30.30") c.Request.Header.Set("X-Forwarded-For", " 20.20.20.20, 30.30.30.30")
c.Request.Header.Set("X-Appengine-Remote-Addr", "50.50.50.50")
c.Request.RemoteAddr = " 40.40.40.40:42123 " c.Request.RemoteAddr = " 40.40.40.40:42123 "
assert.Equal(t, c.ClientIP(), "10.10.10.10") assert.Equal(t, c.ClientIP(), "10.10.10.10")
@ -661,7 +662,15 @@ func TestContextClientIP(t *testing.T) {
assert.Equal(t, c.ClientIP(), "30.30.30.30") assert.Equal(t, c.ClientIP(), "30.30.30.30")
c.Request.Header.Del("X-Forwarded-For") c.Request.Header.Del("X-Forwarded-For")
c.engine.AppEngine = true
assert.Equal(t, c.ClientIP(), "50.50.50.50")
c.Request.Header.Del("X-Appengine-Remote-Addr")
assert.Equal(t, c.ClientIP(), "40.40.40.40") assert.Equal(t, c.ClientIP(), "40.40.40.40")
// no port
c.Request.RemoteAddr = "50.50.50.50"
assert.Equal(t, c.ClientIP(), "")
} }
func TestContextContentType(t *testing.T) { func TestContextContentType(t *testing.T) {

6
gin.go
View File

@ -19,6 +19,7 @@ const Version = "v1.0rc2"
var default404Body = []byte("404 page not found") var default404Body = []byte("404 page not found")
var default405Body = []byte("405 method not allowed") var default405Body = []byte("405 method not allowed")
var defaultAppEngine bool
type HandlerFunc func(*Context) type HandlerFunc func(*Context)
type HandlersChain []HandlerFunc type HandlersChain []HandlerFunc
@ -78,6 +79,10 @@ type (
// handler. // handler.
HandleMethodNotAllowed bool HandleMethodNotAllowed bool
ForwardedByClientIP bool ForwardedByClientIP bool
// #726 #755 If enabled, it will thrust some headers starting with
// 'X-AppEngine...' for better integration with that PaaS.
AppEngine bool
} }
) )
@ -101,6 +106,7 @@ func New() *Engine {
RedirectFixedPath: false, RedirectFixedPath: false,
HandleMethodNotAllowed: false, HandleMethodNotAllowed: false,
ForwardedByClientIP: true, ForwardedByClientIP: true,
AppEngine: defaultAppEngine,
trees: make(methodTrees, 0, 9), trees: make(methodTrees, 0, 9),
} }
engine.RouterGroup.engine = engine engine.RouterGroup.engine = engine