contenttype

package module
v0.0.0-...-ad9861a Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2021 License: Unlicense Imports: 6 Imported by: 0

README

Content-Type support library for Go

This library can be used to parse the value Content-Type header (if one is present) and select an acceptable media type from the Accept header of HTTP request.

Usage

Media types are stored in MediaType structure which has Type (e.g. application), Subtype (e.g. json) and Parameters (e.g. charset: utf-8) attributes. Media types are not stored in a string because media type parameters are part of the media type (RFC 7231, 3.1.1.1. Media Type). To convert a string to MediaType use NewMediaType. To convert MediaType back to string use String function. If the Content-Type header is not present in the request, an empty MediaType is returned.

To get the MediaType of the incoming request call GetMediaType and pass the http.Request pointer to it. The function will return error if the Content-Type header is malformed according to RFC 7231, 3.1.1.5. Content-Type.

To get an acceptable media type from an Accept header of the incoming request call GetAcceptableMediaType and pass the http.Request pointer to it and an array of all the acceptable media types. The function will return the best match following the negotiation rules written in RFC 7231, 5.3.2. Accept or an error if the header is malformed or the content type in the Accept header is not supported. If the Accept header is not present in the request, the first media type from the acceptable type list is returned.

import (
	"log"
	"github.com/elnormous/contenttype"
)

func handleRequest(responseWriter http.ResponseWriter, request *http.Request) {
    mediaType, mediaTypeError := contenttype.GetMediaType(request)
    if mediaTypeError != nil {
        // handle the error
    }
    log.Println("Media type:", mediaType.String())

    availableMediaTypes := []MediaType{
        contenttype.NewMediaType("application/json"),
        contenttype.NewMediaType("application/xml"),
    }

    accepted, extParameters, acceptError := contenttype.GetAcceptableMediaType(request, availableMediaTypes)
    if acceptError != nil {
        // handle the error
    }
    log.Println("Accepted media type:", accepted.String(), "extension parameters:", extParameters)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidExtensionParameter = errors.New("Invalid extension parameter")
View Source
var ErrInvalidMediaRange = errors.New("Invalid media range")
View Source
var ErrInvalidMediaType = errors.New("Invalid media type")
View Source
var ErrInvalidParameter = errors.New("Invalid parameter")
View Source
var ErrInvalidWeight = errors.New("Invalid wieght")
View Source
var ErrNoAcceptableTypeFound = errors.New("No acceptable type found")
View Source
var ErrNoAvailableTypeGiven = errors.New("No available type given")

Functions

func GetAcceptableMediaType

func GetAcceptableMediaType(request interface{}, availableMediaTypes []MediaType) (MediaType, Parameters, error)

Choses a media type from available media types according to the Accept Returns the most suitable media type or an error if no type can be selected

Types

type FastHTTPAdapter

type FastHTTPAdapter struct {
	Request *fasthttp.Request
}

func (FastHTTPAdapter) GetAcceptHeader

func (f FastHTTPAdapter) GetAcceptHeader() string

func (FastHTTPAdapter) GetContentType

func (f FastHTTPAdapter) GetContentType() string

type HTTPAdapter

type HTTPAdapter interface {
	GetAcceptHeader() string
	GetContentType() string
}

type MediaType

type MediaType struct {
	Type       string
	Subtype    string
	Parameters Parameters
}

A struct for media type which holds type, subtype and parameters

func GetMediaType

func GetMediaType(request interface{}) (MediaType, error)

Gets the content of Content-Type header, parses it, and returns the parsed MediaType If the request does not contain the Content-Type header, an empty MediaType is returned

func NewMediaType

func NewMediaType(s string) MediaType

Parses the string and returns an instance of MediaType struct

func (*MediaType) String

func (mediaType *MediaType) String() string

Converts the MediaType to string

type NetHTTPAdapter

type NetHTTPAdapter struct {
	Request *http.Request
}

func (NetHTTPAdapter) GetAcceptHeader

func (n NetHTTPAdapter) GetAcceptHeader() string

func (NetHTTPAdapter) GetContentType

func (n NetHTTPAdapter) GetContentType() string

type NilAdapter

type NilAdapter struct {
}

func (NilAdapter) GetAcceptHeader

func (n NilAdapter) GetAcceptHeader() string

func (NilAdapter) GetContentType

func (n NilAdapter) GetContentType() string

type Parameters

type Parameters = map[string]string

A map for media type parameters

Jump to

Keyboard shortcuts

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