diff options
Diffstat (limited to 'api/context.go')
-rw-r--r-- | api/context.go | 40 |
1 files changed, 16 insertions, 24 deletions
diff --git a/api/context.go b/api/context.go index 02716bb33..9a276a1a1 100644 --- a/api/context.go +++ b/api/context.go @@ -107,21 +107,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { isTokenFromQueryString = true } - protocol := "http" - - // If the request came from the ELB then assume this is produciton - // and redirect all http requests to https - if utils.Cfg.ServiceSettings.UseSSL { - forwardProto := r.Header.Get(model.HEADER_FORWARDED_PROTO) - if forwardProto == "http" { - l4g.Info("redirecting http request to https for %v", r.URL.Path) - http.Redirect(w, r, "https://"+r.Host, http.StatusTemporaryRedirect) - return - } else { - protocol = "https" - } - } - + protocol := GetProtocol(r) c.setSiteURL(protocol + "://" + r.Host) w.Header().Set(model.HEADER_REQUEST_ID, c.RequestId) @@ -209,6 +195,14 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +func GetProtocol(r *http.Request) string { + if r.Header.Get(model.HEADER_FORWARDED_PROTO) == "https" { + return "https" + } else { + return "http" + } +} + func (c *Context) LogAudit(extraInfo string) { audit := &model.Audit{UserId: c.Session.UserId, IpAddress: c.IpAddress, Action: c.Path, ExtraInfo: extraInfo, SessionId: c.Session.Id} if r := <-Srv.Store.Audit().Save(audit); r.Err != nil { @@ -385,6 +379,11 @@ func (c *Context) GetSiteURL() string { func GetIpAddress(r *http.Request) string { address := r.Header.Get(model.HEADER_FORWARDED) + + if len(address) == 0 { + address = r.Header.Get(model.HEADER_REAL_IP) + } + if len(address) == 0 { address, _, _ = net.SplitHostPort(r.RemoteAddr) } @@ -458,20 +457,13 @@ func IsPrivateIpAddress(ipAddress string) bool { func RenderWebError(err *model.AppError, w http.ResponseWriter, r *http.Request) { - protocol := "http" - if utils.Cfg.ServiceSettings.UseSSL { - forwardProto := r.Header.Get(model.HEADER_FORWARDED_PROTO) - if forwardProto != "http" { - protocol = "https" - } - } - + protocol := GetProtocol(r) SiteURL := protocol + "://" + r.Host m := make(map[string]string) m["Message"] = err.Message m["Details"] = err.DetailedError - m["SiteName"] = utils.Cfg.ServiceSettings.SiteName + m["SiteName"] = utils.Cfg.TeamSettings.SiteName m["SiteURL"] = SiteURL w.WriteHeader(err.StatusCode) |