mirror of
https://github.com/kataras/iris.git
synced 2025-12-22 20:37:05 +00:00
(#1554) Add support for all common compressions (write and read)
- Remove the context.Context interface and export the *context, the iris.Context now points to the pointer\nSupport compression and rate limiting in the FileServer\nBit of code organisation Former-commit-id: ad1c61bf968059510c6be9e7f2cceec7da70ba17
This commit is contained in:
2
cache/client/client.go
vendored
2
cache/client/client.go
vendored
@@ -101,7 +101,7 @@ const (
|
||||
// if <=minimumAllowedCacheDuration then the server will try to parse from "cache-control" header
|
||||
//
|
||||
// client-side function
|
||||
func (h *ClientHandler) ServeHTTP(ctx context.Context) {
|
||||
func (h *ClientHandler) ServeHTTP(ctx *context.Context) {
|
||||
// check for deniers, if at least one of them return true
|
||||
// for this specific request, then skip the whole cache
|
||||
if !h.rule.Claim(ctx) {
|
||||
|
||||
6
cache/client/handler.go
vendored
6
cache/client/handler.go
vendored
@@ -63,17 +63,17 @@ func (h *Handler) AddRule(r rule.Rule) *Handler {
|
||||
return h
|
||||
}
|
||||
|
||||
var emptyHandler = func(ctx context.Context) {
|
||||
var emptyHandler = func(ctx *context.Context) {
|
||||
ctx.StopWithText(500, "cache: empty body handler")
|
||||
}
|
||||
|
||||
func parseLifeChanger(ctx context.Context) entry.LifeChanger {
|
||||
func parseLifeChanger(ctx *context.Context) entry.LifeChanger {
|
||||
return func() time.Duration {
|
||||
return time.Duration(ctx.MaxAge()) * time.Second
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(ctx context.Context) {
|
||||
func (h *Handler) ServeHTTP(ctx *context.Context) {
|
||||
// check for pre-cache validators, if at least one of them return false
|
||||
// for this specific request, then skip the whole cache
|
||||
bodyHandler := ctx.NextHandler()
|
||||
|
||||
4
cache/client/rule/chained.go
vendored
4
cache/client/rule/chained.go
vendored
@@ -39,7 +39,7 @@ func Chained(rule Rule, next ...Rule) Rule {
|
||||
}
|
||||
|
||||
// Claim validator
|
||||
func (c *chainedRule) Claim(ctx context.Context) bool {
|
||||
func (c *chainedRule) Claim(ctx *context.Context) bool {
|
||||
if !c.Rule.Claim(ctx) {
|
||||
return false
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func (c *chainedRule) Claim(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
// Valid validator
|
||||
func (c *chainedRule) Valid(ctx context.Context) bool {
|
||||
func (c *chainedRule) Valid(ctx *context.Context) bool {
|
||||
if !c.Rule.Valid(ctx) {
|
||||
return false
|
||||
}
|
||||
|
||||
4
cache/client/rule/conditional.go
vendored
4
cache/client/rule/conditional.go
vendored
@@ -33,11 +33,11 @@ func Conditional(claimPredicate func() bool, validPredicate func() bool) Rule {
|
||||
}
|
||||
|
||||
// Claim validator
|
||||
func (c *conditionalRule) Claim(ctx context.Context) bool {
|
||||
func (c *conditionalRule) Claim(ctx *context.Context) bool {
|
||||
return c.claimPredicate()
|
||||
}
|
||||
|
||||
// Valid validator
|
||||
func (c *conditionalRule) Valid(ctx context.Context) bool {
|
||||
func (c *conditionalRule) Valid(ctx *context.Context) bool {
|
||||
return c.validPredicate()
|
||||
}
|
||||
|
||||
4
cache/client/rule/header.go
vendored
4
cache/client/rule/header.go
vendored
@@ -45,11 +45,11 @@ func HeaderValid(valid ruleset.HeaderPredicate) Rule {
|
||||
}
|
||||
|
||||
// Claim validator
|
||||
func (h *headerRule) Claim(ctx context.Context) bool {
|
||||
func (h *headerRule) Claim(ctx *context.Context) bool {
|
||||
return h.claim(ctx.Request().Header.Get)
|
||||
}
|
||||
|
||||
// Valid validator
|
||||
func (h *headerRule) Valid(ctx context.Context) bool {
|
||||
func (h *headerRule) Valid(ctx *context.Context) bool {
|
||||
return h.valid(ctx.ResponseWriter().Header().Get)
|
||||
}
|
||||
|
||||
4
cache/client/rule/not_satisfied.go
vendored
4
cache/client/rule/not_satisfied.go
vendored
@@ -13,10 +13,10 @@ func NotSatisfied() Rule {
|
||||
return ¬SatisfiedRule{}
|
||||
}
|
||||
|
||||
func (n *notSatisfiedRule) Claim(context.Context) bool {
|
||||
func (n *notSatisfiedRule) Claim(*context.Context) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *notSatisfiedRule) Valid(context.Context) bool {
|
||||
func (n *notSatisfiedRule) Valid(*context.Context) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
8
cache/client/rule/rule.go
vendored
8
cache/client/rule/rule.go
vendored
@@ -1,11 +1,9 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12/context"
|
||||
)
|
||||
import "github.com/kataras/iris/v12/context"
|
||||
|
||||
// Rule a superset of validators
|
||||
type Rule interface {
|
||||
Claim(ctx context.Context) bool
|
||||
Valid(ctx context.Context) bool
|
||||
Claim(ctx *context.Context) bool
|
||||
Valid(ctx *context.Context) bool
|
||||
}
|
||||
|
||||
4
cache/client/rule/satisfied.go
vendored
4
cache/client/rule/satisfied.go
vendored
@@ -15,10 +15,10 @@ func Satisfied() Rule {
|
||||
return &satisfiedRule{}
|
||||
}
|
||||
|
||||
func (n *satisfiedRule) Claim(context.Context) bool {
|
||||
func (n *satisfiedRule) Claim(*context.Context) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (n *satisfiedRule) Valid(context.Context) bool {
|
||||
func (n *satisfiedRule) Valid(*context.Context) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
12
cache/client/rule/validator.go
vendored
12
cache/client/rule/validator.go
vendored
@@ -1,8 +1,6 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12/context"
|
||||
)
|
||||
import "github.com/kataras/iris/v12/context"
|
||||
|
||||
// Validators are introduced to implement the RFC about cache (https://tools.ietf.org/html/rfc7234#section-1.1).
|
||||
|
||||
@@ -18,7 +16,7 @@ import (
|
||||
// One function, accepts the request and returns false if should be denied/ignore, otherwise true.
|
||||
// if at least one return false then the original handler will execute as it's
|
||||
// and the whole cache action(set & get) should be ignored, it will be never go to the step of post-cache validations.
|
||||
type PreValidator func(context.Context) bool
|
||||
type PreValidator func(*context.Context) bool
|
||||
|
||||
// PostValidator type is is introduced to implement the second part of the RFC about cache.
|
||||
//
|
||||
@@ -32,7 +30,7 @@ type PreValidator func(context.Context) bool
|
||||
// the PreValidator checks only for request.
|
||||
//
|
||||
// If a function of type of PostValidator returns true then the (shared-always) cache is allowed to be stored.
|
||||
type PostValidator func(context.Context) bool
|
||||
type PostValidator func(*context.Context) bool
|
||||
|
||||
// validatorRule is a rule witch receives PreValidators and PostValidators
|
||||
// it's a 'complete set of rules', you can call it as a Responsible Validator,
|
||||
@@ -68,7 +66,7 @@ func Validator(preValidators []PreValidator, postValidators []PostValidator) Rul
|
||||
|
||||
// Claim returns true if incoming request can claim for a cached handler
|
||||
// the original handler should run as it is and exit
|
||||
func (v *validatorRule) Claim(ctx context.Context) bool {
|
||||
func (v *validatorRule) Claim(ctx *context.Context) bool {
|
||||
// check for pre-cache validators, if at least one of them return false
|
||||
// for this specific request, then skip the whole cache
|
||||
for _, shouldProcess := range v.preValidators {
|
||||
@@ -82,7 +80,7 @@ func (v *validatorRule) Claim(ctx context.Context) bool {
|
||||
// Valid returns true if incoming request and post-response from the original handler
|
||||
// is valid to be store to the cache, if not(false) then the consumer should just exit
|
||||
// otherwise(true) the consumer should store the cached response
|
||||
func (v *validatorRule) Valid(ctx context.Context) bool {
|
||||
func (v *validatorRule) Valid(ctx *context.Context) bool {
|
||||
// check if it's a valid response, if it's not then just return.
|
||||
for _, valid := range v.postValidators {
|
||||
if !valid(ctx) {
|
||||
|
||||
2
cache/client/ruleset.go
vendored
2
cache/client/ruleset.go
vendored
@@ -28,6 +28,6 @@ var DefaultRuleSet = rule.Chained(
|
||||
|
||||
// NoCache disables the cache for a particular request,
|
||||
// can be used as a middleware or called manually from the handler.
|
||||
func NoCache(ctx context.Context) {
|
||||
func NoCache(ctx *context.Context) {
|
||||
ctx.Header(cfg.NoCacheHeader, "true")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user