github.com/cjdelisle/matterfoss@v5.11.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/services/configservice" 19 "github.com/mattermost/mattermost-server/utils" 20 ) 21 22 type Web struct { 23 GetGlobalAppOptions app.AppOptionCreator 24 ConfigService configservice.ConfigService 25 MainRouter *mux.Router 26 } 27 28 func New(config configservice.ConfigService, globalOptions app.AppOptionCreator, root *mux.Router) *Web { 29 mlog.Debug("Initializing web routes") 30 31 web := &Web{ 32 GetGlobalAppOptions: globalOptions, 33 ConfigService: config, 34 MainRouter: root, 35 } 36 37 web.InitWebhooks() 38 web.InitSaml() 39 web.InitStatic() 40 41 return web 42 } 43 44 // Due to the complexities of UA detection and the ramifications of a misdetection 45 // only older Safari and IE browsers throw incompatibility errors. 46 // Map should be of minimum required browser version. 47 var browserMinimumSupported = map[string]int{ 48 "BrowserIE": 11, 49 "BrowserSafari": 9, 50 } 51 52 func CheckClientCompatability(agentString string) bool { 53 ua := uasurfer.Parse(agentString) 54 55 if version, exist := browserMinimumSupported[ua.Browser.Name.String()]; exist && ua.Browser.Version.Major < version { 56 return false 57 } 58 59 return true 60 } 61 62 func Handle404(config configservice.ConfigService, w http.ResponseWriter, r *http.Request) { 63 err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound) 64 65 mlog.Debug(fmt.Sprintf("%v: code=404 ip=%v", r.URL.Path, utils.GetIpAddress(r))) 66 67 if IsApiCall(config, r) { 68 w.WriteHeader(err.StatusCode) 69 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?" 70 w.Write([]byte(err.ToJson())) 71 } else { 72 utils.RenderWebAppError(config.Config(), w, r, err, config.AsymmetricSigningKey()) 73 } 74 } 75 76 func IsApiCall(config configservice.ConfigService, r *http.Request) bool { 77 subpath, _ := utils.GetSubpathFromConfig(config.Config()) 78 79 return strings.HasPrefix(r.URL.Path, path.Join(subpath, "api")+"/") 80 } 81 82 func IsWebhookCall(a *app.App, r *http.Request) bool { 83 subpath, _ := utils.GetSubpathFromConfig(a.Config()) 84 85 return strings.HasPrefix(r.URL.Path, path.Join(subpath, "hooks")+"/") 86 } 87 88 func ReturnStatusOK(w http.ResponseWriter) { 89 m := make(map[string]string) 90 m[model.STATUS] = model.STATUS_OK 91 w.Write([]byte(model.MapToJson(m))) 92 }