iris/cache/client/rule/header.go
Gerasimos (Makis) Maropoulos 0f113dfcda (#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
2020-07-10 23:21:09 +03:00

56 lines
1.5 KiB
Go

package rule
import (
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/cache/ruleset"
)
// The HeaderPredicate should be alived on each of $package/rule BUT GOLANG DOESN'T SUPPORT type alias and I don't want to have so many copies around
// read more at ../../ruleset.go
// headerRule is a Rule witch receives and checks for a header predicates
// request headers on Claim and response headers on Valid.
type headerRule struct {
claim ruleset.HeaderPredicate
valid ruleset.HeaderPredicate
}
var _ Rule = &headerRule{}
// Header returns a new rule witch claims and execute the post validations trough headers
func Header(claim ruleset.HeaderPredicate, valid ruleset.HeaderPredicate) Rule {
if claim == nil {
claim = ruleset.EmptyHeaderPredicate
}
if valid == nil {
valid = ruleset.EmptyHeaderPredicate
}
return &headerRule{
claim: claim,
valid: valid,
}
}
// HeaderClaim returns a header rule which cares only about claiming (pre-validation)
func HeaderClaim(claim ruleset.HeaderPredicate) Rule {
return Header(claim, nil)
}
// HeaderValid returns a header rule which cares only about valid (post-validation)
func HeaderValid(valid ruleset.HeaderPredicate) Rule {
return Header(nil, valid)
}
// Claim validator
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 {
return h.valid(ctx.ResponseWriter().Header().Get)
}