2011-06-18 07:29:46 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
|
|
|
static void *callback(enum mg_event event,
|
2012-08-03 18:18:31 +08:00
|
|
|
struct mg_connection *conn) {
|
|
|
|
const struct mg_request_info *request_info = mg_get_request_info(conn);
|
|
|
|
|
2011-06-18 07:29:46 +08:00
|
|
|
if (event == MG_NEW_REQUEST) {
|
2012-06-06 14:10:15 +08:00
|
|
|
char content[1024];
|
|
|
|
int content_length = snprintf(content, sizeof(content),
|
|
|
|
"Hello from mongoose! Remote port: %d",
|
|
|
|
request_info->remote_port);
|
2012-06-05 02:08:43 +08:00
|
|
|
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",
|
2012-06-06 14:10:15 +08:00
|
|
|
content_length, content);
|
2012-06-05 02:08:43 +08:00
|
|
|
// Mark as processed
|
|
|
|
return "";
|
2011-06-18 07:29:46 +08:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
struct mg_context *ctx;
|
|
|
|
const char *options[] = {"listening_ports", "8080", NULL};
|
|
|
|
|
|
|
|
ctx = mg_start(&callback, NULL, options);
|
|
|
|
getchar(); // Wait until user hits "enter"
|
|
|
|
mg_stop(ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|