embedded_cpp.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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, rlen);
  159. if (rlen <= 0) {
  160. break;
  161. }
  162. wlen = mg_write(conn, buf, 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. bool
  173. handlePut(CivetServer *server, struct mg_connection *conn)
  174. {
  175. /* Handler may access the request info using mg_get_request_info */
  176. const struct mg_request_info *req_info = mg_get_request_info(conn);
  177. long long rlen, wlen;
  178. long long nlen = 0;
  179. long long tlen = (size_t)req_info->content_length;
  180. char buf[1024];
  181. mg_printf(conn,
  182. "HTTP/1.1 200 OK\r\nContent-Type: "
  183. "text/html\r\nConnection: close\r\n\r\n");
  184. mg_printf(conn, "<html><body>\n");
  185. mg_printf(conn, "<h2>This is the Foo PUT handler!!!</h2>\n");
  186. mg_printf(conn,
  187. "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
  188. req_info->request_method,
  189. req_info->uri,
  190. req_info->http_version);
  191. mg_printf(conn, "<p>Content Length: %li</p>\n", (long)tlen);
  192. mg_printf(conn, "<pre>\n");
  193. while (nlen < tlen) {
  194. rlen = tlen - nlen;
  195. if (rlen > sizeof(buf)) {
  196. rlen = sizeof(buf);
  197. }
  198. rlen = mg_read(conn, buf, rlen);
  199. if (rlen <= 0) {
  200. break;
  201. }
  202. wlen = mg_write(conn, buf, rlen);
  203. if (rlen != rlen) {
  204. break;
  205. }
  206. nlen += wlen;
  207. }
  208. mg_printf(conn, "\n</pre>\n");
  209. mg_printf(conn, "</body></html>\n");
  210. return true;
  211. }
  212. };
  213. int
  214. main(int argc, char *argv[])
  215. {
  216. const char *options[] = {
  217. "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
  218. CivetServer server(options);
  219. ExampleHandler h_ex;
  220. server.addHandler(EXAMPLE_URI, h_ex);
  221. ExitHandler h_exit;
  222. server.addHandler(EXIT_URI, h_exit);
  223. AHandler h_a;
  224. server.addHandler("/a", h_a);
  225. ABHandler h_ab;
  226. server.addHandler("/a/b", h_ab);
  227. FooHandler h_foo;
  228. server.addHandler("**.foo$", h_foo);
  229. printf("Browse files at http://localhost:%s/\n", PORT);
  230. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  231. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  232. while (!exitNow) {
  233. #ifdef _WIN32
  234. Sleep(1000);
  235. #else
  236. sleep(1);
  237. #endif
  238. }
  239. printf("Bye!\n");
  240. return 0;
  241. }