embedded_c.c 7.2 KB

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