spartan

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: Unlicense Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const (
	StatusSuccess     types.Status = 2
	StatusRedirect    types.Status = 3
	StatusClientError types.Status = 4
	StatusServerError types.Status = 5
)

The spartan response types.

View Source
const DefaultMaxRedirects int = 2

DefaultMaxRedirects is the number of chained redirects a Client will perform for a single request by default. This can be changed by altering the MaxRedirects field.

Variables

View Source
var (
	// InvalidRequestLine indicates a malformed first-line of a spartan request.
	InvalidRequestLine = errors.New("invalid request line")

	// InvalidRequestLineEnding says that a spartan request's first line wasn't terminated with CRLF.
	InvalidRequestLineEnding = errors.New("invalid request line ending")
)
View Source
var ExceededMaxRedirects = errors.New("spartan.Client: exceeded MaxRedirects")
View Source
var InvalidResponseHeaderLine = errors.New("Invalid response header line.")

InvalidResponseHeaderLine indicates a malformed spartan response line.

View Source
var InvalidResponseLineEnding = errors.New("Invalid response line ending.")

InvalidResponseLineEnding indicates that a spartan response header didn't end with "\r\n".

View Source
var ServerProtocol types.ServerProtocol = proto{}

Functions

func ClientError

func ClientError(err error) *types.Response

ClientError builds a "client error" spartan response.

func GetRequestBody

func GetRequestBody(request *types.Request) io.Reader

GetRequestBody returns a reader of the spartan request body.

It will return nil if the request has no body.

func GetRequestContentLength

func GetRequestContentLength(request *types.Request) int

GetRequestContentLength reads the remaining un-read number of bytes in a request body.

It will immediately return 0 if there is no request body.

func NewResponseReader

func NewResponseReader(response *types.Response) types.ResponseReader

NewResponseReader builds a reader for a response.

func NewServer

func NewServer(
	ctx context.Context,
	hostname string,
	network string,
	address string,
	handler types.Handler,
	baseLog logging.Logger,
) (types.Server, error)

NewServer builds a spartan server.

func ParseRequest

func ParseRequest(rdr io.Reader) (*types.Request, int, error)

ParseRequest parses a single spartan request and the indicated content-length from a reader.

If ther reader artument is a *bufio.Reader, it will only read a single line from it.

func ParseResponse

func ParseResponse(rdr io.Reader) (*types.Response, error)

func Redirect

func Redirect(url string) *types.Response

Redirect builds a spartan redirect response.

func ServerError

func ServerError(err error) *types.Response

ServerError builds a "server error" spartan response.

func Success

func Success(mediatype string, body io.Reader) *types.Response

Success builds a successful spartan response.

Types

type Client

type Client struct {
	MaxRedirects int
}

Client is used for sending spartan requests and receiving responses.

It carries no state and is reusable simultaneously by multiple goroutines.

The zero value is immediately usabble, but will not follow redirects.

func NewClient added in v1.1.0

func NewClient() Client

NewClient creates a spartan Client which will follow DefaultMaxRedirects.

func (Client) Fetch added in v1.1.0

func (c Client) Fetch(ctx context.Context, url string) (*types.Response, error)

Fetch parses a URL string and fetches the spartan resource.

It will resolve any redirects along the way, up to client.MaxRedirects.

func (Client) IsRedirect added in v1.1.0

func (c Client) IsRedirect(response *types.Response) bool

func (Client) RoundTrip

func (c Client) RoundTrip(ctx context.Context, request *types.Request) (*types.Response, error)

RoundTrip sends a single spartan request and returns its response.

Jump to

Keyboard shortcuts

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