Initial Commit
[ancientarts.git] / api / .rocks / share / tarantool / rocks / http / scm-1 / doc / README.md
1 <a href="http://tarantool.org">
2 <img src="https://avatars2.githubusercontent.com/u/2344919?v=2&s=250"
3 align="right">
4 </a>
5
6 # HTTP server for Tarantool 1.7.5+
7
8 [![Build Status](https://travis-ci.org/tarantool/http.png?branch=tarantool-1.7)](https://travis-ci.org/tarantool/http)
9
10 > **Note:** In Tarantool 1.7.5+, a full-featured HTTP client is available aboard.
11 > For Tarantool 1.6.5+, both HTTP server and client are available
12 > [here](https://github.com/tarantool/http/tree/tarantool-1.6).
13
14 ## Table of contents
15
16 * [Prerequisites](#prerequisites)
17 * [Installation](#installation)
18 * [Usage](#usage)
19 * [Creating a server](#creating-a-server)
20 * [Using routes](#using-routes)
21 * [Contents of app\_dir](#contents-of-app_dir)
22 * [Route handlers](#route-handlers)
23 * [Fields and methods of the Request object](#fields-and-methods-of-the-request-object)
24 * [Fields and methods of the Response object](#fields-and-methods-of-the-response-object)
25 * [Examples](#examples)
26 * [Working with stashes](#working-with-stashes)
27 * [Special stash names](#special-stash-names)
28 * [Working with cookies](#working-with-cookies)
29 * [Rendering a template](#rendering-a-template)
30 * [Template helpers](#template-helpers)
31 * [Hooks](#hooks)
32 * [handler(httpd, req)](#handlerhttpd-req)
33 * [before\_dispatch(httpd, req)](#before_dispatchhttpd-req)
34 * [after\_dispatch(cx, resp)](#after_dispatchcx-resp)
35 * [See also](#see-also)
36
37 ## Prerequisites
38
39 * Tarantool 1.7.5+ with header files (`tarantool` && `tarantool-dev` packages)
40
41 ## Installation
42
43 You can:
44
45 * clone the repository and build the `http` module using CMake:
46
47 ``` bash
48 git clone https://github.com/tarantool/http.git
49 cd http && cmake . -DCMAKE_BUILD_TYPE=RelWithDebugInfo
50 make
51 make install
52 ```
53
54 * install the `http` module using `tarantoolctl`:
55
56 ``` bash
57 tarantoolctl rocks install http
58 ```
59
60 * install the `http` module using LuaRocks
61 (see [TarantoolRocks](https://github.com/tarantool/rocks) for
62 LuaRocks configuration details):
63
64 ``` bash
65 luarocks install https://raw.githubusercontent.com/tarantool/http/master/http-scm-1.rockspec --local
66 ```
67
68 ## Usage
69
70 The server is an object which is configured with HTTP request
71 handlers, routes (paths), templates, and a port to bind to.
72 Unless Tarantool is running under a superuser, port numbers
73 below 1024 may be unavailable.
74
75 The server can be started and stopped anytime. Multiple
76 servers can be created.
77
78 To start a server:
79
80 1. [Create it](#creating-a-server) with `httpd = require('http.server').new(...)`.
81 2. [Configure routing](#using-routes) with `httpd:route(...)`.
82 3. Start it with `httpd:start()`.
83
84 To stop the server, use `httpd:stop()`.
85
86 ## Creating a server
87
88 ```lua
89 httpd = require('http.server').new(host, port[, { options } ])
90 ```
91
92 `host` and `port` must contain the interface and port to bind to.
93
94 `options` may contain:
95
96 * `max_header_size` (default is 4096 bytes) - a limit for
97 HTTP request header size.
98 * `header_timeout` (default: 100 seconds) - a timeout until
99 the server stops reading HTTP headers sent by the client.
100 The server closes the client connection if the client doesn't
101 send its headers within the given amount of time.
102 * `app_dir` (default is '.', the server working directory) -
103 a path to the directory with HTML templates and controllers.
104 * `handler` - a Lua function to handle HTTP requests (this is
105 a handler to use if the module "routing" functionality is not
106 needed).
107 * `charset` - the character set for server responses of
108 type `text/html`, `text/plain` and `application/json`.
109 * `display_errors` - return application errors and backtraces to the client
110 (like PHP).
111 * `log_errors` - log application errors using `log.error()`.
112 * `log_requests` - log incoming requests.
113
114 ## Using routes
115
116 It is possible to automatically route requests between different
117 handlers, depending on the request path. The routing API is inspired
118 by [Mojolicious](http://mojolicio.us/perldoc/Mojolicious/Guides/Routing) API.
119
120 Routes can be defined using:
121
122 * an exact match (e.g. "index.php")
123 * simple regular expressions
124 * extended regular expressions
125
126 Route examples:
127
128 ```text
129 '/' -- a simple route
130 '/abc' -- a simple route
131 '/abc/:cde' -- a route using a simple regular expression
132 '/abc/:cde/:def' -- a route using a simple regular expression
133 '/ghi*path' -- a route using an extended regular expression
134 ```
135
136 To configure a route, use the `route()` method of the `httpd` object:
137
138 ```lua
139 httpd:route({ path = '/path/to' }, 'controller#action')
140 httpd:route({ path = '/', template = 'Hello <%= var %>' }, handle1)
141 httpd:route({ path = '/:abc/cde', file = 'users.html.el' }, handle2)
142 httpd:route({ path = '/objects', method = 'GET' }, handle3)
143 ...
144 ```
145
146 The first argument for `route()` is a Lua table with one or more keys:
147
148 * `file` - a template file name (can be relative to.
149 `{app_dir}/templates`, where `app_dir` is the path set when creating the
150 server). If no template file name extension is provided, the extension is
151 set to ".html.el", meaning HTML with embedded Lua.
152 * `template` - template Lua variable name, in case the template
153 is a Lua variable. If `template` is a function, it's called on every
154 request to get template body. This is useful if template body must be
155 taken from a database.
156 * `path` - route path, as described earlier.
157 * `name` - route name.
158 * `method` - method on the route like `POST`, `GET`, `PUT`, `DELETE`
159
160 The second argument is the route handler to be used to produce
161 a response to the request.
162
163 The typical usage is to avoid passing `file` and `template` arguments,
164 since they take time to evaluate, but these arguments are useful
165 for writing tests or defining HTTP servers with just one "route".
166
167 The handler can also be passed as a string of the form 'filename#functionname'.
168 In that case, the handler body is taken from a file in the
169 `{app_dir}/controllers` directory.
170
171 ## Contents of `app_dir`
172
173 * `public` - a path to static content. Everything stored on this path
174 defines a route which matches the file name, and the HTTP server serves this
175 file automatically, as is. Notice that the server doesn't use `sendfile()`,
176 and it reads the entire content of the file into the memory before passing
177 it to the client. ??? Caching is not used, unless turned on. So this is not
178 suitable for large files, use nginx instead.
179 * `templates` - a path to templates.
180 * `controllers` - a path to *.lua files with Lua controllers. For example,
181 the controller name 'module.submodule#foo' is mapped to
182 `{app_dir}/controllers/module.submodule.lua`.
183
184 ## Route handlers
185
186 A route handler is a function which accepts one argument (**Request**) and
187 returns one value (**Response**).
188
189 ```lua
190 function my_handler(req)
191 -- req is a Request object
192 -- resp is a Response object
193 local resp = req:render({text = req.method..' '..req.path })
194 resp.headers['x-test-header'] = 'test';
195 resp.status = 201
196 return resp
197 end
198 ```
199
200 ### Fields and methods of the Request object
201
202 * `req.method` - HTTP request type (`GET`, `POST` etc).
203 * `req.path` - request path.
204 * `req.query` - request arguments.
205 * `req.proto` - HTTP version (for example, `{ 1, 1 }` is `HTTP/1.1`).
206 * `req.headers` - normalized request headers. A normalized header
207 is in the lower case, all headers joined together into a single string.
208 * `req.peer` - a Lua table with information about the remote peer
209 (like `socket:peer()`).
210 * `tostring(req)` - returns a string representation of the request.
211 * `req:request_line()` - returns the request body.
212 * `req:read(delimiter|chunk|{delimiter = x, chunk = x}, timeout)` - reads the
213 raw request body as a stream (see `socket:read()`).
214 * `req:json()` - returns a Lua table from a JSON request.
215 * `req:post_param(name)` - returns a single POST request a parameter value.
216 If `name` is `nil`, returns all parameters as a Lua table.
217 * `req:query_param(name)` - returns a single GET request parameter value.
218 If `name` is `nil`, returns a Lua table with all arguments.
219 * `req:param(name)` - any request parameter, either GET or POST.
220 * `req:cookie(name)` - to get a cookie in the request.
221 * `req:stash(name[, value])` - get or set a variable "stashed"
222 when dispatching a route.
223 * `req:url_for(name, args, query)` - returns the route's exact URL.
224 * `req:render({})` - create a **Response** object with a rendered template.
225 * `req:redirect_to` - create a **Response** object with an HTTP redirect.
226
227 ### Fields and methods of the Response object
228
229 * `resp.status` - HTTP response code.
230 * `resp.headers` - a Lua table with normalized headers.
231 * `resp.body` - response body (string|table|wrapped\_iterator).
232 * `resp:setcookie({ name = 'name', value = 'value', path = '/', expires = '+1y', domain = 'example.com'))` -
233 adds `Set-Cookie` headers to `resp.headers`.
234
235 ### Examples
236
237 ```lua
238 function my_handler(req)
239 return {
240 status = 200,
241 headers = { ['content-type'] = 'text/html; charset=utf8' },
242 body = [[
243 <html>
244 <body>Hello, world!</body>
245 </html>
246 ]]
247 }
248 end
249 ```
250
251 ## Working with stashes
252
253 ```lua
254 function hello(self)
255 local id = self:stash('id') -- here is :id value
256 local user = box.space.users:select(id)
257 if user == nil then
258 return self:redirect_to('/users_not_found')
259 end
260 return self:render({ user = user })
261 end
262
263 httpd = box.httpd.new('127.0.0.1', 8080)
264 httpd:route(
265 { path = '/:id/view', template = 'Hello, <%= user.name %>' }, hello)
266 httpd:start()
267 ```
268
269 ### Special stash names
270
271 * `controller` - the controller name.
272 * `action` - the handler name in the controller.
273 * `format` - the current output format (e.g. `html`, `txt`). Is
274 detected automatically based on the request's `path` (for example, `/abc.js`
275 sets `format` to `js`). When producing a response, `format` is used
276 to serve the response's 'Content-type:'.
277
278 ## Working with cookies
279
280 To get a cookie, use:
281
282 ```lua
283 function show_user(self)
284
285 local uid = self:cookie('id')
286
287 if uid ~= nil and string.match(uid, '^%d$') ~= nil then
288
289 local user = box.select(users, 0, uid)
290 return self:render({ user = user })
291 end
292
293 return self:redirect_to('/login')
294 end
295 ```
296
297 To set a cookie, use the `cookie()` method as well, but pass to it a Lua
298 table defining the cookie to be set:
299
300 ```lua
301 function user_login(self)
302
303 local login = self:param('login')
304 local password = self:param('password')
305
306 local user = box.select(users, 1, login, password)
307 if user ~= nil then
308 return self:redirect_to('/'):
309 set_cookie({ name = 'uid', value = user[0], expires = '+1y' })
310 end
311
312 -- to login again and again and again
313 return self:redirect_to('/login')
314 end
315 ```
316
317 The table must contain the following fields:
318
319 * `name`
320 * `value`
321 * `path` (optional; if not set, the current request path is used)
322 * `domain` (optional)
323 * `expires` - cookie expire date, or expire offset, for example:
324
325 * `1d` - 1 day
326 * `+1d` - the same
327 * `23d` - 23 days
328 * `+1m` - 1 month (30 days)
329 * `+1y` - 1 year (365 days)
330
331 ## Rendering a template
332
333 Lua can be used inside a response template, for example:
334
335 ```html
336 <html>
337 <head>
338 <title><%= title %></title>
339 </head>
340 <body>
341 <ul>
342 % for i = 1, 10 do
343 <li><%= item[i].key %>: <%= item[i].value %></li>
344 % end
345 </ul>
346 </body>
347 </html>
348 ```
349
350 To embed Lua code into a template, use:
351
352 * `<% lua-here %>` - insert any Lua code, including multi-line.
353 Can be used anywhere in the template.
354 * `% lua-here` - a single-line Lua substitution. Can only be
355 present at the beginning of a line (with optional preceding spaces
356 and tabs, which are ignored).
357
358 A few control characters may follow `%`:
359
360 * `=` (e.g., `<%= value + 1 %>`) - runs the embedded Lua code
361 and inserts the result into HTML. Special HTML characters,
362 such as `<`, `>`, `&`, `"`, are escaped.
363 * `==` (e.g., `<%== value + 10 %>`) - the same, but without
364 escaping.
365
366 A Lua statement inside the template has access to the following
367 environment:
368
369 1. Lua variables defined in the template,
370 1. stashed variables,
371 1. variables standing for keys in the `render` table.
372
373 ## Template helpers
374
375 Helpers are special functions that are available in all HTML
376 templates. These functions must be defined when creating an `httpd` object.
377
378 Setting or deleting a helper:
379
380 ```lua
381 -- setting a helper
382 httpd:helper('time', function(self, ...) return box.time() end)
383 -- deleting a helper
384 httpd:helper('some_name', nil)
385 ```
386
387 Using a helper inside an HTML template:
388
389 ```html
390 <div>
391 Current timestamp: <%= time() %>
392 </div>
393 ```
394
395 A helper function can receive arguments. The first argument is
396 always the current controller. The rest is whatever is
397 passed to the helper from the template.
398
399 ## Hooks
400
401 It is possible to define additional functions invoked at various
402 stages of request processing.
403
404 ### `handler(httpd, req)`
405
406 If `handler` is present in `httpd` options, it gets
407 involved on every HTTP request, and the built-in routing
408 mechanism is unused (no other hooks are called in this case).
409
410 ### `before_dispatch(httpd, req)`
411
412 Is invoked before a request is routed to a handler. The first
413 argument of the hook is the HTTP request to be handled.
414 The return value of the hook is ignored.
415
416 This hook could be used to log a request, or modify request headers.
417
418 ### `after_dispatch(cx, resp)`
419
420 Is invoked after a handler for a route is executed.
421
422 The arguments of the hook are the request passed into the handler,
423 and the response produced by the handler.
424
425 This hook can be used to modify the response.
426 The return value of the hook is ignored.
427
428 ## See also
429
430 * [Tarantool project][Tarantool] on GitHub
431 * [Tests][] for the `http` module
432
433 [Tarantool]: http://github.com/tarantool/tarantool
434 [Tests]: https://github.com/tarantool/http/tree/tarantool-1.7/test