github.com/dschalla/mattermost-server@v4.8.1-rc1+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 "net/http" 8 "strings" 9 10 "github.com/NYTimes/gziphandler" 11 12 l4g "github.com/alecthomas/log4go" 13 "github.com/mattermost/mattermost-server/api" 14 "github.com/mattermost/mattermost-server/model" 15 "github.com/mattermost/mattermost-server/utils" 16 "github.com/mssola/user_agent" 17 ) 18 19 func Init(api3 *api.API) { 20 l4g.Debug(utils.T("web.init.debug")) 21 22 mainrouter := api3.BaseRoutes.Root 23 24 if *api3.App.Config().ServiceSettings.WebserverMode != "disabled" { 25 staticDir, _ := utils.FindDir(model.CLIENT_DIR) 26 l4g.Debug("Using client directory at %v", staticDir) 27 28 staticHandler := staticHandler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir)))) 29 pluginHandler := pluginHandler(api3.App.Config, http.StripPrefix("/static/plugins/", http.FileServer(http.Dir(*api3.App.Config().PluginSettings.ClientDirectory)))) 30 31 if *api3.App.Config().ServiceSettings.WebserverMode == "gzip" { 32 staticHandler = gziphandler.GzipHandler(staticHandler) 33 pluginHandler = gziphandler.GzipHandler(pluginHandler) 34 } 35 36 mainrouter.PathPrefix("/static/plugins/").Handler(pluginHandler) 37 mainrouter.PathPrefix("/static/").Handler(staticHandler) 38 mainrouter.Handle("/{anything:.*}", api3.AppHandlerIndependent(root)).Methods("GET") 39 } 40 } 41 42 func staticHandler(handler http.Handler) http.Handler { 43 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 44 w.Header().Set("Cache-Control", "max-age=31556926, public") 45 if strings.HasSuffix(r.URL.Path, "/") { 46 http.NotFound(w, r) 47 return 48 } 49 handler.ServeHTTP(w, r) 50 }) 51 } 52 53 func pluginHandler(config model.ConfigFunc, handler http.Handler) http.Handler { 54 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 55 if *config().ServiceSettings.EnableDeveloper { 56 w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") 57 } else { 58 w.Header().Set("Cache-Control", "max-age=31556926, public") 59 } 60 if strings.HasSuffix(r.URL.Path, "/") { 61 http.NotFound(w, r) 62 return 63 } 64 handler.ServeHTTP(w, r) 65 }) 66 } 67 68 var browsersNotSupported string = "MSIE/8;MSIE/9;MSIE/10;Internet Explorer/8;Internet Explorer/9;Internet Explorer/10;Safari/7;Safari/8" 69 70 func CheckBrowserCompatability(c *api.Context, r *http.Request) bool { 71 ua := user_agent.New(r.UserAgent()) 72 bname, bversion := ua.Browser() 73 74 browsers := strings.Split(browsersNotSupported, ";") 75 for _, browser := range browsers { 76 version := strings.Split(browser, "/") 77 78 if strings.HasPrefix(bname, version[0]) && strings.HasPrefix(bversion, version[1]) { 79 return false 80 } 81 } 82 83 return true 84 } 85 86 func root(c *api.Context, w http.ResponseWriter, r *http.Request) { 87 if !CheckBrowserCompatability(c, r) { 88 w.Header().Set("Cache-Control", "no-store") 89 page := utils.NewHTMLTemplate(c.App.HTMLTemplates(), "unsupported_browser") 90 page.Props["Title"] = c.T("web.error.unsupported_browser.title") 91 page.Props["Message"] = c.T("web.error.unsupported_browser.message") 92 page.RenderToWriter(w) 93 return 94 } 95 96 if api.IsApiCall(r) { 97 api.Handle404(c.App, w, r) 98 return 99 } 100 101 w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public") 102 103 staticDir, _ := utils.FindDir(model.CLIENT_DIR) 104 http.ServeFile(w, r, staticDir+"root.html") 105 }