Skip to content

Commit

Permalink
mg_add_uri_handler -> mg_set_request_handler()
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Jan 28, 2014
1 parent a6cfbd2 commit 14526a2
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 121 deletions.
41 changes: 26 additions & 15 deletions docs/API.md
Expand Up @@ -60,12 +60,26 @@ that take `struct mg_server *` parameter. Mongoose does not
mutex-protect `struct mg_server *`, therefore the best practice is mutex-protect `struct mg_server *`, therefore the best practice is
to call server management functions from the same thread (an IO thread). to call server management functions from the same thread (an IO thread).


void mg_add_uri_handler(struct mg_server *, const char *uri, mg_handler_t); void mg_set_auth_handler(struct mg_server *, mg_handler_t handler);


Adds an URI handler. If Mongoose gets a request and request's URI starts Sets authorization handler. Called by Mongoose on each request, before
with `uri`, then specified handler is called to serve the request. Thus, an performing any other action. Handler can return either `MG_AUTH_OK` or
`uri` is a match prefix. For example, if `uri` is "/", then all requests will `MG_AUTH_FAIL`. If `handler` returns `MG_AUTH_FAIL`, then Mongoose sends
be routed to the handler, because all URIs start with `/` character. digest authorization request to the client. If `handler returns `MG_AUTH_OK`,
then mongoose proceeds with handling the request. Handler function can use
`mg_authorize_digest()` function to verify authorization, or implement any other
custom authorization mechanism.

void mg_set_request_handler(struct mg_server *, mg_handler_t handler);

Sets a request handler. When set, `handler` will be called for each request.
Possible return values from a handler function are `MG_REQUEST_NOT_PROCESSED`,
`MG_REQUEST_PROCESSED` and `MG_REQUEST_CALL_AGAIN`. If handler returns
`MG_REQUEST_NOT_PROCESSED`, mongoose will proceed with handling the request.
If handler returns `MG_REQUEST_PROCESSED`, that signals mongoose that handler
has already processed the connection, and mongoose will skip to the next
request. `MG_REQUEST_CALL_AGAIN` tells mongoose to call request handler
again and again on each `mg_poll_server()` iteration.


When mongoose buffers in HTTP request and successfully parses it, it calls When mongoose buffers in HTTP request and successfully parses it, it calls
appropriate URI handler immediately for GET requests. For POST requests, appropriate URI handler immediately for GET requests. For POST requests,
Expand All @@ -82,19 +96,16 @@ whether the request is websocket or not. Also, for websocket requests,
there is `struct mg_connection::wsbits` field which contains first byte there is `struct mg_connection::wsbits` field which contains first byte
of the websocket frame which URI handler can examine. Note that to of the websocket frame which URI handler can examine. Note that to
reply to the websocket client, `mg_websocket_write()` should be used. reply to the websocket client, `mg_websocket_write()` should be used.
To reply to the plain HTTP client, `mg_write_data()` should be used. To reply to the plain HTTP client, `mg_write_data()` should be used. Note that

websocket handler must return either `MG_CLIENT_CLOSE` or `MG_CLIENT_CONTINUE`
An URI handler must return a value. If URI handler has sent all data, value.
it should return `1`. If URI handler returns `0`, that signals Mongoose
that URI handler hasn't finished sending data to the client. In this case,
Mongoose will call URI handler after each successful socket write.
`struct mg_connection::wsbits` flag will indicate the status of the write,
`1` means that write has failed and connection will be closed.


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
`struct mg_connection::status_code` parameter. If handler returns 0, it `struct mg_connection::status_code` parameter.
Hanlder should return either `MG_ERROR_PROCESSED` or `MG_ERROR_NOT_PROCESSED`.
If handler returns `MG_ERROR_NOT_PROCESSED`, it
means a handler has not processed the connection, and mongoose proceeds means a handler has not processed the connection, and mongoose proceeds
with sending HTTP error to the client. Otherwise, mongoose does nothing. with sending HTTP error to the client. Otherwise, mongoose does nothing.


Expand Down
2 changes: 1 addition & 1 deletion examples/Makefile
@@ -1,4 +1,4 @@
CFLAGS = -W -Wall -I.. -pthread -g -pipe $(COPT) CFLAGS = -W -Wall -I.. -pthread -g -pipe $(CFLAGS_EXTRA)
DLL_FLAGS = -DLUA_COMPAT_ALL -I../build DLL_FLAGS = -DLUA_COMPAT_ALL -I../build
RM = rm -rf RM = rm -rf


Expand Down
2 changes: 1 addition & 1 deletion examples/auth.c
Expand Up @@ -3,7 +3,7 @@
#include "mongoose.h" #include "mongoose.h"


static int auth_handler(struct mg_connection *conn) { static int auth_handler(struct mg_connection *conn) {
int result = 0; // Not authorized int result = MG_AUTH_FAIL; // Not authorized
FILE *fp; FILE *fp;


// To populate passwords file, do // To populate passwords file, do
Expand Down
4 changes: 2 additions & 2 deletions examples/hello.c
Expand Up @@ -5,7 +5,7 @@
// This function will be called by mongoose on every new request // This function will be called by mongoose on every new request
static int index_html(struct mg_connection *conn) { static int index_html(struct mg_connection *conn) {
mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri); mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri);
return 1; return MG_REQUEST_PROCESSED;
} }


int main(void) { int main(void) {
Expand All @@ -14,7 +14,7 @@ int main(void) {
// Create and configure the server // Create and configure the server
server = mg_create_server(NULL); server = mg_create_server(NULL);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_add_uri_handler(server, "/", index_html); mg_set_request_handler(server, index_html);


// Serve request. Hit Ctrl-C to terminate the program // Serve request. Hit Ctrl-C to terminate the program
printf("Starting on port %s\n", mg_get_option(server, "listening_port")); printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
Expand Down
6 changes: 3 additions & 3 deletions examples/multi_threaded.c
Expand Up @@ -6,7 +6,7 @@ static int request_handler(struct mg_connection *conn) {
mg_send_header(conn, "Content-Type", "text/plain"); mg_send_header(conn, "Content-Type", "text/plain");
mg_printf_data(conn, "This is a reply from server instance # %s", mg_printf_data(conn, "This is a reply from server instance # %s",
(char *) conn->server_param); (char *) conn->server_param);
return 0; return MG_REQUEST_PROCESSED;
} }


static void *serve(void *server) { static void *serve(void *server) {
Expand All @@ -20,8 +20,8 @@ int main(void) {
server1 = mg_create_server((void *) "1"); server1 = mg_create_server((void *) "1");
server2 = mg_create_server((void *) "2"); server2 = mg_create_server((void *) "2");


mg_add_uri_handler(server1, "/", request_handler); mg_set_request_handler(server1, request_handler);
mg_add_uri_handler(server2, "/", request_handler); mg_set_request_handler(server2, request_handler);


// Make both server1 and server2 listen on the same socket // Make both server1 and server2 listen on the same socket
mg_set_option(server1, "listening_port", "8080"); mg_set_option(server1, "listening_port", "8080");
Expand Down
4 changes: 2 additions & 2 deletions examples/post.c
Expand Up @@ -34,13 +34,13 @@ static int handler(struct mg_connection *conn) {
mg_send_data(conn, html_form, strlen(html_form)); mg_send_data(conn, html_form, strlen(html_form));
} }


return 1; return MG_REQUEST_PROCESSED;
} }


int main(void) { int main(void) {
struct mg_server *server = mg_create_server(NULL); struct mg_server *server = mg_create_server(NULL);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_add_uri_handler(server, "/", handler); mg_set_request_handler(server, handler);
printf("Starting on port %s\n", mg_get_option(server, "listening_port")); printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) { for (;;) {
mg_poll_server(server, 1000); mg_poll_server(server, 1000);
Expand Down
4 changes: 2 additions & 2 deletions examples/upload.c
Expand Up @@ -30,7 +30,7 @@ static int index_html(struct mg_connection *conn) {


mg_printf_data(conn, "%s", "</body></html>"); mg_printf_data(conn, "%s", "</body></html>");


return 1; return MG_REQUEST_PROCESSED;
} }


int main(void) { int main(void) {
Expand All @@ -39,7 +39,7 @@ int main(void) {
// Create and configure the server // Create and configure the server
server = mg_create_server(NULL); server = mg_create_server(NULL);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_add_uri_handler(server, "/", index_html); mg_set_request_handler(server, index_html);


// Serve request. Hit Ctrl-C to terminate the program // Serve request. Hit Ctrl-C to terminate the program
printf("Starting on port %s\n", mg_get_option(server, "listening_port")); printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
Expand Down
11 changes: 6 additions & 5 deletions examples/websocket.c
Expand Up @@ -9,7 +9,7 @@ static int iterate_callback(struct mg_connection *c) {
int len = snprintf(buf, sizeof(buf), "%d", * (int *) c->connection_param); int len = snprintf(buf, sizeof(buf), "%d", * (int *) c->connection_param);
mg_websocket_write(c, 1, buf, len); mg_websocket_write(c, 1, buf, len);
} }
return 1; return MG_REQUEST_PROCESSED;
} }


static int index_html(struct mg_connection *conn) { static int index_html(struct mg_connection *conn) {
Expand All @@ -21,11 +21,12 @@ static int index_html(struct mg_connection *conn) {
// times for connection lifetime. // times for connection lifetime.
// Echo websocket data back to the client. // Echo websocket data back to the client.
mg_websocket_write(conn, 1, conn->content, conn->content_len); mg_websocket_write(conn, 1, conn->content, conn->content_len);
return conn->content_len == 4 && !memcmp(conn->content, "exit", 4); return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ?
MG_CLIENT_CLOSE : MG_CLIENT_CONTINUE;
} else { } else {
mg_send_header(conn, "Content-Type", "text/html"); mg_send_header(conn, "Content-Type", "text/html");
mg_send_data(conn, index_html, index_size); mg_send_data(conn, index_html, index_size);
return 1; return MG_REQUEST_PROCESSED;
} }
} }


Expand All @@ -34,12 +35,12 @@ int main(void) {
unsigned int current_timer = 0, last_timer = 0; unsigned int current_timer = 0, last_timer = 0;


mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_add_uri_handler(server, "/", index_html); mg_set_request_handler(server, index_html);


printf("Started on port %s\n", mg_get_option(server, "listening_port")); printf("Started on port %s\n", mg_get_option(server, "listening_port"));
for (;;) { for (;;) {
current_timer = mg_poll_server(server, 100); current_timer = mg_poll_server(server, 100);
if (current_timer - last_timer > 1) { if (current_timer - last_timer > 0) {
last_timer = current_timer; last_timer = current_timer;
mg_iterate_over_connections(server, iterate_callback, &current_timer); mg_iterate_over_connections(server, iterate_callback, &current_timer);
} }
Expand Down
11 changes: 7 additions & 4 deletions examples/websocket.html
Expand Up @@ -24,10 +24,13 @@
out('DISCONNECTED'); out('DISCONNECTED');
}; };
websocket.onmessage = function(ev) { websocket.onmessage = function(ev) {
if (!ev.data) return; // No data, this is a PING message, ignore it if (!ev.data) {
out('<span style="color: blue;">RESPONSE: ' + ev.data + ' </span>'); out('<span style="color: blue;">PING... </span>');
num_messages++; } else {
if (num_messages > 100) { out('<span style="color: blue;">RESPONSE: ' + ev.data + ' </span>');
num_messages++;
}
if (num_messages > 15) {
websocket.send('exit'); websocket.send('exit');
} }
}; };
Expand Down

0 comments on commit 14526a2

Please sign in to comment.