Add context#IsMobile

Former-commit-id: 571ad7c7a2baf4ec58ba6395003aa9c3bedd1f73
This commit is contained in:
kataras 2017-12-04 08:13:45 +02:00
commit e02da3d442

View File

@ -382,7 +382,12 @@ type Context interface {
// Read more at: https://developer.mozilla.org/en-US/docs/AJAX // Read more at: https://developer.mozilla.org/en-US/docs/AJAX
// and https://xhr.spec.whatwg.org/ // and https://xhr.spec.whatwg.org/
IsAjax() bool 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 | // | Response Headers helpers |
// +------------------------------------------------------------+ // +------------------------------------------------------------+
@ -1316,6 +1321,18 @@ func (ctx *context) IsAjax() bool {
return ctx.GetHeader("X-Requested-With") == "XMLHttpRequest" 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 | // | Response Headers helpers |
// +------------------------------------------------------------+ // +------------------------------------------------------------+