nego

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

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

Go to latest
Published: Oct 10, 2021 License: MIT Imports: 3 Imported by: 6

README

nego

Build Status GoDoc Go Report Card

Go package providing an implementation of HTTP Content Negotiation compliant with RFC 7231.

As defined in RFC 7231 the following request headers are sent by a user agent to engage in a proactive negotiation of the response content: Accept, Accept-Charset, Accept-Language and Accept-Encoding. This package provides convenient functions to negotiate the best and acceptable response content type, charset, language and encoding.

Installation

Install using go get github.com/kevinpollet/nego.

Usage

package main

import (
	"log"
	"net/http"
	"github.com/kevinpollet/nego"
)

func main() {
	handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		contentCharset := nego.NegotiateContentCharset(req, "utf-8")
		contentEncoding := nego.NegotiateContentEncoding(req, "gzip", "deflate")
		contentLanguage := nego.NegotiateContentLanguage(req, "fr", "en")
		contentType := nego.NegotiateContentType(req, "text/html", "text/plain")
		...
	})
}

Contributing

Contributions are welcome!

Want to file a bug, request a feature or contribute some code?

  1. Check out the Code of Conduct.
  2. Check for an existing issue corresponding to your bug or feature request.
  3. Open an issue to describe your bug or feature request.

License

MIT

Documentation

Overview

Package nego implements HTTP Content Negotiation functions compliant with RFC 7231.

See https://tools.ietf.org/html/rfc7231#section-5.3 for more details.

Example

This example shows how to use the negotiation functions.

import (
	"net/http"
	"github.com/kevinpollet/nego"
)

handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
	nego.NegotiateContentCharset(req, "utf-8")
	nego.NegotiateContentEncoding(req, "gzip", "deflate")
	nego.NegotiateContentLanguage(req, "fr", "en")
	nego.NegotiateContentType(req, "text/plain")
})

Index

Constants

View Source
const EncodingIdentity = "identity"

The identity encoding constant used as a synonym for "no encoding" in order to communicate when no encoding is preferred.

See https://tools.ietf.org/html/rfc7231#section-5.3.4 for more details.

Variables

This section is empty.

Functions

func NegotiateContentCharset

func NegotiateContentCharset(req *http.Request, offerCharsets ...string) string

NegotiateContentCharset returns the best acceptable charset offer to use in the response according to the Accept-Charset request's header. If the given offer list is empty or no offer is acceptable then, an empty string is returned.

See https://tools.ietf.org/html/rfc7231#section-5.3.3 for more details.

func NegotiateContentEncoding

func NegotiateContentEncoding(req *http.Request, offerEncodings ...string) string

NegotiateContentEncoding returns the best acceptable encoding offer to use in the response according to the Accept-Encoding request's header. If the given offer list is empty or no offer is acceptable then, an empty string is returned.

See https://tools.ietf.org/html/rfc7231#section-5.3.4 for more details.

func NegotiateContentLanguage

func NegotiateContentLanguage(req *http.Request, offerLanguages ...string) string

NegotiateContentLanguage returns the best acceptable language offer to use in the response according to the Accept-Language request's header. If the given offer list is empty or no offer is acceptable then, an empty string is returned.

See https://tools.ietf.org/html/rfc7231#section-5.3.5 for more details.

func NegotiateContentType

func NegotiateContentType(req *http.Request, offerMediaTypes ...string) string

NegotiateContentType returns the best acceptable media type offer to use in the response according to the Accept-Language request's header. If the given offer list is empty or no offer is acceptable then, an empty string is returned.

See https://tools.ietf.org/html/rfc7231#section-5.3.2 for more details.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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