2011-06-18 07:29:46 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
2013-02-02 00:48:30 +08:00
|
|
|
// This function will be called by mongoose on every new request.
|
|
|
|
static int begin_request_handler(struct mg_connection *conn) {
|
2012-08-03 18:18:31 +08:00
|
|
|
const struct mg_request_info *request_info = mg_get_request_info(conn);
|
2013-02-02 00:48:30 +08:00
|
|
|
char content[100];
|
2012-08-03 18:18:31 +08:00
|
|
|
|
2013-02-02 00:48:30 +08:00
|
|
|
// 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;
|
2011-06-18 07:29:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
struct mg_context *ctx;
|
2013-02-02 00:48:30 +08:00
|
|
|
struct mg_callbacks callbacks;
|
|
|
|
|
|
|
|
// List of options. Last element must be NULL.
|
2011-06-18 07:29:46 +08:00
|
|
|
const char *options[] = {"listening_ports", "8080", NULL};
|
|
|
|
|
2013-02-02 00:48:30 +08:00
|
|
|
// 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.
|
2011-06-18 07:29:46 +08:00
|
|
|
mg_stop(ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|