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.
 
 
 

77 lines
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. "net/http"
  17. "strings"
  18. "github.com/google/martian"
  19. )
  20. // Hop-by-hop headers as defined by RFC2616.
  21. //
  22. // http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-14#section-7.1.3.1
  23. var hopByHopHeaders = []string{
  24. "Connection",
  25. "Keep-Alive",
  26. "Proxy-Authenticate",
  27. "Proxy-Authorization",
  28. "Proxy-Connection", // Non-standard, but required for HTTP/2.
  29. "Te",
  30. "Trailer",
  31. "Transfer-Encoding",
  32. "Upgrade",
  33. }
  34. type hopByHopModifier struct{}
  35. // NewHopByHopModifier removes Hop-By-Hop headers from requests and
  36. // responses.
  37. func NewHopByHopModifier() martian.RequestResponseModifier {
  38. return &hopByHopModifier{}
  39. }
  40. // ModifyRequest removes all hop-by-hop headers defined by RFC2616 as
  41. // well as any additional hop-by-hop headers specified in the
  42. // Connection header.
  43. func (m *hopByHopModifier) ModifyRequest(req *http.Request) error {
  44. removeHopByHopHeaders(req.Header)
  45. return nil
  46. }
  47. // ModifyResponse removes all hop-by-hop headers defined by RFC2616 as
  48. // well as any additional hop-by-hop headers specified in the
  49. // Connection header.
  50. func (m *hopByHopModifier) ModifyResponse(res *http.Response) error {
  51. removeHopByHopHeaders(res.Header)
  52. return nil
  53. }
  54. func removeHopByHopHeaders(header http.Header) {
  55. // Additional hop-by-hop headers may be specified in `Connection` headers.
  56. // http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-14#section-9.1
  57. for _, vs := range header["Connection"] {
  58. for _, v := range strings.Split(vs, ",") {
  59. k := http.CanonicalHeaderKey(strings.TrimSpace(v))
  60. header.Del(k)
  61. }
  62. }
  63. for _, k := range hopByHopHeaders {
  64. header.Del(k)
  65. }
  66. }