mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-21 22:18:03 +08:00
eaef5bd133
This reverts commit 1a17e17c462bdd4e1d26d8742f8b7087273e04c2. PUBLISHED_FROM=80028de308c9a021955d1425d2bfee8feb85f193
61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
#include "stdafx.h"
|
|
#include "mongoose.h"
|
|
|
|
static const char *s_http_port = "8000";
|
|
static struct mg_serve_http_opts s_http_server_opts;
|
|
|
|
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
switch (ev) {
|
|
case MG_EV_ACCEPT: {
|
|
char addr[32];
|
|
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
|
|
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
|
|
printf("%p: Connection from %s\r\n", nc, addr);
|
|
break;
|
|
}
|
|
case MG_EV_HTTP_REQUEST: {
|
|
struct http_message *hm = (struct http_message *) ev_data;
|
|
char addr[32];
|
|
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
|
|
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
|
|
printf("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p,
|
|
(int) hm->uri.len, hm->uri.p);
|
|
mg_send_response_line(nc, 200,
|
|
"Content-Type: text/html\r\n"
|
|
"Connection: close");
|
|
mg_printf(nc,
|
|
"\r\n<h1>Hello, %s!</h1>\r\n"
|
|
"You asked for %.*s\r\n",
|
|
addr, (int) hm->uri.len, hm->uri.p);
|
|
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
break;
|
|
}
|
|
case MG_EV_CLOSE: {
|
|
printf("%p: Connection closed\r4\n", nc);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int _tmain(int argc, _TCHAR* argv[]) {
|
|
struct mg_mgr mgr;
|
|
struct mg_connection *nc;
|
|
|
|
mg_mgr_init(&mgr, NULL);
|
|
printf("Starting web server on port %s\n", s_http_port);
|
|
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
|
if (nc == NULL) {
|
|
printf("Failed to create listener\n");
|
|
return 1;
|
|
}
|
|
|
|
mg_set_protocol_http_websocket(nc);
|
|
|
|
for (;;) {
|
|
mg_mgr_poll(&mgr, 1000);
|
|
}
|
|
mg_mgr_free(&mgr);
|
|
|
|
return 0;
|
|
}
|