Former-commit-id: e83456ced3d00d6da3001d083e22a0568748298e
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-06-09 02:20:28 +03:00
parent 6d977dacd2
commit 46ff69e28e
4 changed files with 20 additions and 22 deletions

View File

@ -64,9 +64,7 @@ func (h *Handler) AddRule(r rule.Rule) *Handler {
} }
var emptyHandler = func(ctx context.Context) { var emptyHandler = func(ctx context.Context) {
ctx.StatusCode(500) ctx.StopWithText(500, "cache: empty body handler")
ctx.WriteString("cache: empty body handler")
ctx.StopExecution()
} }
func parseLifeChanger(ctx context.Context) entry.LifeChanger { func parseLifeChanger(ctx context.Context) entry.LifeChanger {

View File

@ -1900,6 +1900,16 @@ func (ctx *context) RemoteAddr() string {
return addr 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. // GetHeader returns the request header's value based on its name.
func (ctx *context) GetHeader(name string) string { func (ctx *context) GetHeader(name string) string {
return ctx.request.Header.Get(name) return ctx.request.Header.Get(name)
@ -2107,7 +2117,7 @@ func (ctx *context) Header(name string, value string) {
ctx.writer.Header().Add(name, value) ctx.writer.Header().Add(name, value)
} }
const contentTypeContextKey = "_iris_content_type" const contentTypeContextKey = "iris.content_type"
func shouldAppendCharset(cType string) bool { func shouldAppendCharset(cType string) bool {
return cType != ContentBinaryHeaderValue && cType != ContentWebassemblyHeaderValue return cType != ContentBinaryHeaderValue && cType != ContentWebassemblyHeaderValue
@ -2158,16 +2168,6 @@ func (ctx *context) GetContentType() string {
return ctx.writer.Header().Get(ContentTypeHeaderKey) 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". // GetContentType returns the request's header value of "Content-Type".
func (ctx *context) GetContentTypeRequested() string { func (ctx *context) GetContentTypeRequested() string {
return TrimHeaderValue(ctx.GetHeader(ContentTypeHeaderKey)) return TrimHeaderValue(ctx.GetHeader(ContentTypeHeaderKey))
@ -3083,6 +3083,8 @@ const (
ContentEncodingHeaderKey = "Content-Encoding" ContentEncodingHeaderKey = "Content-Encoding"
// GzipHeaderValue is the header value of "gzip". // GzipHeaderValue is the header value of "gzip".
GzipHeaderValue = "gzip" GzipHeaderValue = "gzip"
// FlateHeaderValue is the header value of "deflate".
FlateHeaderValue = "deflate"
// AcceptEncodingHeaderKey is the header key of "Accept-Encoding". // AcceptEncodingHeaderKey is the header key of "Accept-Encoding".
AcceptEncodingHeaderKey = "Accept-Encoding" AcceptEncodingHeaderKey = "Accept-Encoding"
// VaryHeaderKey is the header key of "Vary". // 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. // ClientSupportsGzip retruns true if the client supports gzip compression.
func (ctx *context) ClientSupportsGzip() bool { func (ctx *context) ClientSupportsGzip() bool {
if h := ctx.GetHeader(AcceptEncodingHeaderKey); h != "" { 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"){ 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 true
} }
} }
} }
return false return false
} }
@ -3506,8 +3509,7 @@ func (ctx *context) View(filename string, optionalViewModel ...interface{}) erro
err := ctx.app.View(ctx, filename, layout, bindingData) err := ctx.app.View(ctx, filename, layout, bindingData)
if err != nil { if err != nil {
ctx.StatusCode(http.StatusInternalServerError) ctx.StopWithStatus(http.StatusInternalServerError)
ctx.StopExecution()
} }
return err return err

View File

@ -114,7 +114,7 @@ func (t *Transaction) Complete(err error) {
reason = errWstatus.Reason reason = errWstatus.Reason
} }
// get the content type used on this transaction // 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 cType = cTypeH
} }
@ -168,8 +168,7 @@ var RequestTransactionScope = TransactionScopeFunc(func(maybeErr TransactionErrR
ctx.ContentType(maybeErr.ContentType) ctx.ContentType(maybeErr.ContentType)
} else { } else {
// else execute the registered user error and skip the next transactions and all normal flow, // else execute the registered user error and skip the next transactions and all normal flow,
ctx.StatusCode(maybeErr.StatusCode) ctx.StopWithStatus(maybeErr.StatusCode)
ctx.StopExecution()
} }
}) })

View File

@ -51,8 +51,7 @@ func New() context.Handler {
logMessage += fmt.Sprintf("\n%s", stacktrace) logMessage += fmt.Sprintf("\n%s", stacktrace)
ctx.Application().Logger().Warn(logMessage) ctx.Application().Logger().Warn(logMessage)
ctx.StatusCode(500) ctx.StopWithStatus(500)
ctx.StopExecution()
} }
}() }()