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.
 
 
 

85 rivejä
2.2 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 header
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "github.com/google/martian"
  19. "github.com/google/martian/parse"
  20. "github.com/google/martian/proxyutil"
  21. )
  22. func init() {
  23. parse.Register("header.Blacklist", blacklistModifierFromJSON)
  24. }
  25. type blacklistModifier struct {
  26. names []string
  27. }
  28. type blacklistModifierJSON struct {
  29. Names []string `json:"names"`
  30. Scope []parse.ModifierType `json:"scope"`
  31. }
  32. // ModifyRequest deletes all request headers based on the header name.
  33. func (m *blacklistModifier) ModifyRequest(req *http.Request) error {
  34. h := proxyutil.RequestHeader(req)
  35. for _, name := range m.names {
  36. h.Del(name)
  37. }
  38. return nil
  39. }
  40. // ModifyResponse deletes all response headers based on the header name.
  41. func (m *blacklistModifier) ModifyResponse(res *http.Response) error {
  42. h := proxyutil.ResponseHeader(res)
  43. for _, name := range m.names {
  44. h.Del(name)
  45. }
  46. return nil
  47. }
  48. // NewBlacklistModifier returns a modifier that will delete any header that
  49. // matches a name contained in the names parameter.
  50. func NewBlacklistModifier(names ...string) martian.RequestResponseModifier {
  51. return &blacklistModifier{
  52. names: names,
  53. }
  54. }
  55. // blacklistModifierFromJSON takes a JSON message as a byte slice and returns
  56. // a blacklistModifier and an error.
  57. //
  58. // Example JSON configuration message:
  59. // {
  60. // "names": ["X-Header", "Y-Header"],
  61. // "scope": ["request", "result"]
  62. // }
  63. func blacklistModifierFromJSON(b []byte) (*parse.Result, error) {
  64. msg := &blacklistModifierJSON{}
  65. if err := json.Unmarshal(b, msg); err != nil {
  66. return nil, err
  67. }
  68. return parse.NewResult(NewBlacklistModifier(msg.Names...), msg.Scope)
  69. }