mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-06 15:51:25 +00:00
mg_add_uri_handler -> mg_set_request_handler()
This commit is contained in:
1 parent
a6cfbd2536
commit
14526a2109
12 files changed
+116
-120
No files matched your search
+1
-1
@@ -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
|
||||
RM = rm -rf
|
||||
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
#include "mongoose.h"
|
||||
|
||||
static int auth_handler(struct mg_connection *conn) {
|
||||
int result = 0; // Not authorized
|
||||
int result = MG_AUTH_FAIL; // Not authorized
|
||||
FILE *fp;
|
||||
|
||||
// To populate passwords file, do
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@
|
||||
// This function will be called by mongoose on every new request
|
||||
static int index_html(struct mg_connection *conn) {
|
||||
mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri);
|
||||
return 1;
|
||||
return MG_REQUEST_PROCESSED;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
@@ -14,7 +14,7 @@ int main(void) {
|
||||
// Create and configure the server
|
||||
server = mg_create_server(NULL);
|
||||
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
|
||||
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
|
||||
|
||||
@@ -6,7 +6,7 @@ static int request_handler(struct mg_connection *conn) {
|
||||
mg_send_header(conn, "Content-Type", "text/plain");
|
||||
mg_printf_data(conn, "This is a reply from server instance # %s",
|
||||
(char *) conn->server_param);
|
||||
return 0;
|
||||
return MG_REQUEST_PROCESSED;
|
||||
}
|
||||
|
||||
static void *serve(void *server) {
|
||||
@@ -20,8 +20,8 @@ int main(void) {
|
||||
server1 = mg_create_server((void *) "1");
|
||||
server2 = mg_create_server((void *) "2");
|
||||
|
||||
mg_add_uri_handler(server1, "/", request_handler);
|
||||
mg_add_uri_handler(server2, "/", request_handler);
|
||||
mg_set_request_handler(server1, request_handler);
|
||||
mg_set_request_handler(server2, request_handler);
|
||||
|
||||
// Make both server1 and server2 listen on the same socket
|
||||
mg_set_option(server1, "listening_port", "8080");
|
||||
|
||||
+2
-2
@@ -34,13 +34,13 @@ static int handler(struct mg_connection *conn) {
|
||||
mg_send_data(conn, html_form, strlen(html_form));
|
||||
}
|
||||
|
||||
return 1;
|
||||
return MG_REQUEST_PROCESSED;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_server *server = mg_create_server(NULL);
|
||||
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"));
|
||||
for (;;) {
|
||||
mg_poll_server(server, 1000);
|
||||
|
||||
+2
-2
@@ -30,7 +30,7 @@ static int index_html(struct mg_connection *conn) {
|
||||
|
||||
mg_printf_data(conn, "%s", "</body></html>");
|
||||
|
||||
return 1;
|
||||
return MG_REQUEST_PROCESSED;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
@@ -39,7 +39,7 @@ int main(void) {
|
||||
// Create and configure the server
|
||||
server = mg_create_server(NULL);
|
||||
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
|
||||
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
|
||||
|
||||
@@ -9,7 +9,7 @@ static int iterate_callback(struct mg_connection *c) {
|
||||
int len = snprintf(buf, sizeof(buf), "%d", * (int *) c->connection_param);
|
||||
mg_websocket_write(c, 1, buf, len);
|
||||
}
|
||||
return 1;
|
||||
return MG_REQUEST_PROCESSED;
|
||||
}
|
||||
|
||||
static int index_html(struct mg_connection *conn) {
|
||||
@@ -21,11 +21,12 @@ static int index_html(struct mg_connection *conn) {
|
||||
// times for connection lifetime.
|
||||
// Echo websocket data back to the client.
|
||||
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 {
|
||||
mg_send_header(conn, "Content-Type", "text/html");
|
||||
mg_send_data(conn, index_html, index_size);
|
||||
return 1;
|
||||
return MG_REQUEST_PROCESSED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,12 +35,12 @@ int main(void) {
|
||||
unsigned int current_timer = 0, last_timer = 0;
|
||||
|
||||
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"));
|
||||
for (;;) {
|
||||
current_timer = mg_poll_server(server, 100);
|
||||
if (current_timer - last_timer > 1) {
|
||||
if (current_timer - last_timer > 0) {
|
||||
last_timer = current_timer;
|
||||
mg_iterate_over_connections(server, iterate_callback, ¤t_timer);
|
||||
}
|
||||
|
||||
@@ -24,10 +24,13 @@
|
||||
out('DISCONNECTED');
|
||||
};
|
||||
websocket.onmessage = function(ev) {
|
||||
if (!ev.data) return; // No data, this is a PING message, ignore it
|
||||
out('<span style="color: blue;">RESPONSE: ' + ev.data + ' </span>');
|
||||
num_messages++;
|
||||
if (num_messages > 100) {
|
||||
if (!ev.data) {
|
||||
out('<span style="color: blue;">PING... </span>');
|
||||
} else {
|
||||
out('<span style="color: blue;">RESPONSE: ' + ev.data + ' </span>');
|
||||
num_messages++;
|
||||
}
|
||||
if (num_messages > 15) {
|
||||
websocket.send('exit');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user