moonbridge

view reference.txt @ 71:4628be0a7b98

Updated reference to include xread, xread_nb, write_nb
author jbe
date Sat Apr 04 21:28:30 2015 +0200 (2015-04-04)
parents 3d1f23f1dbc6
children 88541c2aab4d
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.
42 Timers are also automatically reset (disabled) when a handler (prepare handler
43 or connect handler) returns. To shutdown processes after a certain time waiting
44 for a new request, use the idle_time parameter of the listen function.
48 Function io.poll(read_fds, write_fds, timeout)
49 ----------------------------------------------
51 This function allows to wait for file descriptors to become ready for reading
52 or writing. It accepts the following arguments:
54 1. Table of file descriptors to wait for reading (optional, may be nil)
55 2. Table of file descriptors to wait for writing (optional, may be nil)
56 3. Timeout in seconds (optional, may be nil or zero to disable timeout)
58 Alternatively to file descriptors, the tables may contain file handles (in
59 which case the file descriptor is automatically extracted).
61 Please note that support for non-blocking I/O operations is limited if you use
62 ordinary file handles (as Moonbridge does). It is possible, however, to wait
63 until the first byte to read is available at a file handle. For more
64 information, see socket.input:pending().
68 Socket object passed to "connect" handler
69 -----------------------------------------
71 For every incoming connection, the registered "connect" handler is called with
72 a single socket object as argument, which is described below:
75 ### socket:cancel()
77 Closes the socket connection by sending a TCP RST package if possible to
78 indicate error condition. Returns true on success, or nil plus error message in
79 case of an I/O error. Using this method on sockets that have already been
80 closed (or canceled) will throw an error.
82 Warning: Previously sent (and flushed) data may be lost during transmission.
85 ### socket:close(timeout)
87 Closes the socket connection (input and output stream) by flushing all data and
88 sending a TCP FIN package. Returns true on success, or nil plus error message
89 in case of an I/O error. Using this method on sockets that have already been
90 closed (or canceled) will throw an error.
92 Warning: Pending data on the input stream may cause connection aborts (TCP RST)
93 depending on the particular operating system used. All pending input data
94 should have been read before calling socket:close().
96 The optional timeout parameter may be used to wait until all data has been sent
97 out, or until the timeout elapses (in which case a TCP RST is sent) whichever
98 happens first. A timeout value of 0 or nil causes immediate return and sending
99 of pending data in background (recommended).
102 ### socket:flush()
104 Alias for socket.output:flush()
107 ### socket.input
109 Lua file handle representing the input stream of the socket connection.
110 Supports the same methods as io.open()'s return values.
113 ### socket.input:xread(maxlen, terminator)
115 Reads as many bytes until either the maximum length is reached (first argument)
116 or a terminating character (second argument as string with a length of 1) is
117 encountered. In the latter case, the terminating character will be included in
118 the result.
120 On EOF, false (as first result value) and an error message (as second result
121 value) are returned. In case of an I/O error, nil and an error message are
122 returned.
124 This method is also available for any other Lua file handle.
127 ### socket.input:xread_nb(maxlen, terminator)
129 Same as socket.input:xread(maxlen, terminator), but non-blocking. If no more
130 data can be read due to blocking, this method returns a string containing the
131 data which has already been read. If no data could be read due to blocking,
132 then an empty string is returned.
134 On EOF, false (as first result value) and an error message (as second result
135 value) are returned. In case of an I/O error, nil and an error message are
136 returned.
138 This method is also available for any other Lua file handle.
141 ### socket.interval
143 Set to the name of an interval timer if the "connect" handler was called due to
144 an elapsed interval timer. Otherwise nil.
147 ### socket:lines()
149 Alias for socket.input:lines()
152 ### socket.local_ip4
154 Local IPv4 address used for the connection. Encoded as 4 raw bytes in form of a
155 string.
158 ### socket.local_ip6
160 Local IPv6 address used for the connection. Encoded as 16 raw bytes in form of
161 a string.
164 ### socket.local_tcpport
166 Local TCP port used for the connection.
169 ### socket.output
171 Lua file handle representing the output stream of the socket connection.
172 Supports the same methods as io.open()'s return values.
175 ### socket.output:close()
177 Performs a half-close (i.e. sends a TCP FIN package in case of a TCP socket).
179 Note: In order to shut down a TCP connection properly, it may be necessary to
180 read any pending data from socket.input before closing the socket completely
181 (e.g. with socket:close() or by returning from the connect handler). If there
182 is still incoming data, a TCP RST packet might be sent which can cause loss of
183 transmitted data.
186 ### socket.output:write_nb(...)
188 Non-blocking write. This function attempts to write as many bytes as possible,
189 returning a string containing all data that could not be written due to
190 blocking (potentially concatenating some or all remaining arguments to create
191 that string).
193 In case of an I/O error, nil (as first result value) and an error message (as
194 second result value) are returned.
196 This method is also available for any other Lua file handle.
199 ### socket:read(...)
201 Alias for socket.input:read()
204 ### socket.remote_ip4
206 Remote IPv4 address used for the connection. Encoded as 4 raw bytes in form of
207 a string.
210 ### socket.remote_ip6
212 Remote IPv6 address used for the connection. Encoded as 16 raw bytes in form of
213 a string.
216 ### socket.remote_tcpport
218 Remote TCP port used for the connection.
221 ### socket:write(...)
223 Alias for socket.output:write(...)
226 ### socket:write_nb(...)
228 Alias for socket.output:write(...)
231 ### socket:xread(maxlen, terminator)
233 Alias for socket.input:xread(...)
236 ### socket:xread_nb(maxlen, terminator)
238 Alias for socket.input:xread_nb(...)
242 HTTP module
243 -----------
245 The http module exports the function http.generate_handler(callback) that
246 converts an HTTP handler to a "connect" handler. See file "example.lua" for an
247 example of invocation. A table with options may be passed either as a second
248 argument, or as a first argument preceeding the callback function (whichever is
249 more convenient).
251 The following options are supported:
253 - request_body_size_limit: maximum size of payload of HTTP request body
254 (transfer encoding is allowed to add a limited amount of extra data)
255 - chunk_size: optional default value for maximum_input_chunk_size and
256 minimum_output_chunk_size
257 - request_header_size_limit: maximum size of HTTP request headers
258 - maximum_input_chunk_size: maximum chunk size when streaming a request body or
259 certain POST fields (bigger chunks will be fragmented automatically)
260 - minimum_output_chunk_size: minimum size for a chunk when sending a response
261 body (smaller chunks will be buffered and concatenated with future data;
262 ignored when request:flush() is called)
263 - static_headers: a set of headers to be included in every HTTP response
264 (may be a string, a table or strings, or a table of key-value pairs)
266 The callback function receives a single request object as argument, which is
267 described below.
270 ### request.body
272 The request body (without headers) as a string. Accessing this value makes
273 further access to request.post_params and request.post_params_list, or
274 invocation of request:stream_request_body(...) impossible.
277 ### request:close_after_finish()
279 Closes the connection after answering the request.
281 This method can only be called before the HTTP response header section has been
282 finished (i.e. before request:finish_headers(), request:send_data(...), or
283 request:finish() were called), but it may be called before a status code has
284 been sent using request:send_status(...).
286 A corresponding "Connection: close" header is automatically sent.
289 ### request.cookies
291 A table with all cookies sent by the client.
294 ### request.defer_reading()
296 Disables automatic request body processing on write. Can be called before
297 sending a HTTP status code to send a response before the request has been fully
298 received.
300 CAUTION: Responding to a request before the request body has been processed may
301 lead to a deadlock if the browser does not process the response while trying to
302 send the request. Therefore, this function should only be used if:
304 - the TCP stack has enough buffer space for the response (i.e. if the response
305 is small enough), and if
306 - a timer is used to cancel the response in case of a deadlock.
308 It is recommended to not use this function unless certain performance tweaks
309 are desired.
312 ### request.faulty
314 Normally set to false. In case of a read or write error on the client
315 connection, this value is set to true before a Lua error is raised.
317 A faulty request handle must not be used, or another Lua error will be raised.
320 ### request:finish()
322 Finishes and flushes a HTTP response. May be called multiple times. An
323 HTTP status, all headers, and the response body (if applicable) must have been
324 previously sent. After calling this method, no further data may be written.
327 ### request:finish_headers()
329 Finishes and flushes the HTTP response header section. May be called multiple
330 times, as long as the request is not finished completely. This method is
331 automatically invoked if the application is beginning to send a response body.
332 After calling this method, no further headers may be sent.
335 ### request:flush()
337 Flushes any pending output data. Note: In order to mark the end of a response
338 body, it is required to call request:finish().
341 ### request.get_params
343 A table that maps field names to their corresponding GET value. If there are
344 several GET values with the given field name, then the first value is used.
346 Note: May be implemented through metamethods, but does support iteration
347 through pairs(...).
350 ### request.get_params_list
352 A table that maps field names to a sequence of their corresponding GET values.
354 Note: May be implemented through metamethods, but does support iteration
355 through pairs(...).
358 ### request.headers
360 A table that maps (case-insensitively) a HTTP header field name to a sequence
361 of values. One entry is created for every occurrence of a header line with the
362 given field name).
365 ### request.headers_csv_string
367 A table that maps (case-insensitively) a HTTP header field name to a comma
368 separated string. Multiple occurrences of the header with the given field name
369 are automatically merged into the comma separated string.
372 ### request.headers_csv_table
374 A table that maps (case-insensitively) a HTTP header field name to a sequence
375 of values. One entry is created for every comma separated value of each header
376 with the given field name.
379 ### request.headers_flags
381 A table that maps (case-insensitively) a HTTP header field name to another
382 table which (again case-insensitively) maps a string to a boolean, depending on
383 whether this string occurred in the list of comma separated values of one
384 header line with the given field name that was the key in the first table.
387 ### request.headers_value
389 A table that maps (case-insensitively) a HTTP header field name to a value. If
390 multiple header lines with the given field name have been received, false is
391 used as value.
394 ### request.method
396 The HTTP request method, e.g. "HEAD", "GET", or "POST".
399 ### request.path
401 The requested path without a leading slash and without the query part (e.g.
402 "index.html" if "/index.html?a=b&c=d" has been requested). For the query part,
403 see request.query.
405 This value will be nil if (and only if) the request method is "OPTIONS" with a
406 request target equal to "*" (see also asterisk-form of request-target in
407 section 5.3.4 in RFC 7230).
410 ### request.post_metadata
412 Only set for multipart/form-data POST requests. A table that maps field names
413 to their corresponding POST metadata table which contains two entries:
414 "file_name" and "content_type". If there are several POST values with the given
415 field name, then the first value/file is used.
418 ### request.post_metadata_list
420 Only set for multipart/form-data POST requests. A table that maps field names
421 to a sequence with their corresponding POST metadata tables. Needed if multiple
422 files are uploaded with the same field name.
425 ### request.post_params
427 A table that maps field names to their corresponding POST value. If there are
428 several POST values with the given field name, then the first value is used.
430 Note: May be implemented through metamethods, but does support iteration
431 through pairs(...).
434 ### request.post_params_list
436 A table that maps field names to a sequence of their corresponding POST values.
438 Note: May be implemented through metamethods, but does support iteration
439 through pairs(...).
442 ### request.query
444 Query part of the request target including the leading question mark, e.g.
445 "?a=b&c=d" if the requested target is "/index.html?a=b&c=d". The data is
446 automatically parsed and made available through request.get_params and
447 request.get_params_list.
449 If there is no query part given in the request target, then this string is
450 the empty string. This value will be nil if (and only if) the request method
451 is "OPTIONS" with a request target equal to "*" (see also asterisk-form of
452 request-target in section 5.3.4 in RFC 7230).
455 ### request:process_request_body()
457 Starts processing the request body (if existent) to set the values
458 request.post_params, request.post_params_list, request.post_metadata, and
459 and request.post_metadata_list and/or to call POST field stream handlers that
460 have been previously registered with request:stream_post_param(...) or
461 request:stream_post_params(...).
463 This method gets invoked automatically when the POST param tables
464 (request.post_params, etc.) are accessed, or if a response is sent (to avoid
465 deadlocks with the webbrowser). (Note: Automatic request body processing on
466 write may be disabled by calling request:defer_reading().)
468 After this method returned, all registered POST field stream handlers have
469 received all data. Registration of other POST field stream handlers is not
470 possible after this method has been called (or after request.post_params_list
471 or request.post_params have been accessed).
474 ### request:send_data(...)
476 Sends data as response body. All arguments are converted via tostring(...) and
477 concatenated. May be called multiple times until the request has been finished
478 by calling request:finish().
480 If the request method (see request.method) is "HEAD", then calls to
481 request:send_data(...) are automatically ignored.
484 ### request:send_header(key, value)
486 Sends a HTTP response header that consists of the given key and the given
487 value. Note: Key and value must be provided as separate arguments. Before any
488 headers can be sent, a HTTP status must have been set with
489 request:send_status(status_string).
492 ### request:send_status(status_string)
494 Sends a HTTP response status that is given as a string consisting of a 3-digit
495 number and an explanatory string, e.g. "200 OK" or "404 Not Found". This
496 function must be called once before any headers or response body data may be
497 sent.
500 ### request.socket
502 The underlaying socket. Can be used to force a TCP RST, etc.
505 ### request:stream_post_param(field_name, callback)
507 Registers a stream handler for the given POST parameter. The callback function
508 will be called in the following manner:
510 - For the initial chunk, the first chunk gets passed as first argument while a
511 table with metadata ("field_name" and "content_type") gets passed as second
512 argument. In case of an immediate EOF (i.e. an empty file), the passed
513 chunk is the empty string. In all other cases the chunk has a length greater
514 than zero.
515 - For any remaining chunks, the respective chunk gets passed as first and only
516 argument (no metadata). Here, the chunk has always a length greater than
517 zero.
518 - To indicate the end of the stream, the callback function is called without
519 arguments. This also happens in case of an immediate EOF (see above).
521 In case of an immediate EOF (i.e. an empty file), the callback function is thus
522 called as follows:
524 - The first time with an empty string as first argument, and with the metadata
525 as second argument.
526 - The second time without any arguments.
529 ### request:stream_post_params(pattern, callback)
531 Same as request:stream_post_param(...) but providing a string pattern to match
532 multiple field names (e.g. "^file_[0-9]+$").
535 ### request:stream_request_body(callback)
537 Start streaming of request body. For each chunk of the request body, the
538 callback function is called with the corresponding chunk. End of data is
539 indicated through return of request:stream_request_body(...) (not by calling
540 the callback without arguments).
542 The function may be called with nil instead of a callback function. In this
543 case, the request body is read and discarded. Only if nil is passed instead of
544 a callback, then the function may also be invoked when the request body has
545 already been read and/or processed. In the latter case, the function performs
546 no operation.

Impressum / About Us