mime

Imports

Imports #

"strings"
"fmt"
"slices"
"strings"
"sync"
"bufio"
"os"
"strings"
"bufio"
"os"
"strings"
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"strings"
"unicode"
"unicode/utf8"
"errors"
"fmt"
"maps"
"slices"
"strings"
"unicode"
"internal/syscall/windows/registry"

Constants & Variables

BEncoding const #

BEncoding represents Base64 encoding scheme as defined by RFC 2045.

const BEncoding = *ast.CallExpr

ErrInvalidMediaParameter var #

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

var ErrInvalidMediaParameter = *ast.CallExpr

QEncoding const #

QEncoding represents the Q-encoding scheme as defined by RFC 2047.

const QEncoding = *ast.CallExpr

builtinTypesLower var #

var builtinTypesLower = map[string]string{...}

errInvalidWord var #

var errInvalidWord = *ast.CallExpr

extensions var #

var extensions sync.Map

extensionsMu var #

extensions maps from MIME type to list of lowercase file extensions: "image/jpeg" => [".jpg", ".jpeg"]

var extensionsMu sync.Mutex

maxBase64Len var #

var maxBase64Len = *ast.CallExpr

maxContentLen const #

maxContentLen is how much content can be encoded, ignoring the header and 2-byte footer.

const maxContentLen = *ast.BinaryExpr

maxEncodedWordLen const #

The maximum length of an encoded-word is 75 characters. See RFC 2047, section 2.

const maxEncodedWordLen = 75

mimeGlobs var #

See https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.21.html for the FreeDesktop Shared MIME-info Database specification.

var mimeGlobs = []string{...}

mimeTypes var #

var mimeTypes sync.Map

mimeTypesLower var #

var mimeTypesLower sync.Map

once var #

var once sync.Once

osInitMime var #

var osInitMime func()

testInitMime var #

var testInitMime func()

typeFiles var #

Common locations for mime.types files on unix.

var typeFiles = []string{...}

typeFiles var #

var typeFiles = []string{...}

upperhex const #

const upperhex = "0123456789ABCDEF"

Type Aliases

WordEncoder type #

A WordEncoder is an RFC 2047 encoded-word encoder.

type WordEncoder byte

Structs

WordDecoder struct #

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

type WordDecoder struct {
CharsetReader func(charset string, input io.Reader) (io.Reader, error)
}

Functions

AddExtensionType function #

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

func AddExtensionType(ext string, typ string) error

Decode method #

Decode decodes an RFC 2047 encoded-word.

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

DecodeHeader method #

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

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

Encode method #

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.

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

ExtensionsByType function #

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 ExtensionsByType(typ string) ([]string, error)

FormatMediaType function #

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 FormatMediaType(t string, param map[string]string) string

ParseMediaType function #

ParseMediaType parses a media type value and any optional parameters, per RFC 1521. 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 ParseMediaType(v string) (mediatype string, params map[string]string, err error)

TypeByExtension function #

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-info database or mime.types file(s) if available under one or more of these names: /usr/local/share/mime/globs2 /usr/share/mime/globs2 /etc/mime.types /etc/apache2/mime.types /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.

func TypeByExtension(ext string) string

bEncode method #

bEncode encodes s using base64 encoding and writes it to buf.

func (e WordEncoder) bEncode(buf *strings.Builder, charset string, s string)

checkMediaTypeDisposition function #

func checkMediaTypeDisposition(s string) error

closeWord function #

closeWord writes the end of an encoded-word into buf.

func closeWord(buf *strings.Builder)

consumeMediaParam function #

func consumeMediaParam(v string) (param string, value string, rest string)

consumeToken function #

consumeToken consumes a token from the beginning of provided string, per RFC 2045 section 5.1 (referenced from 2183), and return the token consumed and the rest of the string. Returns ("", v) on failure to consume at least one character.

func consumeToken(v string) (token string, rest string)

consumeValue function #

consumeValue consumes a "value" per RFC 2045, where a value is either a 'token' or a 'quoted-string'. On success, consumeValue returns the value consumed (and de-quoted/escaped, if a quoted-string) and the rest of the string. On failure, returns ("", v).

func consumeValue(v string) (value string, rest string)

convert method #

func (d *WordDecoder) convert(buf *strings.Builder, charset string, content []byte) error

decode function #

func decode(encoding byte, text string) ([]byte, error)

decode2231Enc function #

func decode2231Enc(v string) (string, bool)

encodeWord method #

encodeWord encodes a string into an encoded-word.

func (e WordEncoder) encodeWord(charset string, s string) string

fromHex function #

func fromHex(b byte) (byte, error)

hasNonWhitespace function #

hasNonWhitespace reports whether s (assumed to be ASCII) contains at least one byte of non-whitespace.

func hasNonWhitespace(s string) bool

init function #

func init()

init function #

func init()

init function #

func init()

init function #

func init()

init function #

func init()

init function #

func init()

initMime function #

func initMime()

initMimeForTests function #

func initMimeForTests() map[string]string

initMimeForTests function #

func initMimeForTests() map[string]string

initMimeForTests function #

func initMimeForTests() map[string]string

initMimePlan9 function #

func initMimePlan9()

initMimeUnix function #

func initMimeUnix()

initMimeWindows function #

func initMimeWindows()

isNotTokenChar function #

func isNotTokenChar(r rune) bool

isTSpecial function #

isTSpecial reports whether rune is in 'tspecials' as defined by RFC 1521 and RFC 2045.

func isTSpecial(r rune) bool

isToken function #

isToken reports whether s is a 'token' as defined by RFC 1521 and RFC 2045.

func isToken(s string) bool

isTokenChar function #

isTokenChar reports whether rune is in 'token' as defined by RFC 1521 and RFC 2045.

func isTokenChar(r rune) bool

isUTF8 function #

func isUTF8(charset string) bool

ishex function #

func ishex(c byte) bool

loadMimeFile function #

func loadMimeFile(filename string)

loadMimeFile function #

func loadMimeFile(filename string)

loadMimeGlobsFile function #

func loadMimeGlobsFile(filename string) error

needsEncoding function #

func needsEncoding(s string) bool

openWord method #

openWord writes the beginning of an encoded-word into buf.

func (e WordEncoder) openWord(buf *strings.Builder, charset string)

percentHexUnescape function #

func percentHexUnescape(s string) (string, error)

qDecode function #

qDecode decodes a Q encoded string.

func qDecode(s string) ([]byte, error)

qEncode method #

qEncode encodes s using Q encoding and writes it to buf. It splits the encoded-words when necessary.

func (e WordEncoder) qEncode(buf *strings.Builder, charset string, s string)

readHexByte function #

readHexByte returns the byte from its quoted-printable representation.

func readHexByte(a byte, b byte) (byte, error)

setExtensionType function #

func setExtensionType(extension string, mimeType string) error

setMimeTypes function #

setMimeTypes is used by initMime's non-test path, and by tests.

func setMimeTypes(lowerExt map[string]string, mixExt map[string]string)

splitWord method #

splitWord closes the current encoded-word and opens a new one.

func (e WordEncoder) splitWord(buf *strings.Builder, charset string)

unhex function #

func unhex(c byte) byte

writeQString function #

writeQString encodes s using Q encoding and writes it to buf.

func writeQString(buf *strings.Builder, s string)

Generated with Arrow