moonbridge

view reference.txt @ 41:b6619de6f494

When writing to disconnected clients, do not use signal 13 (SIGPIPE) but throw I/O error
author jbe
date Sun Mar 08 00:53:35 2015 +0100 (2015-03-08)
parents 2d7e3028d993
children f2efab1ba3d0
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 Socket object passed to "connect" handler
25 -----------------------------------------
27 For every incoming connection, the registered "connect" handler is called with
28 a single socket object as argument, which is described below:
31 ### socket:cancel()
33 Closes the socket connection by sending a TCP RST package if possible to
34 indicate error condition.
36 Warning: Previously sent (and flushed) data may be lost during transmission.
39 ### socket:close(timeout)
41 Closes the socket connection (input and output stream) by flushing all data and
42 sending a TCP FIN package. Performs no operation if stream has already been
43 closed.
45 Warning: Pending data on the input stream may cause connection aborts (TCP RST)
46 depending on the particular operating system used. All pending input data
47 should have been read before calling socket:close().
49 The optional timeout parameter may be used to wait until all data has been sent
50 out, or until the timeout elapses (in which case a TCP RST is sent) whichever
51 happens first. A timeout value of 0 or nil causes immediate return and sending
52 of pending data in background (recommended).
55 ### socket:flush()
57 Alias for socket.output:flush()
60 ### socket.input
62 Lua file handle representing the input stream of the socket connection.
63 Supports the same methods as io.open()'s return values.
66 ### socket.interval
68 Set to the name of an interval timer if the "connect" handler was called due to
69 an elapsed interval timer. Otherwise nil.
72 ### socket:lines()
74 Alias for socket.input:lines()
77 ### socket.local_ip4
79 Local IPv4 address used for the connection. Encoded as 4 raw bytes in form of a
80 string.
83 ### socket.local_ip6
85 Local IPv6 address used for the connection. Encoded as 16 raw bytes in form of
86 a string.
89 ### socket.local_tcpport
91 Local TCP port used for the connection.
94 ### socket.output
96 Lua file handle representing the output stream of the socket connection.
97 Supports the same methods as io.open()'s return values.
100 ### socket:read(...)
102 Alias for socket.input:read()
105 ### socket:readuntil(terminator, maxlen)
107 Reads as many bytes until a byte equal to the terminator value occurs. An
108 optional maximum length may be specified. The terminating byte is included in
109 the return value (unless the maximum length would be exceeded).
111 Also available as :readuntil(...) method for any other Lua file handle
112 (including socket.input)
115 ### socket.remote_ip4
117 Remote IPv4 address used for the connection. Encoded as 4 raw bytes in form of
118 a string.
121 ### socket.remote_ip6
123 Remote IPv6 address used for the connection. Encoded as 16 raw bytes in form of
124 a string.
127 ### socket.remote_tcpport
129 Remote TCP port used for the connection.
132 ### socket:write(...)
134 Alias for socket.output:write(...)
138 HTTP module
139 -----------
141 The http module exports the function http.generate_handler(callback) that
142 converts an HTTP handler to a "connect" handler. See file "example.lua" for an
143 example of invocation. A table with options may be passed either as a second
144 argument, or as a first argument preceeding the callback function (whichever is
145 more convenient).
147 The following options are supported:
149 - request_body_size_limit: maximum size of payload of HTTP request body
150 (transfer encoding is allowed to add a limited amount of extra data)
151 - chunk_size: optional default value for maximum_input_chunk_size and
152 minimum_output_chunk_size
153 - request_header_size_limit: maximum size of HTTP request headers
154 - maximum_input_chunk_size: maximum chunk size when streaming a request body or
155 certain POST fields (bigger chunks will be fragmented automatically)
156 - minimum_output_chunk_size: minimum size for a chunk when sending a response
157 body (smaller chunks will be buffered and concatenated with future data;
158 ignored when request:flush() is called)
159 - static_headers: a set of headers to be included in every HTTP response
160 (may be a string, a table or strings, or a table of key-value pairs)
162 The callback function receives a single request object as argument, which is
163 described below.
166 ### request.body
168 The request body (without headers) as a string. Accessing this value makes
169 further access to request.post_params and request.post_params_list, or
170 invocation of request:stream_request_body(...) impossible.
173 ### request.cookies
175 A table with all cookies sent by the client.
178 ### request.defer_reading()
180 Disables automatic request body processing on write. Can be called before
181 sending a HTTP status code to send a response before the request has been fully
182 received.
184 CAUTION: Responding to a request before the request body has been processed may
185 lead to a deadlock if the browser does not process the response while trying to
186 send the request. Therefore, this function should only be used if:
188 - the TCP stack has enough buffer space for the response (i.e. if the response
189 is small enough), and if
190 - a timer is used to cancel the response in case of a deadlock.
192 It is recommended to not use this function unless certain performance tweaks
193 are desired.
196 ### request:finish()
198 Finishes and flushes a HTTP response. May be called multiple times. An
199 HTTP status, all headers, and the response body (if applicable) must have been
200 previously sent. After calling this method, no further data may be written.
203 ### request:finish_headers()
205 Finishes and flushes the HTTP response header section. May be called multiple
206 times, as long as the request is not finished completely. This method is
207 automatically invoked if the application is beginning to send a response body.
208 After calling this method, no further headers may be sent.
211 ### request:flush()
213 Flushes any pending output data. Note: In order to mark the end of a response
214 body, it is required to call request:finish().
217 ### request.get_params
219 A table that maps field names to their corresponding GET value. If there are
220 several GET values with the given field name, then the first value is used.
222 Note: May be implemented through metamethods, but does support iteration
223 through pairs(...).
226 ### request.get_params_list
228 A table that maps field names to a sequence of their corresponding GET values.
230 Note: May be implemented through metamethods, but does support iteration
231 through pairs(...).
234 ### request.headers
236 A table that maps (case-insensitively) a HTTP header field name to a sequence
237 of values. One entry is created for every occurrence of a header line with the
238 given field name).
241 ### request.headers_csv_string
243 A table that maps (case-insensitively) a HTTP header field name to a comma
244 separated string. Multiple occurrences of the header with the given field name
245 are automatically merged into the comma separated string.
248 ### request.headers_csv_table
250 A table that maps (case-insensitively) a HTTP header field name to a sequence
251 of values. One entry is created for every comma separated value of each header
252 with the given field name.
255 ### request.headers_flags
257 A table that maps (case-insensitively) a HTTP header field name to another
258 table which (again case-insensitively) maps a string to a boolean, depending on
259 whether this string occurred in the list of comma separated values of one
260 header line with the given field name that was the key in the first table.
263 ### request.headers_value
265 A table that maps (case-insensitively) a HTTP header field name to a value. If
266 multiple header lines with the given field name have been received, false is
267 used as value.
270 ### request.method
272 The HTTP request method, e.g. "HEAD", "GET", or "POST".
275 ### request.path
277 The requested path without a leading slash and without the query part (e.g.
278 "index.html" if "/index.html?a=b&c=d" has been requested). For the query part,
279 see request.query.
281 This value will be nil if (and only if) the request method is "OPTIONS" with a
282 request target equal to "*" (see also asterisk-form of request-target in
283 section 5.3.4 in RFC 7230).
286 ### request.post_metadata
288 Only set for multipart/form-data POST requests. A table that maps field names
289 to their corresponding POST metadata table which contains two entries:
290 "file_name" and "content_type". If there are several POST values with the given
291 field name, then the first value/file is used.
294 ### request.post_metadata_list
296 Only set for multipart/form-data POST requests. A table that maps field names
297 to a sequence with their corresponding POST metadata tables. Needed if multiple
298 files are uploaded with the same field name.
301 ### request.post_params
303 A table that maps field names to their corresponding POST value. If there are
304 several POST values with the given field name, then the first value is used.
306 Note: May be implemented through metamethods, but does support iteration
307 through pairs(...).
310 ### request.post_params_list
312 A table that maps field names to a sequence of their corresponding POST values.
314 Note: May be implemented through metamethods, but does support iteration
315 through pairs(...).
318 ### request.query
320 Query part of the request target including the leading question mark, e.g.
321 "?a=b&c=d" if the requested target is "/index.html?a=b&c=d". The data is
322 automatically parsed and made available through request.get_params and
323 request.get_params_list.
325 If there is no query part given in the request target, then this string is
326 the empty string. This value will be nil if (and only if) the request method
327 is "OPTIONS" with a request target equal to "*" (see also asterisk-form of
328 request-target in section 5.3.4 in RFC 7230).
331 ### request:process_request_body()
333 Starts processing the request body (if existent) to set the values
334 request.post_params, request.post_params_list, request.post_metadata, and
335 and request.post_metadata_list and/or to call POST field stream handlers that
336 have been previously registered with request:stream_post_param(...) or
337 request:stream_post_params(...).
339 This method gets invoked automatically when the POST param tables
340 (request.post_params, etc.) are accessed, or if a response is sent (to avoid
341 deadlocks with the webbrowser). (Note: Automatic request body processing on
342 write may be disabled by calling request:defer_reading().)
344 After this method returned, all registered POST field stream handlers have
345 received all data. Registration of other POST field stream handlers is not
346 possible after this method has been called (or after request.post_params_list
347 or request.post_params have been accessed).
350 ### request:send_data(...)
352 Sends data as response body. All arguments are converted via tostring(...) and
353 concatenated. May be called multiple times until the request has been finished
354 by calling request:finish().
356 If the request method (see request.method) is "HEAD", then calls to
357 request:send_data(...) are automatically ignored.
360 ### request:send_header(key, value)
362 Sends a HTTP response header that consists of the given key and the given
363 value. Note: Key and value must be provided as separate arguments. Before any
364 headers can be sent, a HTTP status must have been set with
365 request:send_status(status_string).
368 ### request:send_status(status_string)
370 Sends a HTTP response status that is given as a string consisting of a 3-digit
371 number and an explanatory string, e.g. "200 OK" or "404 Not Found". This
372 function must be called once before any headers or response body data may be
373 sent.
376 ### request.socket
378 The underlaying socket. Can be used to force a TCP RST, etc.
381 ### request:stream_post_param(field_name, callback)
383 Registers a stream handler for the given POST parameter. The callback function
384 will be called in the following manner:
386 - For the initial chunk, the first chunk gets passed as first argument while a
387 table with metadata ("field_name" and "content_type") gets passed as second
388 argument. In case of an immediate EOF (i.e. an empty file), the passed
389 chunk is the empty string. In all other cases the chunk has a length greater
390 than zero.
391 - For any remaining chunks, the respective chunk gets passed as first and only
392 argument (no metadata). Here, the chunk has always a length greater than
393 zero.
394 - To indicate the end of the stream, the callback function is called without
395 arguments. This also happens in case of an immediate EOF (see above).
397 In case of an immediate EOF (i.e. an empty file), the callback function is thus
398 called as follows:
400 - The first time with an empty string as first argument, and with the metadata
401 as second argument.
402 - The second time without any arguments.
405 ### request:stream_post_params(pattern, callback)
407 Same as request:stream_post_param(...) but providing a string pattern to match
408 multiple field names (e.g. "^file_[0-9]+$").
411 ### request:stream_request_body(callback)
413 Start streaming of request body. For each chunk of the request body, the
414 callback function is called with the corresponding chunk. End of data is
415 indicated through return of request:stream_request_body(...) (not by calling
416 the callback without arguments).

Impressum / About Us