embedded_cpp.cpp 4.4 KB

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