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.
 
 
 

108 lines
2.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 martianurl
  15. import (
  16. "encoding/json"
  17. "net/url"
  18. "github.com/google/martian"
  19. "github.com/google/martian/filter"
  20. "github.com/google/martian/log"
  21. "github.com/google/martian/parse"
  22. )
  23. var noop = martian.Noop("url.Filter")
  24. func init() {
  25. parse.Register("url.Filter", filterFromJSON)
  26. }
  27. // Filter runs modifiers iff the request URL matches all of the segments in url.
  28. type Filter struct {
  29. *filter.Filter
  30. }
  31. type filterJSON struct {
  32. Scheme string `json:"scheme"`
  33. Host string `json:"host"`
  34. Path string `json:"path"`
  35. Query string `json:"query"`
  36. Modifier json.RawMessage `json:"modifier"`
  37. ElseModifier json.RawMessage `json:"else"`
  38. Scope []parse.ModifierType `json:"scope"`
  39. }
  40. // NewFilter constructs a filter that applies the modifer when the
  41. // request URL matches all of the provided URL segments.
  42. func NewFilter(u *url.URL) *Filter {
  43. log.Debugf("martianurl.NewFilter: %s", u)
  44. m := NewMatcher(u)
  45. f := filter.New()
  46. f.SetRequestCondition(m)
  47. f.SetResponseCondition(m)
  48. return &Filter{f}
  49. }
  50. // filterFromJSON takes a JSON message as a byte slice and returns a
  51. // parse.Result that contains a URLFilter and a bitmask that represents the
  52. // type of modifier.
  53. //
  54. // Example JSON configuration message:
  55. // {
  56. // "scheme": "https",
  57. // "host": "example.com",
  58. // "path": "/foo/bar",
  59. // "query": "q=value",
  60. // "scope": ["request", "response"],
  61. // "modifier": { ... }
  62. // "else": { ... }
  63. // }
  64. func filterFromJSON(b []byte) (*parse.Result, error) {
  65. msg := &filterJSON{}
  66. if err := json.Unmarshal(b, msg); err != nil {
  67. return nil, err
  68. }
  69. filter := NewFilter(&url.URL{
  70. Scheme: msg.Scheme,
  71. Host: msg.Host,
  72. Path: msg.Path,
  73. RawQuery: msg.Query,
  74. })
  75. m, err := parse.FromJSON(msg.Modifier)
  76. if err != nil {
  77. return nil, err
  78. }
  79. filter.RequestWhenTrue(m.RequestModifier())
  80. filter.ResponseWhenTrue(m.ResponseModifier())
  81. if len(msg.ElseModifier) > 0 {
  82. em, err := parse.FromJSON(msg.ElseModifier)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if em != nil {
  87. filter.RequestWhenFalse(em.RequestModifier())
  88. filter.ResponseWhenFalse(em.ResponseModifier())
  89. }
  90. }
  91. return parse.NewResult(filter, msg.Scope)
  92. }