19 // Context represents the context of the current HTTP request. It holds request and
20 // response objects, path, path parameters, data and registered handler.
22 // Request returns `*http.Request`.
23 Request() *http.Request
25 // SetRequest sets `*http.Request`.
26 SetRequest(r *http.Request)
28 // Response returns `*Response`.
31 // IsTLS returns true if HTTP connection is TLS otherwise false.
34 // IsWebSocket returns true if HTTP connection is WebSocket otherwise false.
37 // Scheme returns the HTTP protocol scheme, `http` or `https`.
40 // RealIP returns the client's network address based on `X-Forwarded-For`
41 // or `X-Real-IP` request header.
44 // Path returns the registered path for the handler.
47 // SetPath sets the registered path for the handler.
50 // Param returns path parameter by name.
51 Param(name string) string
53 // ParamNames returns path parameter names.
56 // SetParamNames sets path parameter names.
57 SetParamNames(names ...string)
59 // ParamValues returns path parameter values.
60 ParamValues() []string
62 // SetParamValues sets path parameter values.
63 SetParamValues(values ...string)
65 // QueryParam returns the query param for the provided name.
66 QueryParam(name string) string
68 // QueryParams returns the query parameters as `url.Values`.
69 QueryParams() url.Values
71 // QueryString returns the URL query string.
74 // FormValue returns the form field value for the provided name.
75 FormValue(name string) string
77 // FormParams returns the form parameters as `url.Values`.
78 FormParams() (url.Values, error)
80 // FormFile returns the multipart form file for the provided name.
81 FormFile(name string) (*multipart.FileHeader, error)
83 // MultipartForm returns the multipart form.
84 MultipartForm() (*multipart.Form, error)
86 // Cookie returns the named cookie provided in the request.
87 Cookie(name string) (*http.Cookie, error)
89 // SetCookie adds a `Set-Cookie` header in HTTP response.
90 SetCookie(cookie *http.Cookie)
92 // Cookies returns the HTTP cookies sent with the request.
93 Cookies() []*http.Cookie
95 // Get retrieves data from the context.
96 Get(key string) interface{}
98 // Set saves data in the context.
99 Set(key string, val interface{})
101 // Bind binds the request body into provided type `i`. The default binder
102 // does it based on Content-Type header.
103 Bind(i interface{}) error
105 // Validate validates provided `i`. It is usually called after `Context#Bind()`.
106 // Validator must be registered using `Echo#Validator`.
107 Validate(i interface{}) error
109 // Render renders a template with data and sends a text/html response with status
110 // code. Renderer must be registered using `Echo.Renderer`.
111 Render(code int, name string, data interface{}) error
113 // HTML sends an HTTP response with status code.
114 HTML(code int, html string) error
116 // HTMLBlob sends an HTTP blob response with status code.
117 HTMLBlob(code int, b []byte) error
119 // String sends a string response with status code.
120 String(code int, s string) error
122 // JSON sends a JSON response with status code.
123 JSON(code int, i interface{}) error
125 // JSONPretty sends a pretty-print JSON with status code.
126 JSONPretty(code int, i interface{}, indent string) error
128 // JSONBlob sends a JSON blob response with status code.
129 JSONBlob(code int, b []byte) error
131 // JSONP sends a JSONP response with status code. It uses `callback` to construct
132 // the JSONP payload.
133 JSONP(code int, callback string, i interface{}) error
135 // JSONPBlob sends a JSONP blob response with status code. It uses `callback`
136 // to construct the JSONP payload.
137 JSONPBlob(code int, callback string, b []byte) error
139 // XML sends an XML response with status code.
140 XML(code int, i interface{}) error
142 // XMLPretty sends a pretty-print XML with status code.
143 XMLPretty(code int, i interface{}, indent string) error
145 // XMLBlob sends an XML blob response with status code.
146 XMLBlob(code int, b []byte) error
148 // Blob sends a blob response with status code and content type.
149 Blob(code int, contentType string, b []byte) error
151 // Stream sends a streaming response with status code and content type.
152 Stream(code int, contentType string, r io.Reader) error
154 // File sends a response with the content of the file.
155 File(file string) error
157 // Attachment sends a response as attachment, prompting client to save the
159 Attachment(file string, name string) error
161 // Inline sends a response as inline, opening the file in the browser.
162 Inline(file string, name string) error
164 // NoContent sends a response with no body and a status code.
165 NoContent(code int) error
167 // Redirect redirects the request to a provided URL with status code.
168 Redirect(code int, url string) error
170 // Error invokes the registered HTTP error handler. Generally used by middleware.
173 // Handler returns the matched handler by router.
174 Handler() HandlerFunc
176 // SetHandler sets the matched handler by router.
177 SetHandler(h HandlerFunc)
179 // Logger returns the `Logger` instance.
182 // Echo returns the `Echo` instance.
185 // Reset resets the context after request completes. It must be called along
186 // with `Echo#AcquireContext()` and `Echo#ReleaseContext()`.
187 // See `Echo#ServeHTTP()`
188 Reset(r *http.Request, w http.ResponseWriter)
192 request *http.Request
205 defaultMemory = 32 << 20 // 32 MB
206 indexPage = "index.html"
210 func (c *context) writeContentType(value string) {
211 header := c.Response().Header()
212 if header.Get(HeaderContentType) == "" {
213 header.Set(HeaderContentType, value)
217 func (c *context) Request() *http.Request {
221 func (c *context) SetRequest(r *http.Request) {
225 func (c *context) Response() *Response {
229 func (c *context) IsTLS() bool {
230 return c.request.TLS != nil
233 func (c *context) IsWebSocket() bool {
234 upgrade := c.request.Header.Get(HeaderUpgrade)
235 return upgrade == "websocket" || upgrade == "Websocket"
238 func (c *context) Scheme() string {
239 // Can't use `r.Request.URL.Scheme`
240 // See: https://groups.google.com/forum/#!topic/golang-nuts/pMUkBlQBDF0
244 if scheme := c.request.Header.Get(HeaderXForwardedProto); scheme != "" {
247 if scheme := c.request.Header.Get(HeaderXForwardedProtocol); scheme != "" {
250 if ssl := c.request.Header.Get(HeaderXForwardedSsl); ssl == "on" {
253 if scheme := c.request.Header.Get(HeaderXUrlScheme); scheme != "" {
259 func (c *context) RealIP() string {
260 if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" {
261 return strings.Split(ip, ", ")[0]
263 if ip := c.request.Header.Get(HeaderXRealIP); ip != "" {
266 ra, _, _ := net.SplitHostPort(c.request.RemoteAddr)
270 func (c *context) Path() string {
274 func (c *context) SetPath(p string) {
278 func (c *context) Param(name string) string {
279 for i, n := range c.pnames {
280 if i < len(c.pvalues) {
289 func (c *context) ParamNames() []string {
293 func (c *context) SetParamNames(names ...string) {
297 func (c *context) ParamValues() []string {
298 return c.pvalues[:len(c.pnames)]
301 func (c *context) SetParamValues(values ...string) {
305 func (c *context) QueryParam(name string) string {
307 c.query = c.request.URL.Query()
309 return c.query.Get(name)
312 func (c *context) QueryParams() url.Values {
314 c.query = c.request.URL.Query()
319 func (c *context) QueryString() string {
320 return c.request.URL.RawQuery
323 func (c *context) FormValue(name string) string {
324 return c.request.FormValue(name)
327 func (c *context) FormParams() (url.Values, error) {
328 if strings.HasPrefix(c.request.Header.Get(HeaderContentType), MIMEMultipartForm) {
329 if err := c.request.ParseMultipartForm(defaultMemory); err != nil {
333 if err := c.request.ParseForm(); err != nil {
337 return c.request.Form, nil
340 func (c *context) FormFile(name string) (*multipart.FileHeader, error) {
341 _, fh, err := c.request.FormFile(name)
345 func (c *context) MultipartForm() (*multipart.Form, error) {
346 err := c.request.ParseMultipartForm(defaultMemory)
347 return c.request.MultipartForm, err
350 func (c *context) Cookie(name string) (*http.Cookie, error) {
351 return c.request.Cookie(name)
354 func (c *context) SetCookie(cookie *http.Cookie) {
355 http.SetCookie(c.Response(), cookie)
358 func (c *context) Cookies() []*http.Cookie {
359 return c.request.Cookies()
362 func (c *context) Get(key string) interface{} {
366 func (c *context) Set(key string, val interface{}) {
373 func (c *context) Bind(i interface{}) error {
374 return c.echo.Binder.Bind(i, c)
377 func (c *context) Validate(i interface{}) error {
378 if c.echo.Validator == nil {
379 return ErrValidatorNotRegistered
381 return c.echo.Validator.Validate(i)
384 func (c *context) Render(code int, name string, data interface{}) (err error) {
385 if c.echo.Renderer == nil {
386 return ErrRendererNotRegistered
388 buf := new(bytes.Buffer)
389 if err = c.echo.Renderer.Render(buf, name, data, c); err != nil {
392 return c.HTMLBlob(code, buf.Bytes())
395 func (c *context) HTML(code int, html string) (err error) {
396 return c.HTMLBlob(code, []byte(html))
399 func (c *context) HTMLBlob(code int, b []byte) (err error) {
400 return c.Blob(code, MIMETextHTMLCharsetUTF8, b)
403 func (c *context) String(code int, s string) (err error) {
404 return c.Blob(code, MIMETextPlainCharsetUTF8, []byte(s))
407 func (c *context) jsonPBlob(code int, callback string, i interface{}) (err error) {
408 enc := json.NewEncoder(c.response)
409 _, pretty := c.QueryParams()["pretty"]
410 if c.echo.Debug || pretty {
411 enc.SetIndent("", " ")
413 c.writeContentType(MIMEApplicationJavaScriptCharsetUTF8)
414 c.response.WriteHeader(code)
415 if _, err = c.response.Write([]byte(callback + "(")); err != nil {
418 if err = enc.Encode(i); err != nil {
421 if _, err = c.response.Write([]byte(");")); err != nil {
427 func (c *context) json(code int, i interface{}, indent string) error {
428 enc := json.NewEncoder(c.response)
430 enc.SetIndent("", indent)
432 c.writeContentType(MIMEApplicationJSONCharsetUTF8)
433 c.response.WriteHeader(code)
437 func (c *context) JSON(code int, i interface{}) (err error) {
439 if _, pretty := c.QueryParams()["pretty"]; c.echo.Debug || pretty {
440 indent = defaultIndent
442 return c.json(code, i, indent)
445 func (c *context) JSONPretty(code int, i interface{}, indent string) (err error) {
446 return c.json(code, i, indent)
449 func (c *context) JSONBlob(code int, b []byte) (err error) {
450 return c.Blob(code, MIMEApplicationJSONCharsetUTF8, b)
453 func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
454 return c.jsonPBlob(code, callback, i)
457 func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
458 c.writeContentType(MIMEApplicationJavaScriptCharsetUTF8)
459 c.response.WriteHeader(code)
460 if _, err = c.response.Write([]byte(callback + "(")); err != nil {
463 if _, err = c.response.Write(b); err != nil {
466 _, err = c.response.Write([]byte(");"))
470 func (c *context) xml(code int, i interface{}, indent string) (err error) {
471 c.writeContentType(MIMEApplicationXMLCharsetUTF8)
472 c.response.WriteHeader(code)
473 enc := xml.NewEncoder(c.response)
475 enc.Indent("", indent)
477 if _, err = c.response.Write([]byte(xml.Header)); err != nil {
483 func (c *context) XML(code int, i interface{}) (err error) {
485 if _, pretty := c.QueryParams()["pretty"]; c.echo.Debug || pretty {
486 indent = defaultIndent
488 return c.xml(code, i, indent)
491 func (c *context) XMLPretty(code int, i interface{}, indent string) (err error) {
492 return c.xml(code, i, indent)
495 func (c *context) XMLBlob(code int, b []byte) (err error) {
496 c.writeContentType(MIMEApplicationXMLCharsetUTF8)
497 c.response.WriteHeader(code)
498 if _, err = c.response.Write([]byte(xml.Header)); err != nil {
501 _, err = c.response.Write(b)
505 func (c *context) Blob(code int, contentType string, b []byte) (err error) {
506 c.writeContentType(contentType)
507 c.response.WriteHeader(code)
508 _, err = c.response.Write(b)
512 func (c *context) Stream(code int, contentType string, r io.Reader) (err error) {
513 c.writeContentType(contentType)
514 c.response.WriteHeader(code)
515 _, err = io.Copy(c.response, r)
519 func (c *context) File(file string) (err error) {
520 f, err := os.Open(file)
522 return NotFoundHandler(c)
528 file = filepath.Join(file, indexPage)
529 f, err = os.Open(file)
531 return NotFoundHandler(c)
534 if fi, err = f.Stat(); err != nil {
538 http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
542 func (c *context) Attachment(file, name string) error {
543 return c.contentDisposition(file, name, "attachment")
546 func (c *context) Inline(file, name string) error {
547 return c.contentDisposition(file, name, "inline")
550 func (c *context) contentDisposition(file, name, dispositionType string) error {
551 c.response.Header().Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%q", dispositionType, name))
555 func (c *context) NoContent(code int) error {
556 c.response.WriteHeader(code)
560 func (c *context) Redirect(code int, url string) error {
561 if code < 300 || code > 308 {
562 return ErrInvalidRedirectCode
564 c.response.Header().Set(HeaderLocation, url)
565 c.response.WriteHeader(code)
569 func (c *context) Error(err error) {
570 c.echo.HTTPErrorHandler(err, c)
573 func (c *context) Echo() *Echo {
577 func (c *context) Handler() HandlerFunc {
581 func (c *context) SetHandler(h HandlerFunc) {
585 func (c *context) Logger() Logger {
589 func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
593 c.handler = NotFoundHandler
597 // NOTE: Don't reset because it has to have length c.echo.maxParam at all times