moonbridge

view reference.txt @ 93:de0e69673953

Removed excess linebreak in reference.txt
author jbe
date Tue Apr 07 04:08:41 2015 +0200 (2015-04-07)
parents 6b26783f9323
children de3982f17d05
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 Socket object passed to "connect" handler
49 -----------------------------------------
51 For every incoming connection, the registered "connect" handler is called with
52 a single socket object as argument, which is described below:
55 ### socket:close(timeout)
57 Closes the socket connection (input and output stream) by flushing all data and
58 sending a TCP FIN packet. If the timeout value is non-nil but zero, a TCP RST
59 is sent instead. If a positive timeout value is given and if the remote peer
60 doesn't respond with a TCP FIN within the given time, a TCP RST is sent in
61 addition to the previously sent TCP FIN packet. If the timeout value is nil or
62 negative, the call returns immediately and the operating system will wait for
63 the peer's TCP FIN packet.
65 Returns true on success, or nil plus error message in case of an I/O error.
66 Using this method on sockets that have already been closed (or reset) will
67 throw an error.
69 Warning: Pending data on the input stream may cause connection aborts (TCP RST)
70 depending on the particular operating system used. All pending input data
71 should have been read (or drained) before calling socket:close().
74 ### socket:drain(maxlen, terminator)
76 Same as socket:read(maxlen, terminator), but discards the input and returns the
77 number of discarded bytes. If no bytes could be read but EOF was encountered,
78 then true is returned.
80 In case of an I/O error, nil (as first return value) plus an error message (as
81 second result value) are returned.
84 ### socket:drain_nb(maxlen, terminator)
86 Same as socket:read_nb(maxlen, terminator), but discards the input and returns
87 the number of discarded bytes. If no bytes could be read but EOF was
88 encountered, then true is returned.
90 In case of an I/O error, nil (as first return value) plus an error message (as
91 second result value) are returned.
94 ### socket:flush(...)
96 Same as socket:write(...) but additionally flushes the socket (i.e. all pending
97 data is passed to the operating system).
99 In case of an I/O error, nil (as first return value) plus an error message (as
100 second result value) are returned. On success, the socket userdata object is
101 returned.
104 ### socket:flush_nb(...)
106 Same as socket:write_nb(...) but additionally flushes the socket (i.e. all
107 pending data is passed to the operating system). The total number of bytes that
108 could not be passed yet to the operating system is returned. Zero is returned
109 if all data could be flushed out.
111 In case of an I/O error, nil (as first return value) plus an error message (as
112 second result value) are returned.
115 ### socket.interval
117 Set to the name of an interval timer if the "connect" handler was called due to
118 an elapsed interval timer. Otherwise nil.
121 ### socket.local_ip4
123 Local IPv4 address used for the connection. Encoded as 4 raw bytes in form of a
124 string.
127 ### socket.local_ip6
129 Local IPv6 address used for the connection. Encoded as 16 raw bytes in form of
130 a string.
133 ### socket.local_tcpport
135 Local TCP port used for the connection.
138 ### socket:read(maxlen, terminator)
140 Read up to maxlen bytes or until an optional termination character is
141 encountered (which is included in the result). The maxlen value may be nil, in
142 which case there is no limit on the number of bytes read.
144 If EOF is encountered before any data could be read, then false (as first
145 return value) plus a notice string (as second return value) are returned.
147 In case of an I/O error, nil (as first return value) plus an error message (as
148 second result value) are returned.
151 ### socket:read_nb(maxlen, terminator)
153 Read up to maxlen bytes, until an optional termination character is encountered
154 (which is included in the result), or until no more data is available for
155 reading. The maxlen value may be nil, in which case there is no limit on the
156 number of bytes read.
158 If EOF is encountered before any data could be read, then false (as first
159 return value) plus a notice string (as second return value) are returned.
161 If no data was available for reading, but no EOF was encountered, then an empty
162 string is returned.
164 In case of an I/O error, nil (as first return value) plus an error message (as
165 second result value) are returned.
168 ### socket.remote_ip4
170 Remote IPv4 address used for the connection. Encoded as 4 raw bytes in form of
171 a string.
174 ### socket.remote_ip6
176 Remote IPv6 address used for the connection. Encoded as 16 raw bytes in form of
177 a string.
180 ### socket.remote_tcpport
182 Remote TCP port used for the connection.
185 ### socket:reset()
187 Alias for socket:close(0). Closes the socket connection by sending a TCP RST
188 packet if possible to indicate error condition.
190 Returns true on success, or nil (as first result value) plus error message (as
191 second result value) in case of an I/O error. Using this method on sockets that
192 have already been closed (or reset) will throw an error.
194 Warning: Previously sent (and flushed) data may be lost during transmission.
197 ### socket:write(...)
199 Takes a variable number of strings and sends them to the peer. The operation is
200 buffered, so to actually send out the data, it is necessary to eventually call
201 socket:flush(), socket:finish(), or socket:close().
203 In case of an I/O error, nil (as first return value) plus an error message (as
204 second result value) are returned. On success, the socket userdata object is
205 returned.
208 ### socket:write_nb(...)
210 Takes a variable number of strings and sends them to the peer. The operation is
211 buffered, so to actually send out the data, it is necessary to eventually call
212 socket:flush_nb(), socket:flush(), socket:finish(), or socket:close().
214 This function always returns immediately (i.e. it does not block). If all data
215 (but a small buffered portion) could be sent out, then zero is returned.
216 Otherwise, all arguments that could not be sent are stored in a buffer of
217 unlimited size (up to memory capabilities) and an integer is returned that
218 indicates the number of bytes currently in the buffer.
220 In case of an I/O error, nil (as first return value) plus an error message (as
221 second result value) are returned.
225 HTTP module
226 -----------
228 The http module exports the function http.generate_handler(callback) that
229 converts an HTTP handler to a "connect" handler. See file "example.lua" for an
230 example of invocation. A table with options may be passed either as a second
231 argument, or as a first argument preceeding the callback function (whichever is
232 more convenient).
234 The following options are supported:
236 - request_body_size_limit: maximum size of payload of HTTP request body
237 (transfer encoding is allowed to add a limited amount of extra data)
238 - chunk_size: optional default value for maximum_input_chunk_size and
239 minimum_output_chunk_size
240 - request_header_size_limit: maximum size of HTTP request headers
241 - maximum_input_chunk_size: maximum chunk size when streaming a request body or
242 certain POST fields (bigger chunks will be fragmented automatically)
243 - minimum_output_chunk_size: minimum size for a chunk when sending a response
244 body (smaller chunks will be buffered and concatenated with future data;
245 ignored when request:flush() is called)
246 - static_headers: a set of headers to be included in every HTTP response
247 (may be a string, a table or strings, or a table of key-value pairs)
249 The callback function receives a single request object as argument, which is
250 described below.
253 ### request.body
255 The request body (without headers) as a string. Accessing this value makes
256 further access to request.post_params and request.post_params_list, or
257 invocation of request:stream_request_body(...) impossible.
260 ### request:close_after_finish()
262 Closes the connection after answering the request.
264 This method can only be called before the HTTP response header section has been
265 finished (i.e. before request:finish_headers(), request:send_data(...), or
266 request:finish() were called), but it may be called before a status code has
267 been sent using request:send_status(...).
269 A corresponding "Connection: close" header is automatically sent.
272 ### request.cookies
274 A table with all cookies sent by the client.
277 ### request.defer_reading()
279 Disables automatic request body processing on write. Can be called before
280 sending a HTTP status code to send a response before the request has been fully
281 received.
283 CAUTION: Responding to a request before the request body has been processed may
284 lead to a deadlock if the browser does not process the response while trying to
285 send the request. Therefore, this function should only be used if:
287 - the TCP stack has enough buffer space for the response (i.e. if the response
288 is small enough), and if
289 - a timer is used to cancel the response in case of a deadlock.
291 It is recommended to not use this function unless certain performance tweaks
292 are desired.
295 ### request.faulty
297 Normally set to false. In case of a read or write error on the client
298 connection, this value is set to true before a Lua error is raised.
300 A faulty request handle must not be used, or another Lua error will be raised.
303 ### request:finish()
305 Finishes and flushes a HTTP response. May be called multiple times. An
306 HTTP status, all headers, and the response body (if applicable) must have been
307 previously sent. After calling this method, no further data may be written.
310 ### request:finish_headers()
312 Finishes and flushes the HTTP response header section. May be called multiple
313 times, as long as the request is not finished completely. This method is
314 automatically invoked if the application is beginning to send a response body.
315 After calling this method, no further headers may be sent.
318 ### request:flush()
320 Flushes any pending output data. Note: In order to mark the end of a response
321 body, it is required to call request:finish().
324 ### request.get_params
326 A table that maps field names to their corresponding GET value. If there are
327 several GET values with the given field name, then the first value is used.
329 Note: May be implemented through metamethods, but does support iteration
330 through pairs(...).
333 ### request.get_params_list
335 A table that maps field names to a sequence of their corresponding GET values.
337 Note: May be implemented through metamethods, but does support iteration
338 through pairs(...).
341 ### request.headers
343 A table that maps (case-insensitively) a HTTP header field name to a sequence
344 of values. One entry is created for every occurrence of a header line with the
345 given field name).
348 ### request.headers_csv_string
350 A table that maps (case-insensitively) a HTTP header field name to a comma
351 separated string. Multiple occurrences of the header with the given field name
352 are automatically merged into the comma separated string.
355 ### request.headers_csv_table
357 A table that maps (case-insensitively) a HTTP header field name to a sequence
358 of values. One entry is created for every comma separated value of each header
359 with the given field name.
362 ### request.headers_flags
364 A table that maps (case-insensitively) a HTTP header field name to another
365 table which (again case-insensitively) maps a string to a boolean, depending on
366 whether this string occurred in the list of comma separated values of one
367 header line with the given field name that was the key in the first table.
370 ### request.headers_value
372 A table that maps (case-insensitively) a HTTP header field name to a value. If
373 multiple header lines with the given field name have been received, false is
374 used as value.
377 ### request.method
379 The HTTP request method, e.g. "HEAD", "GET", or "POST".
382 ### request.path
384 The requested path without a leading slash and without the query part (e.g.
385 "index.html" if "/index.html?a=b&c=d" has been requested). For the query part,
386 see request.query.
388 This value will be nil if (and only if) the request method is "OPTIONS" with a
389 request target equal to "*" (see also asterisk-form of request-target in
390 section 5.3.4 in RFC 7230).
393 ### request.post_metadata
395 Only set for multipart/form-data POST requests. A table that maps field names
396 to their corresponding POST metadata table which contains two entries:
397 "file_name" and "content_type". If there are several POST values with the given
398 field name, then the first value/file is used.
401 ### request.post_metadata_list
403 Only set for multipart/form-data POST requests. A table that maps field names
404 to a sequence with their corresponding POST metadata tables. Needed if multiple
405 files are uploaded with the same field name.
408 ### request.post_params
410 A table that maps field names to their corresponding POST value. If there are
411 several POST values with the given field name, then the first value is used.
413 Note: May be implemented through metamethods, but does support iteration
414 through pairs(...).
417 ### request.post_params_list
419 A table that maps field names to a sequence of their corresponding POST values.
421 Note: May be implemented through metamethods, but does support iteration
422 through pairs(...).
425 ### request.query
427 Query part of the request target including the leading question mark, e.g.
428 "?a=b&c=d" if the requested target is "/index.html?a=b&c=d". The data is
429 automatically parsed and made available through request.get_params and
430 request.get_params_list.
432 If there is no query part given in the request target, then this string is
433 the empty string. This value will be nil if (and only if) the request method
434 is "OPTIONS" with a request target equal to "*" (see also asterisk-form of
435 request-target in section 5.3.4 in RFC 7230).
438 ### request:process_request_body()
440 Starts processing the request body (if existent) to set the values
441 request.post_params, request.post_params_list, request.post_metadata, and
442 and request.post_metadata_list and/or to call POST field stream handlers that
443 have been previously registered with request:stream_post_param(...) or
444 request:stream_post_params(...).
446 This method gets invoked automatically when the POST param tables
447 (request.post_params, etc.) are accessed, or if a response is sent (to avoid
448 deadlocks with the webbrowser). (Note: Automatic request body processing on
449 write may be disabled by calling request:defer_reading().)
451 After this method returned, all registered POST field stream handlers have
452 received all data. Registration of other POST field stream handlers is not
453 possible after this method has been called (or after request.post_params_list
454 or request.post_params have been accessed).
457 ### request:send_data(...)
459 Sends data as response body. All arguments are converted via tostring(...) and
460 concatenated. May be called multiple times until the request has been finished
461 by calling request:finish().
463 If the request method (see request.method) is "HEAD", then calls to
464 request:send_data(...) are automatically ignored.
467 ### request:send_header(key, value)
469 Sends a HTTP response header that consists of the given key and the given
470 value. Note: Key and value must be provided as separate arguments. Before any
471 headers can be sent, a HTTP status must have been set with
472 request:send_status(status_string).
475 ### request:send_status(status_string)
477 Sends a HTTP response status that is given as a string consisting of a 3-digit
478 number and an explanatory string, e.g. "200 OK" or "404 Not Found". This
479 function must be called once before any headers or response body data may be
480 sent.
483 ### request.socket
485 The underlaying socket. Can be used to force a TCP RST, etc.
488 ### request:stream_post_param(field_name, callback)
490 Registers a stream handler for the given POST parameter. The callback function
491 will be called in the following manner:
493 - For the initial chunk, the first chunk gets passed as first argument while a
494 table with metadata ("field_name" and "content_type") gets passed as second
495 argument. In case of an immediate EOF (i.e. an empty file), the passed
496 chunk is the empty string. In all other cases the chunk has a length greater
497 than zero.
498 - For any remaining chunks, the respective chunk gets passed as first and only
499 argument (no metadata). Here, the chunk has always a length greater than
500 zero.
501 - To indicate the end of the stream, the callback function is called without
502 arguments. This also happens in case of an immediate EOF (see above).
504 In case of an immediate EOF (i.e. an empty file), the callback function is thus
505 called as follows:
507 - The first time with an empty string as first argument, and with the metadata
508 as second argument.
509 - The second time without any arguments.
512 ### request:stream_post_params(pattern, callback)
514 Same as request:stream_post_param(...) but providing a string pattern to match
515 multiple field names (e.g. "^file_[0-9]+$").
518 ### request:stream_request_body(callback)
520 Start streaming of request body. For each chunk of the request body, the
521 callback function is called with the corresponding chunk. End of data is
522 indicated through return of request:stream_request_body(...) (not by calling
523 the callback without arguments).
525 The function may be called with nil instead of a callback function. In this
526 case, the request body is read and discarded. Only if nil is passed instead of
527 a callback, then the function may also be invoked when the request body has
528 already been read and/or processed. In the latter case, the function performs
529 no operation.

Impressum / About Us