embedded_cpp.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  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 "8888"
  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. public:
  44. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  45. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  46. mg_printf(conn, "<html><body>");
  47. mg_printf(conn, "<h2>This is the A handler!!!</h2>");
  48. mg_printf(conn, "</body></html>\n");
  49. return true;
  50. }
  51. };
  52. class ABHandler: public CivetHandler
  53. {
  54. public:
  55. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  56. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  57. mg_printf(conn, "<html><body>");
  58. mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
  59. mg_printf(conn, "</body></html>\n");
  60. return true;
  61. }
  62. };
  63. class FooHandler: public CivetHandler
  64. {
  65. public:
  66. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  67. /* Handler may access the request info using mg_get_request_info */
  68. struct mg_request_info * req_info = mg_get_request_info(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 Foo handler!!!</h2>");
  72. mg_printf(conn, "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>",
  73. req_info->request_method, req_info->uri, req_info->http_version);
  74. mg_printf(conn, "</body></html>\n");
  75. return true;
  76. }
  77. };
  78. int main(int argc, char *argv[])
  79. {
  80. const char * options[] = { "document_root", DOCUMENT_ROOT,
  81. "listening_ports", PORT, 0
  82. };
  83. CivetServer server(options);
  84. server.addHandler(EXAMPLE_URI, new ExampleHandler());
  85. server.addHandler(EXIT_URI, new ExitHandler());
  86. server.addHandler("/a", new AHandler());
  87. server.addHandler("/a/b", new ABHandler());
  88. server.addHandler("**.foo$", new FooHandler());
  89. printf("Browse files at http://localhost:%s/\n", PORT);
  90. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  91. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  92. while (!exitNow) {
  93. #ifdef _WIN32
  94. Sleep(1000);
  95. #else
  96. sleep(1);
  97. #endif
  98. }
  99. printf("Bye!\n");
  100. return 0;
  101. }