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.
 
 
 

102 lines
2.5 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 provides utilities for modifying, filtering,
  15. // and verifying URLs in martian.Proxy.
  16. package martianurl
  17. import (
  18. "encoding/json"
  19. "net/http"
  20. "net/url"
  21. "github.com/google/martian"
  22. "github.com/google/martian/parse"
  23. )
  24. // Modifier alters the request URL fields to match the fields of
  25. // url and adds a X-Forwarded-Url header that contains the original
  26. // value of the request URL.
  27. type Modifier struct {
  28. url *url.URL
  29. }
  30. type modifierJSON struct {
  31. Scheme string `json:"scheme"`
  32. Host string `json:"host"`
  33. Path string `json:"path"`
  34. Query string `json:"query"`
  35. Scope []parse.ModifierType `json:"scope"`
  36. }
  37. func init() {
  38. parse.Register("url.Modifier", modifierFromJSON)
  39. }
  40. // ModifyRequest sets the fields of req.URL to m.Url if they are not the zero value.
  41. func (m *Modifier) ModifyRequest(req *http.Request) error {
  42. if m.url.Scheme != "" {
  43. req.URL.Scheme = m.url.Scheme
  44. }
  45. if m.url.Host != "" {
  46. req.URL.Host = m.url.Host
  47. }
  48. if m.url.Path != "" {
  49. req.URL.Path = m.url.Path
  50. }
  51. if m.url.RawQuery != "" {
  52. req.URL.RawQuery = m.url.RawQuery
  53. }
  54. if m.url.Fragment != "" {
  55. req.URL.Fragment = m.url.Fragment
  56. }
  57. return nil
  58. }
  59. // NewModifier overrides the url of the request.
  60. func NewModifier(url *url.URL) martian.RequestModifier {
  61. return &Modifier{
  62. url: url,
  63. }
  64. }
  65. // modifierFromJSON builds a martianurl.Modifier from JSON.
  66. //
  67. // Example modifier JSON:
  68. // {
  69. // "martianurl.Modifier": {
  70. // "scope": ["request"],
  71. // "scheme": "https",
  72. // "host": "www.google.com",
  73. // "path": "/proxy",
  74. // "query": "testing=true"
  75. // }
  76. // }
  77. func modifierFromJSON(b []byte) (*parse.Result, error) {
  78. msg := &modifierJSON{}
  79. if err := json.Unmarshal(b, msg); err != nil {
  80. return nil, err
  81. }
  82. mod := NewModifier(&url.URL{
  83. Scheme: msg.Scheme,
  84. Host: msg.Host,
  85. Path: msg.Path,
  86. RawQuery: msg.Query,
  87. })
  88. return parse.NewResult(mod, msg.Scope)
  89. }