github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  	"strings"
    10  
    11  	"github.com/avct/uasurfer"
    12  	"github.com/gorilla/mux"
    13  
    14  	"github.com/mattermost/mattermost-server/app"
    15  	"github.com/mattermost/mattermost-server/mlog"
    16  	"github.com/mattermost/mattermost-server/model"
    17  	"github.com/mattermost/mattermost-server/utils"
    18  )
    19  
    20  type Web struct {
    21  	App        *app.App
    22  	MainRouter *mux.Router
    23  }
    24  
    25  func NewWeb(a *app.App, root *mux.Router) *Web {
    26  	mlog.Debug("Initializing web routes")
    27  
    28  	web := &Web{
    29  		App:        a,
    30  		MainRouter: root,
    31  	}
    32  
    33  	web.InitWebhooks()
    34  	web.InitSaml()
    35  	web.InitStatic()
    36  
    37  	return web
    38  }
    39  
    40  // Due to the complexities of UA detection and the ramifications of a misdetection
    41  // only older Safari and IE browsers throw incompatibility errors.
    42  // Map should be of minimum required browser version.
    43  var browserMinimumSupported = map[string]int{
    44  	"BrowserIE":     11,
    45  	"BrowserSafari": 9,
    46  }
    47  
    48  func CheckClientCompatability(agentString string) bool {
    49  	ua := uasurfer.Parse(agentString)
    50  
    51  	if version, exist := browserMinimumSupported[ua.Browser.Name.String()]; exist && ua.Browser.Version.Major < version {
    52  		return false
    53  	}
    54  
    55  	return true
    56  }
    57  
    58  func Handle404(a *app.App, w http.ResponseWriter, r *http.Request) {
    59  	err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound)
    60  
    61  	mlog.Debug(fmt.Sprintf("%v: code=404 ip=%v", r.URL.Path, utils.GetIpAddress(r)))
    62  
    63  	if IsApiCall(r) {
    64  		w.WriteHeader(err.StatusCode)
    65  		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?"
    66  		w.Write([]byte(err.ToJson()))
    67  	} else {
    68  		utils.RenderWebAppError(w, r, err, a.AsymmetricSigningKey())
    69  	}
    70  }
    71  
    72  func IsApiCall(r *http.Request) bool {
    73  	return strings.Index(r.URL.Path, "/api/") == 0
    74  }
    75  
    76  func ReturnStatusOK(w http.ResponseWriter) {
    77  	m := make(map[string]string)
    78  	m[model.STATUS] = model.STATUS_OK
    79  	w.Write([]byte(model.MapToJson(m)))
    80  }