embedded_cpp.cpp 4.2 KB

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