| // 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 | |
| import ( | |
| "io" | |
| "net/textproto" | |
| "sort" | |
| "strings" | |
| "time" | |
| ) | |
| // 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 "". | |
| // To access multiple values of a key, access the map directly | |
| // with CanonicalHeaderKey. | |
| func (h Header) Get(key string) string { | |
| return textproto.MIMEHeader(h).Get(key) | |
| } | |
| // 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 "" | |
| } | |
| // Del deletes the values associated with key. | |
| func (h Header) Del(key string) { | |
| textproto.MIMEHeader(h).Del(key) | |
| } | |
| // Write writes a header in wire format. | |
| func (h Header) Write(w io.Writer) error { | |
| return h.WriteSubset(w, nil) | |
| } | |
| var timeFormats = []string{ | |
| TimeFormat, | |
| time.RFC850, | |
| time.ANSIC, | |
| } | |
| // ParseTime parses a time header (such as the Date: header), | |
| // trying each of the three formats allowed by HTTP/1.1: | |
| // TimeFormat, time.RFC850, and time.ANSIC. | |
| func ParseTime(text string) (t time.Time, err error) { | |
| for _, layout := range timeFormats { | |
| t, err = time.Parse(layout, text) | |
| if err == nil { | |
| return | |
| } | |
| } | |
| return | |
| } | |
| var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ") | |
| type writeStringer interface { | |
| WriteString(string) (int, error) | |
| } | |
| // stringWriter implements WriteString on a Writer. | |
| type stringWriter struct { | |
| w io.Writer | |
| } | |
| func (w stringWriter) WriteString(s string) (n int, err error) { | |
| return w.w.Write([]byte(s)) | |
| } | |
| type keyValues struct { | |
| key string | |
| values []string | |
| } | |
| type byKey []keyValues | |
| func (s byKey) Len() int { return len(s) } | |
| func (s byKey) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | |
| func (s byKey) Less(i, j int) bool { return s[i].key < s[j].key } | |
| func (h Header) sortedKeyValues(exclude map[string]bool) []keyValues { | |
| kvs := make([]keyValues, 0, len(h)) | |
| for k, vv := range h { | |
| if !exclude[k] { | |
| kvs = append(kvs, keyValues{k, vv}) | |
| } | |
| } | |
| sort.Sort(byKey(kvs)) | |
| return kvs | |
| } | |
| // WriteSubset writes a header in wire format. | |
| // If exclude is not nil, keys where exclude[key] == true are not written. | |
| func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error { | |
| ws, ok := w.(writeStringer) | |
| if !ok { | |
| ws = stringWriter{w} | |
| } | |
| for _, kv := range h.sortedKeyValues(exclude) { | |
| for _, v := range kv.values { | |
| v = headerNewlineToSpace.Replace(v) | |
| v = textproto.TrimString(v) | |
| for _, s := range []string{kv.key, ": ", v, "\r\n"} { | |
| if _, err := ws.WriteString(s); err != nil { | |
| return err | |
| } | |
| } | |
| } | |
| } | |
| return nil | |
| } | |
| // 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) } | |
| // 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. | |
| // The token is ASCII, so checking only a single byte | |
| // is sufficient. We skip this potential starting | |
| // position if both the first byte and its potential | |
| // ASCII uppercase equivalent (b|0x20) don't match. | |
| // False positives ('^' => '~') are caught by EqualFold. | |
| 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' | |
| } |