http

package
v0.0.0-...-77ed03d Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2017 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package http provides a net/http.Handler implementation that wraps a imageserver.Server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewParamsHashETagFunc

func NewParamsHashETagFunc(newHashFunc func() hash.Hash) func(params imageserver.Params) string

NewParamsHashETagFunc returns a function that hashes the params and returns an ETag value.

It is intended to be used in Handler.ETagFunc.

func ParseQueryBool

func ParseQueryBool(param string, req *http.Request, params imageserver.Params) error

ParseQueryBool takes the param from the HTTP URL query, parse it as an bool and add it to the Params.

func ParseQueryFloat

func ParseQueryFloat(param string, req *http.Request, params imageserver.Params) error

ParseQueryFloat takes the param from the HTTP URL query, parse it as a float64 and add it to the Params.

func ParseQueryInt

func ParseQueryInt(param string, req *http.Request, params imageserver.Params) error

ParseQueryInt takes the param from the HTTP URL query, parse it as an int and add it to the Params.

func ParseQueryInt64

func ParseQueryInt64(param string, req *http.Request, params imageserver.Params) error

ParseQueryInt64 takes the param from the HTTP URL query, parse it as an int64 and add it to the Params.

func ParseQueryString

func ParseQueryString(param string, req *http.Request, params imageserver.Params)

ParseQueryString takes the param from the HTTP URL query and add it to the Params.

Types

type CacheControlPublicHandler

type CacheControlPublicHandler struct {
	http.Handler
}

CacheControlPublicHandler is a net/http.Handler implementation that sets the "Cache-Control" header to "public".

It only sets the header if the status code is StatusOK/204 or StatusNotModified/304.

func (*CacheControlPublicHandler) ServeHTTP

