textproto

package
v0.18.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 10 Imported by: 89

Documentation

Overview

Package textproto implements low-level manipulation of MIME messages.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteHeader

func WriteHeader(w io.Writer, h Header) error

WriteHeader writes a MIME header to w.

Types

type Header struct {
	// contains filtered or unexported fields
}

A Header represents the key-value pairs in a message header.

The header representation is idempotent: if the header can be read and written, the result will be exactly the same as the original (including whitespace and header field ordering). This is required for e.g. DKIM.

Mutating the header is restricted: the only two allowed operations are inserting a new header field at the top and deleting a header field. This is again necessary for DKIM.

Example
package main

import (
	"fmt"

	"github.com/emersion/go-message/textproto"
)

func main() {
	var h textproto.Header
	h.Add("From", "<root@nsa.gov>")
	h.Add("To", "<root@gchq.gov.uk>")
	h.Set("Subject", "Tonight's dinner")

	fmt.Println("From: ", h.Get("From"))
	fmt.Println("Has Received: ", h.Has("Received"))

	fmt.Println("Header fields:")
	fields := h.Fields()
	for fields.Next() {
		fmt.Println("  ", fields.Key())
	}
}
Output:

func HeaderFromMap added in v0.15.0

func HeaderFromMap(m map[string][]string) Header

HeaderFromMap creates a header from a map of header fields.

This function is provided for interoperability with the standard library. If possible, ReadHeader should be used instead to avoid loosing information. The map representation looses the ordering of the fields, the capitalization of the header keys, and the whitespace of the original header.

func ReadHeader

func ReadHeader(r *bufio.Reader) (Header, error)

ReadHeader reads a MIME header from r. The header is a sequence of possibly continued "Key: Value" lines ending in a blank line.

To avoid denial of service attacks, the provided bufio.Reader should be reading from an io.LimitedReader or a similar Reader to bound the size of headers.

func (*Header) Add

func (h *Header) Add(k, v string)

Add adds the key, value pair to the header. It prepends to any existing fields associated with key.

Key and value should obey character requirements of RFC 6532. If you need to format or fold lines manually, use AddRaw.

func (*Header) AddRaw added in v0.11.1

func (h *Header) AddRaw(kv []byte)

AddRaw adds the raw key, value pair to the header.

The supplied byte slice should be a complete field in the "Key: Value" form including trailing CRLF. If there is no comma in the input - AddRaw panics. No changes are made to kv contents and it will be copied into WriteHeader output as is.

kv is directly added to the underlying structure and therefore should not be modified after the AddRaw call.

func (*Header) Copy added in v0.10.1

func (h *Header) Copy() Header

Copy creates an independent copy of the header.

func (*Header) Del

func (h *Header) Del(k string)

Del deletes the values associated with key.

func (*Header) Fields

func (h *Header) Fields() HeaderFields

Fields iterates over all the header fields.

The header may not be mutated while iterating, except using HeaderFields.Del.

func (*Header) FieldsByKey

func (h *Header) FieldsByKey(k string) HeaderFields

FieldsByKey iterates over all fields having the specified key.

The header may not be mutated while iterating, except using HeaderFields.Del.

func (*Header) Get

func (h *Header) Get(k string) string

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns "".

func (*Header) Has

func (h *Header) Has(k string) bool

Has checks whether the header has a field with the specified key.

func (*Header) Len added in v0.10.8

func (h *Header) Len() int

Len returns the number of fields in the header.

func (*Header) Map added in v0.15.0

func (h *Header) Map() map[string][]string

Map returns all header fields as a map.

This function is provided for interoperability with the standard library. If possible, Fields should be used instead to avoid loosing information. The map representation looses the ordering of the fields, the capitalization of the header keys, and the whitespace of the original header.

func (*Header) Raw added in v0.12.0

func (h *Header) Raw(k string) ([]byte, error)

Raw gets the first raw header field associated with the given key.

The returned bytes contain a complete field in the "Key: value" form, including trailing CRLF.

The returned slice should not be modified and becomes invalid when the header is updated.

An error is returned if the header field contains incorrect characters (see RFC 6532).

func (*Header) Set

func (h *Header) Set(k, v string)

Set sets the header fields associated with key to the single field value. It replaces any existing values associated with key.

func (*Header) Values added in v0.14.0

func (h *Header) Values(k string) []string

Values returns all values associated with the given key.

The returned slice should not be modified and becomes invalid when the header is updated.

type HeaderFields

type HeaderFields interface {
	// Next advances to the next header field. It returns true on success, or
	// false if there is no next field.
	Next() (more bool)
	// Key returns the key of the current field.
	Key() string
	// Value returns the value of the current field.
	Value() string
	// Raw returns the raw current header field. See Header.Raw.
	Raw() ([]byte, error)
	// Del deletes the current field.
	Del()
	// Len returns the amount of header fields in the subset of header iterated
	// by this HeaderFields instance.
	//
	// For Fields(), it will return the amount of fields in the whole header section.
	// For FieldsByKey(), it will return the amount of fields with certain key.
	Len() int
}

HeaderFields iterates over header fields. Its cursor starts before the first field of the header. Use Next to advance from field to field.

type MultipartReader added in v0.10.2

type MultipartReader struct {
	// contains filtered or unexported fields
}

MultipartReader is an iterator over parts in a MIME multipart body. MultipartReader's underlying parser consumes its input as needed. Seeking isn't supported.

func NewMultipartReader added in v0.10.2

func NewMultipartReader(r io.Reader, boundary string) *MultipartReader

NewMultipartReader creates a new multipart reader reading from r using the given MIME boundary.

The boundary is usually obtained from the "boundary" parameter of the message's "Content-Type" header. Use mime.ParseMediaType to parse such headers.

func (*MultipartReader) NextPart added in v0.10.2

func (r *MultipartReader) NextPart() (*Part, error)

NextPart returns the next part in the multipart or an error. When there are no more parts, the error io.EOF is returned.

type MultipartWriter added in v0.10.2

type MultipartWriter struct {
	// contains filtered or unexported fields
}

A MultipartWriter generates multipart messages.

func NewMultipartWriter added in v0.10.2

func NewMultipartWriter(w io.Writer) *MultipartWriter

NewMultipartWriter returns a new multipart Writer with a random boundary, writing to w.

func (*MultipartWriter) Boundary added in v0.10.2

func (w *MultipartWriter) Boundary() string

Boundary returns the Writer's boundary.

func (*MultipartWriter) Close added in v0.10.2

func (w *MultipartWriter) Close() error

Close finishes the multipart message and writes the trailing boundary end line to the output.

func (*MultipartWriter) CreatePart added in v0.10.2

func (w *MultipartWriter) CreatePart(header Header) (io.Writer, error)

CreatePart creates a new multipart section with the provided header. The body of the part should be written to the returned Writer. After calling CreatePart, any previous part may no longer be written to.

func (*MultipartWriter) SetBoundary added in v0.10.2

func (w *MultipartWriter) SetBoundary(boundary string) error

SetBoundary overrides the Writer's default randomly-generated boundary separator with an explicit value.

SetBoundary must be called before any parts are created, may only contain certain ASCII characters, and must be non-empty and at most 70 bytes long.

type Part added in v0.10.2

type Part struct {
	Header Header
	// contains filtered or unexported fields
}

A Part represents a single part in a multipart body.

func (*Part) Close added in v0.10.2

func (p *Part) Close() error

func (*Part) Read added in v0.10.2

func (p *Part) Read(d []byte) (n int, err error)

Read reads the body of a part, after its headers and before the next part (if any) begins.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL