2015-03-27 14:21:30 -07:00
|
|
|
package echo
|
2015-03-01 09:45:13 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-07-11 15:20:59 -04:00
|
|
|
"encoding/xml"
|
2015-03-01 09:45:13 -08:00
|
|
|
"net/http"
|
2015-10-16 07:51:42 -07:00
|
|
|
"path/filepath"
|
2015-05-20 14:38:51 -07:00
|
|
|
|
2015-07-05 11:08:17 -07:00
|
|
|
"net/url"
|
2015-07-23 02:44:52 +10:00
|
|
|
|
2015-10-02 11:23:52 -07:00
|
|
|
"bytes"
|
2015-10-13 07:09:53 -07:00
|
|
|
|
2015-11-22 10:38:02 -08:00
|
|
|
"golang.org/x/net/context"
|
2015-10-06 06:48:33 -07:00
|
|
|
"golang.org/x/net/websocket"
|
2015-03-01 09:45:13 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2015-03-14 16:26:44 -07:00
|
|
|
// Context represents context for the current request. It holds request and
|
2015-05-17 22:54:29 -07:00
|
|
|
// response objects, path parameters, data and registered handler.
|
2015-03-01 09:45:13 -08:00
|
|
|
Context struct {
|
2015-11-22 10:38:02 -08:00
|
|
|
context.Context
|
2015-05-22 04:40:01 -07:00
|
|
|
request *http.Request
|
|
|
|
response *Response
|
|
|
|
socket *websocket.Conn
|
2015-11-12 20:23:14 -08:00
|
|
|
path string
|
2015-04-25 22:32:20 -07:00
|
|
|
pnames []string
|
|
|
|
pvalues []string
|
2015-07-05 11:08:17 -07:00
|
|
|
query url.Values
|
2015-04-05 14:21:03 -07:00
|
|
|
store store
|
2015-03-27 14:21:30 -07:00
|
|
|
echo *Echo
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
store map[string]interface{}
|
|
|
|
)
|
|
|
|
|
2015-05-28 16:50:49 -07:00
|
|
|
// NewContext creates a Context object.
|
2015-05-08 11:52:06 -07:00
|
|
|
func NewContext(req *http.Request, res *Response, e *Echo) *Context {
|
|
|
|
return &Context{
|
2015-05-22 04:40:01 -07:00
|
|
|
request: req,
|
|
|
|
response: res,
|
2015-05-08 11:52:06 -07:00
|
|
|
echo: e,
|
2015-06-03 15:18:27 -07:00
|
|
|
pvalues: make([]string, *e.maxParam),
|
2015-05-08 11:52:06 -07:00
|
|
|
store: make(store),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 04:40:01 -07:00
|
|
|
// Request returns *http.Request.
|
|
|
|
func (c *Context) Request() *http.Request {
|
|
|
|
return c.request
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response returns *Response.
|
|
|
|
func (c *Context) Response() *Response {
|
|
|
|
return c.response
|
|
|
|
}
|
|
|
|
|
|
|
|
// Socket returns *websocket.Conn.
|
|
|
|
func (c *Context) Socket() *websocket.Conn {
|
|
|
|
return c.socket
|
|
|
|
}
|
|
|
|
|
2016-02-04 12:37:21 +01:00
|
|
|
// ParamNames returns path parameter names.
|
|
|
|
func (c *Context) ParamNames() []string {
|
|
|
|
return c.pnames
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:03:15 -08:00
|
|
|
// Path returns the registered path for the handler.
|
2015-11-12 20:23:14 -08:00
|
|
|
func (c *Context) Path() string {
|
|
|
|
return c.path
|
|
|
|
}
|
|
|
|
|
2015-03-14 16:26:44 -07:00
|
|
|
// P returns path parameter by index.
|
2015-06-05 15:08:32 -07:00
|
|
|
func (c *Context) P(i int) (value string) {
|
|
|
|
l := len(c.pnames)
|
2015-06-20 18:56:51 -07:00
|
|
|
if i < l {
|
2015-04-25 22:32:20 -07:00
|
|
|
value = c.pvalues[i]
|
|
|
|
}
|
|
|
|
return
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-03-14 16:26:44 -07:00
|
|
|
// Param returns path parameter by name.
|
2015-04-05 20:08:52 -07:00
|
|
|
func (c *Context) Param(name string) (value string) {
|
2015-04-26 09:48:49 -07:00
|
|
|
l := len(c.pnames)
|
2015-04-25 22:32:20 -07:00
|
|
|
for i, n := range c.pnames {
|
2015-06-20 18:56:51 -07:00
|
|
|
if n == name && i < l {
|
2015-04-25 22:32:20 -07:00
|
|
|
value = c.pvalues[i]
|
|
|
|
break
|
2015-04-05 20:08:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-07-05 11:08:17 -07:00
|
|
|
// Query returns query parameter by name.
|
|
|
|
func (c *Context) Query(name string) string {
|
|
|
|
if c.query == nil {
|
|
|
|
c.query = c.request.URL.Query()
|
|
|
|
}
|
|
|
|
return c.query.Get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Form returns form parameter by name.
|
|
|
|
func (c *Context) Form(name string) string {
|
|
|
|
return c.request.FormValue(name)
|
|
|
|
}
|
|
|
|
|
2015-05-30 10:54:55 -07:00
|
|
|
// Get retrieves data from the context.
|
|
|
|
func (c *Context) Get(key string) interface{} {
|
|
|
|
return c.store[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set saves data in the context.
|
|
|
|
func (c *Context) Set(key string, val interface{}) {
|
2015-07-14 10:56:23 -07:00
|
|
|
if c.store == nil {
|
|
|
|
c.store = make(store)
|
|
|
|
}
|
2015-05-30 10:54:55 -07:00
|
|
|
c.store[key] = val
|
|
|
|
}
|
|
|
|
|
2015-07-30 14:43:22 -07:00
|
|
|
// Bind binds the request body into specified type `i`. The default binder does
|
|
|
|
// it based on Content-Type header.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) Bind(i interface{}) error {
|
2015-07-30 14:43:22 -07:00
|
|
|
return c.echo.binder.Bind(c.request, i)
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-07-16 23:03:45 -07:00
|
|
|
// Render renders a template with data and sends a text/html response with status
|
|
|
|
// code. Templates can be registered using `Echo.SetRenderer()`.
|
2015-10-02 11:23:52 -07:00
|
|
|
func (c *Context) Render(code int, name string, data interface{}) (err error) {
|
2015-04-10 21:48:26 -07:00
|
|
|
if c.echo.renderer == nil {
|
2016-01-29 04:19:07 -08:00
|
|
|
return ErrRendererNotRegistered
|
2015-04-10 21:48:26 -07:00
|
|
|
}
|
2015-10-06 06:48:33 -07:00
|
|
|
buf := new(bytes.Buffer)
|
2015-10-02 11:23:52 -07:00
|
|
|
if err = c.echo.renderer.Render(buf, name, data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-07-22 04:44:29 -07:00
|
|
|
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.WriteHeader(code)
|
2015-10-02 11:23:52 -07:00
|
|
|
c.response.Write(buf.Bytes())
|
|
|
|
return
|
2015-03-29 23:35:08 -07:00
|
|
|
}
|
|
|
|
|
2015-11-24 20:03:15 -08:00
|
|
|
// HTML sends an HTTP response with status code.
|
|
|
|
func (c *Context) HTML(code int, html string) (err error) {
|
2015-07-22 04:44:29 -07:00
|
|
|
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
2015-07-08 13:51:08 -07:00
|
|
|
c.response.WriteHeader(code)
|
2015-11-24 20:03:15 -08:00
|
|
|
c.response.Write([]byte(html))
|
2015-07-08 13:51:08 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:03:15 -08:00
|
|
|
// String sends a string response with status code.
|
|
|
|
func (c *Context) String(code int, s string) (err error) {
|
2015-12-01 11:22:45 -08:00
|
|
|
c.response.Header().Set(ContentType, TextPlainCharsetUTF8)
|
2015-07-11 15:20:59 -04:00
|
|
|
c.response.WriteHeader(code)
|
2015-11-24 20:03:15 -08:00
|
|
|
c.response.Write([]byte(s))
|
2015-07-11 15:20:59 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-24 15:28:35 -04:00
|
|
|
// JSON sends a JSON response with status code.
|
2015-10-02 11:23:52 -07:00
|
|
|
func (c *Context) JSON(code int, i interface{}) (err error) {
|
2015-11-08 00:39:09 +01:00
|
|
|
b, err := json.Marshal(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-11-01 19:48:55 +01:00
|
|
|
}
|
2016-02-09 19:26:31 -08:00
|
|
|
return c.JSONBlob(code, b)
|
2015-11-08 00:39:09 +01:00
|
|
|
}
|
2015-11-01 19:48:55 +01:00
|
|
|
|
2015-11-07 20:06:58 -08:00
|
|
|
// JSONIndent sends a JSON response with status code, but it applies prefix and indent to format the output.
|
2015-11-08 00:39:09 +01:00
|
|
|
func (c *Context) JSONIndent(code int, i interface{}, prefix string, indent string) (err error) {
|
|
|
|
b, err := json.MarshalIndent(i, prefix, indent)
|
2015-10-02 11:23:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-09 19:26:31 -08:00
|
|
|
return c.JSONBlob(code, b)
|
2015-11-07 20:06:58 -08:00
|
|
|
}
|
|
|
|
|
2016-02-09 19:26:31 -08:00
|
|
|
// JSONBlob sends a JSON blob response with status code.
|
|
|
|
func (c *Context) JSONBlob(code int, b []byte) (err error) {
|
2015-07-22 04:44:29 -07:00
|
|
|
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.WriteHeader(code)
|
2015-10-02 11:23:52 -07:00
|
|
|
c.response.Write(b)
|
2016-02-09 19:26:31 -08:00
|
|
|
return
|
2015-04-05 14:21:03 -07:00
|
|
|
}
|
|
|
|
|
2015-07-24 15:28:35 -04:00
|
|
|
// JSONP sends a JSONP response with status code. It uses `callback` to construct
|
|
|
|
// the JSONP payload.
|
|
|
|
func (c *Context) JSONP(code int, callback string, i interface{}) (err error) {
|
2015-10-02 11:23:52 -07:00
|
|
|
b, err := json.Marshal(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-24 15:28:35 -04:00
|
|
|
c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8)
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
c.response.Write([]byte(callback + "("))
|
2015-10-02 11:23:52 -07:00
|
|
|
c.response.Write(b)
|
|
|
|
c.response.Write([]byte(");"))
|
2015-07-24 15:28:35 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// XML sends an XML response with status code.
|
2015-10-02 11:23:52 -07:00
|
|
|
func (c *Context) XML(code int, i interface{}) (err error) {
|
|
|
|
b, err := xml.Marshal(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-22 04:44:29 -07:00
|
|
|
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.WriteHeader(code)
|
2015-07-11 15:20:59 -04:00
|
|
|
c.response.Write([]byte(xml.Header))
|
2015-10-02 11:23:52 -07:00
|
|
|
c.response.Write(b)
|
|
|
|
return
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-11-07 20:06:58 -08:00
|
|
|
// XMLIndent sends an XML response with status code, but it applies prefix and indent to format the output.
|
2015-11-08 00:39:09 +01:00
|
|
|
func (c *Context) XMLIndent(code int, i interface{}, prefix string, indent string) (err error) {
|
|
|
|
b, err := xml.MarshalIndent(i, prefix, indent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-07 20:06:58 -08:00
|
|
|
c.xml(code, b)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) xml(code int, b []byte) {
|
2015-11-08 00:39:09 +01:00
|
|
|
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
c.response.Write([]byte(xml.Header))
|
|
|
|
c.response.Write(b)
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:43:27 -07:00
|
|
|
// File sends a response with the content of the file. If `attachment` is set
|
|
|
|
// to true, the client is prompted to save the file with provided `name`,
|
|
|
|
// name can be empty, in that case name of the file is used.
|
2015-10-13 07:09:53 -07:00
|
|
|
func (c *Context) File(path, name string, attachment bool) (err error) {
|
2015-10-16 07:51:42 -07:00
|
|
|
dir, file := filepath.Split(path)
|
2015-08-24 22:01:25 -07:00
|
|
|
if attachment {
|
2015-10-11 11:43:27 -07:00
|
|
|
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
|
2015-07-31 19:25:03 -07:00
|
|
|
}
|
2015-11-23 20:33:13 -08:00
|
|
|
if err = c.echo.serveFile(dir, file, c); err != nil {
|
2015-10-02 11:23:52 -07:00
|
|
|
c.response.Header().Del(ContentDisposition)
|
|
|
|
}
|
|
|
|
return
|
2015-07-31 19:25:03 -07:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:47:48 -07:00
|
|
|
// NoContent sends a response with no body and a status code.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) NoContent(code int) error {
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.WriteHeader(code)
|
2015-04-18 16:47:48 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:20:13 -07:00
|
|
|
// Redirect redirects the request using http.Redirect with status code.
|
2015-07-14 21:35:28 -04:00
|
|
|
func (c *Context) Redirect(code int, url string) error {
|
2015-07-23 02:44:52 +10:00
|
|
|
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
2016-01-29 04:19:07 -08:00
|
|
|
return ErrInvalidRedirectCode
|
2015-07-23 02:44:52 +10:00
|
|
|
}
|
2015-05-29 17:20:13 -07:00
|
|
|
http.Redirect(c.response, c.request, url, code)
|
2015-07-20 19:24:33 -07:00
|
|
|
return nil
|
2015-05-29 17:20:13 -07:00
|
|
|
}
|
|
|
|
|
2015-07-08 13:51:08 -07:00
|
|
|
// Error invokes the registered HTTP error handler. Generally used by middleware.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) Error(err error) {
|
|
|
|
c.echo.httpErrorHandler(err, c)
|
2015-04-19 16:00:23 -07:00
|
|
|
}
|
2015-03-08 23:58:10 -07:00
|
|
|
|
2015-12-01 11:22:45 -08:00
|
|
|
// Echo returns the `Echo` instance.
|
|
|
|
func (c *Context) Echo() *Echo {
|
|
|
|
return c.echo
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:20:13 -07:00
|
|
|
func (c *Context) reset(r *http.Request, w http.ResponseWriter, e *Echo) {
|
2015-05-22 04:40:01 -07:00
|
|
|
c.request = r
|
2015-12-02 08:07:22 -08:00
|
|
|
c.response.reset(w, e)
|
2015-07-13 23:36:56 -07:00
|
|
|
c.query = nil
|
2015-07-14 10:56:23 -07:00
|
|
|
c.store = nil
|
2015-04-02 05:02:52 -07:00
|
|
|
c.echo = e
|
2015-03-06 11:12:33 -08:00
|
|
|
}
|