github.com/levb/mattermost-server@v5.3.1+incompatible/web/web.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package web 5 6 import ( 7 "fmt" 8 "net/http" 9 "path" 10 "strings" 11 12 "github.com/avct/uasurfer" 13 "github.com/gorilla/mux" 14 15 "github.com/mattermost/mattermost-server/app" 16 "github.com/mattermost/mattermost-server/mlog" 17 "github.com/mattermost/mattermost-server/model" 18 "github.com/mattermost/mattermost-server/utils" 19 ) 20 21 type Web struct { 22 App *app.App 23 MainRouter *mux.Router 24 } 25 26 func NewWeb(a *app.App, root *mux.Router) *Web { 27 mlog.Debug("Initializing web routes") 28 29 web := &Web{ 30 App: a, 31 MainRouter: root, 32 } 33 34 web.InitWebhooks() 35 web.InitSaml() 36 web.InitStatic() 37 38 return web 39 } 40 41 // Due to the complexities of UA detection and the ramifications of a misdetection 42 // only older Safari and IE browsers throw incompatibility errors. 43 // Map should be of minimum required browser version. 44 var browserMinimumSupported = map[string]int{ 45 "BrowserIE": 11, 46 "BrowserSafari": 9, 47 } 48 49 func CheckClientCompatability(agentString string) bool { 50 ua := uasurfer.Parse(agentString) 51 52 if version, exist := browserMinimumSupported[ua.Browser.Name.String()]; exist && ua.Browser.Version.Major < version { 53 return false 54 } 55 56 return true 57 } 58 59 func Handle404(a *app.App, w http.ResponseWriter, r *http.Request) { 60 err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound) 61 62 mlog.Debug(fmt.Sprintf("%v: code=404 ip=%v", r.URL.Path, utils.GetIpAddress(r))) 63 64 if IsApiCall(a, r) { 65 w.WriteHeader(err.StatusCode) 66 err.DetailedError = "There doesn't appear to be an api call for the url='" + r.URL.Path + "'. Typo? are you missing a team_id or user_id as part of the url?" 67 w.Write([]byte(err.ToJson())) 68 } else { 69 utils.RenderWebAppError(a.Config(), w, r, err, a.AsymmetricSigningKey()) 70 } 71 } 72 73 func IsApiCall(a *app.App, r *http.Request) bool { 74 subpath, _ := utils.GetSubpathFromConfig(a.Config()) 75 76 return strings.HasPrefix(r.URL.Path, path.Join(subpath, "api")+"/") 77 } 78 79 func IsWebhookCall(a *app.App, r *http.Request) bool { 80 subpath, _ := utils.GetSubpathFromConfig(a.Config()) 81 82 return strings.HasPrefix(r.URL.Path, path.Join(subpath, "hooks")+"/") 83 } 84 85 func ReturnStatusOK(w http.ResponseWriter) { 86 m := make(map[string]string) 87 m[model.STATUS] = model.STATUS_OK 88 w.Write([]byte(model.MapToJson(m))) 89 }