embedded_cpp.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. public:
  17. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  18. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  19. mg_printf(conn, "<html><body>");
  20. mg_printf(conn, "<h2>This is example text!!!</h2>");
  21. mg_printf(conn, "<p>To exit <a href=\"%s\">click here</a></p>",
  22. EXIT_URI);
  23. mg_printf(conn, "</body></html>\n");
  24. return true;
  25. }
  26. };
  27. class ExitHandler: public CivetHandler {
  28. public:
  29. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  30. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  31. mg_printf(conn, "Bye!\n");
  32. exitNow = true;
  33. return true;
  34. }
  35. };
  36. class AHandler: public CivetHandler {
  37. public:
  38. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  39. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  40. mg_printf(conn, "<html><body>");
  41. mg_printf(conn, "<h2>This is the A handler!!!</h2>");
  42. mg_printf(conn, "</body></html>\n");
  43. return true;
  44. }
  45. };
  46. class ABHandler: public CivetHandler {
  47. public:
  48. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  49. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  50. mg_printf(conn, "<html><body>");
  51. mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
  52. mg_printf(conn, "</body></html>\n");
  53. return true;
  54. }
  55. };
  56. int main(int argc, char *argv[]) {
  57. const char * options[] = { "document_root", DOCUMENT_ROOT,
  58. "listening_ports", PORT, 0 };
  59. CivetServer server(options);
  60. server.addHandler(EXAMPLE_URI, new ExampleHandler());
  61. server.addHandler(EXIT_URI, new ExitHandler());
  62. server.addHandler("/a", new AHandler());
  63. server.addHandler("/a/b", new ABHandler()); // going out of order with this to see if it will work.
  64. printf("Browse files at http://localhost:%s/\n", PORT);
  65. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  66. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  67. #define TEST1 "a=1 1&b=2%202&a=3+3"
  68. std::string rtn;
  69. std::string back;
  70. bool ok;
  71. ok = CivetServer::getParam(TEST1,strlen(TEST1),"a",rtn,0);
  72. printf("TEST 1 %s,%d => %s ok=%d\n", "a", 0, rtn.c_str(), ok?1:0);
  73. ok = CivetServer::getParam(TEST1,strlen(TEST1),"a",rtn,1);
  74. printf("TEST 1 %s,%d => %s ok=%d\n", "a", 1, rtn.c_str(), ok?1:0);
  75. ok = CivetServer::getParam(TEST1,strlen(TEST1),"a",rtn,2);
  76. printf("TEST 1 %s,%d => %s ok=%d\n", "a", 2, rtn.c_str(), ok?1:0);
  77. ok = CivetServer::getParam(TEST1,strlen(TEST1),"x",rtn,0);
  78. printf("TEST 1 %s,%d => %s ok=%d\n", "x", 0, rtn.c_str(), ok?1:0);
  79. ok = CivetServer::getParam(TEST1,strlen(TEST1),"b",rtn,0);
  80. printf("TEST 1 %s,%d => %s ok=%d\n", "b", 0, rtn.c_str(), ok?1:0);
  81. CivetServer::urlEncode(TEST1,strlen(TEST1),rtn);
  82. CivetServer::urlDecode(rtn,back);
  83. printf("[%s]\n[%s]\n[%s]\n", TEST1, rtn.c_str(), back.c_str());
  84. char b1[256], b2[256];
  85. mg_url_encode(TEST1, b1, 256);
  86. mg_url_decode(b1, 256, b2,256,1);
  87. printf("[%s]\n[%s]\n[%s]\n", TEST1, b1, b2);
  88. while (!exitNow) {
  89. #ifdef _WIN32
  90. Sleep(1000);
  91. #else
  92. sleep(1);
  93. #endif
  94. }
  95. printf("Bye!\n");
  96. return 0;
  97. }