func (h *CacheControlPublicHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implements net/http.Handler

type Error

type Error struct {
	Code int
	Text string
}

Error is a HTTP error.

It is supported by Handler.

func NewErrorDefaultText

func NewErrorDefaultText(code int) *Error

NewErrorDefaultText creates an Error with the default message associated with the code.

func (*Error) Error

func (err *Error) Error() string

type ExpiresHandler

type ExpiresHandler struct {
	http.Handler
	Expires time.Duration
}

ExpiresHandler is a net/http.Handler implementation that sets the "Expires" header.

It only sets the header if the status code is StatusOK/204 or StatusNotModified/304.

func (*ExpiresHandler) ServeHTTP

func (eh *ExpiresHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implements net/http.Handler.

type Handler

type Handler struct {
	// Parser parses the HTTP request and fills the Params.
	Parser Parser

	// Server handles the image request.
	Server imageserver.Server

	// ETagFunc is an optional function that returns the ETag value for the given Params.
	// See https://en.wikipedia.org/wiki/HTTP_ETag .
	// The returned value must not be enclosed in quotes (they are added automatically).
	ETagFunc func(params imageserver.Params) string

	// ErrorFunc is an optional function that is called if there is an internal error.
	ErrorFunc func(err error, req *http.Request)
}

Handler is a net/http.Handler implementation that wraps a imageserver.Server.

Supported methods are: GET and HEAD. Other method will return a StatusMethodNotAllowed/405 response.

It supports ETag/If-None-Match headers and returns a StatusNotModified/304 response accordingly. But it doesn't check if the Image really exists (the Server is not called).

Steps:

  • Parse the HTTP request, and fill the Params.
  • If the given If-None-Match header matches the ETag, return a StatusNotModified/304 response.
  • Call the Server and get the Image.
  • Return a StatusOK/200 response containing the Image.

Errors (returned by Parser or Server):

  • *imageserver/http.Error will return a response with the given status code and message.
  • *imageserver.ParamError will return a StatusBadRequest/400 response, with a message including the resolved HTTP param.
  • *imageserver.ImageError will return a StatusBadRequest/400 response, with the given message.
  • Other error will return a StatusInternalServerError/500 response, and ErrorFunc will be called.

Returned headers:

  • Content-Type is set for StatusOK/200 response, and contains "image/{Image.Format}".
  • Content-Length is set for StatusOK/200 response, and contains the Image size.
  • ETag is set for StatusOK/200 and StatusNotModified/304 response, and contains the ETag value.

func (*Handler) ServeHTTP

func (handler *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implements net/http.Handler.

type ListParser

type ListParser []Parser

ListParser is a Parser implementation that wraps a list of Parser.

func (ListParser) Parse

func (lp ListParser) Parse(req *http.Request, params imageserver.Params) error

Parse implements Parser.

It iterates through all sub parsers. An error interrupts the iteration.

func (ListParser) Resolve

func (lp ListParser) Resolve(param string) string

Resolve implements Parser.

It iterates through sub parsers, and return the first non-empty string.

type Parser

type Parser interface {
	// Parse parses a *net/http.Request and fill Params.
	Parse(*http.Request, imageserver.Params) error

	// Resolve resolves an internal param to a HTTP param.
	// It returns the resolved HTTP param, or an empty string if it can not be resolved.
	// It is used by Handler to generate errors messages.
	Resolve(param string) (httpParam string)
}

Parser represents a *net/http.Request parser.

type SourceParser

type SourceParser struct{}

SourceParser is a Parser implementation that takes the "source" param from the HTTP URL query.

func (*SourceParser) Parse

func (parser *SourceParser) Parse(req *http.Request, params imageserver.Params) error

Parse implements Parser.

func (*SourceParser) Resolve

func (parser *SourceParser) Resolve(param string) string

Resolve implements Parser.

type SourcePathParser

type SourcePathParser struct{}

SourcePathParser is a Parser implementation that takes the "source" param from the HTTP URL path.

func (*SourcePathParser) Parse

func (parser *SourcePathParser) Parse(req *http.Request, params imageserver.Params) error

Parse implements Parser.

func (*SourcePathParser) Resolve

func (parser *SourcePathParser) Resolve(param string) string

Resolve implements Parser.

type SourcePrefixParser

type SourcePrefixParser struct {
	Parser
	Prefix string
}

SourcePrefixParser is a Parser implementation that adds a prefix to the "source" param.

func (*SourcePrefixParser) Parse

func (ps *SourcePrefixParser) Parse(req *http.Request, params imageserver.Params) error

Parse implements Parser.

type SourceTransformParser

type SourceTransformParser struct {
	Parser
	Transform func(source string) string
}

SourceTransformParser is a Parser implementation that transforms the "source" param.

func (*SourceTransformParser) Parse

func (ps *SourceTransformParser) Parse(req *http.Request, params imageserver.Params) error

Parse implements Parser.

Directories

Path Synopsis
Package crop provides a imageserver/http.Parser implementation for imageserver/image/crop.Processor.
Package crop provides a imageserver/http.Parser implementation for imageserver/image/crop.Processor.
Package gamma provides a imageserver/http.Parser implementation for imageserver/image/gamma.CorrectionProcessor.
Package gamma provides a imageserver/http.Parser implementation for imageserver/image/gamma.CorrectionProcessor.
Package gift provides imageserver/http.Parser implementations for imageserver/image/gift.
Package gift provides imageserver/http.Parser implementations for imageserver/image/gift.
Package graphicsmagick provides a imageserver/http.Parser implementation for imageserver/graphicsmagick.Handler.
Package graphicsmagick provides a imageserver/http.Parser implementation for imageserver/graphicsmagick.Handler.
Package image provides imageserver/http.Parser implementations for imageserver/image.
Package image provides imageserver/http.Parser implementations for imageserver/image.
Package nfntresize provides a imageserver/http.Parser implementation for imageserver/image/nfntresize.Processor.
Package nfntresize provides a imageserver/http.Parser implementation for imageserver/image/nfntresize.Processor.

Jump to

Keyboard shortcuts

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