embedded_cpp.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Copyright (c) 2013-2014 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. _snprintf(buf, sizeof(buf), "D:\\somewhere\\%s\\%s", req_info->remote_user, req_info->local_uri);
  185. buf[sizeof(buf)-1] = 0; /* TODO: check overflow */
  186. f = fopen_recursive(buf, "wb");
  187. if (!f) {
  188. fail = 1;
  189. } else {
  190. while (nlen < tlen) {
  191. rlen = tlen - nlen;
  192. if (rlen > sizeof(buf)) {
  193. rlen = sizeof(buf);
  194. }
  195. rlen = mg_read(conn, buf, (size_t)rlen);
  196. if (rlen <= 0) {
  197. fail = 1;
  198. break;
  199. }
  200. wlen = fwrite(buf, 1, (size_t)rlen, f);
  201. if (rlen != rlen) {
  202. fail = 1;
  203. break;
  204. }
  205. nlen += wlen;
  206. }
  207. fclose(f);
  208. }
  209. if (fail) {
  210. mg_printf(conn,
  211. "HTTP/1.1 409 Conflict\r\n"
  212. "Content-Type: text/plain\r\n"
  213. "Connection: close\r\n\r\n");
  214. MessageBeep(MB_ICONERROR);
  215. } else {
  216. mg_printf(conn,
  217. "HTTP/1.1 201 Created\r\n"
  218. "Content-Type: text/plain\r\n"
  219. "Connection: close\r\n\r\n");
  220. MessageBeep(MB_OK);
  221. }
  222. return true;
  223. }
  224. };
  225. int
  226. main(int argc, char *argv[])
  227. {
  228. const char *options[] = {
  229. "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
  230. std::vector<std::string> cpp_options;
  231. for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
  232. cpp_options.push_back(options[i]);
  233. }
  234. // CivetServer server(options); // <-- C style start
  235. CivetServer server(cpp_options); // <-- C++ style start
  236. ExampleHandler h_ex;
  237. server.addHandler(EXAMPLE_URI, h_ex);
  238. ExitHandler h_exit;
  239. server.addHandler(EXIT_URI, h_exit);
  240. AHandler h_a;
  241. server.addHandler("/a", h_a);
  242. ABHandler h_ab;
  243. server.addHandler("/a/b", h_ab);
  244. FooHandler h_foo;
  245. server.addHandler("", h_foo);
  246. printf("Browse files at http://localhost:%s/\n", PORT);
  247. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  248. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  249. while (!exitNow) {
  250. #ifdef _WIN32
  251. Sleep(1000);
  252. #else
  253. sleep(1);
  254. #endif
  255. }
  256. printf("Bye!\n");
  257. return 0;
  258. }