Documentation
¶
Overview ¶
Package textproto implements generic support for text-based request/response protocols in the style of HTTP, NNTP, and SMTP.
The package provides:
Error, which represents a numeric error response from a server.
Pipeline, to manage pipelined requests and responses in a client.
Reader, to read numeric response code lines, key: value headers, lines wrapped with leading spaces on continuation lines, and whole text blocks ending with a dot on a line by itself.
Writer, to write dot-encoded text blocks.
Conn, a convenient packaging of Reader, Writer, and Pipeline for use with a single network connection.
Index ¶
- func CanonicalMIMEHeaderKey(s string) string
- func TrimBytes(b []byte) []byte
- func TrimString(s string) string
- type Conn
- type Error
- type MIMEHeader
- type Pipeline
- type ProtocolError
- type Reader
- func (r *Reader) DotReader() io.Reader
- func (r *Reader) ReadCodeLine(expectCode int) (code int, message string, err error)
- func (r *Reader) ReadContinuedLine() (string, error)
- func (r *Reader) ReadContinuedLineBytes() ([]byte, error)
- func (r *Reader) ReadDotBytes() ([]byte, error)
- func (r *Reader) ReadDotLines() ([]string, error)
- func (r *Reader) ReadLine() (string, error)
- func (r *Reader) ReadLineBytes() ([]byte, error)
- func (r *Reader) ReadMIMEHeader() (MIMEHeader, error)
- func (r *Reader) ReadResponse(expectCode int) (code int, message string, err error)
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CanonicalMIMEHeaderKey ¶
CanonicalMIMEHeaderKey returns the canonical format of the MIME 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". MIME header keys are assumed to be ASCII only. If s contains a space or invalid header field bytes, it is returned without modifications.
func TrimString ¶ added in go1.1
TrimString returns s without leading and trailing ASCII space.
Types ¶
type Conn ¶
A Conn represents a textual network protocol connection. It consists of a Reader and Writer to manage I/O and a Pipeline to sequence concurrent requests on the connection. These embedded types carry methods with them; see the documentation of those types for details.
func Dial ¶
Dial connects to the given address on the given network using net.Dial and then returns a new Conn for the connection.
func NewConn ¶
func NewConn(conn io.ReadWriteCloser) *Conn
NewConn returns a new Conn using conn for I/O.
func (*Conn) Cmd ¶
Cmd is a convenience method that sends a command after waiting its turn in the pipeline. The command text is the result of formatting format with args and appending \r\n. Cmd returns the id of the command, for use with StartResponse and EndResponse.
For example, a client might run a HELP command that returns a dot-body by using:
id, err := c.Cmd("HELP")
if err != nil {
return nil, err
}
c.StartResponse(id)
defer c.EndResponse(id)
if _, _, err = c.ReadCodeLine(110); err != nil {
return nil, err
}
text, err := c.ReadDotBytes()
if err != nil {
return nil, err
}
return c.ReadCodeLine(250)
type MIMEHeader ¶
A MIMEHeader represents a MIME-style header mapping keys to sets of values.
func (MIMEHeader) Add ¶
func (h MIMEHeader) Add(key, value string)
Add adds the key, value pair to the header. It appends to any existing values associated with key.
func (MIMEHeader) Del ¶
func (h MIMEHeader) Del(key string)
Del deletes the values associated with key.
func (MIMEHeader) Get ¶
func (h MIMEHeader) Get(key string) string
Get gets the first value associated with the given key. It is case insensitive; CanonicalMIMEHeaderKey is used to canonicalize the provided key. If there are no values associated with the key, Get returns "". To access multiple values of a key, or to use non-canonical keys, access the map directly.
func (MIMEHeader) Set ¶
func (h MIMEHeader) Set(key, value string)
Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
A Pipeline manages a pipelined in-order request/response sequence.
To use a Pipeline p to manage multiple clients on a connection, each client should run:
id := p.Next() // take a number p.StartRequest(id) // wait for turn to send request «send request» p.EndRequest(id) // notify Pipeline that request is sent p.StartResponse(id) // wait for turn to read response «read response» p.EndResponse(id) // notify Pipeline that response is read
A pipelined server can use the same calls to ensure that responses computed in parallel are written in the correct order.
func (*Pipeline) EndRequest ¶
EndRequest notifies p that the request with the given id has been sent (or, if this is a server, received).
func (*Pipeline) EndResponse ¶
EndResponse notifies p that the response with the given id has been received (or, if this is a server, sent).
func (*Pipeline) StartRequest ¶
StartRequest blocks until it is time to send (or, if this is a server, receive) the request with the given id.
func (*Pipeline) StartResponse ¶
StartResponse blocks until it is time to receive (or, if this is a server, send) the request with the given id.
type ProtocolError ¶
type ProtocolError string
A ProtocolError describes a protocol violation such as an invalid response or a hung-up connection.