2016-01-29 09:46:11 +02:00
|
|
|
package engine
|
|
|
|
|
2016-02-05 00:40:08 +02:00
|
|
|
import (
|
|
|
|
"io"
|
2016-03-13 01:06:52 +02:00
|
|
|
"mime/multipart"
|
2016-02-05 00:40:08 +02:00
|
|
|
"time"
|
2016-02-10 03:16:46 +02:00
|
|
|
|
2016-03-17 19:21:16 +02:00
|
|
|
"net"
|
2016-03-18 17:47:03 +02:00
|
|
|
|
|
|
|
"github.com/labstack/gommon/log"
|
2016-02-05 00:40:08 +02:00
|
|
|
)
|
2016-01-29 09:46:11 +02:00
|
|
|
|
|
|
|
type (
|
2016-03-18 06:49:06 +02:00
|
|
|
// Server defines the interface for HTTP server.
|
|
|
|
Server interface {
|
2016-03-17 06:24:04 +02:00
|
|
|
// SetHandler sets the handler for the HTTP server.
|
2016-03-08 18:14:25 +02:00
|
|
|
SetHandler(Handler)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// SetLogger sets the logger for the HTTP server.
|
2016-03-06 19:52:32 +02:00
|
|
|
SetLogger(*log.Logger)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Start starts the HTTP server.
|
2016-03-18 21:13:37 +02:00
|
|
|
Start() error
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
// Request defines the interface for HTTP request.
|
2016-01-29 09:46:11 +02:00
|
|
|
Request interface {
|
2016-03-17 05:15:38 +02:00
|
|
|
// TLS returns true if HTTP connection is TLS otherwise false.
|
2016-02-23 07:13:08 +02:00
|
|
|
TLS() bool
|
2016-03-15 04:58:46 +02:00
|
|
|
|
2016-03-17 05:15:38 +02:00
|
|
|
// Scheme returns the HTTP protocol scheme, `http` or `https`.
|
2016-02-23 07:26:36 +02:00
|
|
|
Scheme() string
|
2016-03-17 05:15:38 +02:00
|
|
|
|
2016-03-17 06:24:04 +02:00
|
|
|
// Host returns HTTP request host. Per RFC 2616, this is either the value of
|
|
|
|
// the `Host` header or the host name given in the URL itself.
|
2016-02-09 18:12:37 +02:00
|
|
|
Host() string
|
2016-03-17 05:15:38 +02:00
|
|
|
|
|
|
|
// URI returns the unmodified `Request-URI` sent by the client.
|
2016-02-09 02:48:03 +02:00
|
|
|
URI() string
|
2016-03-17 05:15:38 +02:00
|
|
|
|
|
|
|
// URL returns `engine.URL`.
|
2016-02-09 02:48:03 +02:00
|
|
|
URL() URL
|
2016-03-17 05:15:38 +02:00
|
|
|
|
|
|
|
// Header returns `engine.Header`.
|
2016-01-29 09:46:11 +02:00
|
|
|
Header() Header
|
2016-03-17 05:15:38 +02:00
|
|
|
|
2016-01-29 09:46:11 +02:00
|
|
|
// Proto() string
|
|
|
|
// ProtoMajor() int
|
|
|
|
// ProtoMinor() int
|
2016-03-17 05:15:38 +02:00
|
|
|
|
|
|
|
// UserAgent returns the client's `User-Agent`.
|
2016-03-15 04:58:46 +02:00
|
|
|
UserAgent() string
|
2016-03-17 05:15:38 +02:00
|
|
|
|
|
|
|
// RemoteAddress returns the client's network address.
|
2016-01-29 09:46:11 +02:00
|
|
|
RemoteAddress() string
|
2016-03-17 05:15:38 +02:00
|
|
|
|
2016-03-17 06:24:04 +02:00
|
|
|
// Method returns the request's HTTP method.
|
2016-01-29 09:46:11 +02:00
|
|
|
Method() string
|
2016-03-17 05:15:38 +02:00
|
|
|
|
2016-03-17 06:24:04 +02:00
|
|
|
// SetMethod sets the HTTP method of the request.
|
2016-03-15 07:45:29 +02:00
|
|
|
SetMethod(string)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Body returns request's body.
|
|
|
|
Body() io.Reader
|
|
|
|
|
|
|
|
// FormValue returns form field value for the provided name.
|
2016-01-29 09:46:11 +02:00
|
|
|
FormValue(string) string
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// FormFile returns form file for the provided name.
|
2016-03-13 01:06:52 +02:00
|
|
|
FormFile(string) (*multipart.FileHeader, error)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// MultipartForm returns multipart form.
|
2016-03-13 01:06:52 +02:00
|
|
|
MultipartForm() (*multipart.Form, error)
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
// Response defines the interface for HTTP response.
|
2016-01-29 09:46:11 +02:00
|
|
|
Response interface {
|
2016-03-17 06:24:04 +02:00
|
|
|
// Header returns `engine.Header`
|
2016-01-29 09:46:11 +02:00
|
|
|
Header() Header
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// WriteHeader sends an HTTP response header with status code.
|
2016-01-29 09:46:11 +02:00
|
|
|
WriteHeader(int)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Write writes the data to the connection as part of an HTTP reply.
|
2016-01-29 09:46:11 +02:00
|
|
|
Write(b []byte) (int, error)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Status returns the HTTP response status.
|
2016-01-29 09:46:11 +02:00
|
|
|
Status() int
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Size returns the number of bytes written to HTTP response.
|
2016-01-29 09:46:11 +02:00
|
|
|
Size() int64
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Committed returns true if HTTP response header is written, otherwise false.
|
2016-01-29 09:46:11 +02:00
|
|
|
Committed() bool
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Write returns the HTTP response writer.
|
2016-03-11 02:35:20 +02:00
|
|
|
Writer() io.Writer
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// SetWriter sets the HTTP response writer.
|
2016-03-15 04:58:46 +02:00
|
|
|
SetWriter(io.Writer)
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
// Header defines the interface for HTTP header.
|
2016-01-29 09:46:11 +02:00
|
|
|
Header interface {
|
2016-03-17 06:24:04 +02:00
|
|
|
// Add adds the key, value pair to the header. It appends to any existing values
|
|
|
|
// associated with key.
|
2016-01-29 09:46:11 +02:00
|
|
|
Add(string, string)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Del deletes the values associated with key.
|
2016-01-29 09:46:11 +02:00
|
|
|
Del(string)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Set sets the header entries associated with key to the single element value.
|
|
|
|
// It replaces any existing values associated with key.
|
2016-01-29 09:46:11 +02:00
|
|
|
Set(string, string)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Get gets the first value associated with the given key. If there are
|
|
|
|
// no values associated with the key, Get returns "".
|
2016-03-15 04:58:46 +02:00
|
|
|
Get(string) string
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// Keys returns header keys.
|
2016-03-17 01:27:31 +02:00
|
|
|
Keys() []string
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
// URL defines the interface for HTTP request url.
|
2016-01-29 09:46:11 +02:00
|
|
|
URL interface {
|
2016-03-17 06:24:04 +02:00
|
|
|
// Path returns the request URL path.
|
2016-01-29 09:46:11 +02:00
|
|
|
Path() string
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// SetPath sets the request URL path.
|
2016-03-15 04:58:46 +02:00
|
|
|
SetPath(string)
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// QueryValue returns query parameter value for the provided name.
|
2016-01-29 09:46:11 +02:00
|
|
|
QueryValue(string) string
|
2016-03-17 06:24:04 +02:00
|
|
|
|
|
|
|
// QueryString returns the URL query string.
|
2016-03-15 17:50:43 +02:00
|
|
|
QueryString() string
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:24:45 +02:00
|
|
|
// Config defines engine configuration.
|
2016-01-29 09:46:11 +02:00
|
|
|
Config struct {
|
2016-03-17 06:24:04 +02:00
|
|
|
Address string // TCP address to listen on.
|
2016-03-18 17:47:03 +02:00
|
|
|
Listener net.Listener // Custom `net.Listener`. If set, server accepts connections on it.
|
2016-03-17 06:24:04 +02:00
|
|
|
TLSCertfile string // TLS certificate file path.
|
|
|
|
TLSKeyfile string // TLS key file path.
|
|
|
|
ReadTimeout time.Duration // Maximum duration before timing out read of the request.
|
|
|
|
WriteTimeout time.Duration // Maximum duration before timing out write of the response.
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-03-08 18:14:25 +02:00
|
|
|
|
2016-03-11 18:47:23 +02:00
|
|
|
// Handler defines an interface to server HTTP requests via `ServeHTTP(Request, Response)`
|
|
|
|
// function.
|
2016-03-08 18:14:25 +02:00
|
|
|
Handler interface {
|
|
|
|
ServeHTTP(Request, Response)
|
|
|
|
}
|
|
|
|
|
2016-03-11 18:47:23 +02:00
|
|
|
// HandlerFunc is an adapter to allow the use of `func(Request, Response)` as HTTP handlers.
|
2016-03-08 18:14:25 +02:00
|
|
|
HandlerFunc func(Request, Response)
|
2016-01-29 09:46:11 +02:00
|
|
|
)
|
2016-03-08 18:14:25 +02:00
|
|
|
|
2016-03-11 18:47:23 +02:00
|
|
|
// ServeHTTP serves HTTP request.
|
2016-03-08 18:14:25 +02:00
|
|
|
func (h HandlerFunc) ServeHTTP(req Request, res Response) {
|
2016-03-10 07:17:43 +02:00
|
|
|
h(req, res)
|
2016-03-08 18:14:25 +02:00
|
|
|
}
|