add context#IsMobile

Former-commit-id: 2f857e2b57a10233d4c9ea5ec365b84c1b8b1c76
This commit is contained in:
kataras 2017-12-04 08:12:34 +02:00
parent 27097df7b4
commit 20f68416a7

View File

@ -382,7 +382,12 @@ type Context interface {
// Read more at: https://developer.mozilla.org/en-US/docs/AJAX
// and https://xhr.spec.whatwg.org/
IsAjax() bool
// IsMobile checks if client is using a mobile device(phone or tablet) to communicate with this server.
// If the return value is true that means that the http client using a mobile
// device to communicate with the server, otherwise false.
//
// Keep note that this checks the "User-Agent" request header.
IsMobile() bool
// +------------------------------------------------------------+
// | Response Headers helpers |
// +------------------------------------------------------------+
@ -1316,6 +1321,18 @@ func (ctx *context) IsAjax() bool {
return ctx.GetHeader("X-Requested-With") == "XMLHttpRequest"
}
var isMobileRegex = regexp.MustCompile(`(?i)(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)`)
// IsMobile checks if client is using a mobile device(phone or tablet) to communicate with this server.
// If the return value is true that means that the http client using a mobile
// device to communicate with the server, otherwise false.
//
// Keep note that this checks the "User-Agent" request header.
func (ctx *context) IsMobile() bool {
s := ctx.GetHeader("User-Agent")
return isMobileRegex.MatchString(s)
}
// +------------------------------------------------------------+
// | Response Headers helpers |
// +------------------------------------------------------------+