Added notes on buffering POST data and websocket data

This commit is contained in:
Sergey Lyubka committed 2013-12-22 08:27:23 +00:00
1 parent 0007e47c07
commit 64a744055f
1 file changed
+34 -10
+34 -10
View File
@@ -3,17 +3,24 @@
struct mg_server *mg_create_server(void *server_param); struct mg_server *mg_create_server(void *server_param);
Creates web server instance. Returns opaque instance pointer, or NULL if Creates web server instance. Returns opaque instance pointer, or NULL if
there is not enough memory. Note that this function doesn't make the there is not enough memory. `server_param`: Could be any pointer, or NULL.
server instance to serve. Serving is done by `mg_poll_server()` function. This pointer will be passed
Web server instance has a list of active connections, initially empty, to the callback functions as `struct mg_connection::server_param` field.
and a list of URI handlers, initially empty, a list of configuration A common use case is to pass `this` pointer of the C++ wrapper class
options that can be modified by `mg_set_option()`. as `user_param`, to let the callback get the pointer to the C++
object.
`user_param`: Could be any pointer, or NULL. This pointer will be passed Note that this function doesn't make the
to callback functions as `struct mg_connection::server_param` field. server instance to serve. Serving is done by `mg_poll_server()` function.
A common use case is to pass `this` pointer of the C++ wrapper class Mongoose has single-threaded, event-driven, asynchronous, non-blocking core.
as `user_param`, to let the callback get the pointer to the C++ When server instance is created, it contains an information about
object. the configuration and the state of each connection.
Server instance is capable on listening on only one port. After creation,
`struct mg_server` has a list
of active connections, initially empty. It has a list of URI handlers,
initially empty, and configuration parameters. Configuration can be
altered by `mg_set_option()`, and new URI handlers could be added by
`mg_add_uri_handler()`.
Side-effect: on UNIX, `mg_create_server()` ignores SIGPIPE signals. If custom Side-effect: on UNIX, `mg_create_server()` ignores SIGPIPE signals. If custom
processing is required SIGPIPE, signal handler must be set up after processing is required SIGPIPE, signal handler must be set up after
@@ -60,6 +67,23 @@ with `uri`, then specified handler is called to serve the request. Thus, an
`uri` is a match prefix. For example, if `uri` is "/", then all requests will `uri` is a match prefix. For example, if `uri` is "/", then all requests will
be routed to the handler, because all URIs start with `/` character. be routed to the handler, because all URIs start with `/` character.
When mongoose buffers in HTTP request and successfully parses it, it calls
appropriate URI handler immediately for GET requests. For POST requests,
Mongoose delays the call until the whole POST request is buffered in memory.
POST data is available to the callback as `struct mg_connection::content`,
and POST data length is in `struct mg_connection::content_len`.
Note that websocket connections are treated the same way. Mongoose buffers
websocket frame in memory, and calls URI handler when frame is fully
buffered. Frame data is available `struct mg_connection::content`, and
data length is in `struct mg_connection::content_len`, i.e. very similar to
the POST request. `struct mg_connection::is_websocket` flag indicates
whether the request is websocket or not. Also, for websocket requests,
there is `struct mg_connection::wsbits` field which contains first byte
of the websocket frame which URI handler can examine. Note that to
reply to the websocket client, `mg_websocket_write()` should be used.
To reply to the plain HTTP client, `mg_write()` should be used.
void mg_set_http_error_handler(struct mg_server *, mg_handler_t); void mg_set_http_error_handler(struct mg_server *, mg_handler_t);
Adds HTTP error handler. An actual HTTP error is passed as Adds HTTP error handler. An actual HTTP error is passed as