You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5KB

  1. package rule
  2. import (
  3. "github.com/kataras/iris/v12/context"
  4. "github.com/kataras/iris/v12/cache/ruleset"
  5. )
  6. // 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
  7. // read more at ../../ruleset.go
  8. // headerRule is a Rule witch receives and checks for a header predicates
  9. // request headers on Claim and response headers on Valid.
  10. type headerRule struct {
  11. claim ruleset.HeaderPredicate
  12. valid ruleset.HeaderPredicate
  13. }
  14. var _ Rule = &headerRule{}
  15. // Header returns a new rule witch claims and execute the post validations trough headers
  16. func Header(claim ruleset.HeaderPredicate, valid ruleset.HeaderPredicate) Rule {
  17. if claim == nil {
  18. claim = ruleset.EmptyHeaderPredicate
  19. }
  20. if valid == nil {
  21. valid = ruleset.EmptyHeaderPredicate
  22. }
  23. return &headerRule{
  24. claim: claim,
  25. valid: valid,
  26. }
  27. }
  28. // HeaderClaim returns a header rule which cares only about claiming (pre-validation)
  29. func HeaderClaim(claim ruleset.HeaderPredicate) Rule {
  30. return Header(claim, nil)
  31. }
  32. // HeaderValid returns a header rule which cares only about valid (post-validation)
  33. func HeaderValid(valid ruleset.HeaderPredicate) Rule {
  34. return Header(nil, valid)
  35. }
  36. // Claim validator
  37. func (h *headerRule) Claim(ctx *context.Context) bool {
  38. return h.claim(ctx.Request().Header.Get)
  39. }
  40. // Valid validator
  41. func (h *headerRule) Valid(ctx *context.Context) bool {
  42. return h.valid(ctx.ResponseWriter().Header().Get)
  43. }