2011-02-22 22:39:25 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
2011-04-28 01:16:15 -06:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/textproto"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
2011-02-22 22:39:25 -07:00
|
|
|
|
|
|
|
// A Header represents the key-value pairs in an HTTP header.
|
|
|
|
type Header map[string][]string
|
|
|
|
|
|
|
|
// Add adds the key, value pair to the header.
|
|
|
|
// It appends to any existing values associated with key.
|
|
|
|
func (h Header) Add(key, value string) {
|
|
|
|
textproto.MIMEHeader(h).Add(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the header entries associated with key to
|
|
|
|
// the single element value. It replaces any existing
|
|
|
|
// values associated with key.
|
|
|
|
func (h Header) Set(key, value string) {
|
|
|
|
textproto.MIMEHeader(h).Set(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get gets the first value associated with the given key.
|
|
|
|
// If there are no values associated with the key, Get returns "".
|
2011-11-03 21:37:02 -06:00
|
|
|
// To access multiple values of a key, access the map directly
|
|
|
|
// with CanonicalHeaderKey.
|
2011-02-22 22:39:25 -07:00
|
|
|
func (h Header) Get(key string) string {
|
|
|
|
return textproto.MIMEHeader(h).Get(key)
|
|
|
|
}
|
|
|
|
|
2012-05-28 12:07:24 -06:00
|
|
|
// get is like Get, but key must already be in CanonicalHeaderKey form.
|
|
|
|
func (h Header) get(key string) string {
|
|
|
|
if v := h[key]; len(v) > 0 {
|
|
|
|
return v[0]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2011-02-22 22:39:25 -07:00
|
|
|
// Del deletes the values associated with key.
|
|
|
|
func (h Header) Del(key string) {
|
|
|
|
textproto.MIMEHeader(h).Del(key)
|
|
|
|
}
|
|
|
|
|
2011-04-28 01:16:15 -06:00
|
|
|
// Write writes a header in wire format.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (h Header) Write(w io.Writer) error {
|
2011-04-28 01:16:15 -06:00
|
|
|
return h.WriteSubset(w, nil)
|
|
|
|
}
|
|
|
|
|
2011-10-03 16:19:04 -06:00
|
|
|
var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")
|
|
|
|
|
2011-04-28 01:16:15 -06:00
|
|
|
// WriteSubset writes a header in wire format.
|
|
|
|
// If exclude is not nil, keys where exclude[key] == true are not written.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error {
|
2011-04-28 01:16:15 -06:00
|
|
|
keys := make([]string, 0, len(h))
|
|
|
|
for k := range h {
|
|
|
|
if exclude == nil || !exclude[k] {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
}
|
2011-07-07 18:52:50 -06:00
|
|
|
sort.Strings(keys)
|
2011-04-28 01:16:15 -06:00
|
|
|
for _, k := range keys {
|
|
|
|
for _, v := range h[k] {
|
2011-10-03 16:19:04 -06:00
|
|
|
v = headerNewlineToSpace.Replace(v)
|
2011-04-28 01:16:15 -06:00
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
if _, err := fmt.Fprintf(w, "%s: %s\r\n", k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-02-22 22:39:25 -07:00
|
|
|
// CanonicalHeaderKey returns the canonical format of the
|
|
|
|
// header key s. The canonicalization converts the first
|
|
|
|
// letter and any letter following a hyphen to upper case;
|
|
|
|
// the rest are converted to lowercase. For example, the
|
|
|
|
// canonical key for "accept-encoding" is "Accept-Encoding".
|
|
|
|
func CanonicalHeaderKey(s string) string { return textproto.CanonicalMIMEHeaderKey(s) }
|
2012-05-28 11:55:39 -06:00
|
|
|
|
|
|
|
// hasToken returns whether token appears with v, ASCII
|
|
|
|
// case-insensitive, with space or comma boundaries.
|
|
|
|
// token must be all lowercase.
|
|
|
|
// v may contain mixed cased.
|
|
|
|
func hasToken(v, token string) bool {
|
|
|
|
if len(token) > len(v) || token == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v == token {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for sp := 0; sp <= len(v)-len(token); sp++ {
|
|
|
|
// Check that first character is good.
|
|
|
|
if b := v[sp]; b != token[0] && b|0x20 != token[0] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check that start pos is on a valid token boundary.
|
|
|
|
if sp > 0 && !isTokenBoundary(v[sp-1]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check that end pos is on a valid token boundary.
|
|
|
|
if endPos := sp + len(token); endPos != len(v) && !isTokenBoundary(v[endPos]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.EqualFold(v[sp:sp+len(token)], token) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTokenBoundary(b byte) bool {
|
|
|
|
return b == ' ' || b == ',' || b == '\t'
|
|
|
|
}
|