mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-25 12:04:47 +00:00
API CHANGE: using struct mg_callbacks
This commit is contained in:
1 parent
62162ff3e4
commit
ee55d38b55
10 files changed
+282
-384
No files matched your search
+19
-25
@@ -325,34 +325,25 @@ static void redirect_to_ssl(struct mg_connection *conn,
|
||||
}
|
||||
}
|
||||
|
||||
static void *event_handler(enum mg_event event,
|
||||
struct mg_connection *conn) {
|
||||
static int begin_request_handler(struct mg_connection *conn) {
|
||||
const struct mg_request_info *request_info = mg_get_request_info(conn);
|
||||
void *processed = "yes";
|
||||
int processed = 1;
|
||||
|
||||
if (event == MG_NEW_REQUEST) {
|
||||
if (!request_info->is_ssl) {
|
||||
redirect_to_ssl(conn, request_info);
|
||||
} else if (!is_authorized(conn, request_info)) {
|
||||
redirect_to_login(conn, request_info);
|
||||
} else if (strcmp(request_info->uri, authorize_url) == 0) {
|
||||
authorize(conn, request_info);
|
||||
} else if (strcmp(request_info->uri, "/ajax/get_messages") == 0) {
|
||||
ajax_get_messages(conn, request_info);
|
||||
} else if (strcmp(request_info->uri, "/ajax/send_message") == 0) {
|
||||
ajax_send_message(conn, request_info);
|
||||
} else {
|
||||
// No suitable handler found, mark as not processed. Mongoose will
|
||||
// try to serve the request.
|
||||
processed = NULL;
|
||||
}
|
||||
} else if (event == MG_EVENT_LOG) {
|
||||
printf("%s\n", (const char *) mg_get_request_info(conn)->ev_data);
|
||||
processed = NULL;
|
||||
if (!request_info->is_ssl) {
|
||||
redirect_to_ssl(conn, request_info);
|
||||
} else if (!is_authorized(conn, request_info)) {
|
||||
redirect_to_login(conn, request_info);
|
||||
} else if (strcmp(request_info->uri, authorize_url) == 0) {
|
||||
authorize(conn, request_info);
|
||||
} else if (strcmp(request_info->uri, "/ajax/get_messages") == 0) {
|
||||
ajax_get_messages(conn, request_info);
|
||||
} else if (strcmp(request_info->uri, "/ajax/send_message") == 0) {
|
||||
ajax_send_message(conn, request_info);
|
||||
} else {
|
||||
processed = NULL;
|
||||
// No suitable handler found, mark as not processed. Mongoose will
|
||||
// try to serve the request.
|
||||
processed = 0;
|
||||
}
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
@@ -365,6 +356,7 @@ static const char *options[] = {
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
struct mg_callbacks callbacks;
|
||||
struct mg_context *ctx;
|
||||
|
||||
// Initialize random number generator. It will be used later on for
|
||||
@@ -372,7 +364,9 @@ int main(void) {
|
||||
srand((unsigned) time(0));
|
||||
|
||||
// Setup and start Mongoose
|
||||
if ((ctx = mg_start(&event_handler, NULL, options)) == NULL) {
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.begin_request = begin_request_handler;
|
||||
if ((ctx = mg_start(&callbacks, NULL, options)) == NULL) {
|
||||
printf("%s\n", "Cannot start chat server, fatal exit");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
+35
-21
@@ -2,35 +2,49 @@
|
||||
#include <string.h>
|
||||
#include "mongoose.h"
|
||||
|
||||
static void *callback(enum mg_event event,
|
||||
struct mg_connection *conn) {
|
||||
// This function will be called by mongoose on every new request.
|
||||
static int begin_request_handler(struct mg_connection *conn) {
|
||||
const struct mg_request_info *request_info = mg_get_request_info(conn);
|
||||
char content[100];
|
||||
|
||||
if (event == MG_NEW_REQUEST) {
|
||||
char content[1024];
|
||||
int content_length = snprintf(content, sizeof(content),
|
||||
"Hello from mongoose! Remote port: %d",
|
||||
request_info->remote_port);
|
||||
mg_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Content-Length: %d\r\n" // Always set Content-Length
|
||||
"\r\n"
|
||||
"%s",
|
||||
content_length, content);
|
||||
// Mark as processed
|
||||
return "";
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
// Prepare the message we're going to send
|
||||
int content_length = snprintf(content, sizeof(content),
|
||||
"Hello from mongoose! Remote port: %d",
|
||||
request_info->remote_port);
|
||||
|
||||
// Send HTTP reply to the client
|
||||
mg_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Content-Length: %d\r\n" // Always set Content-Length
|
||||
"\r\n"
|
||||
"%s",
|
||||
content_length, content);
|
||||
|
||||
// Returning non-zero tells mongoose that our function has replied to
|
||||
// the client, and mongoose should not send client any more data.
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_context *ctx;
|
||||
struct mg_callbacks callbacks;
|
||||
|
||||
// List of options. Last element must be NULL.
|
||||
const char *options[] = {"listening_ports", "8080", NULL};
|
||||
|
||||
ctx = mg_start(&callback, NULL, options);
|
||||
getchar(); // Wait until user hits "enter"
|
||||
// Prepare callbacks structure. We have only one callback, the rest are NULL.
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.begin_request = begin_request_handler;
|
||||
|
||||
// Start the web server.
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
|
||||
// Wait until user hits "enter". Server is running in separate thread.
|
||||
// Navigating to http://localhost:8080 will invoke begin_request_handler().
|
||||
getchar();
|
||||
|
||||
// Stop the server.
|
||||
mg_stop(ctx);
|
||||
|
||||
return 0;
|
||||
|
||||
+27
-32
@@ -10,50 +10,45 @@ static const char *html_form =
|
||||
"<input type=\"submit\" />"
|
||||
"</form></body></html>";
|
||||
|
||||
static void *callback(enum mg_event event,
|
||||
struct mg_connection *conn) {
|
||||
static int begin_request_handler(struct mg_connection *conn) {
|
||||
const struct mg_request_info *ri = mg_get_request_info(conn);
|
||||
char post_data[1024], input1[sizeof(post_data)], input2[sizeof(post_data)];
|
||||
int post_data_len;
|
||||
|
||||
if (event == MG_NEW_REQUEST) {
|
||||
if (!strcmp(ri->uri, "/handle_post_request")) {
|
||||
// User has submitted a form, show submitted data and a variable value
|
||||
char post_data[1024],
|
||||
input1[sizeof(post_data)], input2[sizeof(post_data)];
|
||||
int post_data_len;
|
||||
if (!strcmp(ri->uri, "/handle_post_request")) {
|
||||
// User has submitted a form, show submitted data and a variable value
|
||||
post_data_len = mg_read(conn, post_data, sizeof(post_data));
|
||||
|
||||
// Read POST data
|
||||
post_data_len = mg_read(conn, post_data, sizeof(post_data));
|
||||
// Parse form data. input1 and input2 are guaranteed to be NUL-terminated
|
||||
mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1));
|
||||
mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2));
|
||||
|
||||
// Parse form data. input1 and input2 are guaranteed to be NUL-terminated
|
||||
mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1));
|
||||
mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2));
|
||||
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n\r\n"
|
||||
"Submitted data: [%.*s]\n"
|
||||
"Submitted data length: %d bytes\n"
|
||||
"input_1: [%s]\n"
|
||||
"input_2: [%s]\n",
|
||||
post_data_len, post_data, post_data_len, input1, input2);
|
||||
} else {
|
||||
// Show HTML form.
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Length: %d\r\n"
|
||||
"Content-Type: text/html\r\n\r\n%s",
|
||||
(int) strlen(html_form), html_form);
|
||||
}
|
||||
// Mark as processed
|
||||
return "";
|
||||
// Send reply to the client, showing submitted form values.
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n\r\n"
|
||||
"Submitted data: [%.*s]\n"
|
||||
"Submitted data length: %d bytes\n"
|
||||
"input_1: [%s]\n"
|
||||
"input_2: [%s]\n",
|
||||
post_data_len, post_data, post_data_len, input1, input2);
|
||||
} else {
|
||||
return NULL;
|
||||
// Show HTML form.
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Length: %d\r\n"
|
||||
"Content-Type: text/html\r\n\r\n%s",
|
||||
(int) strlen(html_form), html_form);
|
||||
}
|
||||
return 1; // Mark request as processed
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_context *ctx;
|
||||
const char *options[] = {"listening_ports", "8080", NULL};
|
||||
struct mg_callbacks callbacks;
|
||||
|
||||
ctx = mg_start(&callback, NULL, options);
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.begin_request = begin_request_handler;
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
getchar(); // Wait until user hits "enter"
|
||||
mg_stop(ctx);
|
||||
|
||||
|
||||
+28
-26
@@ -17,42 +17,44 @@ typedef __int64 int64_t;
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static void *callback(enum mg_event event, struct mg_connection *conn) {
|
||||
if (event == MG_NEW_REQUEST) {
|
||||
if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) {
|
||||
mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
|
||||
mg_upload(conn, "/tmp");
|
||||
} else {
|
||||
// Show HTML form. Make sure it has enctype="multipart/form-data" attr.
|
||||
static const char *html_form =
|
||||
"<html><body>Upload example."
|
||||
"<form method=\"POST\" action=\"/handle_post_request\" "
|
||||
" enctype=\"multipart/form-data\">"
|
||||
"<input type=\"file\" name=\"file\" /> <br/>"
|
||||
"<input type=\"submit\" value=\"Upload\" />"
|
||||
"</form></body></html>";
|
||||
static int begin_request_handler(struct mg_connection *conn) {
|
||||
if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) {
|
||||
mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
|
||||
mg_upload(conn, "/tmp");
|
||||
} else {
|
||||
// Show HTML form. Make sure it has enctype="multipart/form-data" attr.
|
||||
static const char *html_form =
|
||||
"<html><body>Upload example."
|
||||
"<form method=\"POST\" action=\"/handle_post_request\" "
|
||||
" enctype=\"multipart/form-data\">"
|
||||
"<input type=\"file\" name=\"file\" /> <br/>"
|
||||
"<input type=\"submit\" value=\"Upload\" />"
|
||||
"</form></body></html>";
|
||||
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Length: %d\r\n"
|
||||
"Content-Type: text/html\r\n\r\n%s",
|
||||
(int) strlen(html_form), html_form);
|
||||
}
|
||||
// Mark as processed
|
||||
return "";
|
||||
} else if (event == MG_UPLOAD) {
|
||||
mg_printf(conn, "Saved [%s]", mg_get_request_info(conn)->ev_data);
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Length: %d\r\n"
|
||||
"Content-Type: text/html\r\n\r\n%s",
|
||||
(int) strlen(html_form), html_form);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
// Mark request as processed
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void upload_handler(struct mg_connection *conn, const char *path) {
|
||||
mg_printf(conn, "Saved [%s]", path);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_context *ctx;
|
||||
const char *options[] = {"listening_ports", "8080", NULL};
|
||||
struct mg_callbacks callbacks;
|
||||
|
||||
ctx = mg_start(&callback, NULL, options);
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.begin_request = begin_request_handler;
|
||||
callbacks.upload = upload_handler;
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
getchar(); // Wait until user hits "enter"
|
||||
pause();
|
||||
mg_stop(ctx);
|
||||
|
||||
return 0;
|
||||
|
||||
+49
-48
@@ -5,69 +5,70 @@
|
||||
#include <string.h>
|
||||
#include "mongoose.h"
|
||||
|
||||
static void *callback(enum mg_event event, struct mg_connection *conn) {
|
||||
if (event == MG_WEBSOCKET_READY) {
|
||||
unsigned char buf[40];
|
||||
buf[0] = 0x81;
|
||||
buf[1] = snprintf((char *) buf + 2, sizeof(buf) - 2, "%s", "server ready");
|
||||
mg_write(conn, buf, 2 + buf[1]);
|
||||
return ""; // MG_WEBSOCKET_READY return value is ignored
|
||||
} else if (event == MG_WEBSOCKET_MESSAGE) {
|
||||
unsigned char buf[200], reply[200];
|
||||
int n, i, mask_len, xor, msg_len, len;
|
||||
static void websocket_ready_handler(struct mg_connection *conn) {
|
||||
unsigned char buf[40];
|
||||
buf[0] = 0x81;
|
||||
buf[1] = snprintf((char *) buf + 2, sizeof(buf) - 2, "%s", "server ready");
|
||||
mg_write(conn, buf, 2 + buf[1]);
|
||||
}
|
||||
|
||||
// Read message from the client.
|
||||
// Accept only small (<126 bytes) messages.
|
||||
len = 0;
|
||||
msg_len = mask_len = 0;
|
||||
for (;;) {
|
||||
if ((n = mg_read(conn, buf + len, sizeof(buf) - len)) <= 0) {
|
||||
return ""; // Read error, close websocket
|
||||
static int websocket_data_handler(struct mg_connection *conn) {
|
||||
unsigned char buf[200], reply[200];
|
||||
int n, i, mask_len, xor, msg_len, len;
|
||||
|
||||
// Read message from the client.
|
||||
// Accept only small (<126 bytes) messages.
|
||||
len = 0;
|
||||
msg_len = mask_len = 0;
|
||||
for (;;) {
|
||||
if ((n = mg_read(conn, buf + len, sizeof(buf) - len)) <= 0) {
|
||||
return 0; // Read error, close websocket
|
||||
}
|
||||
len += n;
|
||||
if (len >= 2) {
|
||||
msg_len = buf[1] & 127;
|
||||
mask_len = (buf[1] & 128) ? 4 : 0;
|
||||
if (msg_len > 125) {
|
||||
return 0; // Message is too long, close websocket
|
||||
}
|
||||
len += n;
|
||||
if (len >= 2) {
|
||||
msg_len = buf[1] & 127;
|
||||
mask_len = (buf[1] & 128) ? 4 : 0;
|
||||
if (msg_len > 125) {
|
||||
return ""; // Message is too long, close websocket
|
||||
}
|
||||
// If we've buffered the whole message, exit the loop
|
||||
if (len >= 2 + mask_len + msg_len) {
|
||||
break;
|
||||
}
|
||||
// If we've buffered the whole message, exit the loop
|
||||
if (len >= 2 + mask_len + msg_len) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare frame
|
||||
reply[0] = 0x81; // text, FIN set
|
||||
reply[1] = msg_len;
|
||||
|
||||
// Copy message from request to reply, applying the mask if required.
|
||||
for (i = 0; i < msg_len; i++) {
|
||||
xor = mask_len == 0 ? 0 : buf[2 + (i % 4)];
|
||||
reply[i + 2] = buf[i + 2 + mask_len] ^ xor;
|
||||
}
|
||||
|
||||
// Echo the message back to the client
|
||||
mg_write(conn, reply, 2 + msg_len);
|
||||
|
||||
// Return non-NULL means stoping websocket conversation.
|
||||
// Close the conversation if client has sent us "exit" string.
|
||||
return memcmp(reply + 2, "exit", 4) == 0 ? "" : NULL;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Prepare frame
|
||||
reply[0] = 0x81; // text, FIN set
|
||||
reply[1] = msg_len;
|
||||
|
||||
// Copy message from request to reply, applying the mask if required.
|
||||
for (i = 0; i < msg_len; i++) {
|
||||
xor = mask_len == 0 ? 0 : buf[2 + (i % 4)];
|
||||
reply[i + 2] = buf[i + 2 + mask_len] ^ xor;
|
||||
}
|
||||
|
||||
// Echo the message back to the client
|
||||
mg_write(conn, reply, 2 + msg_len);
|
||||
|
||||
// Returnint zero means stoping websocket conversation.
|
||||
// Close the conversation if client has sent us "exit" string.
|
||||
return memcmp(reply + 2, "exit", 4);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_context *ctx;
|
||||
struct mg_callbacks callbacks;
|
||||
const char *options[] = {
|
||||
"listening_ports", "8080",
|
||||
"document_root", "websocket_html_root",
|
||||
NULL
|
||||
};
|
||||
|
||||
ctx = mg_start(&callback, NULL, options);
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.websocket_ready = websocket_ready_handler;
|
||||
callbacks.websocket_data = websocket_data_handler;
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
getchar(); // Wait until user hits "enter"
|
||||
mg_stop(ctx);
|
||||
|
||||
|
||||
Reference in new issue
Block a user