From 46ff69e28e6e4039db9bc7a4472e182fd3d72a5a Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Tue, 9 Jun 2020 02:20:28 +0300 Subject: [PATCH] minor Former-commit-id: e83456ced3d00d6da3001d083e22a0568748298e --- cache/client/handler.go | 4 +--- context/context.go | 30 ++++++++++++++++-------------- context/transaction.go | 5 ++--- middleware/recover/recover.go | 3 +-- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/cache/client/handler.go b/cache/client/handler.go index 959c9bae..0e10b014 100644 --- a/cache/client/handler.go +++ b/cache/client/handler.go @@ -64,9 +64,7 @@ func (h *Handler) AddRule(r rule.Rule) *Handler { } var emptyHandler = func(ctx context.Context) { - ctx.StatusCode(500) - ctx.WriteString("cache: empty body handler") - ctx.StopExecution() + ctx.StopWithText(500, "cache: empty body handler") } func parseLifeChanger(ctx context.Context) entry.LifeChanger { diff --git a/context/context.go b/context/context.go index 964ddf19..73e9f029 100644 --- a/context/context.go +++ b/context/context.go @@ -1900,6 +1900,16 @@ func (ctx *context) RemoteAddr() string { return addr } +// TrimHeaderValue returns the "v[0:first space or semicolon]". +func TrimHeaderValue(v string) string { + for i, char := range v { + if char == ' ' || char == ';' { + return v[:i] + } + } + return v +} + // GetHeader returns the request header's value based on its name. func (ctx *context) GetHeader(name string) string { return ctx.request.Header.Get(name) @@ -2107,7 +2117,7 @@ func (ctx *context) Header(name string, value string) { ctx.writer.Header().Add(name, value) } -const contentTypeContextKey = "_iris_content_type" +const contentTypeContextKey = "iris.content_type" func shouldAppendCharset(cType string) bool { return cType != ContentBinaryHeaderValue && cType != ContentWebassemblyHeaderValue @@ -2158,16 +2168,6 @@ func (ctx *context) GetContentType() string { return ctx.writer.Header().Get(ContentTypeHeaderKey) } -// TrimHeaderValue returns the "v[0:first space or semicolon]". -func TrimHeaderValue(v string) string { - for i, char := range v { - if char == ' ' || char == ';' { - return v[:i] - } - } - return v -} - // GetContentType returns the request's header value of "Content-Type". func (ctx *context) GetContentTypeRequested() string { return TrimHeaderValue(ctx.GetHeader(ContentTypeHeaderKey)) @@ -3083,6 +3083,8 @@ const ( ContentEncodingHeaderKey = "Content-Encoding" // GzipHeaderValue is the header value of "gzip". GzipHeaderValue = "gzip" + // FlateHeaderValue is the header value of "deflate". + FlateHeaderValue = "deflate" // AcceptEncodingHeaderKey is the header key of "Accept-Encoding". AcceptEncodingHeaderKey = "Accept-Encoding" // VaryHeaderKey is the header key of "Vary". @@ -3250,12 +3252,13 @@ func (ctx *context) StreamWriter(writer func(w io.Writer) bool) { // ClientSupportsGzip retruns true if the client supports gzip compression. func (ctx *context) ClientSupportsGzip() bool { if h := ctx.GetHeader(AcceptEncodingHeaderKey); h != "" { - for _, v := range strings.Split(h, ";") { + for _, v := range strings.Split(h, ",") { if strings.Contains(v, GzipHeaderValue) { // we do Contains because sometimes browsers has the q=, we don't use it atm. || strings.Contains(v,"deflate"){ return true } } } + return false } @@ -3506,8 +3509,7 @@ func (ctx *context) View(filename string, optionalViewModel ...interface{}) erro err := ctx.app.View(ctx, filename, layout, bindingData) if err != nil { - ctx.StatusCode(http.StatusInternalServerError) - ctx.StopExecution() + ctx.StopWithStatus(http.StatusInternalServerError) } return err diff --git a/context/transaction.go b/context/transaction.go index f6b2dfb3..778146e7 100644 --- a/context/transaction.go +++ b/context/transaction.go @@ -114,7 +114,7 @@ func (t *Transaction) Complete(err error) { reason = errWstatus.Reason } // get the content type used on this transaction - if cTypeH := t.context.ResponseWriter().Header().Get(ContentTypeHeaderKey); cTypeH != "" { + if cTypeH := t.context.GetContentType(); cTypeH != "" { cType = cTypeH } @@ -168,8 +168,7 @@ var RequestTransactionScope = TransactionScopeFunc(func(maybeErr TransactionErrR ctx.ContentType(maybeErr.ContentType) } else { // else execute the registered user error and skip the next transactions and all normal flow, - ctx.StatusCode(maybeErr.StatusCode) - ctx.StopExecution() + ctx.StopWithStatus(maybeErr.StatusCode) } }) diff --git a/middleware/recover/recover.go b/middleware/recover/recover.go index 1ae77805..bceda578 100644 --- a/middleware/recover/recover.go +++ b/middleware/recover/recover.go @@ -51,8 +51,7 @@ func New() context.Handler { logMessage += fmt.Sprintf("\n%s", stacktrace) ctx.Application().Logger().Warn(logMessage) - ctx.StatusCode(500) - ctx.StopExecution() + ctx.StopWithStatus(500) } }()