validator.go 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package rule
  2. import "github.com/kataras/iris/context"
  3. // Validators are introduced to implement the RFC about cache (https://tools.ietf.org/html/rfc7234#section-1.1).
  4. // PreValidator like middleware, executes before the cache action begins, if a callback returns false
  5. // then this specific cache action, with specific request, is ignored and the real (original)
  6. // handler is executed instead.
  7. //
  8. // I'll not add all specifications here I'll give the opportunity (public API in the httpcache package-level)
  9. // to the end-user to specify her/his ignore rules too (ignore-only for now).
  10. //
  11. // Each package, nethttp and fhttp should implement their own encapsulations because of different request object.
  12. //
  13. // One function, accepts the request and returns false if should be denied/ignore, otherwise true.
  14. // if at least one return false then the original handler will execute as it's
  15. // and the whole cache action(set & get) should be ignored, it will be never go to the step of post-cache validations.
  16. type PreValidator func(*context.Context) bool
  17. // PostValidator type is is introduced to implement the second part of the RFC about cache.
  18. //
  19. // Q: What's the difference between this and a PreValidator?
  20. // A: PreValidator runs BEFORE trying to get the cache, it cares only for the request
  21. // and if at least one PreValidator returns false then it just runs the original handler and stop there, at the other hand
  22. // a PostValidator runs if all PreValidators returns true and original handler is executed but with a response recorder,
  23. // also the PostValidator should return true to store the cached response.
  24. // Last, a PostValidator accepts a context
  25. // in order to be able to catch the original handler's response,
  26. // the PreValidator checks only for request.
  27. //
  28. // If a function of type of PostValidator returns true then the (shared-always) cache is allowed to be stored.
  29. type PostValidator func(*context.Context) bool
  30. // validatorRule is a rule witch receives PreValidators and PostValidators
  31. // it's a 'complete set of rules', you can call it as a Responsible Validator,
  32. // it's used when you the user wants to check for special things inside a request and a response.
  33. type validatorRule struct {
  34. // preValidators a list of PreValidator functions, execute before real cache begins
  35. // if at least one of them returns false then the original handler will execute as it's
  36. // and the whole cache action(set & get) will be skipped for this specific client's request.
  37. //
  38. // Read-only 'runtime'
  39. preValidators []PreValidator
  40. // postValidators a list of PostValidator functions, execute after the original handler is executed with the response recorder
  41. // and exactly before this cached response is saved,
  42. // if at least one of them returns false then the response will be not saved for this specific client's request.
  43. //
  44. // Read-only 'runtime'
  45. postValidators []PostValidator
  46. }
  47. var _ Rule = &validatorRule{}
  48. // DefaultValidator returns a new validator which contains the default pre and post cache validators
  49. func DefaultValidator() Rule { return Validator(nil, nil) }
  50. // Validator receives the preValidators and postValidators and returns a new Validator rule
  51. func Validator(preValidators []PreValidator, postValidators []PostValidator) Rule {
  52. return &validatorRule{
  53. preValidators: preValidators,
  54. postValidators: postValidators,
  55. }
  56. }
  57. // Claim returns true if incoming request can claim for a cached handler
  58. // the original handler should run as it is and exit
  59. func (v *validatorRule) Claim(ctx *context.Context) bool {
  60. // check for pre-cache validators, if at least one of them return false
  61. // for this specific request, then skip the whole cache
  62. for _, shouldProcess := range v.preValidators {
  63. if !shouldProcess(ctx) {
  64. return false
  65. }
  66. }
  67. return true
  68. }
  69. // Valid returns true if incoming request and post-response from the original handler
  70. // is valid to be store to the cache, if not(false) then the consumer should just exit
  71. // otherwise(true) the consumer should store the cached response
  72. func (v *validatorRule) Valid(ctx *context.Context) bool {
  73. // check if it's a valid response, if it's not then just return.
  74. for _, valid := range v.postValidators {
  75. if !valid(ctx) {
  76. return false
  77. }
  78. }
  79. return true
  80. }