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"
|
2016-02-15 08:11:29 -08:00
|
|
|
"io"
|
|
|
|
"mime"
|
2016-03-21 13:10:20 -07:00
|
|
|
"mime/multipart"
|
2015-03-01 09:45:13 -08:00
|
|
|
"net/http"
|
2016-03-29 15:26:25 -07:00
|
|
|
"os"
|
2015-10-16 07:51:42 -07:00
|
|
|
"path/filepath"
|
2015-12-03 17:23:53 -08:00
|
|
|
"time"
|
2015-05-20 14:38:51 -07:00
|
|
|
|
2015-12-21 15:20:49 -08:00
|
|
|
"github.com/labstack/echo/engine"
|
2016-03-06 09:52:32 -08:00
|
|
|
"github.com/labstack/gommon/log"
|
2015-12-04 08:13:26 -08:00
|
|
|
|
2015-10-02 11:23:52 -07:00
|
|
|
"bytes"
|
2015-10-13 07:09:53 -07:00
|
|
|
|
2015-12-21 15:20:49 -08:00
|
|
|
netContext "golang.org/x/net/context"
|
2015-03-01 09:45:13 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-03-19 15:47:20 -07: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-03 17:23:53 -08:00
|
|
|
Context interface {
|
2015-12-21 15:20:49 -08:00
|
|
|
netContext.Context
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// NetContext returns `http://blog.golang.org/context.Context` interface.
|
2016-03-14 12:21:47 -07:00
|
|
|
NetContext() netContext.Context
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// SetNetContext sets `http://blog.golang.org/context.Context` interface.
|
2016-03-14 19:58:46 -07:00
|
|
|
SetNetContext(netContext.Context)
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Request returns `engine.Request` interface.
|
2015-12-21 15:20:49 -08:00
|
|
|
Request() engine.Request
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Request returns `engine.Response` interface.
|
2015-12-21 15:20:49 -08:00
|
|
|
Response() engine.Response
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Path returns the registered path for the handler.
|
2015-12-03 17:23:53 -08:00
|
|
|
Path() string
|
2016-03-19 15:47:20 -07:00
|
|
|
|
2016-04-16 15:53:27 -07:00
|
|
|
// SetPath sets the registered path for the handler.
|
|
|
|
SetPath(string)
|
|
|
|
|
2016-03-19 15:47:20 -07:00
|
|
|
// P returns path parameter by index.
|
2015-12-03 17:23:53 -08:00
|
|
|
P(int) string
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Param returns path parameter by name.
|
2015-12-03 17:23:53 -08:00
|
|
|
Param(string) string
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// ParamNames returns path parameter names.
|
2016-03-05 20:03:11 -08:00
|
|
|
ParamNames() []string
|
2016-03-19 15:47:20 -07:00
|
|
|
|
2016-04-16 15:53:27 -07:00
|
|
|
// SetParamNames sets path parameter names.
|
2016-04-17 09:41:20 -07:00
|
|
|
SetParamNames(...string)
|
2016-04-16 15:53:27 -07:00
|
|
|
|
|
|
|
// ParamValues returns path parameter values.
|
|
|
|
ParamValues() []string
|
|
|
|
|
|
|
|
// SetParamValues sets path parameter values.
|
2016-04-17 09:41:20 -07:00
|
|
|
SetParamValues(...string)
|
2016-04-16 15:53:27 -07:00
|
|
|
|
2016-03-21 13:10:20 -07:00
|
|
|
// QueryParam returns the query param for the provided name. It is an alias
|
|
|
|
// for `engine.URL#QueryParam()`.
|
|
|
|
QueryParam(string) string
|
2016-03-19 15:47:20 -07:00
|
|
|
|
2016-04-16 15:53:27 -07:00
|
|
|
// QueryParams returns the query parameters as map. It is an alias for `engine.URL#QueryParams()`.
|
2016-03-23 11:10:22 -05:00
|
|
|
QueryParams() map[string][]string
|
|
|
|
|
2016-03-21 13:10:20 -07:00
|
|
|
// FormValue returns the form field value for the provided name. It is an
|
|
|
|
// alias for `engine.Request#FormValue()`.
|
|
|
|
FormValue(string) string
|
|
|
|
|
2016-03-23 11:10:22 -05:00
|
|
|
// FormParams returns the form parameters as map. It is an alias for `engine.Request#FormParams()`.
|
|
|
|
FormParams() map[string][]string
|
|
|
|
|
2016-03-21 13:10:20 -07:00
|
|
|
// FormFile returns the multipart form file for the provided name. It is an
|
|
|
|
// alias for `engine.Request#FormFile()`.
|
|
|
|
FormFile(string) (*multipart.FileHeader, error)
|
|
|
|
|
|
|
|
// MultipartForm returns the multipart form. It is an alias for `engine.Request#MultipartForm()`.
|
|
|
|
MultipartForm() (*multipart.Form, error)
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Get retrieves data from the context.
|
2015-12-03 17:23:53 -08:00
|
|
|
Get(string) interface{}
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Set saves data in the context.
|
2016-03-14 19:58:46 -07:00
|
|
|
Set(string, interface{})
|
2016-03-19 15:47:20 -07:00
|
|
|
|
2016-04-21 18:35:49 -05:00
|
|
|
// Del deletes data from the context.
|
2016-04-16 15:53:27 -07:00
|
|
|
Del(string)
|
|
|
|
|
2016-04-21 18:35:49 -05:00
|
|
|
// Exists checks if that key exists in the context.
|
|
|
|
Exists(string) bool
|
|
|
|
|
2016-04-02 19:32:52 -07:00
|
|
|
// Bind binds the request body into provided type `i`. The default binder
|
|
|
|
// does it based on Content-Type header.
|
2015-12-03 17:23:53 -08:00
|
|
|
Bind(interface{}) error
|
2016-03-19 15:47:20 -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-12-03 17:23:53 -08:00
|
|
|
Render(int, string, interface{}) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// HTML sends an HTTP response with status code.
|
2015-12-03 17:23:53 -08:00
|
|
|
HTML(int, string) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// String sends a string response with status code.
|
2015-12-03 17:23:53 -08:00
|
|
|
String(int, string) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// JSON sends a JSON response with status code.
|
2015-12-03 17:23:53 -08:00
|
|
|
JSON(int, interface{}) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// JSONBlob sends a JSON blob response with status code.
|
2016-02-10 16:51:43 -08:00
|
|
|
JSONBlob(int, []byte) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// JSONP sends a JSONP response with status code. It uses `callback` to construct
|
|
|
|
// the JSONP payload.
|
2015-12-03 17:23:53 -08:00
|
|
|
JSONP(int, string, interface{}) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// XML sends an XML response with status code.
|
2015-12-03 17:23:53 -08:00
|
|
|
XML(int, interface{}) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// XMLBlob sends a XML blob response with status code.
|
2016-02-10 16:51:43 -08:00
|
|
|
XMLBlob(int, []byte) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// File sends a response with the content of the file.
|
2016-03-12 05:14:15 -08:00
|
|
|
File(string) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
2016-03-29 16:17:36 -07:00
|
|
|
// Attachment sends a response from `io.ReaderSeeker` as attachment, prompting
|
|
|
|
// client to save the file.
|
|
|
|
Attachment(io.ReadSeeker, string) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// NoContent sends a response with no body and a status code.
|
2015-12-03 17:23:53 -08:00
|
|
|
NoContent(int) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Redirect redirects the request with status code.
|
2015-12-03 17:23:53 -08:00
|
|
|
Redirect(int, string) error
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Error invokes the registered HTTP error handler. Generally used by middleware.
|
2015-12-03 17:23:53 -08:00
|
|
|
Error(err error)
|
2016-03-19 15:47:20 -07:00
|
|
|
|
2016-04-11 16:49:20 -07:00
|
|
|
// Handler returns the matched handler by router.
|
|
|
|
Handler() HandlerFunc
|
|
|
|
|
2016-04-16 15:53:27 -07:00
|
|
|
// SetHandler sets the matched handler by router.
|
|
|
|
SetHandler(HandlerFunc)
|
|
|
|
|
2016-03-19 15:47:20 -07:00
|
|
|
// Logger returns the `Logger` instance.
|
2016-03-06 09:52:32 -08:00
|
|
|
Logger() *log.Logger
|
2016-03-19 15:47:20 -07:00
|
|
|
|
|
|
|
// Echo returns the `Echo` instance.
|
2016-02-18 14:04:28 -08:00
|
|
|
Echo() *Echo
|
2016-03-19 15:47:20 -07:00
|
|
|
|
2016-03-27 16:29:41 -07: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-29 16:17:36 -07:00
|
|
|
ServeContent(io.ReadSeeker, string, time.Time) error
|
2016-03-27 16:29:41 -07:00
|
|
|
|
2016-03-19 15:47:20 -07:00
|
|
|
// Reset resets the context after request completes. It must be called along
|
|
|
|
// with `Echo#GetContext()` and `Echo#PutContext()`. See `Echo#ServeHTTP()`
|
|
|
|
Reset(engine.Request, engine.Response)
|
2015-12-03 17:23:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
context struct {
|
2016-03-14 08:09:48 -07:00
|
|
|
netContext netContext.Context
|
|
|
|
request engine.Request
|
|
|
|
response engine.Response
|
|
|
|
path string
|
|
|
|
pnames []string
|
|
|
|
pvalues []string
|
|
|
|
store store
|
2016-04-02 14:19:39 -07:00
|
|
|
handler HandlerFunc
|
2016-03-14 08:09:48 -07:00
|
|
|
echo *Echo
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
2015-12-03 17:23:53 -08:00
|
|
|
|
2015-03-01 09:45:13 -08:00
|
|
|
store map[string]interface{}
|
|
|
|
)
|
|
|
|
|
2016-02-15 08:11:29 -08:00
|
|
|
const (
|
|
|
|
indexPage = "index.html"
|
|
|
|
)
|
|
|
|
|
2016-03-14 12:21:47 -07:00
|
|
|
func (c *context) NetContext() netContext.Context {
|
|
|
|
return c.netContext
|
|
|
|
}
|
|
|
|
|
2016-03-14 19:58:46 -07:00
|
|
|
func (c *context) SetNetContext(ctx netContext.Context) {
|
|
|
|
c.netContext = ctx
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) Deadline() (deadline time.Time, ok bool) {
|
2016-03-14 08:09:48 -07:00
|
|
|
return c.netContext.Deadline()
|
2015-12-03 17:23:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Done() <-chan struct{} {
|
2016-03-14 08:09:48 -07:00
|
|
|
return c.netContext.Done()
|
2015-12-03 17:23:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Err() error {
|
2016-03-14 08:09:48 -07:00
|
|
|
return c.netContext.Err()
|
2015-12-03 17:23:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Value(key interface{}) interface{} {
|
2016-03-14 08:09:48 -07:00
|
|
|
return c.netContext.Value(key)
|
|
|
|
}
|
|
|
|
|
2015-12-21 15:20:49 -08:00
|
|
|
func (c *context) Request() engine.Request {
|
2015-05-22 04:40:01 -07:00
|
|
|
return c.request
|
|
|
|
}
|
|
|
|
|
2015-12-21 15:20:49 -08:00
|
|
|
func (c *context) Response() engine.Response {
|
2015-05-22 04:40:01 -07:00
|
|
|
return c.response
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) Path() string {
|
2015-11-12 20:23:14 -08:00
|
|
|
return c.path
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:53:27 -07:00
|
|
|
func (c *context) SetPath(p string) {
|
|
|
|
c.path = p
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) P(i int) (value string) {
|
2015-06-05 15:08:32 -07:00
|
|
|
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-12-03 17:23:53 -08: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
|
|
|
}
|
|
|
|
|
2016-03-05 20:03:11 -08:00
|
|
|
func (c *context) ParamNames() []string {
|
|
|
|
return c.pnames
|
|
|
|
}
|
|
|
|
|
2016-04-17 09:41:20 -07:00
|
|
|
func (c *context) SetParamNames(names ...string) {
|
2016-04-16 15:53:27 -07:00
|
|
|
c.pnames = names
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) ParamValues() []string {
|
|
|
|
return c.pvalues
|
|
|
|
}
|
|
|
|
|
2016-04-17 09:41:20 -07:00
|
|
|
func (c *context) SetParamValues(values ...string) {
|
2016-04-16 15:53:27 -07:00
|
|
|
c.pvalues = values
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:10:20 -07:00
|
|
|
func (c *context) QueryParam(name string) string {
|
|
|
|
return c.request.URL().QueryParam(name)
|
2015-07-05 11:08:17 -07:00
|
|
|
}
|
|
|
|
|
2016-03-23 11:10:22 -05:00
|
|
|
func (c *context) QueryParams() map[string][]string {
|
|
|
|
return c.request.URL().QueryParams()
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:10:20 -07:00
|
|
|
func (c *context) FormValue(name string) string {
|
2016-01-28 23:46:11 -08:00
|
|
|
return c.request.FormValue(name)
|
2015-07-05 11:08:17 -07:00
|
|
|
}
|
|
|
|
|
2016-03-23 11:10:22 -05:00
|
|
|
func (c *context) FormParams() map[string][]string {
|
|
|
|
return c.request.FormParams()
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:10:20 -07:00
|
|
|
func (c *context) FormFile(name string) (*multipart.FileHeader, error) {
|
|
|
|
return c.request.FormFile(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) MultipartForm() (*multipart.Form, error) {
|
|
|
|
return c.request.MultipartForm()
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-14 19:58:46 -07:00
|
|
|
func (c *context) Get(key string) interface{} {
|
|
|
|
return c.store[key]
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:53:27 -07:00
|
|
|
func (c *context) Del(key string) {
|
|
|
|
delete(c.store, key)
|
|
|
|
}
|
|
|
|
|
2016-04-21 18:35:49 -05:00
|
|
|
func (c *context) Exists(key string) bool {
|
|
|
|
_, ok := c.store[key]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) Bind(i interface{}) error {
|
2016-03-05 20:03:11 -08:00
|
|
|
return c.echo.binder.Bind(i, c)
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08: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-02-15 08:11:29 -08:00
|
|
|
return ErrRendererNotRegistered
|
2015-04-10 21:48:26 -07:00
|
|
|
}
|
2015-10-06 06:48:33 -07:00
|
|
|
buf := new(bytes.Buffer)
|
2016-03-05 20:03:11 -08:00
|
|
|
if err = c.echo.renderer.Render(buf, name, data, c); err != nil {
|
2015-10-02 11:23:52 -07:00
|
|
|
return
|
|
|
|
}
|
2016-04-06 07:28:53 -07:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-12 15:06:52 -08:00
|
|
|
_, err = c.response.Write(buf.Bytes())
|
2015-10-02 11:23:52 -07:00
|
|
|
return
|
2015-03-29 23:35:08 -07:00
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) HTML(code int, html string) (err error) {
|
2016-04-06 07:28:53 -07:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
|
2015-07-08 13:51:08 -07:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-12 15:06:52 -08:00
|
|
|
_, err = c.response.Write([]byte(html))
|
2015-07-08 13:51:08 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) String(code int, s string) (err error) {
|
2016-04-06 07:28:53 -07:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
|
2015-07-11 15:20:59 -04:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-12 15:06:52 -08:00
|
|
|
_, err = c.response.Write([]byte(s))
|
2015-07-11 15:20:59 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) JSON(code int, i interface{}) (err error) {
|
2015-11-08 00:39:09 +01:00
|
|
|
b, err := json.Marshal(i)
|
2016-02-10 16:51:43 -08:00
|
|
|
if c.echo.Debug() {
|
|
|
|
b, err = json.MarshalIndent(i, "", " ")
|
2015-11-01 19:48:55 +01:00
|
|
|
}
|
2015-10-02 11:23:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-10 16:51:43 -08:00
|
|
|
return c.JSONBlob(code, b)
|
2015-11-07 20:06:58 -08:00
|
|
|
}
|
|
|
|
|
2016-02-10 16:51:43 -08:00
|
|
|
func (c *context) JSONBlob(code int, b []byte) (err error) {
|
2016-04-06 07:28:53 -07:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-12 15:06:52 -08:00
|
|
|
_, err = c.response.Write(b)
|
2016-02-10 16:51:43 -08:00
|
|
|
return
|
2015-04-05 14:21:03 -07:00
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
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
|
|
|
|
}
|
2016-04-06 07:28:53 -07:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
|
2015-07-24 15:28:35 -04:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-12 15:06:52 -08: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 15:28:35 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) XML(code int, i interface{}) (err error) {
|
2015-10-02 11:23:52 -07:00
|
|
|
b, err := xml.Marshal(i)
|
2016-02-10 16:51:43 -08:00
|
|
|
if c.echo.Debug() {
|
|
|
|
b, err = xml.MarshalIndent(i, "", " ")
|
2015-10-02 11:23:52 -07:00
|
|
|
}
|
2015-11-08 00:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-10 16:51:43 -08:00
|
|
|
return c.XMLBlob(code, b)
|
2015-11-07 20:06:58 -08:00
|
|
|
}
|
|
|
|
|
2016-02-10 16:51:43 -08:00
|
|
|
func (c *context) XMLBlob(code int, b []byte) (err error) {
|
2016-04-06 07:28:53 -07:00
|
|
|
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
|
2015-11-08 00:39:09 +01:00
|
|
|
c.response.WriteHeader(code)
|
2016-03-12 15:06:52 -08:00
|
|
|
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = c.response.Write(b)
|
2016-02-10 16:51:43 -08:00
|
|
|
return
|
2015-11-08 00:39:09 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 05:14:15 -08:00
|
|
|
func (c *context) File(file string) error {
|
2016-03-29 15:26:25 -07:00
|
|
|
f, err := os.Open(file)
|
2016-03-12 05:14:15 -08:00
|
|
|
if err != nil {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, _ := f.Stat()
|
|
|
|
if fi.IsDir() {
|
2016-04-10 20:56:10 -07:00
|
|
|
file = filepath.Join(file, "index.html")
|
2016-03-29 15:26:25 -07:00
|
|
|
f, err = os.Open(file)
|
2016-03-12 05:14:15 -08:00
|
|
|
if err != nil {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2016-04-10 20:56:10 -07:00
|
|
|
if fi, err = f.Stat(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-12 05:14:15 -08:00
|
|
|
}
|
2016-03-27 21:03:35 -07:00
|
|
|
return c.ServeContent(f, fi.Name(), fi.ModTime())
|
2016-03-12 05:14:15 -08:00
|
|
|
}
|
|
|
|
|
2016-03-29 16:17:36 -07:00
|
|
|
func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
|
2016-04-06 07:28:53 -07:00
|
|
|
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
|
|
|
c.response.Header().Set(HeaderContentDisposition, "attachment; filename="+name)
|
2016-02-15 08:11:29 -08:00
|
|
|
c.response.WriteHeader(http.StatusOK)
|
2016-03-12 11:49:45 -08:00
|
|
|
_, err = io.Copy(c.response, r)
|
2015-10-02 11:23:52 -07:00
|
|
|
return
|
2015-07-31 19:25:03 -07:00
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08: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-12-03 17:23:53 -08: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-02-15 08:11:29 -08:00
|
|
|
return ErrInvalidRedirectCode
|
2015-07-23 02:44:52 +10:00
|
|
|
}
|
2016-04-06 07:28:53 -07:00
|
|
|
c.response.Header().Set(HeaderLocation, url)
|
2016-02-20 12:54:43 -08:00
|
|
|
c.response.WriteHeader(code)
|
2015-07-20 19:24:33 -07:00
|
|
|
return nil
|
2015-05-29 17:20:13 -07:00
|
|
|
}
|
|
|
|
|
2015-12-03 17:23:53 -08:00
|
|
|
func (c *context) Error(err error) {
|
2015-05-20 14:38:51 -07:00
|
|
|
c.echo.httpErrorHandler(err, c)
|
2015-04-19 16:00:23 -07:00
|
|
|
}
|
2015-03-08 23:58:10 -07:00
|
|
|
|
2016-02-18 14:04:28 -08:00
|
|
|
func (c *context) Echo() *Echo {
|
|
|
|
return c.echo
|
|
|
|
}
|
|
|
|
|
2016-04-11 16:49:20 -07:00
|
|
|
func (c *context) Handler() HandlerFunc {
|
|
|
|
return c.handler
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:53:27 -07:00
|
|
|
func (c *context) SetHandler(h HandlerFunc) {
|
|
|
|
c.handler = h
|
2015-12-04 08:13:26 -08:00
|
|
|
}
|
|
|
|
|
2016-04-16 15:53:27 -07:00
|
|
|
func (c *context) Logger() *log.Logger {
|
|
|
|
return c.echo.logger
|
2015-12-01 11:22:45 -08:00
|
|
|
}
|
|
|
|
|
2016-03-29 16:17:36 -07:00
|
|
|
func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
|
2016-04-24 10:21:23 -07:00
|
|
|
req := c.Request()
|
|
|
|
res := c.Response()
|
2016-03-27 16:29:41 -07:00
|
|
|
|
2016-04-24 10:21:23 -07:00
|
|
|
if t, err := time.Parse(http.TimeFormat, req.Header().Get(HeaderIfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
|
|
|
|
res.Header().Del(HeaderContentType)
|
|
|
|
res.Header().Del(HeaderContentLength)
|
2016-03-27 16:29:41 -07:00
|
|
|
return c.NoContent(http.StatusNotModified)
|
|
|
|
}
|
|
|
|
|
2016-04-24 10:21:23 -07:00
|
|
|
res.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
|
|
|
res.Header().Set(HeaderLastModified, modtime.UTC().Format(http.TimeFormat))
|
|
|
|
res.WriteHeader(http.StatusOK)
|
|
|
|
_, err := io.Copy(res, content)
|
2016-03-12 05:14:15 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-29 16:17:36 -07: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 08:11:29 -08:00
|
|
|
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
2016-04-06 07:28:53 -07:00
|
|
|
t = MIMEOctetStream
|
2016-02-15 08:11:29 -08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-24 10:21:23 -07:00
|
|
|
func (c *context) Reset(req engine.Request, res engine.Response) {
|
2016-03-16 08:24:25 -07:00
|
|
|
c.netContext = nil
|
2016-04-24 10:21:23 -07:00
|
|
|
c.request = req
|
|
|
|
c.response = res
|
2015-07-14 10:56:23 -07:00
|
|
|
c.store = nil
|
2016-03-08 19:31:11 -08:00
|
|
|
c.handler = notFoundHandler
|
2015-03-06 11:12:33 -08:00
|
|
|
}
|