moonbridge

view reference.txt @ 50:0dd15d642124

Proper handling of I/O errors; Added property "request.faulty"; Removed "io_error_handler" hook; Added documentation for global function "timeout"
author jbe
date Fri Mar 20 02:27:28 2015 +0100 (2015-03-20)
parents e835cda61478
children 042bb4854aa6
line source
2 Moonbridge reference
3 ====================
7 Global function listen(...)
8 ---------------------------
10 This function initializes the Moonbridge Network Server. It may be called
11 multiple times. However, it is not allowed to register additional listeners by
12 calling listen(...) from a "prepare", "connect", or "finish" handler.
14 See file "example.lua" for parametrization of the listen(...) function.
16 Warning: Moonbridge will fork the Lua environment to handle parallel requests.
17 Functions provided as "prepare", "connect", and "finish" handlers may access
18 global variables, but for every child process these global variables will not
19 be shared! If you require a global state, a DBMS, cache server, or similar is
20 necessary.
24 Global function timeout(...)
25 ----------------------------
27 Calling this function with a positive number (time in seconds) sets a timer
28 that kills the current process after the selected time runs out. The remaining
29 time can be queried by calling this function without arguments.
31 Calling this function with a single argument that is the number zero will
32 disable the timeout.
34 Another mode of operation is selected by passing two arguments: a time (in
35 seconds) as first argument and a function as second argument. In this case, a
36 sub-timer will be used to limit the execution time of the function. In case of
37 timeout, the process will be killed (and the timeout function does not return).
38 If the time for the sub-timer is longer than a previously set timeout (using
39 the timeout(...) function with one argument), the shorter timeout (of the
40 previous call of timeout(...)) will have precedence.
44 Socket object passed to "connect" handler
45 -----------------------------------------
47 For every incoming connection, the registered "connect" handler is called with
48 a single socket object as argument, which is described below:
51 ### socket:cancel()
53 Closes the socket connection by sending a TCP RST package if possible to
54 indicate error condition. Returns true on success, or nil plus error message in
55 case of an I/O error. Using this method on sockets that have already been
56 closed (or canceled) will throw an error.
58 Warning: Previously sent (and flushed) data may be lost during transmission.
61 ### socket:close(timeout)
63 Closes the socket connection (input and output stream) by flushing all data and
64 sending a TCP FIN package. Returns true on success, or nil plus error message
65 in case of an I/O error. Using this method on sockets that have already been
66 closed (or canceled) will throw an error.
68 Warning: Pending data on the input stream may cause connection aborts (TCP RST)
69 depending on the particular operating system used. All pending input data
70 should have been read before calling socket:close().
72 The optional timeout parameter may be used to wait until all data has been sent
73 out, or until the timeout elapses (in which case a TCP RST is sent) whichever
74 happens first. A timeout value of 0 or nil causes immediate return and sending
75 of pending data in background (recommended).
78 ### socket:flush()
80 Alias for socket.output:flush()
83 ### socket.input
85 Lua file handle representing the input stream of the socket connection.
86 Supports the same methods as io.open()'s return values.
89 ### socket.interval
91 Set to the name of an interval timer if the "connect" handler was called due to
92 an elapsed interval timer. Otherwise nil.
95 ### socket:lines()
97 Alias for socket.input:lines()
100 ### socket.local_ip4
102 Local IPv4 address used for the connection. Encoded as 4 raw bytes in form of a
103 string.
106 ### socket.local_ip6
108 Local IPv6 address used for the connection. Encoded as 16 raw bytes in form of
109 a string.
112 ### socket.local_tcpport
114 Local TCP port used for the connection.
117 ### socket.output
119 Lua file handle representing the output stream of the socket connection.
120 Supports the same methods as io.open()'s return values.
123 ### socket:read(...)
125 Alias for socket.input:read()
128 ### socket:readuntil(terminator, maxlen)
130 Reads as many bytes until a byte equal to the terminator value occurs. An
131 optional maximum length may be specified. The terminating byte is included in
132 the return value (unless the maximum length would be exceeded). On EOF, nil is
133 returned. In case of an I/O error, nil (as first result value) plus an error
134 message (as second result value) is returned.
136 This method is also available as :readuntil(...) for any other Lua file handle
137 (including socket.input).
140 ### socket.remote_ip4
142 Remote IPv4 address used for the connection. Encoded as 4 raw bytes in form of
143 a string.
146 ### socket.remote_ip6
148 Remote IPv6 address used for the connection. Encoded as 16 raw bytes in form of
149 a string.
152 ### socket.remote_tcpport
154 Remote TCP port used for the connection.
157 ### socket:write(...)
159 Alias for socket.output:write(...)
163 HTTP module
164 -----------
166 The http module exports the function http.generate_handler(callback) that
167 converts an HTTP handler to a "connect" handler. See file "example.lua" for an
168 example of invocation. A table with options may be passed either as a second
169 argument, or as a first argument preceeding the callback function (whichever is
170 more convenient).
172 The following options are supported:
174 - request_body_size_limit: maximum size of payload of HTTP request body
175 (transfer encoding is allowed to add a limited amount of extra data)
176 - chunk_size: optional default value for maximum_input_chunk_size and
177 minimum_output_chunk_size
178 - request_header_size_limit: maximum size of HTTP request headers
179 - maximum_input_chunk_size: maximum chunk size when streaming a request body or
180 certain POST fields (bigger chunks will be fragmented automatically)
181 - minimum_output_chunk_size: minimum size for a chunk when sending a response
182 body (smaller chunks will be buffered and concatenated with future data;
183 ignored when request:flush() is called)
184 - static_headers: a set of headers to be included in every HTTP response
185 (may be a string, a table or strings, or a table of key-value pairs)
187 The callback function receives a single request object as argument, which is
188 described below.
191 ### request.body
193 The request body (without headers) as a string. Accessing this value makes
194 further access to request.post_params and request.post_params_list, or
195 invocation of request:stream_request_body(...) impossible.
198 ### request.cookies
200 A table with all cookies sent by the client.
203 ### request.defer_reading()
205 Disables automatic request body processing on write. Can be called before
206 sending a HTTP status code to send a response before the request has been fully
207 received.
209 CAUTION: Responding to a request before the request body has been processed may
210 lead to a deadlock if the browser does not process the response while trying to
211 send the request. Therefore, this function should only be used if:
213 - the TCP stack has enough buffer space for the response (i.e. if the response
214 is small enough), and if
215 - a timer is used to cancel the response in case of a deadlock.
217 It is recommended to not use this function unless certain performance tweaks
218 are desired.
221 ### request.faulty
223 Normally set to false. In case of a read or write error on the client
224 connection, this value is set to true before a Lua error is raised.
226 A faulty request handle must not be used, or another Lua error will be raised.
229 ### request:finish()
231 Finishes and flushes a HTTP response. May be called multiple times. An
232 HTTP status, all headers, and the response body (if applicable) must have been
233 previously sent. After calling this method, no further data may be written.
236 ### request:finish_headers()
238 Finishes and flushes the HTTP response header section. May be called multiple
239 times, as long as the request is not finished completely. This method is
240 automatically invoked if the application is beginning to send a response body.
241 After calling this method, no further headers may be sent.
244 ### request:flush()
246 Flushes any pending output data. Note: In order to mark the end of a response
247 body, it is required to call request:finish().
250 ### request.get_params
252 A table that maps field names to their corresponding GET value. If there are
253 several GET values with the given field name, then the first value is used.
255 Note: May be implemented through metamethods, but does support iteration
256 through pairs(...).
259 ### request.get_params_list
261 A table that maps field names to a sequence of their corresponding GET values.
263 Note: May be implemented through metamethods, but does support iteration
264 through pairs(...).
267 ### request.headers
269 A table that maps (case-insensitively) a HTTP header field name to a sequence
270 of values. One entry is created for every occurrence of a header line with the
271 given field name).
274 ### request.headers_csv_string
276 A table that maps (case-insensitively) a HTTP header field name to a comma
277 separated string. Multiple occurrences of the header with the given field name
278 are automatically merged into the comma separated string.
281 ### request.headers_csv_table
283 A table that maps (case-insensitively) a HTTP header field name to a sequence
284 of values. One entry is created for every comma separated value of each header
285 with the given field name.
288 ### request.headers_flags
290 A table that maps (case-insensitively) a HTTP header field name to another
291 table which (again case-insensitively) maps a string to a boolean, depending on
292 whether this string occurred in the list of comma separated values of one
293 header line with the given field name that was the key in the first table.
296 ### request.headers_value
298 A table that maps (case-insensitively) a HTTP header field name to a value. If
299 multiple header lines with the given field name have been received, false is
300 used as value.
303 ### request.method
305 The HTTP request method, e.g. "HEAD", "GET", or "POST".
308 ### request.path
310 The requested path without a leading slash and without the query part (e.g.
311 "index.html" if "/index.html?a=b&c=d" has been requested). For the query part,
312 see request.query.
314 This value will be nil if (and only if) the request method is "OPTIONS" with a
315 request target equal to "*" (see also asterisk-form of request-target in
316 section 5.3.4 in RFC 7230).
319 ### request.post_metadata
321 Only set for multipart/form-data POST requests. A table that maps field names
322 to their corresponding POST metadata table which contains two entries:
323 "file_name" and "content_type". If there are several POST values with the given
324 field name, then the first value/file is used.
327 ### request.post_metadata_list
329 Only set for multipart/form-data POST requests. A table that maps field names
330 to a sequence with their corresponding POST metadata tables. Needed if multiple
331 files are uploaded with the same field name.
334 ### request.post_params
336 A table that maps field names to their corresponding POST value. If there are
337 several POST values with the given field name, then the first value is used.
339 Note: May be implemented through metamethods, but does support iteration
340 through pairs(...).
343 ### request.post_params_list
345 A table that maps field names to a sequence of their corresponding POST values.
347 Note: May be implemented through metamethods, but does support iteration
348 through pairs(...).
351 ### request.query
353 Query part of the request target including the leading question mark, e.g.
354 "?a=b&c=d" if the requested target is "/index.html?a=b&c=d". The data is
355 automatically parsed and made available through request.get_params and
356 request.get_params_list.
358 If there is no query part given in the request target, then this string is
359 the empty string. This value will be nil if (and only if) the request method
360 is "OPTIONS" with a request target equal to "*" (see also asterisk-form of
361 request-target in section 5.3.4 in RFC 7230).
364 ### request:process_request_body()
366 Starts processing the request body (if existent) to set the values
367 request.post_params, request.post_params_list, request.post_metadata, and
368 and request.post_metadata_list and/or to call POST field stream handlers that
369 have been previously registered with request:stream_post_param(...) or
370 request:stream_post_params(...).
372 This method gets invoked automatically when the POST param tables
373 (request.post_params, etc.) are accessed, or if a response is sent (to avoid
374 deadlocks with the webbrowser). (Note: Automatic request body processing on
375 write may be disabled by calling request:defer_reading().)
377 After this method returned, all registered POST field stream handlers have
378 received all data. Registration of other POST field stream handlers is not
379 possible after this method has been called (or after request.post_params_list
380 or request.post_params have been accessed).
383 ### request:send_data(...)
385 Sends data as response body. All arguments are converted via tostring(...) and
386 concatenated. May be called multiple times until the request has been finished
387 by calling request:finish().
389 If the request method (see request.method) is "HEAD", then calls to
390 request:send_data(...) are automatically ignored.
393 ### request:send_header(key, value)
395 Sends a HTTP response header that consists of the given key and the given
396 value. Note: Key and value must be provided as separate arguments. Before any
397 headers can be sent, a HTTP status must have been set with
398 request:send_status(status_string).
401 ### request:send_status(status_string)
403 Sends a HTTP response status that is given as a string consisting of a 3-digit
404 number and an explanatory string, e.g. "200 OK" or "404 Not Found". This
405 function must be called once before any headers or response body data may be
406 sent.
409 ### request.socket
411 The underlaying socket. Can be used to force a TCP RST, etc.
414 ### request:stream_post_param(field_name, callback)
416 Registers a stream handler for the given POST parameter. The callback function
417 will be called in the following manner:
419 - For the initial chunk, the first chunk gets passed as first argument while a
420 table with metadata ("field_name" and "content_type") gets passed as second
421 argument. In case of an immediate EOF (i.e. an empty file), the passed
422 chunk is the empty string. In all other cases the chunk has a length greater
423 than zero.
424 - For any remaining chunks, the respective chunk gets passed as first and only
425 argument (no metadata). Here, the chunk has always a length greater than
426 zero.
427 - To indicate the end of the stream, the callback function is called without
428 arguments. This also happens in case of an immediate EOF (see above).
430 In case of an immediate EOF (i.e. an empty file), the callback function is thus
431 called as follows:
433 - The first time with an empty string as first argument, and with the metadata
434 as second argument.
435 - The second time without any arguments.
438 ### request:stream_post_params(pattern, callback)
440 Same as request:stream_post_param(...) but providing a string pattern to match
441 multiple field names (e.g. "^file_[0-9]+$").
444 ### request:stream_request_body(callback)
446 Start streaming of request body. For each chunk of the request body, the
447 callback function is called with the corresponding chunk. End of data is
448 indicated through return of request:stream_request_body(...) (not by calling
449 the callback without arguments).
451 The function may be called with nil instead of a callback function. In this
452 case, the request body is read and discarded. Only if nil is passed instead of
453 a callback, then the function may also be invoked when the request body has
454 already been read and/or processed. In the latter case, the function performs
455 no operation.

Impressum / About Us