Documentation ¶
Overview ¶
Package middleware provides some types and functions common among middleware.
Index ¶
- Variables
- func IndexFile(root http.FileSystem, fpath string, indexFiles []string) (string, bool)
- func NewResponseRecorder(w http.ResponseWriter) *responseRecorder
- func SetLastModifiedHeader(w http.ResponseWriter, modTime time.Time)
- func SplitCommandAndArgs(command string) (cmd string, args []string, err error)
- type Context
- func (c Context) Cookie(name string) string
- func (c Context) Header(name string) string
- func (c Context) Host() (string, error)
- func (c Context) IP() string
- func (c Context) Include(filename string) (string, error)
- func (c Context) Method() string
- func (c Context) Now(format string) string
- func (c Context) NowDate() time.Time
- func (c Context) PathMatches(pattern string) bool
- func (c Context) Port() (string, error)
- func (c Context) Replace(input, find, replacement string) string
- func (c Context) StripExt(path string) string
- func (c Context) StripHTML(s string) string
- func (c Context) Truncate(input string, length int) string
- func (c Context) URI() string
- type Handler
- type HandlerFunc
- type LogRoller
- type Middleware
- type Path
- type Replacer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var IndexPages = []string{
"index.html",
"index.htm",
"index.txt",
"default.html",
"default.htm",
"default.txt",
}
IndexPages is a list of pages that may be understood as the "index" files to directories.
Functions ¶
func IndexFile ¶ added in v0.6.0
IndexFile looks for a file in /root/fpath/indexFile for each string in indexFiles. If an index file is found, it returns the root-relative path to the file and true. If no index file is found, empty string and false is returned. fpath must end in a forward slash '/' otherwise no index files will be tried (directory paths must end in a forward slash according to HTTP).
All paths passed into and returned from this function use '/' as the path separator, just like URLs. IndexFle handles path manipulation internally for systems that use different path separators.
func NewResponseRecorder ¶
func NewResponseRecorder(w http.ResponseWriter) *responseRecorder
NewResponseRecorder makes and returns a new responseRecorder, which captures the HTTP Status code from the ResponseWriter and also the length of the response body written through it. Because a status is not set unless WriteHeader is called explicitly, this constructor initializes with a status code of 200 to cover the default case.
func SetLastModifiedHeader ¶ added in v0.8.0
func SetLastModifiedHeader(w http.ResponseWriter, modTime time.Time)
SetLastModifiedHeader checks if the provided modTime is valid and if it is sets it as a Last-Modified header to the ResponseWriter. If the modTime is in the future the current time is used instead.
func SplitCommandAndArgs ¶
SplitCommandAndArgs takes a command string and parses it shell-style into the command and its separate arguments.
Example ¶
var commandLine string var command string var args []string // just for the test - change GOOS and reset it at the end of the test runtimeGoos = "windows" defer func() { runtimeGoos = runtime.GOOS }() commandLine = `mkdir /P "C:\Program Files"` command, args, _ = SplitCommandAndArgs(commandLine) fmt.Printf("Windows: %s: %s [%s]\n", commandLine, command, strings.Join(args, ",")) // set GOOS to linux runtimeGoos = "linux" commandLine = `mkdir -p /path/with\ space` command, args, _ = SplitCommandAndArgs(commandLine) fmt.Printf("Linux: %s: %s [%s]\n", commandLine, command, strings.Join(args, ","))
Output: Windows: mkdir /P "C:\Program Files": mkdir [/P,C:\Program Files] Linux: mkdir -p /path/with\ space: mkdir [-p,/path/with space]
Types ¶
type Context ¶ added in v0.7.4
type Context struct { Root http.FileSystem Req *http.Request // This is used to access information about the URL. URL *url.URL }
Context is the context with which Caddy templates are executed.
func (Context) Host ¶ added in v0.7.4
Host returns the hostname portion of the Host header from the HTTP request.
func (Context) IP ¶ added in v0.7.4
IP gets the (remote) IP address of the client making the request.
func (Context) Include ¶ added in v0.7.4
Include returns the contents of filename relative to the site root
func (Context) NowDate ¶ added in v0.7.5
NowDate returns the current date/time that can be used in other time functions.
func (Context) PathMatches ¶ added in v0.7.4
PathMatches returns true if the path portion of the request URL matches pattern.
func (Context) Port ¶ added in v0.7.4
Port returns the port portion of the Host header if specified.
func (Context) Replace ¶ added in v0.7.4
Replace replaces instances of find in input with replacement.
func (Context) StripExt ¶ added in v0.7.6
StripExt returns the input string without the extension, which is the suffix starting with the final '.' character but not before the final path separator ('/') character. If there is no extension, the whole input is returned.
func (Context) StripHTML ¶ added in v0.7.6
StripHTML returns s without HTML tags. It is fairly naive but works with most valid HTML inputs.
type Handler ¶
Handler is like http.Handler except ServeHTTP returns a status code and an error. The status code is for the client's benefit; the error value is for the server's benefit. The status code will be sent to the client while the error value will be logged privately. Sometimes, an error status code (4xx or 5xx) may be returned with a nil error when there is no reason to log the error on the server.
If a HandlerFunc returns an error (status >= 400), it should NOT write to the response. This philosophy makes middleware.Handler different from http.Handler: error handling should happen at the application layer or in dedicated error-handling middleware only rather than with an "every middleware for itself" paradigm.
The application or error-handling middleware should incorporate logic to ensure that the client always gets a proper response according to the status code. For security reasons, it should probably not reveal the actual error message. (Instead it should be logged, for example.)
Handlers which do write to the response should return a status value < 400 as a signal that a response has been written. In other words, only error-handling middleware or the application will write to the response for a status code >= 400. When ANY handler writes to the response, it should return a status code < 400 to signal others to NOT write to the response again, which would be erroneous.
func FileServer ¶ added in v0.7.6
func FileServer(root http.FileSystem, hide []string) Handler
FileServer implements a production-ready file server and is the 'default' handler for all requests to Caddy. It simply loads and serves the URI requested. If Caddy is run without any extra configuration/directives, this is the only middleware handler that runs. It is not in its own folder like most other middleware handlers because it does not require a directive. It is a special case.
FileServer is adapted from the one in net/http by the Go authors. Significant modifications have been made.
Original license:
Copyright 2009 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
type HandlerFunc ¶
HandlerFunc is a convenience type like http.HandlerFunc, except ServeHTTP returns a status code and an error. See Handler documentation for more information.
func (HandlerFunc) ServeHTTP ¶
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
ServeHTTP implements the Handler interface.
type LogRoller ¶ added in v0.7.6
LogRoller implements a middleware that provides a rolling logger.
func (LogRoller) GetLogWriter ¶ added in v0.7.6
GetLogWriter returns an io.Writer that writes to a rolling logger.
type Middleware ¶
Middleware is the middle layer which represents the traditional idea of middleware: it chains one Handler to the next by being passed the next Handler in the chain.
type Replacer ¶ added in v0.6.0
Replacer is a type which can replace placeholder substrings in a string with actual values from a http.Request and responseRecorder. Always use NewReplacer to get one of these.
func NewReplacer ¶
NewReplacer makes a new replacer based on r and rr. Do not create a new replacer until r and rr have all the needed values, because this function copies those values into the replacer. rr may be nil if it is not available. emptyValue should be the string that is used in place of empty string (can still be empty string).
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package basicauth implements HTTP Basic Authentication.
|
Package basicauth implements HTTP Basic Authentication. |
Package browse provides middleware for listing files in a directory when directory path is requested instead of a specific file.
|
Package browse provides middleware for listing files in a directory when directory path is requested instead of a specific file. |
Package errors implements an HTTP error handling middleware.
|
Package errors implements an HTTP error handling middleware. |
Package extensions contains middleware for clean URLs.
|
Package extensions contains middleware for clean URLs. |
Package fastcgi has middleware that acts as a FastCGI client.
|
Package fastcgi has middleware that acts as a FastCGI client. |
Package gzip provides a simple middleware layer that performs gzip compression on the response.
|
Package gzip provides a simple middleware layer that performs gzip compression on the response. |
Package headers provides middleware that appends headers to requests based on a set of configuration rules that define which routes receive which headers.
|
Package headers provides middleware that appends headers to requests based on a set of configuration rules that define which routes receive which headers. |
Package inner provides a simple middleware that (a) prevents access to internal locations and (b) allows to return files from internal location by setting a special header, e.g.
|
Package inner provides a simple middleware that (a) prevents access to internal locations and (b) allows to return files from internal location by setting a special header, e.g. |
Package log implements basic but useful request (access) logging middleware.
|
Package log implements basic but useful request (access) logging middleware. |
Package markdown is middleware to render markdown files as HTML on-the-fly.
|
Package markdown is middleware to render markdown files as HTML on-the-fly. |
Package proxy is middleware that proxies requests.
|
Package proxy is middleware that proxies requests. |
Package redirect is middleware for redirecting certain requests to other locations.
|
Package redirect is middleware for redirecting certain requests to other locations. |
Package rewrite is middleware for rewriting requests internally to a different path.
|
Package rewrite is middleware for rewriting requests internally to a different path. |
Package templates implements template execution for files to be dynamically rendered for the client.
|
Package templates implements template execution for files to be dynamically rendered for the client. |
Package websocket implements a WebSocket server by executing a command and piping its input and output through the WebSocket connection.
|
Package websocket implements a WebSocket server by executing a command and piping its input and output through the WebSocket connection. |