Documentation ¶
Index ¶
- Variables
- func CleanPath(p string) string
- func Error(w http.ResponseWriter, error string, code int)
- func NotFound(w http.ResponseWriter, r *http.Request)
- type Handler
- type HandlerFunc
- type MuxEntry
- type ServeMux
- func (mux *ServeMux) CompilePatternMatcher()
- func (mux *ServeMux) Handle(pattern string, handler Handler) (my *addCriteriaType)
- func (mux *ServeMux) HandleErrors(status int, h Handler)
- func (mux *ServeMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) *addCriteriaType
- func (mux *ServeMux) Handler(r *http.Request) (h Handler, pattern string, err error)
- func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mux *ServeMux) String() (s string)
Constants ¶
This section is empty.
Variables ¶
var (
ErrNotFound = errors.New("404 Not Found")
)
Functions ¶
Types ¶
type Handler ¶
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request)
}
Objects implementing the Handler interface can be registered to serve a particular path or subtree in the HTTP server.
ServeHTTP should write reply headers and data to the http.ResponseWriter and then return. Returning signals that the request is finished and that the HTTP server can move on to the next request on the connection.
If ServeHTTP panics, the server (the caller of ServeHTTP) assumes that the effect of the panic was isolated to the active request. It recovers the panic, logs a stack trace to the server error log, and hangs up the connection.
func NotFoundHandler ¶
func NotFoundHandler() Handler
NotFoundHandler returns a simple request handler that replies to each request with a “404 page not found” reply.
type HandlerFunc ¶
type HandlerFunc func(http.ResponseWriter, *http.Request)
The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
func (HandlerFunc) ServeHTTP ¶
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP calls f(w, r).
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.
Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash). Longer patterns take precedence over shorter ones, so that if there are handlers registered for both "/images/" and "/images/thumbnails/", the latter handler will be called for paths beginning "/images/thumbnails/" and the former will receive requests for any other paths in the "/images/" subtree.
Note that since a pattern ending in a slash names a rooted subtree, the pattern "/" matches all paths not matched by other registered patterns, not just the URL with Path == "/".
Patterns may optionally begin with a host name, restricting matches to URLs on that host only. Host-specific patterns take precedence over general patterns, so that a handler might register for the two patterns "/codesearch" and "codesearch.google.com/" without also taking over requests for "http://www.google.com/".
ServeMux also takes care of sanitizing the URL request path, redirecting any request containing . or .. elements to an equivalent .- and ..-free URL.
func (*ServeMux) CompilePatternMatcher ¶
func (mux *ServeMux) CompilePatternMatcher()
This should be called when you are done adding or changing the routes. This will process the routs into the necessary data for running matching. It gets called automatically if you forget - however that means that any errors in processing will not show up until the first request is made. Better to just call it yourself curing initialization.
func (*ServeMux) Handle ¶
Handle registers the handler for the given pattern. If a handler already exists for pattern, Handle panics.
func (*ServeMux) HandleErrors ¶
www.WriteHeader(http.StatusForbidden) exactMatcher.HandleErrors ( errorHandlerFunc)
func (*ServeMux) HandleFunc ¶
func (mux *ServeMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) *addCriteriaType
HandleFunc registers the handler function for the given pattern.
func (*ServeMux) Handler ¶
Handler returns the handler to use for the given request, consulting r.Method, r.Host, and r.URL.Path. It always returns a non-nil handler. If the path is not in its canonical form, the handler will be an internally-generated handler that redirects to the canonical path.
Handler also returns the registered pattern that matches the request or, in the case of internally-generated redirects, the pattern that will match after following the redirect.
If there is no registered handler that applies to the request, Handler returns a “page not found” handler and an empty pattern.