2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-07-11 21:20:59 +02:00
|
|
|
"encoding/xml"
|
2016-02-15 18:11:29 +02:00
|
|
|
"io"
|
|
|
|
"mime"
|
2015-03-01 19:45:13 +02:00
|
|
|
"net/http"
|
2016-02-15 18:11:29 +02:00
|
|
|
"os"
|
2015-10-16 16:51:42 +02:00
|
|
|
"path/filepath"
|
2015-12-04 03:23:53 +02:00
|
|
|
"time"
|
2015-05-20 23:38:51 +02:00
|
|
|
|
2015-12-22 01:20:49 +02:00
|
|
|
"github.com/labstack/echo/engine"
|
2016-03-06 19:52:32 +02:00
|
|
|
"github.com/labstack/gommon/log"
|
2015-12-04 18:13:26 +02:00
|
|
|
|
2015-07-05 20:08:17 +02:00
|
|
|
"net/url"
|
2015-07-22 18:44:52 +02:00
|
|
|
|
2015-10-02 20:23:52 +02:00
|
|
|
"bytes"
|
2015-10-13 16:09:53 +02:00
|
|
|
|
2015-12-22 01:20:49 +02:00
|
|
|
netContext "golang.org/x/net/context"
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2015-03-15 01:26:44 +02:00
|
|
|
// Context represents context for the current request. It holds request and
|
2015-05-18 07:54:29 +02:00
|
|
|
// response objects, path parameters, data and registered handler.
|
2015-12-04 03:23:53 +02:00
|
|
|
Context interface {
|
2015-12-22 01:20:49 +02:00
|
|
|
netContext.Context
|
|
|
|
Request() engine.Request
|
|
|
|
Response() engine.Response
|
2015-12-04 03:23:53 +02:00
|
|
|
Path() string
|
|
|
|
P(int) string
|
|
|
|
Param(string) string
|
2016-03-06 06:03:11 +02:00
|
|
|
ParamNames() []string
|
2015-12-04 03:23:53 +02:00
|
|
|
Query(string) string
|
|
|
|
Form(string) string
|
|
|
|
Set(string, interface{})
|
|
|
|
Get(string) interface{}
|
|
|
|
Bind(interface{}) error
|
|
|
|
Render(int, string, interface{}) error
|
|
|
|
HTML(int, string) error
|
|
|
|
String(int, string) error
|
|
|
|
JSON(int, interface{}) error
|
2016-02-11 02:51:43 +02:00
|
|
|
JSONBlob(int, []byte) error
|
2015-12-04 03:23:53 +02:00
|
|
|
JSONP(int, string, interface{}) error
|
|
|
|
XML(int, interface{}) error
|
2016-02-11 02:51:43 +02:00
|
|
|
XMLBlob(int, []byte) error
|
2016-02-15 18:11:29 +02:00
|
|
|
Attachment(string) error
|
2015-12-04 03:23:53 +02:00
|
|
|
NoContent(int) error
|
|
|
|
Redirect(int, string) error
|
|
|
|
Error(err error)
|
2016-02-15 18:11:29 +02:00
|
|
|
Handle(Context) error
|
2016-03-06 19:52:32 +02:00
|
|
|
Logger() *log.Logger
|
2016-02-19 00:04:28 +02:00
|
|
|
Echo() *Echo
|
2016-02-09 02:48:03 +02:00
|
|
|
Object() *context
|
2015-12-04 03:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
context struct {
|
2015-12-22 01:20:49 +02:00
|
|
|
request engine.Request
|
|
|
|
response engine.Response
|
2015-11-13 06:23:14 +02:00
|
|
|
path string
|
2015-04-26 07:32:20 +02:00
|
|
|
pnames []string
|
|
|
|
pvalues []string
|
2015-07-05 20:08:17 +02:00
|
|
|
query url.Values
|
2015-04-05 23:21:03 +02:00
|
|
|
store store
|
2016-02-15 18:11:29 +02:00
|
|
|
handler Handler
|
2015-03-27 23:21:30 +02:00
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-12-04 03:23:53 +02:00
|
|
|
|
2015-03-01 19:45:13 +02:00
|
|
|
store map[string]interface{}
|
|
|
|
)
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
const (
|
|
|
|
indexPage = "index.html"
|
|
|
|
)
|
|
|
|
|
2015-05-29 01:50:49 +02:00
|
|
|
// NewContext creates a Context object.
|
2015-12-22 01:20:49 +02:00
|
|
|
func NewContext(req engine.Request, res engine.Response, e *Echo) Context {
|
2015-12-04 03:23:53 +02:00
|
|
|
return &context{
|
2015-05-22 13:40:01 +02:00
|
|
|
request: req,
|
|
|
|
response: res,
|
2015-05-08 20:52:06 +02:00
|
|
|
echo: e,
|
2015-06-04 00:18:27 +02:00
|
|
|
pvalues: make([]string, *e.maxParam),
|
2015-05-08 20:52:06 +02:00
|
|
|
store: make(store),
|
2016-02-15 18:11:29 +02:00
|
|
|
handler: notFoundHandler,
|
2015-05-08 20:52:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
func (c *context) Handle(ctx Context) error {
|
|
|
|
return c.handler.Handle(ctx)
|
|
|
|
}
|
|
|
|
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Deadline() (deadline time.Time, ok bool) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Done() <-chan struct{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Err() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Value(key interface{}) interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:40:01 +02:00
|
|
|
// Request returns *http.Request.
|
2015-12-22 01:20:49 +02:00
|
|
|
func (c *context) Request() engine.Request {
|
2015-05-22 13:40:01 +02:00
|
|
|
return c.request
|
|
|
|
}
|
|
|
|
|
2016-03-10 22:05:33 +02:00
|
|
|
// Response returns `engine.Response`.
|
2015-12-22 01:20:49 +02:00
|
|
|
func (c *context) Response() engine.Response {
|
2015-05-22 13:40:01 +02:00
|
|
|
return c.response
|
|
|
|
}
|
|
|
|
|
2015-11-25 06:03:15 +02:00
|
|
|
// Path returns the registered path for the handler.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Path() string {
|
2015-11-13 06:23:14 +02:00
|
|
|
return c.path
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// P returns path parameter by index.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) P(i int) (value string) {
|
2015-06-06 00:08:32 +02:00
|
|
|
l := len(c.pnames)
|
2015-06-21 03:56:51 +02:00
|
|
|
if i < l {
|
2015-04-26 07:32:20 +02:00
|
|
|
value = c.pvalues[i]
|
|
|
|
}
|
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// Param returns path parameter by name.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Param(name string) (value string) {
|
2015-04-26 18:48:49 +02:00
|
|
|
l := len(c.pnames)
|
2015-04-26 07:32:20 +02:00
|
|
|
for i, n := range c.pnames {
|
2015-06-21 03:56:51 +02:00
|
|
|
if n == name && i < l {
|
2015-04-26 07:32:20 +02:00
|
|
|
value = c.pvalues[i]
|
|
|
|
break
|
2015-04-06 05:08:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 06:03:11 +02:00
|
|
|
// ParamNames returns path parameter names.
|
|
|
|
func (c *context) ParamNames() []string {
|
|
|
|
return c.pnames
|
|
|
|
}
|
|
|
|
|
2015-07-05 20:08:17 +02:00
|
|
|
// Query returns query parameter by name.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Query(name string) string {
|
2016-01-29 09:46:11 +02:00
|
|
|
return c.request.URL().QueryValue(name)
|
2015-07-05 20:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Form returns form parameter by name.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Form(name string) string {
|
2016-01-29 09:46:11 +02:00
|
|
|
return c.request.FormValue(name)
|
2015-07-05 20:08:17 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// Get retrieves data from the context.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Get(key string) interface{} {
|
2015-05-30 19:54:55 +02:00
|
|
|
return c.store[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set saves data in the context.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Set(key string, val interface{}) {
|
2015-07-14 19:56:23 +02:00
|
|
|
if c.store == nil {
|
|
|
|
c.store = make(store)
|
|
|
|
}
|
2015-05-30 19:54:55 +02:00
|
|
|
c.store[key] = val
|
|
|
|
}
|
|
|
|
|
2015-07-30 23:43:22 +02:00
|
|
|
// Bind binds the request body into specified type `i`. The default binder does
|
|
|
|
// it based on Content-Type header.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Bind(i interface{}) error {
|
2016-03-06 06:03:11 +02:00
|
|
|
return c.echo.binder.Bind(i, c)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 08:03:45 +02:00
|
|
|
// Render renders a template with data and sends a text/html response with status
|
|
|
|
// code. Templates can be registered using `Echo.SetRenderer()`.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Render(code int, name string, data interface{}) (err error) {
|
2015-04-11 06:48:26 +02:00
|
|
|
if c.echo.renderer == nil {
|
2016-02-15 18:11:29 +02:00
|
|
|
return ErrRendererNotRegistered
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2015-10-06 15:48:33 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2016-03-06 06:03:11 +02:00
|
|
|
if err = c.echo.renderer.Render(buf, name, data, c); err != nil {
|
2015-10-02 20:23:52 +02:00
|
|
|
return
|
|
|
|
}
|
2015-07-22 13:44:29 +02:00
|
|
|
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
2015-05-22 13:40:01 +02:00
|
|
|
c.response.WriteHeader(code)
|
2015-10-02 20:23:52 +02:00
|
|
|
c.response.Write(buf.Bytes())
|
|
|
|
return
|
2015-03-30 08:35:08 +02:00
|
|
|
}
|
|
|
|
|
2015-11-25 06:03:15 +02:00
|
|
|
// HTML sends an HTTP response with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) HTML(code int, html string) (err error) {
|
2015-07-22 13:44:29 +02:00
|
|
|
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
2015-07-08 22:51:08 +02:00
|
|
|
c.response.WriteHeader(code)
|
2015-11-25 06:03:15 +02:00
|
|
|
c.response.Write([]byte(html))
|
2015-07-08 22:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-25 06:03:15 +02:00
|
|
|
// String sends a string response with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) String(code int, s string) (err error) {
|
2015-12-01 21:22:45 +02:00
|
|
|
c.response.Header().Set(ContentType, TextPlainCharsetUTF8)
|
2015-07-11 21:20:59 +02:00
|
|
|
c.response.WriteHeader(code)
|
2015-11-25 06:03:15 +02:00
|
|
|
c.response.Write([]byte(s))
|
2015-07-11 21:20:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-24 21:28:35 +02:00
|
|
|
// JSON sends a JSON response with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) JSON(code int, i interface{}) (err error) {
|
2015-11-08 01:39:09 +02:00
|
|
|
b, err := json.Marshal(i)
|
2016-02-11 02:51:43 +02:00
|
|
|
if c.echo.Debug() {
|
|
|
|
b, err = json.MarshalIndent(i, "", " ")
|
2015-11-01 20:48:55 +02:00
|
|
|
}
|
2015-10-02 20:23:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-11 02:51:43 +02:00
|
|
|
return c.JSONBlob(code, b)
|
2015-11-08 06:06:58 +02:00
|
|
|
}
|
|
|
|
|
2016-02-11 02:51:43 +02:00
|
|
|
// JSONBlob sends a JSON blob response with status code.
|
|
|
|
func (c *context) JSONBlob(code int, b []byte) (err error) {
|
2015-07-22 13:44:29 +02:00
|
|
|
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
2015-05-22 13:40:01 +02:00
|
|
|
c.response.WriteHeader(code)
|
2015-10-02 20:23:52 +02:00
|
|
|
c.response.Write(b)
|
2016-02-11 02:51:43 +02:00
|
|
|
return
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 21:28:35 +02:00
|
|
|
// JSONP sends a JSONP response with status code. It uses `callback` to construct
|
|
|
|
// the JSONP payload.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
|
2015-10-02 20:23:52 +02:00
|
|
|
b, err := json.Marshal(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-24 21:28:35 +02:00
|
|
|
c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8)
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
c.response.Write([]byte(callback + "("))
|
2015-10-02 20:23:52 +02:00
|
|
|
c.response.Write(b)
|
|
|
|
c.response.Write([]byte(");"))
|
2015-07-24 21:28:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// XML sends an XML response with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) XML(code int, i interface{}) (err error) {
|
2015-10-02 20:23:52 +02:00
|
|
|
b, err := xml.Marshal(i)
|
2016-02-11 02:51:43 +02:00
|
|
|
if c.echo.Debug() {
|
|
|
|
b, err = xml.MarshalIndent(i, "", " ")
|
2015-10-02 20:23:52 +02:00
|
|
|
}
|
2015-11-08 01:39:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-11 02:51:43 +02:00
|
|
|
return c.XMLBlob(code, b)
|
2015-11-08 06:06:58 +02:00
|
|
|
}
|
|
|
|
|
2016-02-11 02:51:43 +02:00
|
|
|
// XMLBlob sends a XML blob response with status code.
|
|
|
|
func (c *context) XMLBlob(code int, b []byte) (err error) {
|
2015-11-08 01:39:09 +02:00
|
|
|
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
c.response.Write([]byte(xml.Header))
|
|
|
|
c.response.Write(b)
|
2016-02-11 02:51:43 +02:00
|
|
|
return
|
2015-11-08 01:39:09 +02:00
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
// Attachment sends specified file as an attachment to the client.
|
|
|
|
func (c *context) Attachment(file string) (err error) {
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2015-10-02 20:23:52 +02:00
|
|
|
}
|
2016-02-15 18:11:29 +02:00
|
|
|
_, name := filepath.Split(file)
|
|
|
|
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
|
|
|
|
c.response.Header().Set(ContentType, c.detectContentType(file))
|
|
|
|
c.response.WriteHeader(http.StatusOK)
|
|
|
|
_, err = io.Copy(c.response, f)
|
2015-10-02 20:23:52 +02:00
|
|
|
return
|
2015-08-01 04:25:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 01:47:48 +02:00
|
|
|
// NoContent sends a response with no body and a status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) NoContent(code int) error {
|
2015-05-22 13:40:01 +02:00
|
|
|
c.response.WriteHeader(code)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-20 22:54:43 +02:00
|
|
|
// Redirect redirects the request with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Redirect(code int, url string) error {
|
2015-07-22 18:44:52 +02:00
|
|
|
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
2016-02-15 18:11:29 +02:00
|
|
|
return ErrInvalidRedirectCode
|
2015-07-22 18:44:52 +02:00
|
|
|
}
|
2016-02-20 22:54:43 +02:00
|
|
|
c.response.Header().Set(Location, url)
|
|
|
|
c.response.WriteHeader(code)
|
2015-07-21 04:24:33 +02:00
|
|
|
return nil
|
2015-05-30 02:20:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-08 22:51:08 +02:00
|
|
|
// Error invokes the registered HTTP error handler. Generally used by middleware.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (c *context) Error(err error) {
|
2015-05-20 23:38:51 +02:00
|
|
|
c.echo.httpErrorHandler(err, c)
|
2015-04-20 01:00:23 +02:00
|
|
|
}
|
2015-03-09 08:58:10 +02:00
|
|
|
|
2016-02-22 05:58:19 +02:00
|
|
|
// Echo returns the `Echo` instance.
|
2016-02-19 00:04:28 +02:00
|
|
|
func (c *context) Echo() *Echo {
|
|
|
|
return c.echo
|
|
|
|
}
|
|
|
|
|
2015-12-04 18:13:26 +02:00
|
|
|
// Logger returns the `Logger` instance.
|
2016-03-06 19:52:32 +02:00
|
|
|
func (c *context) Logger() *log.Logger {
|
2015-12-04 18:13:26 +02:00
|
|
|
return c.echo.logger
|
|
|
|
}
|
|
|
|
|
2016-02-09 02:48:03 +02:00
|
|
|
// Object returns the `context` object.
|
|
|
|
func (c *context) Object() *context {
|
2015-12-04 18:13:26 +02:00
|
|
|
return c
|
2015-12-01 21:22:45 +02:00
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
func (c *context) detectContentType(name string) (t string) {
|
|
|
|
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
|
|
|
t = OctetStream
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) reset(req engine.Request, res engine.Response) {
|
2015-12-22 01:20:49 +02:00
|
|
|
c.request = req
|
|
|
|
c.response = res
|
2015-07-14 08:36:56 +02:00
|
|
|
c.query = nil
|
2015-07-14 19:56:23 +02:00
|
|
|
c.store = nil
|
2016-03-09 05:31:11 +02:00
|
|
|
c.handler = notFoundHandler
|
2015-03-06 21:12:33 +02:00
|
|
|
}
|