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-08-22 23:46:21 +02:00
|
|
|
"fmt"
|
2016-02-15 18:11:29 +02:00
|
|
|
"io"
|
|
|
|
"mime"
|
2016-03-21 22:10:20 +02:00
|
|
|
"mime/multipart"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net"
|
2015-03-01 19:45:13 +02:00
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/url"
|
2016-03-30 00:26:25 +02:00
|
|
|
"os"
|
2015-10-16 16:51:42 +02:00
|
|
|
"path/filepath"
|
2016-09-23 07:53:44 +02:00
|
|
|
"strings"
|
2015-12-04 03:23:53 +02:00
|
|
|
"time"
|
2015-05-20 23:38:51 +02:00
|
|
|
|
2016-09-23 14:55:27 +02:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
|
2015-10-02 20:23:52 +02:00
|
|
|
"bytes"
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-03-20 00:47:20 +02:00
|
|
|
// Context represents the context of the current HTTP request. It holds request and
|
|
|
|
// response objects, path, path parameters, data and registered handler.
|
2015-12-04 03:23:53 +02:00
|
|
|
Context interface {
|
2016-09-23 07:53:44 +02:00
|
|
|
// Request returns `*http.Request`.
|
|
|
|
Request() *http.Request
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
// SetWebSocket sets `*http.Request`.
|
|
|
|
SetRequest(*http.Request)
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// Request returns `*Response`.
|
|
|
|
Response() *Response
|
|
|
|
|
2016-09-23 14:55:27 +02:00
|
|
|
// WebSocket returns `*websocket.Conn`.
|
|
|
|
WebSocket() *websocket.Conn
|
|
|
|
|
|
|
|
// SetWebSocket sets `*websocket.Conn`.
|
|
|
|
SetWebSocket(*websocket.Conn)
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// IsTLS returns true if HTTP connection is TLS otherwise false.
|
|
|
|
IsTLS() bool
|
|
|
|
|
|
|
|
// Scheme returns the HTTP protocol scheme, `http` or `https`.
|
|
|
|
Scheme() string
|
|
|
|
|
|
|
|
// RealIP returns the client's network address based on `X-Forwarded-For`
|
|
|
|
// or `X-Real-IP` request header.
|
|
|
|
RealIP() string
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// Path returns the registered path for the handler.
|
2015-12-04 03:23:53 +02:00
|
|
|
Path() string
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
// SetPath sets the registered path for the handler.
|
|
|
|
SetPath(string)
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Param returns path parameter by name.
|
2015-12-04 03:23:53 +02:00
|
|
|
Param(string) string
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// ParamNames returns path parameter names.
|
2016-03-06 06:03:11 +02:00
|
|
|
ParamNames() []string
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
// SetParamNames sets path parameter names.
|
2016-04-17 18:41:20 +02:00
|
|
|
SetParamNames(...string)
|
2016-04-17 00:53:27 +02:00
|
|
|
|
|
|
|
// ParamValues returns path parameter values.
|
|
|
|
ParamValues() []string
|
|
|
|
|
|
|
|
// SetParamValues sets path parameter values.
|
2016-04-17 18:41:20 +02:00
|
|
|
SetParamValues(...string)
|
2016-04-17 00:53:27 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// QueryParam returns the query param for the provided name.
|
2016-03-21 22:10:20 +02:00
|
|
|
QueryParam(string) string
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// QueryParams returns the query parameters as `url.Values`.
|
|
|
|
QueryParams() url.Values
|
2016-03-23 18:10:22 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// QueryString returns the URL query string.
|
|
|
|
QueryString() string
|
|
|
|
|
|
|
|
// FormValue returns the form field value for the provided name.
|
2016-03-21 22:10:20 +02:00
|
|
|
FormValue(string) string
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// FormParams returns the form parameters as `url.Values`.
|
|
|
|
FormParams() (url.Values, error)
|
2016-03-23 18:10:22 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// FormFile returns the multipart form file for the provided name.
|
2016-03-21 22:10:20 +02:00
|
|
|
FormFile(string) (*multipart.FileHeader, error)
|
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
// MultipartForm returns the multipart form.
|
2016-03-21 22:10:20 +02:00
|
|
|
MultipartForm() (*multipart.Form, error)
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
// Cookie returns the named cookie provided in the request.
|
2016-09-23 07:53:44 +02:00
|
|
|
Cookie(string) (*http.Cookie, error)
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// SetCookie adds a `Set-Cookie` header in HTTP response.
|
2016-09-23 07:53:44 +02:00
|
|
|
SetCookie(*http.Cookie)
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// Cookies returns the HTTP cookies sent with the request.
|
2016-09-23 07:53:44 +02:00
|
|
|
Cookies() []*http.Cookie
|
2016-05-03 01:19:35 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Get retrieves data from the context.
|
2015-12-04 03:23:53 +02:00
|
|
|
Get(string) interface{}
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// Set saves data in the context.
|
2016-03-15 04:58:46 +02:00
|
|
|
Set(string, interface{})
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
// Bind binds the request body into provided type `i`. The default binder
|
|
|
|
// does it based on Content-Type header.
|
2015-12-04 03:23:53 +02:00
|
|
|
Bind(interface{}) error
|
2016-03-20 00:47:20 +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
|
|
|
Render(int, string, interface{}) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// HTML sends an HTTP response with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
HTML(int, string) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// String sends a string response with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
String(int, string) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// JSON sends a JSON response with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
JSON(int, interface{}) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// JSONBlob sends a JSON blob response with status code.
|
2016-02-11 02:51:43 +02:00
|
|
|
JSONBlob(int, []byte) error
|
2016-03-20 00:47:20 +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
|
|
|
JSONP(int, string, interface{}) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-09-10 00:30:07 +02:00
|
|
|
// JSONPBlob sends a JSONP blob response with status code. It uses `callback`
|
|
|
|
// to construct the JSONP payload.
|
|
|
|
JSONPBlob(int, string, []byte) error
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// XML sends an XML response with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
XML(int, interface{}) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// XMLBlob sends a XML blob response with status code.
|
2016-02-11 02:51:43 +02:00
|
|
|
XMLBlob(int, []byte) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-09-10 00:30:07 +02:00
|
|
|
// Blob sends a blob response with status code and content type.
|
|
|
|
Blob(int, string, []byte) error
|
|
|
|
|
|
|
|
// Stream sends a streaming response with status code and content type.
|
|
|
|
Stream(int, string, io.Reader) error
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// File sends a response with the content of the file.
|
2016-03-12 15:14:15 +02:00
|
|
|
File(string) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-03-30 01:17:36 +02:00
|
|
|
// Attachment sends a response from `io.ReaderSeeker` as attachment, prompting
|
|
|
|
// client to save the file.
|
|
|
|
Attachment(io.ReadSeeker, string) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-08-22 23:46:21 +02:00
|
|
|
// Inline sends a response from `io.ReaderSeeker` as inline, opening
|
|
|
|
// the file in the browser.
|
|
|
|
Inline(io.ReadSeeker, string) error
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// NoContent sends a response with no body and a status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
NoContent(int) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// Redirect redirects the request with status code.
|
2015-12-04 03:23:53 +02:00
|
|
|
Redirect(int, string) error
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// Error invokes the registered HTTP error handler. Generally used by middleware.
|
2015-12-04 03:23:53 +02:00
|
|
|
Error(err error)
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-04-12 01:49:20 +02:00
|
|
|
// Handler returns the matched handler by router.
|
|
|
|
Handler() HandlerFunc
|
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
// SetHandler sets the matched handler by router.
|
|
|
|
SetHandler(HandlerFunc)
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Logger returns the `Logger` instance.
|
2016-09-25 21:14:50 +02:00
|
|
|
Logger() Logger
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// Echo returns the `Echo` instance.
|
2016-02-19 00:04:28 +02:00
|
|
|
Echo() *Echo
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-03-28 01:29:41 +02:00
|
|
|
// ServeContent sends static content from `io.Reader` and handles caching
|
|
|
|
// via `If-Modified-Since` request header. It automatically sets `Content-Type`
|
|
|
|
// and `Last-Modified` response headers.
|
2016-03-30 01:17:36 +02:00
|
|
|
ServeContent(io.ReadSeeker, string, time.Time) error
|
2016-03-28 01:29:41 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Reset resets the context after request completes. It must be called along
|
2016-05-04 02:23:31 +02:00
|
|
|
// with `Echo#AcquireContext()` and `Echo#ReleaseContext()`.
|
|
|
|
// See `Echo#ServeHTTP()`
|
2016-09-23 07:53:44 +02:00
|
|
|
Reset(*http.Request, http.ResponseWriter)
|
2015-12-04 03:23:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
context struct {
|
2016-09-23 14:55:27 +02:00
|
|
|
request *http.Request
|
|
|
|
response *Response
|
|
|
|
webSocket *websocket.Conn
|
|
|
|
path string
|
|
|
|
pnames []string
|
|
|
|
pvalues []string
|
|
|
|
query url.Values
|
|
|
|
handler HandlerFunc
|
2016-10-14 01:45:27 +02:00
|
|
|
store Map
|
2016-09-23 14:55:27 +02:00
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
const (
|
2016-09-23 07:53:44 +02:00
|
|
|
defaultMemory = 32 << 20 // 32 MB
|
|
|
|
indexPage = "index.html"
|
2016-02-15 18:11:29 +02:00
|
|
|
)
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Request() *http.Request {
|
|
|
|
return c.request
|
2016-03-14 17:09:48 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) SetRequest(r *http.Request) {
|
|
|
|
c.request = r
|
2015-05-22 13:40:01 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Response() *Response {
|
2015-05-22 13:40:01 +02:00
|
|
|
return c.response
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) WebSocket() *websocket.Conn {
|
2016-09-23 14:55:27 +02:00
|
|
|
return c.webSocket
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) SetWebSocket(ws *websocket.Conn) {
|
2016-09-23 14:55:27 +02:00
|
|
|
c.webSocket = ws
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) IsTLS() bool {
|
2016-09-23 07:53:44 +02:00
|
|
|
return c.request.TLS != nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Scheme() string {
|
2016-09-23 07:53:44 +02:00
|
|
|
// Can't use `r.Request.URL.Scheme`
|
|
|
|
// See: https://groups.google.com/forum/#!topic/golang-nuts/pMUkBlQBDF0
|
|
|
|
if c.IsTLS() {
|
|
|
|
return "https"
|
|
|
|
}
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) RealIP() string {
|
2016-09-23 07:53:44 +02:00
|
|
|
ra := c.request.RemoteAddr
|
|
|
|
if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" {
|
|
|
|
ra = ip
|
|
|
|
} else if ip := c.request.Header.Get(HeaderXRealIP); ip != "" {
|
|
|
|
ra = ip
|
|
|
|
} else {
|
|
|
|
ra, _, _ = net.SplitHostPort(ra)
|
|
|
|
}
|
|
|
|
return ra
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Path() string {
|
2015-11-13 06:23:14 +02:00
|
|
|
return c.path
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) SetPath(p string) {
|
2016-04-17 00:53:27 +02:00
|
|
|
c.path = p
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +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-10-11 02:31:26 +02:00
|
|
|
func (c *context) ParamNames() []string {
|
2016-03-06 06:03:11 +02:00
|
|
|
return c.pnames
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) SetParamNames(names ...string) {
|
2016-04-17 00:53:27 +02:00
|
|
|
c.pnames = names
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) ParamValues() []string {
|
2016-04-17 00:53:27 +02:00
|
|
|
return c.pvalues
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) SetParamValues(values ...string) {
|
2016-04-17 00:53:27 +02:00
|
|
|
c.pvalues = values
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) QueryParam(name string) string {
|
2016-09-23 07:53:44 +02:00
|
|
|
if c.query == nil {
|
|
|
|
c.query = c.request.URL.Query()
|
|
|
|
}
|
|
|
|
return c.query.Get(name)
|
2015-07-05 20:08:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) QueryParams() url.Values {
|
2016-09-23 07:53:44 +02:00
|
|
|
if c.query == nil {
|
|
|
|
c.query = c.request.URL.Query()
|
|
|
|
}
|
|
|
|
return c.query
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) QueryString() string {
|
2016-09-23 07:53:44 +02:00
|
|
|
return c.request.URL.RawQuery
|
2016-03-23 18:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) FormValue(name string) string {
|
2016-01-29 09:46:11 +02:00
|
|
|
return c.request.FormValue(name)
|
2015-07-05 20:08:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) FormParams() (url.Values, error) {
|
2016-09-23 07:53:44 +02:00
|
|
|
if strings.HasPrefix(c.request.Header.Get(HeaderContentType), MIMEMultipartForm) {
|
|
|
|
if err := c.request.ParseMultipartForm(defaultMemory); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := c.request.ParseForm(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.request.Form, nil
|
2016-03-23 18:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) FormFile(name string) (*multipart.FileHeader, error) {
|
2016-09-23 07:53:44 +02:00
|
|
|
_, fh, err := c.request.FormFile(name)
|
|
|
|
return fh, err
|
2016-03-21 22:10:20 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) MultipartForm() (*multipart.Form, error) {
|
2016-09-23 07:53:44 +02:00
|
|
|
err := c.request.ParseMultipartForm(defaultMemory)
|
|
|
|
return c.request.MultipartForm, err
|
2016-03-21 22:10:20 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Cookie(name string) (*http.Cookie, error) {
|
2016-05-03 01:19:35 +02:00
|
|
|
return c.request.Cookie(name)
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) SetCookie(cookie *http.Cookie) {
|
2016-09-23 07:53:44 +02:00
|
|
|
http.SetCookie(c.Response(), cookie)
|
2016-05-03 01:19:35 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Cookies() []*http.Cookie {
|
2016-05-03 01:19:35 +02:00
|
|
|
return c.request.Cookies()
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Set(key string, val interface{}) {
|
|
|
|
if c.store == nil {
|
2016-10-14 01:45:27 +02:00
|
|
|
c.store = make(Map)
|
2016-10-11 02:31:26 +02:00
|
|
|
}
|
|
|
|
c.store[key] = val
|
2015-05-30 19:54:55 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Get(key string) interface{} {
|
|
|
|
return c.store[key]
|
2016-04-22 01:35:49 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Bind(i interface{}) error {
|
2016-09-23 07:53:44 +02:00
|
|
|
return c.echo.Binder.Bind(i, c)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Render(code int, name string, data interface{}) (err error) {
|
2016-09-23 07:53:44 +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-09-23 07:53:44 +02:00
|
|
|
if err = c.echo.Renderer.Render(buf, name, data, c); err != nil {
|
2015-10-02 20:23:52 +02:00
|
|
|
return
|
|
|
|
}
|
2016-07-27 18:11:34 +02:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
|
2015-05-22 13:40:01 +02:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-13 01:06:52 +02:00
|
|
|
_, err = c.response.Write(buf.Bytes())
|
2015-10-02 20:23:52 +02:00
|
|
|
return
|
2015-03-30 08:35:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) HTML(code int, html string) (err error) {
|
2016-07-27 18:11:34 +02:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
|
2015-07-08 22:51:08 +02:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-13 01:06:52 +02:00
|
|
|
_, err = c.response.Write([]byte(html))
|
2015-07-08 22:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) String(code int, s string) (err error) {
|
2016-07-27 18:11:34 +02:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
|
2015-07-11 21:20:59 +02:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-13 01:06:52 +02:00
|
|
|
_, err = c.response.Write([]byte(s))
|
2015-07-11 21:20:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +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-09-23 07:53:44 +02:00
|
|
|
if c.echo.Debug {
|
2016-02-11 02:51:43 +02:00
|
|
|
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-10-11 02:31:26 +02:00
|
|
|
func (c *context) JSONBlob(code int, b []byte) (err error) {
|
2016-09-10 00:30:07 +02:00
|
|
|
return c.Blob(code, MIMEApplicationJSONCharsetUTF8, b)
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +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
|
|
|
|
}
|
2016-09-10 00:30:07 +02:00
|
|
|
return c.JSONPBlob(code, callback, b)
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
|
2016-07-27 18:11:34 +02:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
|
2015-07-24 21:28:35 +02:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-13 01:06:52 +02:00
|
|
|
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = c.response.Write(b); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = c.response.Write([]byte(");"))
|
2015-07-24 21:28:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +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-09-23 07:53:44 +02:00
|
|
|
if c.echo.Debug {
|
2016-02-11 02:51:43 +02:00
|
|
|
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-10-11 02:31:26 +02:00
|
|
|
func (c *context) XMLBlob(code int, b []byte) (err error) {
|
2016-03-13 01:06:52 +02:00
|
|
|
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-09-10 00:30:07 +02:00
|
|
|
return c.Blob(code, MIMEApplicationXMLCharsetUTF8, b)
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Blob(code int, contentType string, b []byte) (err error) {
|
2016-09-10 00:30:07 +02:00
|
|
|
c.response.Header().Set(HeaderContentType, contentType)
|
|
|
|
c.response.WriteHeader(code)
|
2016-03-13 01:06:52 +02:00
|
|
|
_, err = c.response.Write(b)
|
2016-02-11 02:51:43 +02:00
|
|
|
return
|
2015-11-08 01:39:09 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Stream(code int, contentType string, r io.Reader) (err error) {
|
2016-09-10 00:30:07 +02:00
|
|
|
c.response.Header().Set(HeaderContentType, contentType)
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
_, err = io.Copy(c.response, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) File(file string) error {
|
2016-03-30 00:26:25 +02:00
|
|
|
f, err := os.Open(file)
|
2016-03-12 15:14:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, _ := f.Stat()
|
|
|
|
if fi.IsDir() {
|
2016-04-11 05:56:10 +02:00
|
|
|
file = filepath.Join(file, "index.html")
|
2016-03-30 00:26:25 +02:00
|
|
|
f, err = os.Open(file)
|
2016-03-12 15:14:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2016-04-11 05:56:10 +02:00
|
|
|
if fi, err = f.Stat(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
2016-03-28 06:03:35 +02:00
|
|
|
return c.ServeContent(f, fi.Name(), fi.ModTime())
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
|
2016-08-22 23:46:21 +02:00
|
|
|
return c.contentDisposition(r, name, "attachment")
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Inline(r io.ReadSeeker, name string) (err error) {
|
2016-08-22 23:46:21 +02:00
|
|
|
return c.contentDisposition(r, name, "inline")
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) contentDisposition(r io.ReadSeeker, name, dispositionType string) (err error) {
|
2016-07-27 18:11:34 +02:00
|
|
|
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
2016-08-22 23:46:21 +02:00
|
|
|
c.response.Header().Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
|
2016-02-15 18:11:29 +02:00
|
|
|
c.response.WriteHeader(http.StatusOK)
|
2016-03-12 21:49:45 +02:00
|
|
|
_, err = io.Copy(c.response, r)
|
2015-10-02 20:23:52 +02:00
|
|
|
return
|
2015-08-01 04:25:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +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-10-11 02:31:26 +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-04-06 16:28:53 +02:00
|
|
|
c.response.Header().Set(HeaderLocation, url)
|
2016-02-20 22:54:43 +02:00
|
|
|
c.response.WriteHeader(code)
|
2015-07-21 04:24:33 +02:00
|
|
|
return nil
|
2015-05-30 02:20:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Error(err error) {
|
2016-09-23 07:53:44 +02:00
|
|
|
c.echo.HTTPErrorHandler(err, c)
|
2015-04-20 01:00:23 +02:00
|
|
|
}
|
2015-03-09 08:58:10 +02:00
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Echo() *Echo {
|
2016-02-19 00:04:28 +02:00
|
|
|
return c.echo
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Handler() HandlerFunc {
|
2016-04-12 01:49:20 +02:00
|
|
|
return c.handler
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) SetHandler(h HandlerFunc) {
|
2016-04-17 00:53:27 +02:00
|
|
|
c.handler = h
|
2015-12-04 18:13:26 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Logger() Logger {
|
2016-09-23 07:53:44 +02:00
|
|
|
return c.echo.Logger
|
2015-12-01 21:22:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
|
2016-04-24 19:21:23 +02:00
|
|
|
req := c.Request()
|
|
|
|
res := c.Response()
|
2016-03-28 01:29:41 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
if t, err := time.Parse(http.TimeFormat, req.Header.Get(HeaderIfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
|
2016-04-24 19:21:23 +02:00
|
|
|
res.Header().Del(HeaderContentType)
|
|
|
|
res.Header().Del(HeaderContentLength)
|
2016-03-28 01:29:41 +02:00
|
|
|
return c.NoContent(http.StatusNotModified)
|
|
|
|
}
|
|
|
|
|
2016-07-27 18:11:34 +02:00
|
|
|
res.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
2016-04-24 19:21:23 +02:00
|
|
|
res.Header().Set(HeaderLastModified, modtime.UTC().Format(http.TimeFormat))
|
|
|
|
res.WriteHeader(http.StatusOK)
|
|
|
|
_, err := io.Copy(res, content)
|
2016-03-12 15:14:15 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-30 01:17:36 +02:00
|
|
|
// ContentTypeByExtension returns the MIME type associated with the file based on
|
|
|
|
// its extension. It returns `application/octet-stream` incase MIME type is not
|
|
|
|
// found.
|
|
|
|
func ContentTypeByExtension(name string) (t string) {
|
2016-02-15 18:11:29 +02:00
|
|
|
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
2016-04-06 16:28:53 +02:00
|
|
|
t = MIMEOctetStream
|
2016-02-15 18:11:29 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
|
|
|
|
c.query = nil
|
|
|
|
c.store = nil
|
2016-09-23 07:53:44 +02:00
|
|
|
c.request = r
|
|
|
|
c.response.reset(w)
|
2016-07-05 17:43:46 +02:00
|
|
|
c.handler = NotFoundHandler
|
2015-03-06 21:12:33 +02:00
|
|
|
}
|