embedded_cpp.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Copyright (c) 2013-2017 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. #ifdef _WIN32
  8. #include <windows.h>
  9. #else
  10. #include <unistd.h>
  11. #endif
  12. #define DOCUMENT_ROOT "."
  13. #define PORT "8081"
  14. #define EXAMPLE_URI "/example"
  15. #define EXIT_URI "/exit"
  16. bool exitNow = false;
  17. class ExampleHandler : public CivetHandler
  18. {
  19. public:
  20. bool
  21. handleGet(CivetServer *server, struct mg_connection *conn)
  22. {
  23. mg_printf(conn,
  24. "HTTP/1.1 200 OK\r\nContent-Type: "
  25. "text/html\r\nConnection: close\r\n\r\n");
  26. mg_printf(conn, "<html><body>\r\n");
  27. mg_printf(conn,
  28. "<h2>This is an example text from a C++ handler</h2>\r\n");
  29. mg_printf(conn,
  30. "<p>To see a page from the A handler <a "
  31. "href=\"a\">click here</a></p>\r\n");
  32. mg_printf(conn,
  33. "<p>To see a page from the A handler with a parameter "
  34. "<a href=\"a?param=1\">click here</a></p>\r\n");
  35. mg_printf(conn,
  36. "<p>To see a page from the A/B handler <a "
  37. "href=\"a/b\">click here</a></p>\r\n");
  38. mg_printf(conn,
  39. "<p>To see a page from the *.foo handler <a "
  40. "href=\"xy.foo\">click here</a></p>\r\n");
  41. mg_printf(conn,
  42. "<p>To exit <a href=\"%s\">click here</a></p>\r\n",
  43. EXIT_URI);
  44. mg_printf(conn, "</body></html>\r\n");
  45. return true;
  46. }
  47. };
  48. class ExitHandler : public CivetHandler
  49. {
  50. public:
  51. bool
  52. handleGet(CivetServer *server, struct mg_connection *conn)
  53. {
  54. mg_printf(conn,
  55. "HTTP/1.1 200 OK\r\nContent-Type: "
  56. "text/plain\r\nConnection: close\r\n\r\n");
  57. mg_printf(conn, "Bye!\n");
  58. exitNow = true;
  59. return true;
  60. }
  61. };
  62. class AHandler : public CivetHandler
  63. {
  64. private:
  65. bool
  66. handleAll(const char *method,
  67. CivetServer *server,
  68. struct mg_connection *conn)
  69. {
  70. std::string s = "";
  71. mg_printf(conn,
  72. "HTTP/1.1 200 OK\r\nContent-Type: "
  73. "text/html\r\nConnection: close\r\n\r\n");
  74. mg_printf(conn, "<html><body>");
  75. mg_printf(conn, "<h2>This is the A handler for \"%s\" !</h2>", method);
  76. if (CivetServer::getParam(conn, "param", s)) {
  77. mg_printf(conn, "<p>param set to %s</p>", s.c_str());
  78. } else {
  79. mg_printf(conn, "<p>param not set</p>");
  80. }
  81. mg_printf(conn, "</body></html>\n");
  82. return true;
  83. }
  84. public:
  85. bool
  86. handleGet(CivetServer *server, struct mg_connection *conn)
  87. {
  88. return handleAll("GET", server, conn);
  89. }
  90. bool
  91. handlePost(CivetServer *server, struct mg_connection *conn)
  92. {
  93. return handleAll("POST", server, conn);
  94. }
  95. };
  96. class ABHandler : public CivetHandler
  97. {
  98. public:
  99. bool
  100. handleGet(CivetServer *server, struct mg_connection *conn)
  101. {
  102. mg_printf(conn,
  103. "HTTP/1.1 200 OK\r\nContent-Type: "
  104. "text/html\r\nConnection: close\r\n\r\n");
  105. mg_printf(conn, "<html><body>");
  106. mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
  107. mg_printf(conn, "</body></html>\n");
  108. return true;
  109. }
  110. };
  111. class FooHandler : public CivetHandler
  112. {
  113. public:
  114. bool
  115. handleGet(CivetServer *server, struct mg_connection *conn)
  116. {
  117. /* Handler may access the request info using mg_get_request_info */
  118. const struct mg_request_info *req_info = mg_get_request_info(conn);
  119. mg_printf(conn,
  120. "HTTP/1.1 200 OK\r\nContent-Type: "
  121. "text/html\r\nConnection: close\r\n\r\n");
  122. mg_printf(conn, "<html><body>\n");
  123. mg_printf(conn, "<h2>This is the Foo GET handler!!!</h2>\n");
  124. mg_printf(conn,
  125. "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
  126. req_info->request_method,
  127. req_info->uri,
  128. req_info->http_version);
  129. mg_printf(conn, "</body></html>\n");
  130. return true;
  131. }
  132. bool
  133. handlePost(CivetServer *server, struct mg_connection *conn)
  134. {
  135. /* Handler may access the request info using mg_get_request_info */
  136. const struct mg_request_info *req_info = mg_get_request_info(conn);
  137. long long rlen, wlen;
  138. long long nlen = 0;
  139. long long tlen = req_info->content_length;
  140. char buf[1024];
  141. mg_printf(conn,
  142. "HTTP/1.1 200 OK\r\nContent-Type: "
  143. "text/html\r\nConnection: close\r\n\r\n");
  144. mg_printf(conn, "<html><body>\n");
  145. mg_printf(conn, "<h2>This is the Foo POST handler!!!</h2>\n");
  146. mg_printf(conn,
  147. "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
  148. req_info->request_method,
  149. req_info->uri,
  150. req_info->http_version);
  151. mg_printf(conn, "<p>Content Length: %li</p>\n", (long)tlen);
  152. mg_printf(conn, "<pre>\n");
  153. while (nlen < tlen) {
  154. rlen = tlen - nlen;
  155. if (rlen > sizeof(buf)) {
  156. rlen = sizeof(buf);
  157. }
  158. rlen = mg_read(conn, buf, (size_t)rlen);
  159. if (rlen <= 0) {
  160. break;
  161. }
  162. wlen = mg_write(conn, buf, (size_t)rlen);
  163. if (rlen != rlen) {
  164. break;
  165. }
  166. nlen += wlen;
  167. }
  168. mg_printf(conn, "\n</pre>\n");
  169. mg_printf(conn, "</body></html>\n");
  170. return true;
  171. }
  172. #define fopen_recursive fopen
  173. bool
  174. handlePut(CivetServer *server, struct mg_connection *conn)
  175. {
  176. /* Handler may access the request info using mg_get_request_info */
  177. const struct mg_request_info *req_info = mg_get_request_info(conn);
  178. long long rlen, wlen;
  179. long long nlen = 0;
  180. long long tlen = req_info->content_length;
  181. FILE * f;
  182. char buf[1024];
  183. int fail = 0;
  184. #ifdef _WIN32
  185. _snprintf(buf, sizeof(buf), "D:\\somewhere\\%s\\%s", req_info->remote_user, req_info->local_uri);
  186. buf[sizeof(buf)-1] = 0;
  187. if (strlen(buf)>255) {
  188. /* Windows will not work with path > 260 (MAX_PATH), unless we use
  189. * the unicode API. However, this is just an example code: A real
  190. * code will probably never store anything to D:\\somewhere and
  191. * must be adapted to the specific needs anyhow. */
  192. fail = 1;
  193. f = NULL;
  194. } else {
  195. f = fopen_recursive(buf, "wb");
  196. }
  197. #else
  198. snprintf(buf, sizeof(buf), "~/somewhere/%s/%s", req_info->remote_user, req_info->local_uri);
  199. buf[sizeof(buf)-1] = 0;
  200. if (strlen(buf)>1020) {
  201. /* The string is too long and probably truncated. Make sure an
  202. * UTF-8 string is never truncated between the UTF-8 code bytes.
  203. * This example code must be adapted to the specific needs. */
  204. fail = 1;
  205. f = NULL;
  206. } else {
  207. f = fopen_recursive(buf, "w");
  208. }
  209. #endif
  210. if (!f) {
  211. fail = 1;
  212. } else {
  213. while (nlen < tlen) {
  214. rlen = tlen - nlen;
  215. if (rlen > sizeof(buf)) {
  216. rlen = sizeof(buf);
  217. }
  218. rlen = mg_read(conn, buf, (size_t)rlen);
  219. if (rlen <= 0) {
  220. fail = 1;
  221. break;
  222. }
  223. wlen = fwrite(buf, 1, (size_t)rlen, f);
  224. if (rlen != rlen) {
  225. fail = 1;
  226. break;
  227. }
  228. nlen += wlen;
  229. }
  230. fclose(f);
  231. }
  232. if (fail) {
  233. mg_printf(conn,
  234. "HTTP/1.1 409 Conflict\r\n"
  235. "Content-Type: text/plain\r\n"
  236. "Connection: close\r\n\r\n");
  237. } else {
  238. mg_printf(conn,
  239. "HTTP/1.1 201 Created\r\n"
  240. "Content-Type: text/plain\r\n"
  241. "Connection: close\r\n\r\n");
  242. }
  243. return true;
  244. }
  245. };
  246. int
  247. main(int argc, char *argv[])
  248. {
  249. const char *options[] = {
  250. "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
  251. std::vector<std::string> cpp_options;
  252. for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
  253. cpp_options.push_back(options[i]);
  254. }
  255. // CivetServer server(options); // <-- C style start
  256. CivetServer server(cpp_options); // <-- C++ style start
  257. ExampleHandler h_ex;
  258. server.addHandler(EXAMPLE_URI, h_ex);
  259. ExitHandler h_exit;
  260. server.addHandler(EXIT_URI, h_exit);
  261. AHandler h_a;
  262. server.addHandler("/a", h_a);
  263. ABHandler h_ab;
  264. server.addHandler("/a/b", h_ab);
  265. FooHandler h_foo;
  266. server.addHandler("", h_foo);
  267. printf("Browse files at http://localhost:%s/\n", PORT);
  268. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  269. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  270. while (!exitNow) {
  271. #ifdef _WIN32
  272. Sleep(1000);
  273. #else
  274. sleep(1);
  275. #endif
  276. }
  277. printf("Bye!\n");
  278. return 0;
  279. }