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