embedded_c.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2013-2015 the CivetWeb developers
  3. * Copyright (c) 2013 No Face Press, LLC
  4. * License http://opensource.org/licenses/mit-license.php MIT License
  5. */
  6. /* Simple example program on how to use Embedded C interface. */
  7. #ifdef _WIN32
  8. #include <Windows.h>
  9. #else
  10. #include <unistd.h>
  11. #endif
  12. #include <string.h>
  13. #include "civetweb.h"
  14. #define DOCUMENT_ROOT "."
  15. #ifdef USE_IPV6
  16. #define PORT "8888,[::]:8888"
  17. #else
  18. #define PORT "8888"
  19. #endif
  20. #define EXAMPLE_URI "/example"
  21. #define EXIT_URI "/exit"
  22. int exitNow = 0;
  23. int ExampleHandler(struct mg_connection *conn, void *cbdata)
  24. {
  25. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  26. mg_printf(conn, "<html><body>");
  27. mg_printf(conn, "<h2>This is an example text from a C handler</h2>");
  28. mg_printf(conn, "<p>To see a page from the A handler <a href=\"A\">click A</a></p>");
  29. mg_printf(conn, "<p>To see a page from the A handler <a href=\"A/A\">click A/A</a></p>");
  30. mg_printf(conn, "<p>To see a page from the A/B handler <a href=\"A/B\">click A/B</a></p>");
  31. mg_printf(conn, "<p>To see a page from the B handler (0) <a href=\"B\">click B</a></p>");
  32. mg_printf(conn, "<p>To see a page from the B handler (1) <a href=\"B/A\">click B/A</a></p>");
  33. mg_printf(conn, "<p>To see a page from the B handler (2) <a href=\"B/B\">click B/B</a></p>");
  34. mg_printf(conn, "<p>To see a page from the *.foo handler <a href=\"xy.foo\">click xy.foo</a></p>");
  35. #ifdef USE_WEBSOCKET
  36. mg_printf(conn, "<p>To test websocket handler <a href=\"/websocket\">click websocket</a></p>");
  37. #endif
  38. mg_printf(conn, "<p>To exit <a href=\"%s\">click exit</a></p>", EXIT_URI);
  39. mg_printf(conn, "</body></html>\n");
  40. return 1;
  41. }
  42. int ExitHandler(struct mg_connection *conn, void *cbdata)
  43. {
  44. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  45. mg_printf(conn, "Bye!\n");
  46. exitNow = 1;
  47. return 1;
  48. }
  49. int AHandler(struct mg_connection *conn, void *cbdata)
  50. {
  51. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  52. mg_printf(conn, "<html><body>");
  53. mg_printf(conn, "<h2>This is the A handler!!!</h2>");
  54. mg_printf(conn, "</body></html>\n");
  55. return 1;
  56. }
  57. int ABHandler(struct mg_connection *conn, void *cbdata)
  58. {
  59. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  60. mg_printf(conn, "<html><body>");
  61. mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
  62. mg_printf(conn, "</body></html>\n");
  63. return 1;
  64. }
  65. int BXHandler(struct mg_connection *conn, void *cbdata)
  66. {
  67. /* Handler may access the request info using mg_get_request_info */
  68. const struct mg_request_info * req_info = mg_get_request_info(conn);
  69. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  70. mg_printf(conn, "<html><body>");
  71. mg_printf(conn, "<h2>This is the BX handler %p!!!</h2>", cbdata);
  72. mg_printf(conn, "<p>The actual uri is %s</p>", req_info->uri);
  73. mg_printf(conn, "</body></html>\n");
  74. return 1;
  75. }
  76. int FooHandler(struct mg_connection *conn, void *cbdata)
  77. {
  78. /* Handler may access the request info using mg_get_request_info */
  79. const struct mg_request_info * req_info = mg_get_request_info(conn);
  80. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  81. mg_printf(conn, "<html><body>");
  82. mg_printf(conn, "<h2>This is the Foo handler!!!</h2>");
  83. mg_printf(conn, "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>",
  84. req_info->request_method, req_info->uri, req_info->http_version);
  85. mg_printf(conn, "</body></html>\n");
  86. return 1;
  87. }
  88. int WebSocketStartHandler(struct mg_connection *conn, void *cbdata)
  89. {
  90. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  91. mg_printf(conn, "<!DOCTYPE html>\n");
  92. mg_printf(conn, "<html>\n<head>\n");
  93. mg_printf(conn, "<meta charset=\"UTF-8\">\n");
  94. mg_printf(conn, "<title>Embedded websocket example</title>\n");
  95. #ifdef USE_WEBSOCKET
  96. /* mg_printf(conn, "<script type=\"text/javascript\"><![CDATA[\n"); ... xhtml style */
  97. mg_printf(conn, "<script>\n");
  98. mg_printf(conn,
  99. "function load() {\n"
  100. " var wsproto = (location.protocol === 'https:') ? 'wss:' : 'ws:';\n"
  101. " connection = new WebSocket(wsproto + '//' + window.location.host + '/websocket');\n"
  102. " websock_text_field = document.getElementById('websock_text_field');\n"
  103. " connection.onmessage = function (e) {\n"
  104. " websock_text_field.innerHTML=e.data;\n"
  105. " }\n"
  106. " connection.onerror = function (error) {\n"
  107. " alert('WebSocket error');\n"
  108. " connection.close();\n"
  109. " }\n"
  110. "}\n"
  111. );
  112. /* mg_printf(conn, "]]></script>\n"); ... xhtml style */
  113. mg_printf(conn, "</script>\n");
  114. mg_printf(conn, "</head>\n<body onload=\"load()\">\n");
  115. mg_printf(conn, "<div id='websock_text_field'>No websocket connection yet</div>\n");
  116. #else
  117. mg_printf(conn, "</head>\n<body>\n");
  118. mg_printf(conn, "Example not compiled with USE_WEBSOCKET\n");
  119. #endif
  120. mg_printf(conn, "</body>\n</html>\n");
  121. return 1;
  122. }
  123. #ifdef USE_WEBSOCKET
  124. #define MAX_WS_CLIENTS 5 /* just for the test: a small number that can be reached */
  125. /* a real server should use a much higher number here */
  126. struct t_ws_client {
  127. struct mg_connection * conn;
  128. int state;
  129. } static ws_clients[MAX_WS_CLIENTS];
  130. #define ASSERT(x) {if (!(x)) {fprintf(stderr, "Assertion failed in line %u\n", (unsigned)__LINE__);}}
  131. static unsigned long cnt;
  132. int WebSocketConnectHandler(const struct mg_connection * conn, void *cbdata)
  133. {
  134. struct mg_context *ctx = mg_get_context(conn);
  135. int reject = 1;
  136. int i;
  137. mg_lock_context(ctx);
  138. for (i=0; i<MAX_WS_CLIENTS; i++) {
  139. if (ws_clients[i].conn == NULL) {
  140. ws_clients[i].conn = (struct mg_connection *) conn;
  141. ws_clients[i].state = 1;
  142. mg_set_user_connection_data(conn, (void*) (ws_clients+i));
  143. reject = 0;
  144. break;
  145. }
  146. }
  147. mg_unlock_context(ctx);
  148. fprintf(stdout, "Websocket client %s\r\n\r\n", (reject ? "rejected" : "accepted"));
  149. return reject;
  150. }
  151. void WebSocketReadyHandler(struct mg_connection * conn, void *cbdata)
  152. {
  153. const char * text = "Hello from the websocket ready handler";
  154. struct t_ws_client * client = mg_get_user_connection_data(conn);
  155. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
  156. fprintf(stdout, "Greeting message sent to websocket client\r\n\r\n");
  157. ASSERT(client->conn == conn);
  158. ASSERT(client->state == 1);
  159. client->state = 2;
  160. }
  161. int WebsocketDataHandler(struct mg_connection * conn, int bits, char * data, size_t len, void *cbdata)
  162. {
  163. struct t_ws_client * client = mg_get_user_connection_data(conn);
  164. ASSERT(client->conn == conn);
  165. ASSERT(client->state >= 1);
  166. fprintf(stdout, "Websocket got data:\r\n");
  167. fwrite(data, len, 1, stdout);
  168. fprintf(stdout, "\r\n\r\n");
  169. return 1;
  170. }
  171. void WebSocketCloseHandler(const struct mg_connection * conn, void *cbdata)
  172. {
  173. struct mg_context *ctx = mg_get_context(conn);
  174. struct t_ws_client * client = mg_get_user_connection_data(conn);
  175. ASSERT(client->conn == conn);
  176. ASSERT(client->state >= 1);
  177. mg_lock_context(ctx);
  178. client->state = 0;
  179. client->conn = NULL;
  180. mg_unlock_context(ctx);
  181. fprintf(stdout, "Client droped from the set of webserver connections\r\n\r\n");
  182. }
  183. void InformWebsockets(struct mg_context *ctx)
  184. {
  185. char text[32];
  186. int i;
  187. sprintf(text, "%lu", ++cnt);
  188. mg_lock_context(ctx);
  189. for (i=0; i<MAX_WS_CLIENTS; i++) {
  190. if (ws_clients[i].state == 2) {
  191. mg_websocket_write(ws_clients[i].conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
  192. }
  193. }
  194. mg_unlock_context(ctx);
  195. }
  196. #endif
  197. int main(int argc, char *argv[])
  198. {
  199. const char * options[] = { "document_root", DOCUMENT_ROOT,
  200. "listening_ports", PORT,
  201. "request_timeout_ms", "10000",
  202. #ifdef USE_WEBSOCKET
  203. "websocket_timeout_ms", "3600000",
  204. #endif
  205. 0
  206. };
  207. struct mg_callbacks callbacks;
  208. struct mg_context *ctx;
  209. struct mg_server_ports ports[32];
  210. size_t port_cnt, n;
  211. memset(&callbacks, 0, sizeof(callbacks));
  212. ctx = mg_start(&callbacks, 0, options);
  213. /* Handler EXAMPLE_URI to explain the example */
  214. mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0);
  215. mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0);
  216. /* Handler for /A* and special handler for /A/B */
  217. mg_set_request_handler(ctx, "/A", AHandler, 0);
  218. mg_set_request_handler(ctx, "/A/B", ABHandler, 0);
  219. /* Handler for /B, /B/A, /B/B but not for /B* */
  220. mg_set_request_handler(ctx, "/B$", BXHandler, (void*)0);
  221. mg_set_request_handler(ctx, "/B/A$", BXHandler, (void*)1);
  222. mg_set_request_handler(ctx, "/B/B$", BXHandler, (void*)2);
  223. /* Handler for all files with .foo extention */
  224. mg_set_request_handler(ctx, "**.foo$", FooHandler, 0);
  225. /* HTTP site to open a websocket connection */
  226. mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0);
  227. #ifdef USE_WEBSOCKET
  228. /* WS site for the websocket connection */
  229. mg_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, WebsocketDataHandler, WebSocketCloseHandler, 0);
  230. #endif
  231. memset(ports, 0, sizeof(ports));
  232. port_cnt = mg_get_server_ports(ctx, 32, ports);
  233. for (n=0; n<port_cnt && n<32; n++) {
  234. const char *proto = ports[n].is_ssl ? "https" : "http";
  235. const char *host = ports[n].protocol==2 ? "[::1]" : "127.0.0.1";
  236. printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port);
  237. printf("Run example at %s://%s:%i%s\n", proto, host, ports[n].port, EXAMPLE_URI);
  238. printf("Exit at %s://%s:%i%s\n", proto, host, ports[n].port, EXIT_URI);
  239. printf("\n");
  240. }
  241. while (!exitNow) {
  242. #ifdef _WIN32
  243. Sleep(1000);
  244. #else
  245. sleep(1);
  246. #endif
  247. #ifdef USE_WEBSOCKET
  248. InformWebsockets(ctx);
  249. #endif
  250. }
  251. mg_stop(ctx);
  252. printf("Bye!\n");
  253. return 0;
  254. }