github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/utils/api.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  	"strings"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  func CheckOrigin(r *http.Request, allowedOrigins string) bool {
    15  	origin := r.Header.Get("Origin")
    16  	if allowedOrigins == "*" {
    17  		return true
    18  	}
    19  	for _, allowed := range strings.Split(allowedOrigins, " ") {
    20  		if allowed == origin {
    21  			return true
    22  		}
    23  	}
    24  	return false
    25  }
    26  
    27  func OriginChecker(allowedOrigins string) func(*http.Request) bool {
    28  	return func(r *http.Request) bool {
    29  		return CheckOrigin(r, allowedOrigins)
    30  	}
    31  }
    32  
    33  func RenderWebError(err *model.AppError, w http.ResponseWriter, r *http.Request) {
    34  	message := err.Message
    35  	details := err.DetailedError
    36  
    37  	status := http.StatusTemporaryRedirect
    38  	if err.StatusCode != http.StatusInternalServerError {
    39  		status = err.StatusCode
    40  	}
    41  
    42  	http.Redirect(
    43  		w,
    44  		r,
    45  		"/error?message="+url.QueryEscape(message)+
    46  			"&details="+url.QueryEscape(details),
    47  		status)
    48  }