Only lookup hostname if the results are going to need it.

This commit is contained in:
George Shammas 2014-07-10 18:34:34 +00:00
parent 8f3934c13a
commit 5e97b47197
1 changed files with 18 additions and 6 deletions

24
main.go
View File

@ -40,6 +40,15 @@ func Logger() gin.HandlerFunc {
} }
} }
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func mainHandler(c *gin.Context) { func mainHandler(c *gin.Context) {
fields := strings.Split(c.Params.ByName("field"), ".") fields := strings.Split(c.Params.ByName("field"), ".")
ip, err := net.ResolveTCPAddr("tcp", c.Req.RemoteAddr) ip, err := net.ResolveTCPAddr("tcp", c.Req.RemoteAddr)
@ -51,16 +60,19 @@ func mainHandler(c *gin.Context) {
c.Set("ua", c.Req.UserAgent()) c.Set("ua", c.Req.UserAgent())
c.Set("lang", c.Req.Header.Get("Accept-Language")) c.Set("lang", c.Req.Header.Get("Accept-Language"))
c.Set("encoding", c.Req.Header.Get("Accept-Encoding")) c.Set("encoding", c.Req.Header.Get("Accept-Encoding"))
c.Set("method", c.Req.Method) c.Set("method", c.Req.Method)
c.Set("mime", c.Req.Header.Get("Accept")) c.Set("mime", c.Req.Header.Get("Accept"))
c.Set("referer", c.Req.Header.Get("Referer")) c.Set("referer", c.Req.Header.Get("Referer"))
c.Set("forwarded", c.Req.Header.Get("X-Forwarded-For")) c.Set("forwarded", c.Req.Header.Get("X-Forwarded-For"))
hostnames, err := net.LookupAddr(ip.IP.String()) // Only lookup hostname if the results are going to need it.
if err != nil { if stringInSlice(fields[0], []string{"", "all", "host"}) {
c.Set("host", "") hostnames, err := net.LookupAddr(ip.IP.String())
} else { if err != nil {
c.Set("host", hostnames[0]) c.Set("host", "")
} else {
c.Set("host", hostnames[0])
}
} }
wantsJSON := false wantsJSON := false