httputil

package module
v0.0.0-...-1abeeca Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2013 License: MIT Imports: 9 Imported by: 0

README

httputil is a go library, which provides HTTP utility functions as well as its
own http.Handler to complement the "net/http" and "net/http/httputil" packages
found in the standard library.

For more information, see `go doc`.

Documentation

Overview

Package httputil provides HTTP utility functions as well as its own http.Handler to complement the "net/http" and "net/http/httputil" packages found in the standard library.

Index

Constants

View Source
const CommonLogFmt = `%s - - [%s] "%s %s %s" %d %d "%s" "%s" %d`

Common Log Format See <http://httpd.apache.org/docs/1.3/logs.html#common>

Variables

View Source
var (
	LogFmt = CommonLogFmt // Log format to use

)

Functions

func Error

func Error(w http.ResponseWriter, err string, code int)

Error wraps http.Error by writing appropriate error messages depending on the handler's Content-Type.

For example, with the Content-Type set to application/json and the error string "oops!", the response body would be `{"error":"oops!"}`

func Notify

func Notify(ch chan *Access)

Notify sends all HTTP access events to the specified channel.

Types

type Access

type Access struct {
	RemoteAddr    string
	Time          time.Time
	Method        string
	RequestURI    string
	Proto         string
	StatusCode    int
	ContentLength int64
	Referer       string
	UserAgent     string
	Duration      time.Duration
	Request       *http.Request
}

Access represents a single HTTP access event (an answered request).

func (*Access) String

func (a *Access) String() string

String returns the string representation of an *Access according to LogFmt.

type Handler

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

Handler implements http.Handler.

func NewHandler

func NewHandler(inner http.Handler, ctype string) *Handler

NewHandler returns a new Handler which wraps the given http.Handler.

func (*Handler) Accept

func (h *Handler) Accept(mime string)

Accept instructs the handler to only fulfill requests which have an Accept header of the given MIME type. Otherwise, a 406 Not Acceptable is returned.

func (*Handler) Allow

func (h *Handler) Allow(methods ...string)

Allow instructs the handler to only fulfill requests which use one of the given HTTP methods. Otherwise, a 405 Method Not Allowed is returned.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves an HTTP request by calling the underlying http.Handler. Request duration and status are logged.

type ResponseWriter

type ResponseWriter struct {
	StatusCode  int
	ContentType string
	// contains filtered or unexported fields
}

ResponseWriter wraps an http.ResponseWriter with additional capabilities.

func (*ResponseWriter) HasStatus

func (rw *ResponseWriter) HasStatus() bool

HasStatus returns whether or not the ResponseWriter has a status.

func (*ResponseWriter) Header

func (rw *ResponseWriter) Header() http.Header

Write wraps (*http.ResponseWriter).Header.

func (*ResponseWriter) Write

func (rw *ResponseWriter) Write(b []byte) (int, error)

Write wraps (*http.ResponseWriter).Write.

func (*ResponseWriter) WriteHeader

func (rw *ResponseWriter) WriteHeader(status int)

WriteHeader wraps (*http.ResponseWriter).WriteHeader and records the given status.

Jump to

Keyboard shortcuts

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