Documentation ¶
Overview ¶
Package gofast implements the FastCGI protocol. Currently only the responder role is supported. The protocol is defined at http://www.mit.edu/~yandros/doc/specs/fcgi-spec.html
Index ¶
- type Authorizer
- type Client
- type ClientFactory
- type ClientFunc
- type ClientPool
- type ConnFactory
- type FileSystemRouter
- type Handler
- type Middleware
- func Chain(middlewares ...Middleware) Middleware
- func MapEndpoint(endpointFile string) Middleware
- func MapFilterRequest(fs http.FileSystem) Middleware
- func MapFilterRequestTemp(fs embed.FS) Middleware
- func NewAuthPrepare() Middleware
- func NewFileEndpoint(endpointFile string) Middleware
- func NewFilterFS(fs http.FileSystem) Middleware
- func NewFilterFSTemp(fs embed.FS) Middleware
- func NewFilterLocalFS(root string) Middleware
- func NewPHPFS(root string) Middleware
- type PoolClient
- type Request
- type ResponsePipe
- type Role
- type SessionHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Authorizer ¶
type Authorizer struct {
// contains filtered or unexported fields
}
Authorizer guard a given http.Handler
Since this is implemented as a generic http.Handler middleware, you may use this with other non-gofast library components as long as they implements the http.Handler interface.
func NewAuthorizer ¶
func NewAuthorizer(clientFactory ClientFactory, sessionHandler SessionHandler) *Authorizer
NewAuthorizer creates an authorizer
type Client ¶
type Client interface { // Do a proper FastCGI request. // Returns the response streams (stdout and stderr) // and the request validation error. // // Note: protocol error will be written to the stderr // stream in the ResponsePipe. Do(req *Request) (resp *ResponsePipe, err error) // Close the underlying connection Close() error }
Client is a client interface of FastCGI application process through given connection (net.Conn)
type ClientFactory ¶
ClientFactory creates new FPM client with proper connection to the FPM application.
func SimpleClientFactory ¶
func SimpleClientFactory(connFactory ConnFactory) ClientFactory
SimpleClientFactory returns a ClientFactory implementation with the given ConnFactory.
type ClientFunc ¶
type ClientFunc func(req *Request) (resp *ResponsePipe, err error)
ClientFunc is a function wrapper of a Client interface shortcut implementation. Mainly for testing and development purpose.
func (ClientFunc) Do ¶
func (c ClientFunc) Do(req *Request) (resp *ResponsePipe, err error)
Do implements Client.Do
type ClientPool ¶
type ClientPool struct {
// contains filtered or unexported fields
}
ClientPool pools client created from a given ClientFactory.
func NewClientPool ¶
func NewClientPool( clientFactory ClientFactory, scale uint, expires time.Duration, ) *ClientPool
NewClientPool creates a *ClientPool from the given ClientFactory and pool it to scale with expiration.
func (*ClientPool) CreateClient ¶
func (p *ClientPool) CreateClient() (c Client, err error)
CreateClient implements ClientFactory
type ConnFactory ¶
ConnFactory creates new network connections to the FPM application
func SimpleConnFactory ¶
func SimpleConnFactory(network, address string) ConnFactory
SimpleConnFactory creates the simplest ConnFactory implementation.
type FileSystemRouter ¶
type FileSystemRouter struct { // DocRoot stores the ordinary Apache DocumentRoot parameter DocRoot string // Exts stores accepted extensions Exts []string // DirIndex stores ordinary Apache DirectoryIndex parameter // for to identify file to show in directory DirIndex []string }
FileSystemRouter helps to produce Middleware implementation for mapping path related fastcgi parameters. See method Router for usage.
func (*FileSystemRouter) Router ¶
func (fs *FileSystemRouter) Router() Middleware
Router returns a Middleware that prepare session parameters that are path related. With information provided in the FileSystemRouter, it will route request to script files which path matches the http request path.
i.e. classic PHP hosting environment like Apache + mod_php
Parameters included:
PATH_INFO PATH_TRANSLATED SCRIPT_NAME SCRIPT_FILENAME DOCUMENT_URI DOCUMENT_ROOT
type Handler ¶
Handler is implements http.Handler and provide logger changing method.
func NewHandler ¶
func NewHandler(sessionHandler SessionHandler, clientFactory ClientFactory) Handler
NewHandler returns the default Handler implementation. This default Handler act as the "web server" component in fastcgi specification, which connects fastcgi "application" through the network/address and passthrough I/O as specified.
type Middleware ¶
type Middleware func(SessionHandler) SessionHandler
Middleware transform a SessionHandler as another SessionHandler. The middlewares provided by this library helps to map fastcgi parameters according to the need of different application.
You may also implement your own Middleware can be provided to modify the *Request, add extra business logic in between, rewrite the response stream from *ResponsePipe. or better handle errors
Ordinary fastcgi parameters on nginx (for PHP at least):
fastcgi_split_path_info ^(.+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;
func Chain ¶
func Chain(middlewares ...Middleware) Middleware
Chain chains middlewares into a single middleware.
The middlewares will be chained from outer to inner. The first middleware will be the first to handle Client and Request. It is also the last to handle ResponsePipe and error.
func MapEndpoint ¶
func MapEndpoint(endpointFile string) Middleware
MapEndpoint returns a Middleware implementation that prepare session for application with only 1 file as endpoint (i.e. it will handle script routing on its own). Suitable for web.py based application.
Parameters included:
PATH_INFO PATH_TRANSLATED SCRIPT_NAME SCRIPT_FILENAME DOCUMENT_URI DOCUMENT_ROOT
func MapFilterRequest ¶
func MapFilterRequest(fs http.FileSystem) Middleware
MapFilterRequest changes the request role to RoleFilter and add the Data stream from the given file system, if file exists. Also set the required params to request.
If the file do not exists or cannot be opened, the middleware will return empty response pipe and the error.
TODO: provide way to inject authorization check
func MapFilterRequestTemp ¶ added in v0.6.1
func MapFilterRequestTemp(fs embed.FS) Middleware
MapFilterRequestTemp changes the request role to RoleFilter and add the Data stream from the given file system, if file exists. Also set the required params to request.
If the file do not exists or cannot be opened, the middleware will return empty response pipe and the error.
TODO: merge this functionality with the above.
func NewAuthPrepare ¶
func NewAuthPrepare() Middleware
NewAuthPrepare chains BasicParamsMap, MapHeader, FilterAuthReqParams to implement Middleware that prepares a authorizer request
func NewFileEndpoint ¶
func NewFileEndpoint(endpointFile string) Middleware
NewFileEndpoint chains BasicParamsMap, MapHeader and MapEndpoint to implement Middleware that prepares an ordinary web.py hosting session environment
func NewFilterFS ¶
func NewFilterFS(fs http.FileSystem) Middleware
NewFilterFS chains BasicParamsMap, MapHeader and MapFilterRequest to implement Middleware that prepares a fastcgi Filter session environment.
func NewFilterFSTemp ¶ added in v0.6.1
func NewFilterFSTemp(fs embed.FS) Middleware
NewFilterFS chains BasicParamsMap, MapHeader and MapFilterRequest to implement Middleware that prepares a fastcgi Filter session environment.
func NewFilterLocalFS ¶
func NewFilterLocalFS(root string) Middleware
NewFilterLocalFS is a shortcut to use NewFilterFS with a http.FileSystem created for the given local folder.
func NewPHPFS ¶
func NewPHPFS(root string) Middleware
NewPHPFS chains BasicParamsMap, MapHeader and FileSystemRouter to implement Middleware that prepares an ordinary PHP hosting session environment.
type PoolClient ¶
PoolClient wraps a client and alter the Close method for pool return / destroy.
func (*PoolClient) Close ¶
func (pc *PoolClient) Close() error
Close close the inner client only if it is expired. Otherwise it will return itself to the pool.
func (*PoolClient) Expired ¶
func (pc *PoolClient) Expired() bool
Expired check if the client expired
type Request ¶
type Request struct { Raw *http.Request Role Role Params map[string]string Stdin io.ReadCloser Data io.ReadCloser KeepConn bool }
Request hold information of a standard FastCGI request
func NewAuthRequest ¶
NewAuthRequest returns a new *http.Request and a *Request with the body buffered into new NopReader.
func NewRequest ¶
NewRequest returns a standard FastCGI request with a unique request ID allocted by the client
type ResponsePipe ¶
type ResponsePipe struct {
// contains filtered or unexported fields
}
ResponsePipe contains readers and writers that handles all FastCGI output streams
func BasicSession ¶
func BasicSession(client Client, req *Request) (*ResponsePipe, error)
BasicSession is the default SessionHandler used in the default Handler
func NewResponsePipe ¶
func NewResponsePipe() (p *ResponsePipe)
NewResponsePipe returns an initialized new ResponsePipe struct
func (*ResponsePipe) WriteTo ¶
func (pipes *ResponsePipe) WriteTo(rw http.ResponseWriter, ew io.Writer) (err error)
WriteTo writes the given output into http.ResponseWriter
type SessionHandler ¶
type SessionHandler func(client Client, req *Request) (resp *ResponsePipe, err error)
SessionHandler handles the gofast *Reqeust with the provided given Client. The Client should properly handle the transport to the fastcgi application. Should do proper routing or other parameter mapping here.
func BasicParamsMap ¶
func BasicParamsMap(inner SessionHandler) SessionHandler
BasicParamsMap implements Middleware. It maps basic parameters to the req.Params.
Parameters included:
CONTENT_TYPE CONTENT_LENGTH HTTPS GATEWAY_INTERFACE REMOTE_ADDR REMOTE_PORT SERVER_PORT SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE REDIRECT_STATUS REQUEST_METHOD REQUEST_SCHEME REQUEST_URI QUERY_STRING
func FilterAuthReqParams ¶
func FilterAuthReqParams(inner SessionHandler) SessionHandler
FilterAuthReqParams filter out FCGI_PARAMS key-value that is explicitly forbidden to passed on in factcgi specification, include:
CONTENT_LENGTH; PATH_INFO; PATH_TRANSLATED; and SCRIPT_NAME
func MapHeader ¶
func MapHeader(inner SessionHandler) SessionHandler
MapHeader implement Middleware to map header field HTTP_*
It is a convention to map header field SomeRandomField to HTTP_SOME_RANDOM_FIELD. For example, if a header field "X-Hello-World" is in the header, it will be mapped as "HTTP_X_HELLO_WORLD" in the fastcgi parameter field.
Note: HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH cannot be overridden.
func MapRemoteHost ¶
func MapRemoteHost(inner SessionHandler) SessionHandler
MapRemoteHost does reverse DNS lookup on the r.RemoteAddr IP address.