Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

217 Zeilen
5.8 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package filter provides a modifier that executes a given set of child
  15. // modifiers based on the evaluated value of the provided conditional.
  16. package filter
  17. import (
  18. "net/http"
  19. "github.com/google/martian"
  20. "github.com/google/martian/log"
  21. "github.com/google/martian/verify"
  22. )
  23. var noop = martian.Noop("Filter")
  24. // Filter is a modifer that contains conditions to evaluate on request and
  25. // response as well as a set of modifiers to execute based on the value of
  26. // the provided RequestCondition or ResponseCondition.
  27. type Filter struct {
  28. reqcond RequestCondition
  29. rescond ResponseCondition
  30. treqmod martian.RequestModifier
  31. tresmod martian.ResponseModifier
  32. freqmod martian.RequestModifier
  33. fresmod martian.ResponseModifier
  34. }
  35. // New returns a pointer to a Filter with all child modifiers initialized to
  36. // the noop modifier.
  37. func New() *Filter {
  38. return &Filter{
  39. treqmod: noop,
  40. tresmod: noop,
  41. fresmod: noop,
  42. freqmod: noop,
  43. }
  44. }
  45. // SetRequestCondition sets the condition to evaluate on requests.
  46. func (f *Filter) SetRequestCondition(reqcond RequestCondition) {
  47. f.reqcond = reqcond
  48. }
  49. // SetResponseCondition sets the condition to evaluate on responses.
  50. func (f *Filter) SetResponseCondition(rescond ResponseCondition) {
  51. f.rescond = rescond
  52. }
  53. // SetRequestModifier sets the martian.RequestModifier that is executed
  54. // when the RequestCondition evaluates to True. This function is provided
  55. // to maintain backwards compatability with filtering prior to filter.Filter.
  56. func (f *Filter) SetRequestModifier(reqmod martian.RequestModifier) {
  57. f.RequestWhenTrue(reqmod)
  58. }
  59. // RequestWhenTrue sets the martian.RequestModifier that is executed
  60. // when the RequestCondition evaluates to True.
  61. func (f *Filter) RequestWhenTrue(mod martian.RequestModifier) {
  62. if mod == nil {
  63. f.treqmod = noop
  64. return
  65. }
  66. f.treqmod = mod
  67. }
  68. // SetResponseModifier sets the martian.ResponseModifier that is executed
  69. // when the ResponseCondition evaluates to True. This function is provided
  70. // to maintain backwards compatability with filtering prior to filter.Filter.
  71. func (f *Filter) SetResponseModifier(resmod martian.ResponseModifier) {
  72. f.ResponseWhenTrue(resmod)
  73. }
  74. // RequestWhenFalse sets the martian.RequestModifier that is executed
  75. // when the RequestCondition evaluates to False.
  76. func (f *Filter) RequestWhenFalse(mod martian.RequestModifier) {
  77. if mod == nil {
  78. f.freqmod = noop
  79. return
  80. }
  81. f.freqmod = mod
  82. }
  83. // ResponseWhenTrue sets the martian.ResponseModifier that is executed
  84. // when the ResponseCondition evaluates to True.
  85. func (f *Filter) ResponseWhenTrue(mod martian.ResponseModifier) {
  86. if mod == nil {
  87. f.tresmod = noop
  88. return
  89. }
  90. f.tresmod = mod
  91. }
  92. // ResponseWhenFalse sets the martian.ResponseModifier that is executed
  93. // when the ResponseCondition evaluates to False.
  94. func (f *Filter) ResponseWhenFalse(mod martian.ResponseModifier) {
  95. if mod == nil {
  96. f.fresmod = noop
  97. return
  98. }
  99. f.fresmod = mod
  100. }
  101. // ModifyRequest evaluates reqcond and executes treqmod iff reqcond evaluates
  102. // to true; otherwise, freqmod is executed.
  103. func (f *Filter) ModifyRequest(req *http.Request) error {
  104. match := f.reqcond.MatchRequest(req)
  105. if match {
  106. log.Debugf("filter.ModifyRequest: matched %s", req.URL)
  107. return f.treqmod.ModifyRequest(req)
  108. }
  109. return f.freqmod.ModifyRequest(req)
  110. }
  111. // ModifyResponse evaluates rescond and executes tresmod iff rescond evaluates
  112. // to true; otherwise, fresmod is executed.
  113. func (f *Filter) ModifyResponse(res *http.Response) error {
  114. match := f.rescond.MatchResponse(res)
  115. if match {
  116. requ := ""
  117. if res.Request != nil {
  118. requ = res.Request.URL.String()
  119. }
  120. log.Debugf("filter.ModifyResponse: %s", requ)
  121. return f.tresmod.ModifyResponse(res)
  122. }
  123. return f.fresmod.ModifyResponse(res)
  124. }
  125. // VerifyRequests returns an error containing all the verification errors
  126. // returned by request verifiers.
  127. func (f *Filter) VerifyRequests() error {
  128. merr := martian.NewMultiError()
  129. freqv, ok := f.freqmod.(verify.RequestVerifier)
  130. if ok {
  131. if ve := freqv.VerifyRequests(); ve != nil {
  132. merr.Add(ve)
  133. }
  134. }
  135. treqv, ok := f.treqmod.(verify.RequestVerifier)
  136. if ok {
  137. if ve := treqv.VerifyRequests(); ve != nil {
  138. merr.Add(ve)
  139. }
  140. }
  141. if merr.Empty() {
  142. return nil
  143. }
  144. return merr
  145. }
  146. // VerifyResponses returns an error containing all the verification errors
  147. // returned by response verifiers.
  148. func (f *Filter) VerifyResponses() error {
  149. merr := martian.NewMultiError()
  150. tresv, ok := f.tresmod.(verify.ResponseVerifier)
  151. if ok {
  152. if ve := tresv.VerifyResponses(); ve != nil {
  153. merr.Add(ve)
  154. }
  155. }
  156. fresv, ok := f.fresmod.(verify.ResponseVerifier)
  157. if ok {
  158. if ve := fresv.VerifyResponses(); ve != nil {
  159. merr.Add(ve)
  160. }
  161. }
  162. if merr.Empty() {
  163. return nil
  164. }
  165. return merr
  166. }
  167. // ResetRequestVerifications resets the state of the contained request verifiers.
  168. func (f *Filter) ResetRequestVerifications() {
  169. if treqv, ok := f.treqmod.(verify.RequestVerifier); ok {
  170. treqv.ResetRequestVerifications()
  171. }
  172. if freqv, ok := f.freqmod.(verify.RequestVerifier); ok {
  173. freqv.ResetRequestVerifications()
  174. }
  175. }
  176. // ResetResponseVerifications resets the state of the contained request verifiers.
  177. func (f *Filter) ResetResponseVerifications() {
  178. if tresv, ok := f.tresmod.(verify.ResponseVerifier); ok {
  179. tresv.ResetResponseVerifications()
  180. }
  181. }