Statistics
| Branch: | Tag: | Revision:

mongoose / examples / WinCE / http_server / http_server.cpp @ eaef5bd1

History | View | Annotate | Download (1.68 KB)

1
#include "stdafx.h"
2
#include "mongoose.h"
3

    
4
static const char *s_http_port = "8000";
5
static struct mg_serve_http_opts s_http_server_opts;
6

    
7
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
8
 switch (ev) {
9
    case MG_EV_ACCEPT: {
10
      char addr[32];
11
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
12
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
13
      printf("%p: Connection from %s\r\n", nc, addr);
14
      break;
15
    }
16
    case MG_EV_HTTP_REQUEST: {
17
      struct http_message *hm = (struct http_message *) ev_data;
18
      char addr[32];
19
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
20
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
21
      printf("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p,
22
             (int) hm->uri.len, hm->uri.p);
23
      mg_send_response_line(nc, 200,
24
                            "Content-Type: text/html\r\n"
25
                            "Connection: close");
26
      mg_printf(nc,
27
                "\r\n<h1>Hello, %s!</h1>\r\n"
28
                "You asked for %.*s\r\n",
29
                addr, (int) hm->uri.len, hm->uri.p);
30
      nc->flags |= MG_F_SEND_AND_CLOSE;
31
      break;
32
    }
33
    case MG_EV_CLOSE: {
34
      printf("%p: Connection closed\r4\n", nc);
35
      break;
36
    }
37
  }
38
}
39

    
40
int _tmain(int argc, _TCHAR* argv[]) {
41
  struct mg_mgr mgr;
42
  struct mg_connection *nc;
43

    
44
  mg_mgr_init(&mgr, NULL);
45
  printf("Starting web server on port %s\n", s_http_port);
46
  nc = mg_bind(&mgr, s_http_port, ev_handler);
47
  if (nc == NULL) {
48
    printf("Failed to create listener\n");
49
    return 1;
50
  }
51

    
52
  mg_set_protocol_http_websocket(nc);
53

    
54
  for (;;) {
55
    mg_mgr_poll(&mgr, 1000);
56
  }
57
  mg_mgr_free(&mgr);
58

    
59
  return 0;
60
}