embedded_c.c 9.2 KB

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