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"
|
2015-03-01 19:45:13 +02:00
|
|
|
"net/http"
|
2015-10-11 20:43:27 +02:00
|
|
|
spath "path"
|
2015-05-20 23:38:51 +02:00
|
|
|
|
2015-06-30 20:51:08 +02:00
|
|
|
"fmt"
|
|
|
|
|
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-06 15:48:33 +02:00
|
|
|
"golang.org/x/net/websocket"
|
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-03-01 19:45:13 +02:00
|
|
|
Context struct {
|
2015-05-22 13:40:01 +02:00
|
|
|
request *http.Request
|
|
|
|
response *Response
|
|
|
|
socket *websocket.Conn
|
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
|
2015-03-27 23:21:30 +02:00
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
store map[string]interface{}
|
|
|
|
)
|
|
|
|
|
2015-05-29 01:50:49 +02:00
|
|
|
// NewContext creates a Context object.
|
2015-05-08 20:52:06 +02:00
|
|
|
func NewContext(req *http.Request, res *Response, e *Echo) *Context {
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:40:01 +02: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
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// P returns path parameter by index.
|
2015-06-06 00:08:32 +02:00
|
|
|
func (c *Context) P(i int) (value string) {
|
|
|
|
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-04-06 05:08:52 +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
|
|
|
}
|
|
|
|
|
2015-07-05 20:08:17 +02: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 19:54:55 +02: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 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-05-20 23:38:51 +02:00
|
|
|
func (c *Context) Bind(i interface{}) error {
|
2015-07-30 23:43:22 +02:00
|
|
|
return c.echo.binder.Bind(c.request, i)
|
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-10-02 20:23:52 +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 {
|
2015-05-20 23:38:51 +02:00
|
|
|
return RendererNotRegistered
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2015-10-06 15:48:33 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2015-10-02 20:23:52 +02:00
|
|
|
if err = c.echo.renderer.Render(buf, name, data); err != nil {
|
|
|
|
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-07-24 21:28:35 +02:00
|
|
|
// HTML formats according to a format specifier and sends HTML response with
|
2015-07-08 22:51:08 +02:00
|
|
|
// status code.
|
|
|
|
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
|
2015-10-02 20:23:52 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err = fmt.Fprintf(buf, format, a...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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-10-02 20:23:52 +02:00
|
|
|
c.response.Write(buf.Bytes())
|
2015-07-08 22:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-24 21:28:35 +02:00
|
|
|
// String formats according to a format specifier and sends text response with status
|
|
|
|
// code.
|
2015-07-11 21:20:59 +02:00
|
|
|
func (c *Context) String(code int, format string, a ...interface{}) (err error) {
|
2015-10-02 20:23:52 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err = fmt.Fprintf(buf, format, a...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-11 21:20:59 +02:00
|
|
|
c.response.Header().Set(ContentType, TextPlain)
|
|
|
|
c.response.WriteHeader(code)
|
2015-10-02 20:23:52 +02:00
|
|
|
c.response.Write(buf.Bytes())
|
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-10-02 20:23:52 +02:00
|
|
|
func (c *Context) JSON(code int, i interface{}) (err error) {
|
|
|
|
b, err := json.Marshal(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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)
|
|
|
|
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.
|
|
|
|
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-10-02 20:23:52 +02:00
|
|
|
func (c *Context) XML(code int, i interface{}) (err error) {
|
|
|
|
b, err := xml.Marshal(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-22 13:44:29 +02:00
|
|
|
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
2015-05-22 13:40:01 +02:00
|
|
|
c.response.WriteHeader(code)
|
2015-07-11 21:20:59 +02:00
|
|
|
c.response.Write([]byte(xml.Header))
|
2015-10-02 20:23:52 +02:00
|
|
|
c.response.Write(b)
|
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-10-11 20:43:27 +02: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.
|
|
|
|
func (c *Context) File(name, path string, attachment bool) (err error) {
|
|
|
|
dir, file := spath.Split(path)
|
2015-08-25 07:01:25 +02:00
|
|
|
if attachment {
|
2015-10-11 20:43:27 +02:00
|
|
|
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
|
2015-08-01 04:25:03 +02:00
|
|
|
}
|
2015-10-02 20:23:52 +02:00
|
|
|
if err = serveFile(dir, file, c); err != nil {
|
|
|
|
c.response.Header().Del(ContentDisposition)
|
|
|
|
}
|
|
|
|
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-05-20 23:38:51 +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
|
|
|
|
}
|
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// Redirect redirects the request using http.Redirect with status code.
|
2015-07-15 03:35:28 +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 {
|
|
|
|
return InvalidRedirectCode
|
|
|
|
}
|
2015-05-30 02:20:13 +02:00
|
|
|
http.Redirect(c.response, c.request, url, 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-05-20 23:38:51 +02:00
|
|
|
func (c *Context) Error(err error) {
|
|
|
|
c.echo.httpErrorHandler(err, c)
|
2015-04-20 01:00:23 +02:00
|
|
|
}
|
2015-03-09 08:58:10 +02:00
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
func (c *Context) reset(r *http.Request, w http.ResponseWriter, e *Echo) {
|
2015-05-22 13:40:01 +02:00
|
|
|
c.request = r
|
|
|
|
c.response.reset(w)
|
2015-07-14 08:36:56 +02:00
|
|
|
c.query = nil
|
2015-07-14 19:56:23 +02:00
|
|
|
c.store = nil
|
2015-04-02 14:02:52 +02:00
|
|
|
c.echo = e
|
2015-03-06 21:12:33 +02:00
|
|
|
}
|