httpaccept

package module
v0.0.0-...-45de8a1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: MIT Imports: 3 Imported by: 0

README

go-httpaccess

Package httpaccess implements tools to deal with the HTTP "Accept" header.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/sourcecode.social/reiver/go-httpaccess

GoDoc

Example

//
values := httprequest.Header.Values("Accept")

// ...

accept, err := httpaccess.Parse(values...)

// ...

mediatype, err := accept.Negotiate("text/gemini", "text/html", "application/xhtml+xml")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPAccept

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

HTTPAccept represents the values of the Accept headers of an HTTP request.

Note that an HTTP request can have multople Accept headers.

func Create

func Create(mediaranges ...mediarange.MediaRange) HTTPAccept

Create creates an HTTPAccept.

For example:

// Accept: application/activity+json
accept := httpaccept.Create(
	mediarange.Create("application", "activity+json"), // application/activity+json
)

// Accept: image/png,image/gif
accept := httpaccept.Create(
	mediarange.Create("image", "png"), // image/png
	mediarange.Create("image", "gif"), // image/gif
)

func Parse

func Parse(headers ...string) (HTTPAccept, error)

Parse parses a list of strings and returns an HTTPAccept.

For example:

mr, err := mediaaccept.Parse("text/html,application/xhtml+xml, application/xml , image/avif,image/webp, */*")

And:

mr, err := mediaaccept.Parse("image/png,image/gif", "text/html,text/plain", "*/*")

func (HTTPAccept) Negotiate

func (receiver HTTPAccept) Negotiate(mediatypes ...string) (string, error)

Negotiate figures out if there is an acceptable media-type, and if there is returns the "best" one.

For example:

var accept httpaccept.HTTPAccept

// ...

contentType, err := accept.Negotiate("text/gemini", "text/html", "application/activity+json")

func (HTTPAccept) String

func (receiver HTTPAccept) String() string

String returns the serialized HTTP Accept header.

For example:

// "Accept: text/html,application/xhtml+xml\r\n"
s := httpaccept.Create(mediarange.Create("text", "html"), mediarange.Create("application","xhtml+xml")).String()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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