embedded_c.c 11 KB

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