Documentation
¶
Overview ¶
Package server provides classes and functions for the HTTP server side of the middleware.
In particular, the package handles client sessions by producing credentials for logged user and by verifying these credentials for each request.
It is a wrapper around net/http.
Index ¶
- Variables
- func AddSessionIdToRequest(req *http.Request, sessionId string)
- func BaseURL() string
- func Compress(h http.Handler) http.Handler
- func CompressHandlerLevel(h http.Handler, level int) http.Handler
- func Handle(pattern string, handler Handler, interceptors ...Interceptor)
- func HandleFunc(pattern string, fct HandleFunction, interceptors ...Interceptor)
- func HostOnly(address string) string
- func MakeSessionId() (string, error)
- func NewSession(st gs.Store, opts *gs.Options, sessionId string, user User) (session *gs.Session)
- func SessionKeys() [][]byte
- func Start() error
- func URLPortWithDefault(url *url.URL) (port string)
- type HandleFunction
- type Handler
- type HandlerFunc
- type HandlerWrapper
- type HttpError
- type Interceptor
- type Request
- type Response
- type User
Constants ¶
This section is empty.
Variables ¶
var Ok bool
Whether the package is usable. May be false if there is no configuration for the package.
var SessionOptions gs.Options
SessionOptions reflects the configured options for sessions. Modifying it has no effect on the sessions generated by the package.
Functions ¶
func AddSessionIdToRequest ¶ added in v0.0.4
AddSessionIdToRequest adds a session id to an http.Request. This function is meant to be used by HTTP clients and tests.
func Compress ¶ added in v0.0.4
Compress gzip compresses HTTP responses for clients that support it via the 'Accept-Encoding' header.
Compressing TLS traffic may leak the page contents to an attacker if the page contains user input: http://security.stackexchange.com/a/102015/12208
func CompressHandlerLevel ¶ added in v0.0.4
CompressHandlerLevel gzip compresses HTTP responses with specified compression level for clients that support it via the 'Accept-Encoding' header.
The compression level should be gzip.DefaultCompression, gzip.NoCompression, or any integer value between gzip.BestSpeed and gzip.BestCompression inclusive. gzip.DefaultCompression is used in case of invalid compression level.
func Handle ¶
func Handle(pattern string, handler Handler, interceptors ...Interceptor)
Handle registers the handler for the given pattern. See http.ServeMux for a description of the pattern format.
func HandleFunc ¶
func HandleFunc(pattern string, fct HandleFunction, interceptors ...Interceptor)
Handle registers the handler function for the given pattern. See http.ServeMux for a description of the pattern format.
func MakeSessionId ¶
MakeSessionId create a new session id.
This is a low level function, made available for tests.
func NewSession ¶
NewSession creates a new session for the given user.
This is a low level function, made available for tests. Use SendLoginAccepted instead.
func SessionKeys ¶
func SessionKeys() [][]byte
SessionKeys retrieves the session keys for test purpose.
This is a low level function, made available for tests.
func URLPortWithDefault ¶ added in v0.0.4
URLPortWithDefault returns the port part of url.Host, without the leading colon. If url does not have a port, a default value is guessed from url.Scheme.
Types ¶
type HandleFunction ¶
type Handler ¶
A Handler responds to an HTTP request.
The Handle method should read the Request then use Response's methods to send the response. The Context must be checked for completion and transmitted to all called functions.
As a convenience, if the Handle method panics with an HttpError then that error is send as response.
type HandlerWrapper ¶
type HandlerWrapper struct {
// contains filtered or unexported fields
}
HandlerWrapper wraps a Handler into an http.Handler.
func NewHandlerWrapper ¶
func NewHandlerWrapper(pattern string, handler Handler) HandlerWrapper
func (HandlerWrapper) ServeHTTP ¶
func (self HandlerWrapper) ServeHTTP(wr http.ResponseWriter, original *http.Request)
ServeHTTP implements http.Handler.
type HttpError ¶
type HttpError struct {
// HTTP status code for the error.
Code int
// contains filtered or unexported fields
}
A HttpError is an error that can be send as an HTTP response.
func NewHttpError ¶
NewHttpError constructs a new HttpError.
The code is to be sent as the HTTP code of the response. It should be a constant from the net/http package. The message (msg) is to be sent as body of the HTTP response. This is the public description of the error. The detail is the private description of the error, to be displayed in the logs.
func NewInternalHttpError ¶
NewInternalHttpError wraps another error into an InternalServerError HttpError. This function is particularly usefull to panic inside an Handler, see Handler.
type Interceptor ¶ added in v0.0.4
type Interceptor = alice.Constructor
type Request ¶
type Request struct {
// User is the session user.
// It is nil if no user is successfully logged in the current session.
User *User
// SessionError is the error raised when checking the session informations send by the client.
// It is nil either if the client did not send any session information (in which case User is nil
// too) or if the session has been successfully checked (in which case Use is not nil).
SessionError error
// FullPath contains all path elements of the request made by the client.
FullPath []string
// RemainingPath contains the path elements after the pattern corresponding to the current
// Handler.
RemainingPath []string
// contains filtered or unexported fields
}
A request represents an HTTP request to be handled by the server.
func NewRequest ¶ added in v0.0.4
NewRequest is the only constructor for Request. It is a low-level function that should not be used outside the package and tests.
func (*Request) CheckPOST ¶ added in v0.0.4
CheckPOST ensures that the request is particularly safe.
This method returns nil only if the method is POST and there is an Origin header with the correct host.
func (*Request) UnmarshalJSONBody ¶
UnmarshalJSONBody retrieves the body of the request as a JSON object. See json.Unmarshal for details of the unmarshalling process.
type Response ¶
type Response interface {
// SendJSON sends a JSON as response.
// On success statuc code is http.StatusOK.
SendJSON(ctx context.Context, data interface{})
// SendError sends an error as response.
// If the error is an HttpError, its code and msg are used in the HTPP response.
// Also log the error.
SendError(context.Context, error)
// SendLoginAccepted create new credential for the user and send it as response.
SendLoginAccepted(context.Context, User, *Request)
}
Directories
¶
| Path | Synopsis |
|---|---|
|
Package logger provides logging facilities with incremental prefixes stored in contexts.
|
Package logger provides logging facilities with incremental prefixes stored in contexts. |
|
Package servertest provides methods and types to test server.Handler implementations.
|
Package servertest provides methods and types to test server.Handler implementations. |