mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
Made Context.RequestHeader(string) public access.
This commit is contained in:
parent
27f912f5b2
commit
efddcf406a
16
context.go
16
context.go
@ -306,11 +306,11 @@ func (c *Context) BindWith(obj interface{}, b binding.Binding) error {
|
|||||||
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
||||||
func (c *Context) ClientIP() string {
|
func (c *Context) ClientIP() string {
|
||||||
if c.engine.ForwardedByClientIP {
|
if c.engine.ForwardedByClientIP {
|
||||||
clientIP := strings.TrimSpace(c.requestHeader("X-Real-Ip"))
|
clientIP := strings.TrimSpace(c.RequestHeader("X-Real-Ip"))
|
||||||
if len(clientIP) > 0 {
|
if len(clientIP) > 0 {
|
||||||
return clientIP
|
return clientIP
|
||||||
}
|
}
|
||||||
clientIP = c.requestHeader("X-Forwarded-For")
|
clientIP = c.RequestHeader("X-Forwarded-For")
|
||||||
if index := strings.IndexByte(clientIP, ','); index >= 0 {
|
if index := strings.IndexByte(clientIP, ','); index >= 0 {
|
||||||
clientIP = clientIP[0:index]
|
clientIP = clientIP[0:index]
|
||||||
}
|
}
|
||||||
@ -327,14 +327,12 @@ func (c *Context) ClientIP() string {
|
|||||||
|
|
||||||
// ContentType returns the Content-Type header of the request.
|
// ContentType returns the Content-Type header of the request.
|
||||||
func (c *Context) ContentType() string {
|
func (c *Context) ContentType() string {
|
||||||
return filterFlags(c.requestHeader("Content-Type"))
|
return filterFlags(c.RequestHeader("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) requestHeader(key string) string {
|
// RequestHeader returns the value for the respective header key, or "" if not existent.
|
||||||
if values, _ := c.Request.Header[key]; len(values) > 0 {
|
func (c *Context) RequestHeader(key string) string {
|
||||||
return values[0]
|
return c.Request.Header.Get(key)
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
@ -515,7 +513,7 @@ func (c *Context) NegotiateFormat(offered ...string) string {
|
|||||||
assert1(len(offered) > 0, "you must provide at least one offer")
|
assert1(len(offered) > 0, "you must provide at least one offer")
|
||||||
|
|
||||||
if c.Accepted == nil {
|
if c.Accepted == nil {
|
||||||
c.Accepted = parseAccept(c.requestHeader("Accept"))
|
c.Accepted = parseAccept(c.RequestHeader("Accept"))
|
||||||
}
|
}
|
||||||
if len(c.Accepted) == 0 {
|
if len(c.Accepted) == 0 {
|
||||||
return offered[0]
|
return offered[0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user