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.
 
 
 

94 lines
2.6 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 martianurl
  15. import (
  16. "encoding/json"
  17. "regexp"
  18. "github.com/google/martian/filter"
  19. "github.com/google/martian/parse"
  20. )
  21. func init() {
  22. parse.Register("url.RegexFilter", regexFilterFromJSON)
  23. }
  24. // URLRegexFilter runs Modifier if the request URL matches the regex, and runs ElseModifier if not.
  25. // This is not to be confused with url.Filter that does string matching on URL segments.
  26. type URLRegexFilter struct {
  27. *filter.Filter
  28. }
  29. type regexFilterJSON struct {
  30. Regex string `json:"regex"`
  31. Modifier json.RawMessage `json:"modifier"`
  32. ElseModifier json.RawMessage `json:"else"`
  33. Scope []parse.ModifierType `json:"scope"`
  34. }
  35. // NewRegexFilter constructs a filter that matches on regular expressions.
  36. func NewRegexFilter(r *regexp.Regexp) *URLRegexFilter {
  37. filter := filter.New()
  38. matcher := NewRegexMatcher(r)
  39. filter.SetRequestCondition(matcher)
  40. filter.SetResponseCondition(matcher)
  41. return &URLRegexFilter{filter}
  42. }
  43. // regexFilterFromJSON takes a JSON message as a byte slice and returns a
  44. // parse.Result that contains a URLRegexFilter and a scope. The regex syntax is RE2
  45. // as described at https://golang.org/s/re2syntax.
  46. //
  47. // Example JSON configuration message:
  48. // {
  49. // "scope": ["request", "response"],
  50. // "regex": ".*www.example.com.*"
  51. // }
  52. func regexFilterFromJSON(b []byte) (*parse.Result, error) {
  53. msg := &regexFilterJSON{}
  54. if err := json.Unmarshal(b, msg); err != nil {
  55. return nil, err
  56. }
  57. matcher, err := regexp.Compile(msg.Regex)
  58. if err != nil {
  59. return nil, err
  60. }
  61. filter := NewRegexFilter(matcher)
  62. m, err := parse.FromJSON(msg.Modifier)
  63. if err != nil {
  64. return nil, err
  65. }
  66. filter.RequestWhenTrue(m.RequestModifier())
  67. filter.ResponseWhenTrue(m.ResponseModifier())
  68. if len(msg.ElseModifier) > 0 {
  69. em, err := parse.FromJSON(msg.ElseModifier)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if em != nil {
  74. filter.RequestWhenFalse(em.RequestModifier())
  75. filter.ResponseWhenFalse(em.ResponseModifier())
  76. }
  77. }
  78. return parse.NewResult(filter, msg.Scope)
  79. }