2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
2017-03-28 04:58:19 -04:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2017-04-20 09:55:02 -04:00
|
|
|
"net/url"
|
2017-03-28 04:58:19 -04:00
|
|
|
"strings"
|
2017-04-20 09:55:02 -04:00
|
|
|
|
|
|
|
|
"github.com/mattermost/platform/model"
|
2017-03-28 04:58:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type OriginCheckerProc func(*http.Request) bool
|
|
|
|
|
|
|
|
|
|
func OriginChecker(r *http.Request) bool {
|
|
|
|
|
origin := r.Header.Get("Origin")
|
2017-05-04 22:21:28 +01:00
|
|
|
return *Cfg.ServiceSettings.AllowCorsFrom == "*" || strings.Contains(*Cfg.ServiceSettings.AllowCorsFrom, origin)
|
2017-03-28 04:58:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetOriginChecker(r *http.Request) OriginCheckerProc {
|
|
|
|
|
if len(*Cfg.ServiceSettings.AllowCorsFrom) > 0 {
|
|
|
|
|
return OriginChecker
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-04-20 09:55:02 -04:00
|
|
|
|
|
|
|
|
func RenderWebError(err *model.AppError, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
T, _ := GetTranslationsAndLocale(w, r)
|
|
|
|
|
|
|
|
|
|
title := T("api.templates.error.title", map[string]interface{}{"SiteName": ClientCfg["SiteName"]})
|
|
|
|
|
message := err.Message
|
|
|
|
|
details := err.DetailedError
|
|
|
|
|
link := "/"
|
|
|
|
|
linkMessage := T("api.templates.error.link")
|
|
|
|
|
|
|
|
|
|
status := http.StatusTemporaryRedirect
|
|
|
|
|
if err.StatusCode != http.StatusInternalServerError {
|
|
|
|
|
status = err.StatusCode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.Redirect(
|
|
|
|
|
w,
|
|
|
|
|
r,
|
|
|
|
|
"/error?title="+url.QueryEscape(title)+
|
|
|
|
|
"&message="+url.QueryEscape(message)+
|
|
|
|
|
"&details="+url.QueryEscape(details)+
|
|
|
|
|
"&link="+url.QueryEscape(link)+
|
|
|
|
|
"&linkmessage="+url.QueryEscape(linkMessage),
|
|
|
|
|
status)
|
|
|
|
|
}
|