embedded_c.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 1024
  119. static struct mg_connection * ws_clients[MAX_WS_CLIENTS];
  120. static unsigned long cnt;
  121. int WebSocketConnectHandler(const struct mg_connection * conn, void *cbdata)
  122. {
  123. int reject = 0;
  124. fprintf(stdout, "Websocket client %s\r\n\r\n", reject ? "rejected" : "accepted");
  125. return reject;
  126. }
  127. void WebSocketReadyHandler(struct mg_connection * conn, void *cbdata)
  128. {
  129. struct mg_context *ctx = mg_get_context(conn);
  130. int i;
  131. const char * text = "Hello from the websocket ready handler";
  132. mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
  133. fprintf(stdout, "Client added to the set of webserver connections\r\n\r\n");
  134. mg_lock_context(ctx);
  135. for (i=0; i<MAX_WS_CLIENTS; i++) {
  136. if (ws_clients[i] == NULL) {
  137. ws_clients[i] = conn;
  138. break;
  139. }
  140. }
  141. mg_unlock_context(ctx);
  142. }
  143. int WebsocketDataHandler(struct mg_connection * conn, int bits, char * data, size_t len, void *cbdata)
  144. {
  145. fprintf(stdout, "Websocket got data:\r\n");
  146. fwrite(data, len, 1, stdout);
  147. fprintf(stdout, "\r\n\r\n");
  148. return 1;
  149. }
  150. void WebSocketCloseHandler(const struct mg_connection * conn, void *cbdata)
  151. {
  152. struct mg_context *ctx = mg_get_context((struct mg_connection *) /* TODO: check const_casts */ conn);
  153. int i;
  154. mg_lock_context(ctx);
  155. for (i=0; i<MAX_WS_CLIENTS; i++) {
  156. if (ws_clients[i] == conn) {
  157. ws_clients[i] = NULL;
  158. break;
  159. }
  160. }
  161. mg_unlock_context(ctx);
  162. fprintf(stdout, "Client droped from the set of webserver connections\r\n\r\n");
  163. }
  164. void InformWebsockets(struct mg_context *ctx)
  165. {
  166. char text[32];
  167. int i;
  168. sprintf(text, "%lu", ++cnt);
  169. mg_lock_context(ctx);
  170. for (i=0; i<MAX_WS_CLIENTS; i++) {
  171. if (ws_clients[i] != NULL) {
  172. mg_websocket_write(ws_clients[i], WEBSOCKET_OPCODE_TEXT, text, strlen(text));
  173. }
  174. }
  175. mg_unlock_context(ctx);
  176. }
  177. #endif
  178. int main(int argc, char *argv[])
  179. {
  180. const char * options[] = { "document_root", DOCUMENT_ROOT,
  181. "listening_ports", PORT, 0
  182. };
  183. struct mg_callbacks callbacks;
  184. struct mg_context *ctx;
  185. memset(&callbacks, 0, sizeof(callbacks));
  186. ctx = mg_start(&callbacks, 0, options);
  187. /* Handler EXAMPLE_URI to explain the example */
  188. mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0);
  189. mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0);
  190. /* Handler for /A* and special handler for /A/B */
  191. mg_set_request_handler(ctx, "/A", AHandler, 0);
  192. mg_set_request_handler(ctx, "/A/B", ABHandler, 0);
  193. /* Handler for /B, /B/A, /B/B but not for /B* */
  194. mg_set_request_handler(ctx, "/B$", BXHandler, (void*)0);
  195. mg_set_request_handler(ctx, "/B/A$", BXHandler, (void*)1);
  196. mg_set_request_handler(ctx, "/B/B$", BXHandler, (void*)2);
  197. /* Handler for all files with .foo extention */
  198. mg_set_request_handler(ctx, "**.foo$", FooHandler, 0);
  199. /* HTTP site to open a websocket connection */
  200. mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0);
  201. /* WS site for the websocket connection */
  202. mg_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, WebsocketDataHandler, WebSocketCloseHandler, 0);
  203. printf("Browse files at http://localhost:%s/\n", PORT);
  204. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  205. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  206. while (!exitNow) {
  207. #ifdef _WIN32
  208. Sleep(1000);
  209. #else
  210. sleep(1);
  211. #endif
  212. #ifdef USE_WEBSOCKET
  213. InformWebsockets(ctx);
  214. #endif
  215. }
  216. mg_stop(ctx);
  217. printf("Bye!\n");
  218. return 0;
  219. }