version: 1.10

package mime

import "mime"

Overview

Package mime implements parts of the MIME spec.

Index

Examples

Package files

encodedword.go grammar.go mediatype.go type.go type_unix.go

Constants

  1. const (
  2. // BEncoding represents Base64 encoding scheme as defined by RFC 2045.
  3. BEncoding = WordEncoder('b')
  4. // QEncoding represents the Q-encoding scheme as defined by RFC 2047.
  5. QEncoding = WordEncoder('q')
  6. )

Variables

  1. var ErrInvalidMediaParameter = errors.New("mime: invalid media parameter")

ErrInvalidMediaParameter is returned by ParseMediaType if the media type value was found but there was an error parsing the optional parameters

func AddExtensionType

  1. func AddExtensionType(ext, typ string) error

AddExtensionType sets the MIME type associated with the extension ext to typ. The extension should begin with a leading dot, as in “.html”.

func ExtensionsByType

  1. func ExtensionsByType(typ string) ([]string, error)

ExtensionsByType returns the extensions known to be associated with the MIME type typ. The returned extensions will each begin with a leading dot, as in “.html”. When typ has no associated extensions, ExtensionsByType returns an nil slice.

func FormatMediaType

  1. func FormatMediaType(t string, param map[string]string) string

FormatMediaType serializes mediatype t and the parameters param as a media type conforming to RFC 2045 and RFC 2616. The type and parameter names are written in lower-case. When any of the arguments result in a standard violation then FormatMediaType returns the empty string.

func ParseMediaType

  1. func ParseMediaType(v string) (mediatype string, params map[string]string, err error)

ParseMediaType parses a media type value and any optional parameters, per RFC

  1. Media types are the values in Content-Type and Content-Disposition headers (RFC 2183). On success, ParseMediaType returns the media type converted to lowercase and trimmed of white space and a non-nil map. If there is an error parsing the optional parameter, the media type will be returned along with the error ErrInvalidMediaParameter. The returned map, params, maps from the lowercase attribute to the attribute value with its case preserved.

func TypeByExtension

  1. func TypeByExtension(ext string) string

TypeByExtension returns the MIME type associated with the file extension ext. The extension ext should begin with a leading dot, as in “.html”. When ext has no associated type, TypeByExtension returns “”.

Extensions are looked up first case-sensitively, then case-insensitively.

The built-in table is small but on unix it is augmented by the local system’s mime.types file(s) if available under one or more of these names:

  1. /etc/mime.types
  2. /etc/apache2/mime.types
  3. /etc/apache/mime.types

On Windows, MIME types are extracted from the registry.

Text types have the charset parameter set to “utf-8” by default.

type WordDecoder

  1. type WordDecoder struct {
  2. // CharsetReader, if non-nil, defines a function to generate
  3. // charset-conversion readers, converting from the provided
  4. // charset into UTF-8.
  5. // Charsets are always lower-case. utf-8, iso-8859-1 and us-ascii charsets
  6. // are handled by default.
  7. // One of the CharsetReader's result values must be non-nil.
  8. CharsetReader func(charset string, input io.Reader) (io.Reader, error)
  9. }

A WordDecoder decodes MIME headers containing RFC 2047 encoded-words.

func (*WordDecoder) Decode

  1. func (d *WordDecoder) Decode(word string) (string, error)

Decode decodes an RFC 2047 encoded-word.

Example:

  1. dec := new(mime.WordDecoder)
  2. header, err := dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
  3. if err != nil {
  4. panic(err)
  5. }
  6. fmt.Println(header)
  7. dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
  8. switch charset {
  9. case "x-case":
  10. // Fake character set for example.
  11. // Real use would integrate with packages such
  12. // as code.google.com/p/go-charset
  13. content, err := ioutil.ReadAll(input)
  14. if err != nil {
  15. return nil, err
  16. }
  17. return bytes.NewReader(bytes.ToUpper(content)), nil
  18. default:
  19. return nil, fmt.Errorf("unhandled charset %q", charset)
  20. }
  21. }
  22. header, err = dec.Decode("=?x-case?q?hello!?=")
  23. if err != nil {
  24. panic(err)
  25. }
  26. fmt.Println(header)
  27. // Output:
  28. // ¡Hola, señor!
  29. // HELLO!

func (*WordDecoder) DecodeHeader

  1. func (d *WordDecoder) DecodeHeader(header string) (string, error)

DecodeHeader decodes all encoded-words of the given string. It returns an error if and only if CharsetReader of d returns an error.

Example:

  1. dec := new(mime.WordDecoder)
  2. header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <eric@example.org>, =?utf-8?q?Ana=C3=AFs?= <anais@example.org>")
  3. if err != nil {
  4. panic(err)
  5. }
  6. fmt.Println(header)
  7. header, err = dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=")
  8. if err != nil {
  9. panic(err)
  10. }
  11. fmt.Println(header)
  12. dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
  13. switch charset {
  14. case "x-case":
  15. // Fake character set for example.
  16. // Real use would integrate with packages such
  17. // as code.google.com/p/go-charset
  18. content, err := ioutil.ReadAll(input)
  19. if err != nil {
  20. return nil, err
  21. }
  22. return bytes.NewReader(bytes.ToUpper(content)), nil
  23. default:
  24. return nil, fmt.Errorf("unhandled charset %q", charset)
  25. }
  26. }
  27. header, err = dec.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=")
  28. if err != nil {
  29. panic(err)
  30. }
  31. fmt.Println(header)
  32. // Output:
  33. // Éric <eric@example.org>, Anaïs <anais@example.org>
  34. // ¡Hola, señor!
  35. // HELLO WORLD!

type WordEncoder

  1. type WordEncoder byte

A WordEncoder is an RFC 2047 encoded-word encoder.

func (WordEncoder) Encode

  1. func (e WordEncoder) Encode(charset, s string) string

Encode returns the encoded-word form of s. If s is ASCII without special characters, it is returned unchanged. The provided charset is the IANA charset name of s. It is case insensitive.

Example:

  1. fmt.Println(mime.QEncoding.Encode("utf-8", "¡Hola, señor!"))
  2. fmt.Println(mime.QEncoding.Encode("utf-8", "Hello!"))
  3. fmt.Println(mime.BEncoding.Encode("UTF-8", "¡Hola, señor!"))
  4. fmt.Println(mime.QEncoding.Encode("ISO-8859-1", "Caf\xE9"))
  5. // Output:
  6. // =?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=
  7. // Hello!
  8. // =?UTF-8?b?wqFIb2xhLCBzZcOxb3Ih?=
  9. // =?ISO-8859-1?q?Caf=E9?=

Subdirectories