embedded_c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. 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. static struct mg_connection * ws_client;
  105. static unsigned long cnt;
  106. int WebSocketConnectHandler(const struct mg_connection * conn, void *cbdata)
  107. {
  108. int reject = 0;
  109. fprintf(stdout, "Websocket client %s\r\n\r\n", reject ? "rejected" : "accepted");
  110. return reject;
  111. }
  112. void WebSocketReadyHandler(const struct mg_connection * conn, void *cbdata)
  113. {
  114. struct mg_context *ctx = mg_get_context((struct mg_connection *) /* TODO: check const_casts */ conn);
  115. const char * text = "Hello from the websocket ready handler";
  116. /* TODO: check "const struct mg_connection *" vs "struct mg_connection *" everywhere */
  117. mg_websocket_write((struct mg_connection *)conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
  118. fprintf(stdout, "Client added to the set of webserver connections\r\n\r\n");
  119. mg_lock_context(ctx);
  120. ws_client = conn;
  121. mg_unlock_context(ctx);
  122. }
  123. int WebsocketDataHandler(const struct mg_connection * conn, int bits, char * data, size_t len, void *cbdata)
  124. {
  125. fprintf(stdout, "Websocket got data:\r\n");
  126. fwrite(data, len, 1, stdout);
  127. fprintf(stdout, "\r\n\r\n");
  128. return 1;
  129. }
  130. void WebSocketCloseHandler(const struct mg_connection * conn, void *cbdata)
  131. {
  132. struct mg_context *ctx = mg_get_context((struct mg_connection *) /* TODO: check const_casts */ conn);
  133. mg_lock_context(ctx);
  134. ws_client = NULL;
  135. mg_unlock_context(ctx);
  136. fprintf(stdout, "Client droped from the set of webserver connections\r\n\r\n");
  137. }
  138. void InformWebsockets(struct mg_context *ctx)
  139. {
  140. char text[32];
  141. mg_lock_context(ctx);
  142. if (ws_client) {
  143. sprintf(text, "%lu", ++cnt);
  144. mg_websocket_write(ws_client, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
  145. }
  146. mg_unlock_context(ctx);
  147. }
  148. #endif
  149. int main(int argc, char *argv[])
  150. {
  151. const char * options[] = { "document_root", DOCUMENT_ROOT,
  152. "listening_ports", PORT, 0
  153. };
  154. struct mg_callbacks callbacks;
  155. struct mg_context *ctx;
  156. memset(&callbacks, 0, sizeof(callbacks));
  157. ctx = mg_start(&callbacks, 0, options);
  158. mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0);
  159. mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0);
  160. mg_set_request_handler(ctx, "/a", AHandler, 0);
  161. mg_set_request_handler(ctx, "/a/b", ABHandler, 0);
  162. mg_set_request_handler(ctx, "**.foo$", FooHandler, 0);
  163. mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0);
  164. mg_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, WebsocketDataHandler, WebSocketCloseHandler, 0);
  165. printf("Browse files at http://localhost:%s/\n", PORT);
  166. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  167. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  168. while (!exitNow) {
  169. #ifdef _WIN32
  170. Sleep(1000);
  171. #else
  172. sleep(1);
  173. #endif
  174. #ifdef USE_WEBSOCKET
  175. InformWebsockets(ctx);
  176. #endif
  177. }
  178. mg_stop(ctx);
  179. printf("Bye!\n");
  180. return 0;
  181. }