Home | History | Annotate | Download | only in textproto
      1 // Copyright 2010 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package textproto
      6 
      7 // A MIMEHeader represents a MIME-style header mapping
      8 // keys to sets of values.
      9 type MIMEHeader map[string][]string
     10 
     11 // Add adds the key, value pair to the header.
     12 // It appends to any existing values associated with key.
     13 func (h MIMEHeader) Add(key, value string) {
     14 	key = CanonicalMIMEHeaderKey(key)
     15 	h[key] = append(h[key], value)
     16 }
     17 
     18 // Set sets the header entries associated with key to
     19 // the single element value. It replaces any existing
     20 // values associated with key.
     21 func (h MIMEHeader) Set(key, value string) {
     22 	h[CanonicalMIMEHeaderKey(key)] = []string{value}
     23 }
     24 
     25 // Get gets the first value associated with the given key.
     26 // It is case insensitive; CanonicalMIMEHeaderKey is used
     27 // to canonicalize the provided key.
     28 // If there are no values associated with the key, Get returns "".
     29 // To access multiple values of a key, or to use non-canonical keys,
     30 // access the map directly.
     31 func (h MIMEHeader) Get(key string) string {
     32 	if h == nil {
     33 		return ""
     34 	}
     35 	v := h[CanonicalMIMEHeaderKey(key)]
     36 	if len(v) == 0 {
     37 		return ""
     38 	}
     39 	return v[0]
     40 }
     41 
     42 // Del deletes the values associated with key.
     43 func (h MIMEHeader) Del(key string) {
     44 	delete(h, CanonicalMIMEHeaderKey(key))
     45 }
     46