content

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: MIT Imports: 7 Imported by: 26

Documentation

Overview

Package content provides content negotiation handlers for the ozzo routing package.

Index

Constants

MIME types

View Source
const Language = "Language"

Language is the key used to store and retrieve the chosen language in routing.Context

Variables

DataWriters lists all supported content types and the corresponding data writers. By default, JSON, XML, and HTML are supported. You may modify this variable before calling TypeNegotiator to customize supported data writers.

Functions

func LanguageNegotiator

func LanguageNegotiator(languages ...string) routing.Handler

LanguageNegotiator returns a content language negotiation handler.

The method takes a list of languages (locale IDs) that are supported by the application. The negotiator will determine the best language to use by checking the Accept-Language request header. If no match is found, the first language will be used.

In a handler, you can access the chosen language through routing.Context like the following:

func(c *routing.Context) error {
    language := c.Get(content.Language).(string)
}

If you do not specify languages, the negotiator will set the language to be "en-US".

func NegotiateContentType

func NegotiateContentType(r *http.Request, offers []string, defaultOffer string) string

NegotiateContentType negotiates the content types based on the given request and allowed types.

func TypeNegotiator

func TypeNegotiator(formats ...string) routing.Handler

TypeNegotiator returns a content type negotiation handler.

The method takes a list of response MIME types that are supported by the application. The negotiator will determine the best response MIME type to use by checking the "Accept" HTTP header. If no match is found, the first MIME type will be used.

The negotiator will set the "Content-Type" response header as the chosen MIME type. It will call routing.Context.SetDataWriter() to set the appropriate data writer that can write data in the negotiated format.

If you do not specify any supported MIME types, the negotiator will use "text/html" as the response MIME type.

Types

type AcceptRange

type AcceptRange struct {
	Type       string
	Subtype    string
	Weight     float64
	Parameters map[string]string
	// contains filtered or unexported fields
}

AcceptRange represents an accept range as defined in https://tools.ietf.org/html/rfc7231#section-5.3.2

Accept = #( media-range [ accept-params ] )

media-range    = ( "*/*"
                 / ( type "/" "*" )
                 / ( type "/" subtype )
                 ) *( OWS ";" OWS parameter )
accept-params  = weight *( accept-ext )
accept-ext = OWS ";" OWS token [ "=" ( token / quoted-string ) ]

func AcceptMediaTypes

func AcceptMediaTypes(r *http.Request) []AcceptRange

AcceptMediaTypes builds a list of AcceptRange from the given HTTP request.

func ParseAcceptRange

func ParseAcceptRange(accept string) AcceptRange

ParseAcceptRange parses a single accept string into an AcceptRange.

func ParseAcceptRanges

func ParseAcceptRanges(accepts string) []AcceptRange

ParseAcceptRanges parses an Accept header into a list of AcceptRange

func (AcceptRange) RawString

func (a AcceptRange) RawString() string

RawString returns the raw string in the request specifying the accept range.

type HTMLDataWriter

type HTMLDataWriter struct{}

HTMLDataWriter sets the "Content-Type" response header as "text/html; charset=UTF-8" and calls routing.DefaultDataWriter to write the given data to the response.

func (*HTMLDataWriter) SetHeader

func (w *HTMLDataWriter) SetHeader(res http.ResponseWriter)

SetHeader sets the Content-Type response header.

func (*HTMLDataWriter) Write

func (w *HTMLDataWriter) Write(res http.ResponseWriter, data interface{}) error

type JSONDataWriter

type JSONDataWriter struct{}

JSONDataWriter sets the "Content-Type" response header as "application/json" and writes the given data in JSON format to the response.

func (*JSONDataWriter) SetHeader

func (w *JSONDataWriter) SetHeader(res http.ResponseWriter)

SetHeader sets the Content-Type response header.

func (*JSONDataWriter) Write

func (w *JSONDataWriter) Write(res http.ResponseWriter, data interface{}) (err error)

type XMLDataWriter

type XMLDataWriter struct{}

XMLDataWriter sets the "Content-Type" response header as "application/xml; charset=UTF-8" and writes the given data in XML format to the response.

func (*XMLDataWriter) SetHeader

func (w *XMLDataWriter) SetHeader(res http.ResponseWriter)

SetHeader sets the Content-Type response header.

func (*XMLDataWriter) Write

func (w *XMLDataWriter) Write(res http.ResponseWriter, data interface{}) (err error)

Jump to

Keyboard shortcuts

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