2018-05-14 10:24:58 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2019-11-29 12:59:40 +01:00
|
|
|
// See LICENSE.txt for license information.
|
2018-05-14 10:24:58 -04:00
|
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2018-06-21 14:31:51 -04:00
|
|
|
"path"
|
2018-05-14 10:24:58 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2020-06-16 18:15:31 -04:00
|
|
|
"github.com/NYTimes/gziphandler"
|
2019-11-28 14:39:38 +01:00
|
|
|
"github.com/mattermost/mattermost-server/v5/mlog"
|
|
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
|
|
|
|
"github.com/mattermost/mattermost-server/v5/utils"
|
|
|
|
|
"github.com/mattermost/mattermost-server/v5/utils/fileutils"
|
2018-05-14 10:24:58 -04:00
|
|
|
)
|
|
|
|
|
|
2018-12-18 05:37:42 -08:00
|
|
|
var robotsTxt = []byte("User-agent: *\nDisallow: /\n")
|
|
|
|
|
|
2018-05-14 10:24:58 -04:00
|
|
|
func (w *Web) InitStatic() {
|
2018-11-28 10:56:21 -08:00
|
|
|
if *w.ConfigService.Config().ServiceSettings.WebserverMode != "disabled" {
|
2019-02-11 21:21:23 -05:00
|
|
|
if err := utils.UpdateAssetsSubpathFromConfig(w.ConfigService.Config()); err != nil {
|
|
|
|
|
mlog.Error("Failed to update assets subpath from config", mlog.Err(err))
|
|
|
|
|
}
|
2018-06-14 11:26:22 -04:00
|
|
|
|
2018-12-17 08:51:46 -08:00
|
|
|
staticDir, _ := fileutils.FindDir(model.CLIENT_DIR)
|
2019-09-12 18:22:48 +02:00
|
|
|
mlog.Debug("Using client directory", mlog.String("clientDir", staticDir))
|
2018-05-14 10:24:58 -04:00
|
|
|
|
2018-11-28 10:56:21 -08:00
|
|
|
subpath, _ := utils.GetSubpathFromConfig(w.ConfigService.Config())
|
2018-06-21 14:31:51 -04:00
|
|
|
|
2020-06-16 18:15:31 -04:00
|
|
|
staticHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static"), http.FileServer(http.Dir(staticDir))))
|
2020-01-08 14:43:59 -06:00
|
|
|
pluginHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static", "plugins"), http.FileServer(http.Dir(*w.ConfigService.Config().PluginSettings.ClientDirectory))))
|
2018-05-14 10:24:58 -04:00
|
|
|
|
2018-11-28 10:56:21 -08:00
|
|
|
if *w.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
|
2020-06-16 18:15:31 -04:00
|
|
|
staticHandler = gziphandler.GzipHandler(staticHandler)
|
2018-05-14 10:24:58 -04:00
|
|
|
pluginHandler = gziphandler.GzipHandler(pluginHandler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.MainRouter.PathPrefix("/static/plugins/").Handler(pluginHandler)
|
|
|
|
|
w.MainRouter.PathPrefix("/static/").Handler(staticHandler)
|
2018-12-18 05:37:42 -08:00
|
|
|
w.MainRouter.Handle("/robots.txt", http.HandlerFunc(robotsHandler))
|
2019-08-15 14:30:40 -04:00
|
|
|
w.MainRouter.Handle("/unsupported_browser.js", http.HandlerFunc(unsupportedBrowserScriptHandler))
|
2018-05-14 10:24:58 -04:00
|
|
|
w.MainRouter.Handle("/{anything:.*}", w.NewStaticHandler(root)).Methods("GET")
|
2018-06-21 14:31:51 -04:00
|
|
|
|
|
|
|
|
// When a subpath is defined, it's necessary to handle redirects without a
|
|
|
|
|
// trailing slash. We don't want to use StrictSlash on the w.MainRouter and affect
|
|
|
|
|
// all routes, just /subpath -> /subpath/.
|
|
|
|
|
w.MainRouter.HandleFunc("", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-09-27 03:34:45 -04:00
|
|
|
r.URL.Path += "/"
|
|
|
|
|
http.Redirect(w, r, r.URL.String(), http.StatusFound)
|
2018-06-21 14:31:51 -04:00
|
|
|
}))
|
2018-05-14 10:24:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func root(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
if !CheckClientCompatability(r.UserAgent()) {
|
2019-11-12 09:14:57 -05:00
|
|
|
renderUnsupportedBrowser(c.App, w, r)
|
2018-05-14 10:24:58 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 14:31:51 -04:00
|
|
|
if IsApiCall(c.App, r) {
|
2018-05-14 10:24:58 -04:00
|
|
|
Handle404(c.App, w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public")
|
|
|
|
|
|
2018-12-17 08:51:46 -08:00
|
|
|
staticDir, _ := fileutils.FindDir(model.CLIENT_DIR)
|
2018-05-14 10:24:58 -04:00
|
|
|
http.ServeFile(w, r, filepath.Join(staticDir, "root.html"))
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 16:29:52 -04:00
|
|
|
func staticFilesHandler(handler http.Handler) http.Handler {
|
2018-05-14 10:24:58 -04:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-01-08 14:43:59 -06:00
|
|
|
//wrap our ResponseWriter with our no-cache 404-handler
|
|
|
|
|
w = ¬FoundNoCacheResponseWriter{ResponseWriter: w}
|
|
|
|
|
|
2018-05-14 10:24:58 -04:00
|
|
|
w.Header().Set("Cache-Control", "max-age=31556926, public")
|
2019-12-05 23:57:30 -04:00
|
|
|
|
|
|
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-05-19 08:40:13 -04:00
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 14:43:59 -06:00
|
|
|
type notFoundNoCacheResponseWriter struct {
|
|
|
|
|
http.ResponseWriter
|
|
|
|
|
}
|
2019-12-05 23:57:30 -04:00
|
|
|
|
2020-01-08 14:43:59 -06:00
|
|
|
func (w *notFoundNoCacheResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
|
if statusCode == http.StatusNotFound {
|
|
|
|
|
// we have a 404, update our cache header first then fall through
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, public")
|
|
|
|
|
}
|
|
|
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
2018-05-14 10:24:58 -04:00
|
|
|
}
|
2018-12-18 05:37:42 -08:00
|
|
|
|
|
|
|
|
func robotsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Write(robotsTxt)
|
|
|
|
|
}
|
2019-08-15 14:30:40 -04:00
|
|
|
|
|
|
|
|
func unsupportedBrowserScriptHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
templatesDir, _ := fileutils.FindDir("templates")
|
|
|
|
|
http.ServeFile(w, r, filepath.Join(templatesDir, "unsupported_browser.js"))
|
|
|
|
|
}
|