embedded_c.c 9.5 KB

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