embedded_cpp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* Copyright (c) 2013-2018 the Civetweb developers
  2. * Copyright (c) 2013 No Face Press, LLC
  3. * License http://opensource.org/licenses/mit-license.php MIT License
  4. */
  5. // Simple example program on how to use Embedded C++ interface.
  6. #include "CivetServer.h"
  7. #include <cstring>
  8. #ifdef _WIN32
  9. #include <windows.h>
  10. #else
  11. #include <unistd.h>
  12. #endif
  13. #define DOCUMENT_ROOT "."
  14. #define PORT "8081"
  15. #define EXAMPLE_URI "/example"
  16. #define EXIT_URI "/exit"
  17. bool exitNow = false;
  18. class ExampleHandler : public CivetHandler
  19. {
  20. public:
  21. bool
  22. handleGet(CivetServer *server, struct mg_connection *conn)
  23. {
  24. mg_printf(conn,
  25. "HTTP/1.1 200 OK\r\nContent-Type: "
  26. "text/html\r\nConnection: close\r\n\r\n");
  27. mg_printf(conn, "<html><body>\r\n");
  28. mg_printf(conn,
  29. "<h2>This is an example text from a C++ handler</h2>\r\n");
  30. mg_printf(conn,
  31. "<p>To see a page from the A handler <a "
  32. "href=\"a\">click here</a></p>\r\n");
  33. mg_printf(conn,
  34. "<p>To see a page from the A handler with a parameter "
  35. "<a href=\"a?param=1\">click here</a></p>\r\n");
  36. mg_printf(conn,
  37. "<p>To see a page from the A/B handler <a "
  38. "href=\"a/b\">click here</a></p>\r\n");
  39. mg_printf(conn,
  40. "<p>To see a page from the *.foo handler <a "
  41. "href=\"xy.foo\">click here</a></p>\r\n");
  42. mg_printf(conn,
  43. "<p>To see a page from the WebSocket handler <a "
  44. "href=\"ws\">click here</a></p>\r\n");
  45. mg_printf(conn,
  46. "<p>To exit <a href=\"%s\">click here</a></p>\r\n",
  47. EXIT_URI);
  48. mg_printf(conn, "</body></html>\r\n");
  49. return true;
  50. }
  51. };
  52. class ExitHandler : public CivetHandler
  53. {
  54. public:
  55. bool
  56. handleGet(CivetServer *server, struct mg_connection *conn)
  57. {
  58. mg_printf(conn,
  59. "HTTP/1.1 200 OK\r\nContent-Type: "
  60. "text/plain\r\nConnection: close\r\n\r\n");
  61. mg_printf(conn, "Bye!\n");
  62. exitNow = true;
  63. return true;
  64. }
  65. };
  66. class AHandler : public CivetHandler
  67. {
  68. private:
  69. bool
  70. handleAll(const char *method,
  71. CivetServer *server,
  72. struct mg_connection *conn)
  73. {
  74. std::string s = "";
  75. mg_printf(conn,
  76. "HTTP/1.1 200 OK\r\nContent-Type: "
  77. "text/html\r\nConnection: close\r\n\r\n");
  78. mg_printf(conn, "<html><body>");
  79. mg_printf(conn, "<h2>This is the A handler for \"%s\" !</h2>", method);
  80. if (CivetServer::getParam(conn, "param", s)) {
  81. mg_printf(conn, "<p>param set to %s</p>", s.c_str());
  82. } else {
  83. mg_printf(conn, "<p>param not set</p>");
  84. }
  85. mg_printf(conn, "</body></html>\n");
  86. return true;
  87. }
  88. public:
  89. bool
  90. handleGet(CivetServer *server, struct mg_connection *conn)
  91. {
  92. return handleAll("GET", server, conn);
  93. }
  94. bool
  95. handlePost(CivetServer *server, struct mg_connection *conn)
  96. {
  97. return handleAll("POST", server, conn);
  98. }
  99. };
  100. class ABHandler : public CivetHandler
  101. {
  102. public:
  103. bool
  104. handleGet(CivetServer *server, struct mg_connection *conn)
  105. {
  106. mg_printf(conn,
  107. "HTTP/1.1 200 OK\r\nContent-Type: "
  108. "text/html\r\nConnection: close\r\n\r\n");
  109. mg_printf(conn, "<html><body>");
  110. mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
  111. mg_printf(conn, "</body></html>\n");
  112. return true;
  113. }
  114. };
  115. class FooHandler : public CivetHandler
  116. {
  117. public:
  118. bool
  119. handleGet(CivetServer *server, struct mg_connection *conn)
  120. {
  121. /* Handler may access the request info using mg_get_request_info */
  122. const struct mg_request_info *req_info = mg_get_request_info(conn);
  123. mg_printf(conn,
  124. "HTTP/1.1 200 OK\r\nContent-Type: "
  125. "text/html\r\nConnection: close\r\n\r\n");
  126. mg_printf(conn, "<html><body>\n");
  127. mg_printf(conn, "<h2>This is the Foo GET handler!!!</h2>\n");
  128. mg_printf(conn,
  129. "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
  130. req_info->request_method,
  131. req_info->request_uri,
  132. req_info->http_version);
  133. mg_printf(conn, "</body></html>\n");
  134. return true;
  135. }
  136. bool
  137. handlePost(CivetServer *server, struct mg_connection *conn)
  138. {
  139. /* Handler may access the request info using mg_get_request_info */
  140. const struct mg_request_info *req_info = mg_get_request_info(conn);
  141. long long rlen, wlen;
  142. long long nlen = 0;
  143. long long tlen = req_info->content_length;
  144. char buf[1024];
  145. mg_printf(conn,
  146. "HTTP/1.1 200 OK\r\nContent-Type: "
  147. "text/html\r\nConnection: close\r\n\r\n");
  148. mg_printf(conn, "<html><body>\n");
  149. mg_printf(conn, "<h2>This is the Foo POST handler!!!</h2>\n");
  150. mg_printf(conn,
  151. "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
  152. req_info->request_method,
  153. req_info->request_uri,
  154. req_info->http_version);
  155. mg_printf(conn, "<p>Content Length: %li</p>\n", (long)tlen);
  156. mg_printf(conn, "<pre>\n");
  157. while (nlen < tlen) {
  158. rlen = tlen - nlen;
  159. if (rlen > sizeof(buf)) {
  160. rlen = sizeof(buf);
  161. }
  162. rlen = mg_read(conn, buf, (size_t)rlen);
  163. if (rlen <= 0) {
  164. break;
  165. }
  166. wlen = mg_write(conn, buf, (size_t)rlen);
  167. if (wlen != rlen) {
  168. break;
  169. }
  170. nlen += wlen;
  171. }
  172. mg_printf(conn, "\n</pre>\n");
  173. mg_printf(conn, "</body></html>\n");
  174. return true;
  175. }
  176. #define fopen_recursive fopen
  177. bool
  178. handlePut(CivetServer *server, struct mg_connection *conn)
  179. {
  180. /* Handler may access the request info using mg_get_request_info */
  181. const struct mg_request_info *req_info = mg_get_request_info(conn);
  182. long long rlen, wlen;
  183. long long nlen = 0;
  184. long long tlen = req_info->content_length;
  185. FILE * f;
  186. char buf[1024];
  187. int fail = 0;
  188. #ifdef _WIN32
  189. _snprintf(buf, sizeof(buf), "D:\\somewhere\\%s\\%s", req_info->remote_user, req_info->local_uri);
  190. buf[sizeof(buf)-1] = 0;
  191. if (strlen(buf)>255) {
  192. /* Windows will not work with path > 260 (MAX_PATH), unless we use
  193. * the unicode API. However, this is just an example code: A real
  194. * code will probably never store anything to D:\\somewhere and
  195. * must be adapted to the specific needs anyhow. */
  196. fail = 1;
  197. f = NULL;
  198. } else {
  199. f = fopen_recursive(buf, "wb");
  200. }
  201. #else
  202. snprintf(buf, sizeof(buf), "~/somewhere/%s/%s", req_info->remote_user, req_info->local_uri);
  203. buf[sizeof(buf)-1] = 0;
  204. if (strlen(buf)>1020) {
  205. /* The string is too long and probably truncated. Make sure an
  206. * UTF-8 string is never truncated between the UTF-8 code bytes.
  207. * This example code must be adapted to the specific needs. */
  208. fail = 1;
  209. f = NULL;
  210. } else {
  211. f = fopen_recursive(buf, "w");
  212. }
  213. #endif
  214. if (!f) {
  215. fail = 1;
  216. } else {
  217. while (nlen < tlen) {
  218. rlen = tlen - nlen;
  219. if (rlen > sizeof(buf)) {
  220. rlen = sizeof(buf);
  221. }
  222. rlen = mg_read(conn, buf, (size_t)rlen);
  223. if (rlen <= 0) {
  224. fail = 1;
  225. break;
  226. }
  227. wlen = fwrite(buf, 1, (size_t)rlen, f);
  228. if (wlen != rlen) {
  229. fail = 1;
  230. break;
  231. }
  232. nlen += wlen;
  233. }
  234. fclose(f);
  235. }
  236. if (fail) {
  237. mg_printf(conn,
  238. "HTTP/1.1 409 Conflict\r\n"
  239. "Content-Type: text/plain\r\n"
  240. "Connection: close\r\n\r\n");
  241. } else {
  242. mg_printf(conn,
  243. "HTTP/1.1 201 Created\r\n"
  244. "Content-Type: text/plain\r\n"
  245. "Connection: close\r\n\r\n");
  246. }
  247. return true;
  248. }
  249. };
  250. class WsStartHandler : public CivetHandler
  251. {
  252. public:
  253. bool
  254. handleGet(CivetServer *server, struct mg_connection *conn)
  255. {
  256. mg_printf(conn,
  257. "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
  258. "close\r\n\r\n");
  259. mg_printf(conn, "<!DOCTYPE html>\n");
  260. mg_printf(conn, "<html>\n<head>\n");
  261. mg_printf(conn, "<meta charset=\"UTF-8\">\n");
  262. mg_printf(conn, "<title>Embedded websocket example</title>\n");
  263. #ifdef USE_WEBSOCKET
  264. /* mg_printf(conn, "<script type=\"text/javascript\"><![CDATA[\n"); ...
  265. * xhtml style */
  266. mg_printf(conn, "<script>\n");
  267. mg_printf(
  268. conn,
  269. "var i=0\n"
  270. "function load() {\n"
  271. " var wsproto = (location.protocol === 'https:') ? 'wss:' : 'ws:';\n"
  272. " connection = new WebSocket(wsproto + '//' + window.location.host + "
  273. "'/websocket');\n"
  274. " websock_text_field = "
  275. "document.getElementById('websock_text_field');\n"
  276. " connection.onmessage = function (e) {\n"
  277. " websock_text_field.innerHTML=e.data;\n"
  278. " i=i+1;"
  279. " connection.send(i);\n"
  280. " }\n"
  281. " connection.onerror = function (error) {\n"
  282. " alert('WebSocket error');\n"
  283. " connection.close();\n"
  284. " }\n"
  285. "}\n");
  286. /* mg_printf(conn, "]]></script>\n"); ... xhtml style */
  287. mg_printf(conn, "</script>\n");
  288. mg_printf(conn, "</head>\n<body onload=\"load()\">\n");
  289. mg_printf(
  290. conn,
  291. "<div id='websock_text_field'>No websocket connection yet</div>\n");
  292. #else
  293. mg_printf(conn, "</head>\n<body>\n");
  294. mg_printf(conn, "Example not compiled with USE_WEBSOCKET\n");
  295. #endif
  296. mg_printf(conn, "</body>\n</html>\n");
  297. return 1;
  298. }
  299. };
  300. #ifdef USE_WEBSOCKET
  301. class WebSocketHandler : public CivetWebSocketHandler {
  302. virtual bool handleConnection(CivetServer *server,
  303. const struct mg_connection *conn) {
  304. printf("WS connected\n");
  305. return true;
  306. }
  307. virtual void handleReadyState(CivetServer *server,
  308. struct mg_connection *conn) {
  309. printf("WS ready\n");
  310. const char *text = "Hello from the websocket ready handler";
  311. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT, text, strlen(text));
  312. }
  313. virtual bool handleData(CivetServer *server,
  314. struct mg_connection *conn,
  315. int bits,
  316. char *data,
  317. size_t data_len) {
  318. printf("WS got %lu bytes: ", (long unsigned)data_len);
  319. fwrite(data, 1, data_len, stdout);
  320. printf("\n");
  321. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT, data, data_len);
  322. return (data_len<4);
  323. }
  324. virtual void handleClose(CivetServer *server,
  325. const struct mg_connection *conn) {
  326. printf("WS closed\n");
  327. }
  328. };
  329. #endif
  330. int
  331. main(int argc, char *argv[])
  332. {
  333. const char *options[] = {
  334. "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
  335. std::vector<std::string> cpp_options;
  336. for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
  337. cpp_options.push_back(options[i]);
  338. }
  339. // CivetServer server(options); // <-- C style start
  340. CivetServer server(cpp_options); // <-- C++ style start
  341. ExampleHandler h_ex;
  342. server.addHandler(EXAMPLE_URI, h_ex);
  343. ExitHandler h_exit;
  344. server.addHandler(EXIT_URI, h_exit);
  345. AHandler h_a;
  346. server.addHandler("/a", h_a);
  347. ABHandler h_ab;
  348. server.addHandler("/a/b", h_ab);
  349. WsStartHandler h_ws;
  350. server.addHandler("/ws", h_ws);
  351. #ifdef NO_FILES
  352. /* This handler will handle "everything else", including
  353. * requests to files. If this handler is installed,
  354. * NO_FILES should be set. */
  355. FooHandler h_foo;
  356. server.addHandler("", h_foo);
  357. printf("See a page from the \"all\" handler at http://localhost:%s/\n", PORT);
  358. #else
  359. FooHandler h_foo;
  360. server.addHandler("**.foo", h_foo);
  361. printf("Browse files at http://localhost:%s/\n", PORT);
  362. #endif
  363. #ifdef USE_WEBSOCKET
  364. WebSocketHandler h_websocket;
  365. server.addWebSocketHandler("/websocket", h_websocket);
  366. printf("Run websocket example at http://localhost:%s/ws\n", PORT);
  367. #endif
  368. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  369. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  370. while (!exitNow) {
  371. #ifdef _WIN32
  372. Sleep(1000);
  373. #else
  374. sleep(1);
  375. #endif
  376. }
  377. printf("Bye!\n");
  378. return 0;
  379. }