@@ -22,40 +22,108 @@ import (
2222)
2323
2424type (
25- // Context represents context for the current request. It holds request and
26- // response objects, path parameters, data and registered handler.
25+ // Context represents the context of the current HTTP request. It holds request and
26+ // response objects, path, path parameters, data and registered handler.
2727 Context interface {
2828 netContext.Context
29+
30+ // NetContext returns `http://blog.golang.org/context.Context` interface.
2931 NetContext () netContext.Context
32+
33+ // SetNetContext sets `http://blog.golang.org/context.Context` interface.
3034 SetNetContext (netContext.Context )
35+
36+ // Request returns `engine.Request` interface.
3137 Request () engine.Request
38+
39+ // Request returns `engine.Response` interface.
3240 Response () engine.Response
41+
42+ // Path returns the registered path for the handler.
3343 Path () string
44+
45+ // P returns path parameter by index.
3446 P (int ) string
47+
48+ // Param returns path parameter by name.
3549 Param (string ) string
50+
51+ // ParamNames returns path parameter names.
3652 ParamNames () []string
53+
54+ // Query returns query parameter by name.
3755 Query (string ) string
56+
57+ // Form returns form parameter by name.
3858 Form (string ) string
59+
60+ // Get retrieves data from the context.
3961 Get (string ) interface {}
62+
63+ // Set saves data in the context.
4064 Set (string , interface {})
65+
66+ // Bind binds the request body into provided type `i`. The default binder does
67+ // it based on Content-Type header.
4168 Bind (interface {}) error
69+
70+ // Render renders a template with data and sends a text/html response with status
71+ // code. Templates can be registered using `Echo.SetRenderer()`.
4272 Render (int , string , interface {}) error
73+
74+ // HTML sends an HTTP response with status code.
4375 HTML (int , string ) error
76+
77+ // String sends a string response with status code.
4478 String (int , string ) error
79+
80+ // JSON sends a JSON response with status code.
4581 JSON (int , interface {}) error
82+
83+ // JSONBlob sends a JSON blob response with status code.
4684 JSONBlob (int , []byte ) error
85+
86+ // JSONP sends a JSONP response with status code. It uses `callback` to construct
87+ // the JSONP payload.
4788 JSONP (int , string , interface {}) error
89+
90+ // XML sends an XML response with status code.
4891 XML (int , interface {}) error
92+
93+ // XMLBlob sends a XML blob response with status code.
4994 XMLBlob (int , []byte ) error
95+
96+ // File sends a response with the content of the file.
5097 File (string ) error
98+
99+ // Attachment sends a response from `io.Reader` as attachment, prompting client
100+ // to save the file.
51101 Attachment (io.Reader , string ) error
102+
103+ // NoContent sends a response with no body and a status code.
52104 NoContent (int ) error
105+
106+ // Redirect redirects the request with status code.
53107 Redirect (int , string ) error
108+
109+ // Error invokes the registered HTTP error handler. Generally used by middleware.
54110 Error (err error )
111+
112+ // Handler implements `Handler` interface.
55113 Handle (Context ) error
114+
115+ // Logger returns the `Logger` instance.
56116 Logger () * log.Logger
117+
118+ // Echo returns the `Echo` instance.
57119 Echo () * Echo
120+
121+ // Object returns the `context` instance.
58122 Object () * context
123+
124+ // Reset resets the context after request completes. It must be called along
125+ // with `Echo#GetContext()` and `Echo#PutContext()`. See `Echo#ServeHTTP()`
126+ Reset (engine.Request , engine.Response )
59127 }
60128
61129 context struct {
@@ -118,22 +186,18 @@ func (c *context) Handle(ctx Context) error {
118186 return c .handler .Handle (ctx )
119187}
120188
121- // Request returns *http.Request.
122189func (c * context ) Request () engine.Request {
123190 return c .request
124191}
125192
126- // Response returns `engine.Response`.
127193func (c * context ) Response () engine.Response {
128194 return c .response
129195}
130196
131- // Path returns the registered path for the handler.
132197func (c * context ) Path () string {
133198 return c .path
134199}
135200
136- // P returns path parameter by index.
137201func (c * context ) P (i int ) (value string ) {
138202 l := len (c .pnames )
139203 if i < l {
@@ -142,7 +206,6 @@ func (c *context) P(i int) (value string) {
142206 return
143207}
144208
145- // Param returns path parameter by name.
146209func (c * context ) Param (name string ) (value string ) {
147210 l := len (c .pnames )
148211 for i , n := range c .pnames {
@@ -154,42 +217,33 @@ func (c *context) Param(name string) (value string) {
154217 return
155218}
156219
157- // ParamNames returns path parameter names.
158220func (c * context ) ParamNames () []string {
159221 return c .pnames
160222}
161223
162- // Query returns query parameter by name.
163224func (c * context ) Query (name string ) string {
164225 return c .request .URL ().QueryValue (name )
165226}
166227
167- // Form returns form parameter by name.
168228func (c * context ) Form (name string ) string {
169229 return c .request .FormValue (name )
170230}
171231
172- // Set saves data in the context.
173232func (c * context ) Set (key string , val interface {}) {
174233 if c .store == nil {
175234 c .store = make (store )
176235 }
177236 c .store [key ] = val
178237}
179238
180- // Get retrieves data from the context.
181239func (c * context ) Get (key string ) interface {} {
182240 return c .store [key ]
183241}
184242
185- // Bind binds the request body into provided type `i`. The default binder does
186- // it based on Content-Type header.
187243func (c * context ) Bind (i interface {}) error {
188244 return c .echo .binder .Bind (i , c )
189245}
190246
191- // Render renders a template with data and sends a text/html response with status
192- // code. Templates can be registered using `Echo.SetRenderer()`.
193247func (c * context ) Render (code int , name string , data interface {}) (err error ) {
194248 if c .echo .renderer == nil {
195249 return ErrRendererNotRegistered
@@ -204,23 +258,20 @@ func (c *context) Render(code int, name string, data interface{}) (err error) {
204258 return
205259}
206260
207- // HTML sends an HTTP response with status code.
208261func (c * context ) HTML (code int , html string ) (err error ) {
209262 c .response .Header ().Set (ContentType , TextHTMLCharsetUTF8 )
210263 c .response .WriteHeader (code )
211264 _ , err = c .response .Write ([]byte (html ))
212265 return
213266}
214267
215- // String sends a string response with status code.
216268func (c * context ) String (code int , s string ) (err error ) {
217269 c .response .Header ().Set (ContentType , TextPlainCharsetUTF8 )
218270 c .response .WriteHeader (code )
219271 _ , err = c .response .Write ([]byte (s ))
220272 return
221273}
222274
223- // JSON sends a JSON response with status code.
224275func (c * context ) JSON (code int , i interface {}) (err error ) {
225276 b , err := json .Marshal (i )
226277 if c .echo .Debug () {
@@ -232,16 +283,13 @@ func (c *context) JSON(code int, i interface{}) (err error) {
232283 return c .JSONBlob (code , b )
233284}
234285
235- // JSONBlob sends a JSON blob response with status code.
236286func (c * context ) JSONBlob (code int , b []byte ) (err error ) {
237287 c .response .Header ().Set (ContentType , ApplicationJSONCharsetUTF8 )
238288 c .response .WriteHeader (code )
239289 _ , err = c .response .Write (b )
240290 return
241291}
242292
243- // JSONP sends a JSONP response with status code. It uses `callback` to construct
244- // the JSONP payload.
245293func (c * context ) JSONP (code int , callback string , i interface {}) (err error ) {
246294 b , err := json .Marshal (i )
247295 if err != nil {
@@ -259,7 +307,6 @@ func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
259307 return
260308}
261309
262- // XML sends an XML response with status code.
263310func (c * context ) XML (code int , i interface {}) (err error ) {
264311 b , err := xml .Marshal (i )
265312 if c .echo .Debug () {
@@ -271,7 +318,6 @@ func (c *context) XML(code int, i interface{}) (err error) {
271318 return c .XMLBlob (code , b )
272319}
273320
274- // XMLBlob sends a XML blob response with status code.
275321func (c * context ) XMLBlob (code int , b []byte ) (err error ) {
276322 c .response .Header ().Set (ContentType , ApplicationXMLCharsetUTF8 )
277323 c .response .WriteHeader (code )
@@ -282,7 +328,6 @@ func (c *context) XMLBlob(code int, b []byte) (err error) {
282328 return
283329}
284330
285- // File sends a response with the content of the file.
286331func (c * context ) File (file string ) error {
287332 root , file := filepath .Split (file )
288333 fs := http .Dir (root )
@@ -305,8 +350,6 @@ func (c *context) File(file string) error {
305350 return ServeContent (c .Request (), c .Response (), f , fi )
306351}
307352
308- // Attachment sends a response from `io.Reader` as attachment, prompting client
309- // to save the file.
310353func (c * context ) Attachment (r io.Reader , name string ) (err error ) {
311354 c .response .Header ().Set (ContentType , detectContentType (name ))
312355 c .response .Header ().Set (ContentDisposition , "attachment; filename=" + name )
@@ -315,13 +358,11 @@ func (c *context) Attachment(r io.Reader, name string) (err error) {
315358 return
316359}
317360
318- // NoContent sends a response with no body and a status code.
319361func (c * context ) NoContent (code int ) error {
320362 c .response .WriteHeader (code )
321363 return nil
322364}
323365
324- // Redirect redirects the request with status code.
325366func (c * context ) Redirect (code int , url string ) error {
326367 if code < http .StatusMultipleChoices || code > http .StatusTemporaryRedirect {
327368 return ErrInvalidRedirectCode
@@ -331,26 +372,24 @@ func (c *context) Redirect(code int, url string) error {
331372 return nil
332373}
333374
334- // Error invokes the registered HTTP error handler. Generally used by middleware.
335375func (c * context ) Error (err error ) {
336376 c .echo .httpErrorHandler (err , c )
337377}
338378
339- // Echo returns the `Echo` instance.
340379func (c * context ) Echo () * Echo {
341380 return c .echo
342381}
343382
344- // Logger returns the `Logger` instance.
345383func (c * context ) Logger () * log.Logger {
346384 return c .echo .logger
347385}
348386
349- // Object returns the `context` object.
350387func (c * context ) Object () * context {
351388 return c
352389}
353390
391+ // ServeContent sends a response from `io.Reader`. It automatically sets the `Content-Type`
392+ // and `Last-Modified` headers.
354393func ServeContent (req engine.Request , res engine.Response , f http.File , fi os.FileInfo ) error {
355394 res .Header ().Set (ContentType , detectContentType (fi .Name ()))
356395 res .Header ().Set (LastModified , fi .ModTime ().UTC ().Format (http .TimeFormat ))
@@ -366,7 +405,7 @@ func detectContentType(name string) (t string) {
366405 return
367406}
368407
369- func (c * context ) reset (req engine.Request , res engine.Response ) {
408+ func (c * context ) Reset (req engine.Request , res engine.Response ) {
370409 c .netContext = nil
371410 c .request = req
372411 c .response = res
0 commit comments