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
|
|
|
|
2016-06-01 03:29:11 +02:00
|
|
|
"github.com/labstack/echo/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-06-01 03:29:11 +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-28 15:57:31 +02:00
|
|
|
// IsTLS returns true if HTTP connection is TLS otherwise false.
|
|
|
|
IsTLS() 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
|
|
|
|
2016-04-13 07:39:29 +02:00
|
|
|
// SetURI sets the URI of the request.
|
|
|
|
SetURI(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-05-10 04:13:02 +02:00
|
|
|
// Referer returns the referring URL, if sent in the request.
|
|
|
|
Referer() string
|
|
|
|
|
|
|
|
// Protocol returns the protocol version string of the HTTP request.
|
|
|
|
// Protocol() string
|
|
|
|
|
|
|
|
// ProtocolMajor returns the major protocol version of the HTTP request.
|
|
|
|
// ProtocolMajor() int
|
|
|
|
|
|
|
|
// ProtocolMinor returns the minor protocol version of the HTTP request.
|
|
|
|
// ProtocolMinor() int
|
2016-03-17 05:15:38 +02:00
|
|
|
|
2016-03-28 15:57:31 +02:00
|
|
|
// ContentLength returns the size of request's body.
|
2016-05-19 17:30:20 +02:00
|
|
|
ContentLength() int64
|
2016-03-28 15:57:31 +02:00
|
|
|
|
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-08-18 03:26:58 +02:00
|
|
|
// RealIP returns the client's network ip address.
|
|
|
|
RealIP() string
|
2016-08-16 10:45:41 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Method returns the request's HTTP function.
|
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
|
|
|
|
|
2016-05-01 05:08:06 +02:00
|
|
|
// Body sets request's body.
|
|
|
|
SetBody(io.Reader)
|
|
|
|
|
2016-03-21 22:10:20 +02:00
|
|
|
// FormValue returns the 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
|
|
|
|
2016-03-23 18:10:22 +02:00
|
|
|
// FormParams returns the form parameters.
|
|
|
|
FormParams() map[string][]string
|
|
|
|
|
2016-03-21 22:10:20 +02:00
|
|
|
// FormFile returns the multipart 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
|
|
|
|
2016-03-21 22:10:20 +02:00
|
|
|
// MultipartForm returns the multipart form.
|
2016-03-13 01:06:52 +02:00
|
|
|
MultipartForm() (*multipart.Form, error)
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// Cookie returns the named cookie provided in the request.
|
2016-05-03 07:41:07 +02:00
|
|
|
Cookie(string) (Cookie, error)
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// Cookies returns the HTTP cookies sent with the request.
|
|
|
|
Cookies() []Cookie
|
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
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
// SetCookie adds a `Set-Cookie` header in HTTP response.
|
|
|
|
SetCookie(Cookie)
|
|
|
|
|
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
|
|
|
|
2016-05-22 16:58:21 +02:00
|
|
|
// Keys returns the header keys.
|
2016-03-17 01:27:31 +02:00
|
|
|
Keys() []string
|
2016-05-22 16:58:21 +02:00
|
|
|
|
|
|
|
// Contains checks if the header is set.
|
|
|
|
Contains(string) bool
|
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
|
|
|
|
2016-03-21 22:10:20 +02:00
|
|
|
// QueryParam returns the query param for the provided name.
|
|
|
|
QueryParam(string) string
|
2016-03-17 06:24:04 +02:00
|
|
|
|
2016-03-23 18:10:22 +02:00
|
|
|
// QueryParam returns the query parameters as map.
|
|
|
|
QueryParams() map[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-05-03 01:19:35 +02:00
|
|
|
// Cookie defines the interface for HTTP cookie.
|
|
|
|
Cookie interface {
|
|
|
|
// Name returns the name of the cookie.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// Value returns the value of the cookie.
|
|
|
|
Value() string
|
|
|
|
|
|
|
|
// Path returns the path of the cookie.
|
|
|
|
Path() string
|
|
|
|
|
|
|
|
// Domain returns the domain of the cookie.
|
|
|
|
Domain() string
|
|
|
|
|
|
|
|
// Expires returns the expiry time of the cookie.
|
|
|
|
Expires() time.Time
|
|
|
|
|
|
|
|
// Secure indicates if cookie is secured.
|
|
|
|
Secure() bool
|
|
|
|
|
|
|
|
// HTTPOnly indicate if cookies is HTTP only.
|
|
|
|
HTTPOnly() bool
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Config defines engine config.
|
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-06-16 23:25:32 +02:00
|
|
|
TLSCertFile string // TLS certificate file path.
|
|
|
|
TLSKeyFile string // TLS key file path.
|
2016-03-17 06:24:04 +02:00
|
|
|
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-20 00:47:20 +02:00
|
|
|
// HandlerFunc is an adapter to allow the use of `func(Request, Response)` as
|
|
|
|
// an HTTP handler.
|
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-04-24 19:21:23 +02:00
|
|
|
func (h HandlerFunc) ServeHTTP(req Request, res Response) {
|
|
|
|
h(req, res)
|
2016-03-08 18:14:25 +02:00
|
|
|
}
|