embedded_cpp.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  24. mg_printf(conn, "<html><body>\r\n");
  25. mg_printf(conn,
  26. "<h2>This is an example text from a C++ handler</h2>\r\n");
  27. mg_printf(conn, "<p>To see a page from the A handler <a "
  28. "href=\"a\">click here</a></p>\r\n");
  29. mg_printf(conn, "<p>To see a page from the A handler with a parameter "
  30. "<a href=\"a?param=1\">click here</a></p>\r\n");
  31. mg_printf(conn, "<p>To see a page from the A/B handler <a "
  32. "href=\"a/b\">click here</a></p>\r\n");
  33. mg_printf(conn, "<p>To see a page from the *.foo handler <a "
  34. "href=\"xy.foo\">click here</a></p>\r\n");
  35. mg_printf(conn,
  36. "<p>To exit <a href=\"%s\">click here</a></p>\r\n",
  37. EXIT_URI);
  38. mg_printf(conn, "</body></html>\r\n");
  39. return true;
  40. }
  41. };
  42. class ExitHandler : public CivetHandler
  43. {
  44. public:
  45. bool
  46. handleGet(CivetServer *server, struct mg_connection *conn)
  47. {
  48. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  49. mg_printf(conn, "Bye!\n");
  50. exitNow = true;
  51. return true;
  52. }
  53. };
  54. class AHandler : public CivetHandler
  55. {
  56. private:
  57. bool
  58. handleAll(const char *method,
  59. CivetServer *server,
  60. struct mg_connection *conn)
  61. {
  62. std::string s = "";
  63. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  64. mg_printf(conn, "<html><body>");
  65. mg_printf(conn, "<h2>This is the A handler for \"%s\" !</h2>", method);
  66. if (CivetServer::getParam(conn, "param", s)) {
  67. mg_printf(conn, "<p>param set to %s</p>", s.c_str());
  68. } else {
  69. mg_printf(conn, "<p>param not set</p>");
  70. }
  71. mg_printf(conn, "</body></html>\n");
  72. return true;
  73. }
  74. public:
  75. bool
  76. handleGet(CivetServer *server, struct mg_connection *conn)
  77. {
  78. return handleAll("GET", server, conn);
  79. }
  80. bool
  81. handlePost(CivetServer *server, struct mg_connection *conn)
  82. {
  83. return handleAll("POST", server, conn);
  84. }
  85. };
  86. class ABHandler : public CivetHandler
  87. {
  88. public:
  89. bool
  90. handleGet(CivetServer *server, struct mg_connection *conn)
  91. {
  92. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  93. mg_printf(conn, "<html><body>");
  94. mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
  95. mg_printf(conn, "</body></html>\n");
  96. return true;
  97. }
  98. };
  99. class FooHandler : public CivetHandler
  100. {
  101. public:
  102. bool
  103. handleGet(CivetServer *server, struct mg_connection *conn)
  104. {
  105. /* Handler may access the request info using mg_get_request_info */
  106. const struct mg_request_info *req_info = mg_get_request_info(conn);
  107. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  108. mg_printf(conn, "<html><body>");
  109. mg_printf(conn, "<h2>This is the Foo handler!!!</h2>");
  110. mg_printf(conn,
  111. "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>",
  112. req_info->request_method,
  113. req_info->uri,
  114. req_info->http_version);
  115. mg_printf(conn, "</body></html>\n");
  116. return true;
  117. }
  118. };
  119. int
  120. main(int argc, char *argv[])
  121. {
  122. const char *options[] = {
  123. "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
  124. CivetServer server(options);
  125. ExampleHandler h_ex;
  126. server.addHandler(EXAMPLE_URI, h_ex);
  127. ExitHandler h_exit;
  128. server.addHandler(EXIT_URI, h_exit);
  129. AHandler h_a;
  130. server.addHandler("/a", h_a);
  131. ABHandler h_ab;
  132. server.addHandler("/a/b", h_ab);
  133. FooHandler h_foo;
  134. server.addHandler("**.foo$", h_foo);
  135. printf("Browse files at http://localhost:%s/\n", PORT);
  136. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  137. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  138. while (!exitNow) {
  139. #ifdef _WIN32
  140. Sleep(1000);
  141. #else
  142. sleep(1);
  143. #endif
  144. }
  145. printf("Bye!\n");
  146. return 0;
  147. }