encodeHeader is for usage on client side to write request header. encodeHeader encodes headers and returns their HPACK bytes. headers must contain an even number of key/value pairs. There may be multiple pairs for keys (e.g. "cookie"). The :method, :path, and :scheme headers default to GET, / and
(headers ...string)
| 162 | // multiple pairs for keys (e.g. "cookie"). The :method, :path, and |
| 163 | // :scheme headers default to GET, / and https. |
| 164 | func (rcw *rawConnWrapper) encodeHeader(headers ...string) []byte { |
| 165 | if len(headers)%2 == 1 { |
| 166 | panic("odd number of kv args") |
| 167 | } |
| 168 | |
| 169 | rcw.headerBuf.Reset() |
| 170 | |
| 171 | if len(headers) == 0 { |
| 172 | // Fast path, mostly for benchmarks, so test code doesn't pollute |
| 173 | // profiles when we're looking to improve server allocations. |
| 174 | rcw.encodeHeaderField(":method", "GET") |
| 175 | rcw.encodeHeaderField(":path", "/") |
| 176 | rcw.encodeHeaderField(":scheme", "https") |
| 177 | return rcw.headerBuf.Bytes() |
| 178 | } |
| 179 | |
| 180 | if len(headers) == 2 && headers[0] == ":method" { |
| 181 | // Another fast path for benchmarks. |
| 182 | rcw.encodeHeaderField(":method", headers[1]) |
| 183 | rcw.encodeHeaderField(":path", "/") |
| 184 | rcw.encodeHeaderField(":scheme", "https") |
| 185 | return rcw.headerBuf.Bytes() |
| 186 | } |
| 187 | |
| 188 | pseudoCount := map[string]int{} |
| 189 | keys := []string{":method", ":path", ":scheme"} |
| 190 | vals := map[string][]string{ |
| 191 | ":method": {"GET"}, |
| 192 | ":path": {"/"}, |
| 193 | ":scheme": {"https"}, |
| 194 | } |
| 195 | for len(headers) > 0 { |
| 196 | k, v := headers[0], headers[1] |
| 197 | headers = headers[2:] |
| 198 | if _, ok := vals[k]; !ok { |
| 199 | keys = append(keys, k) |
| 200 | } |
| 201 | if strings.HasPrefix(k, ":") { |
| 202 | pseudoCount[k]++ |
| 203 | if pseudoCount[k] == 1 { |
| 204 | vals[k] = []string{v} |
| 205 | } else { |
| 206 | // Allows testing of invalid headers w/ dup pseudo fields. |
| 207 | vals[k] = append(vals[k], v) |
| 208 | } |
| 209 | } else { |
| 210 | vals[k] = append(vals[k], v) |
| 211 | } |
| 212 | } |
| 213 | for _, k := range keys { |
| 214 | for _, v := range vals[k] { |
| 215 | rcw.encodeHeaderField(k, v) |
| 216 | } |
| 217 | } |
| 218 | return rcw.headerBuf.Bytes() |
| 219 | } |
| 220 | |
| 221 | func (rcw *rawConnWrapper) writeHeaders(p http2.HeadersFrameParam) error { |
nothing calls this directly
no test coverage detected