forked from google/martian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
196 lines (169 loc) · 4.41 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package proxyutil
import (
"fmt"
"net/http"
"strconv"
)
// Header is a generic representation of a set of HTTP headers for requests and
// responses.
type Header struct {
h http.Header
host func() string
cl func() int64
te func() []string
setHost func(string)
setCL func(int64)
setTE func([]string)
}
// RequestHeader returns a new set of headers from a request.
func RequestHeader(req *http.Request) *Header {
return &Header{
h: req.Header,
host: func() string { return req.Host },
cl: func() int64 { return req.ContentLength },
te: func() []string { return req.TransferEncoding },
setHost: func(host string) { req.Host = host },
setCL: func(cl int64) { req.ContentLength = cl },
setTE: func(te []string) { req.TransferEncoding = te },
}
}
// ResponseHeader returns a new set of headers from a request.
func ResponseHeader(res *http.Response) *Header {
return &Header{
h: res.Header,
host: func() string { return "" },
cl: func() int64 { return res.ContentLength },
te: func() []string { return res.TransferEncoding },
setHost: func(string) {},
setCL: func(cl int64) { res.ContentLength = cl },
setTE: func(te []string) { res.TransferEncoding = te },
}
}
// Set sets value at header name for the request or response.
func (h *Header) Set(name, value string) error {
switch http.CanonicalHeaderKey(name) {
case "Host":
h.setHost(value)
case "Content-Length":
cl, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
h.setCL(cl)
case "Transfer-Encoding":
h.setTE([]string{value})
default:
h.h.Set(name, value)
}
return nil
}
// Add appends the value to the existing header at name for the request or
// response.
func (h *Header) Add(name, value string) error {
switch http.CanonicalHeaderKey(name) {
case "Host":
if h.host() != "" {
return fmt.Errorf("proxyutil: illegal header multiple: %s", "Host")
}
return h.Set(name, value)
case "Content-Length":
if h.cl() > 0 {
return fmt.Errorf("proxyutil: illegal header multiple: %s", "Content-Length")
}
return h.Set(name, value)
case "Transfer-Encoding":
h.setTE(append(h.te(), value))
default:
h.h.Add(name, value)
}
return nil
}
// Get returns the first value at header name for the request or response.
func (h *Header) Get(name string) string {
switch http.CanonicalHeaderKey(name) {
case "Host":
return h.host()
case "Content-Length":
if h.cl() < 0 {
return ""
}
return strconv.FormatInt(h.cl(), 10)
case "Transfer-Encoding":
if len(h.te()) < 1 {
return ""
}
return h.te()[0]
default:
return h.h.Get(name)
}
}
// All returns all the values for header name. If the header does not exist it
// returns nil, false.
func (h *Header) All(name string) ([]string, bool) {
switch http.CanonicalHeaderKey(name) {
case "Host":
if h.host() == "" {
return nil, false
}
return []string{h.host()}, true
case "Content-Length":
if h.cl() <= 0 {
return nil, false
}
return []string{strconv.FormatInt(h.cl(), 10)}, true
case "Transfer-Encoding":
if h.te() == nil {
return nil, false
}
return h.te(), true
default:
vs, ok := h.h[http.CanonicalHeaderKey(name)]
return vs, ok
}
}
// Del deletes the header at name for the request or response.
func (h *Header) Del(name string) {
switch http.CanonicalHeaderKey(name) {
case "Host":
h.setHost("")
case "Content-Length":
h.setCL(-1)
case "Transfer-Encoding":
h.setTE(nil)
default:
h.h.Del(name)
}
}
// Map returns an http.Header that includes Host, Content-Length, and
// Transfer-Encoding.
func (h *Header) Map() http.Header {
hm := make(http.Header)
for k, vs := range h.h {
hm[k] = vs
}
for _, k := range []string{
"Host",
"Content-Length",
"Transfer-Encoding",
} {
vs, ok := h.All(k)
if !ok {
continue
}
hm[k] = vs
}
return hm
}