embedded_c.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifdef _WIN32
  7. #include <Windows.h>
  8. #else
  9. #include <unistd.h>
  10. #endif
  11. #include <string.h>
  12. #include "civetweb.h"
  13. #define DOCUMENT_ROOT "."
  14. #define PORT "8888"
  15. #define EXAMPLE_URI "/example"
  16. #define EXIT_URI "/exit"
  17. int exitNow = 0;
  18. int ExampleHandler(struct mg_connection *conn, void *cbdata) {
  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 example text!!!</h2>");
  22. mg_printf(conn, "<p>To exit <a href=\"%s\">click here</a></p>",
  23. EXIT_URI);
  24. mg_printf(conn, "</body></html>\n");
  25. return 1;
  26. }
  27. int ExitHandler(struct mg_connection *conn, void *cbdata) {
  28. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  29. mg_printf(conn, "Bye!\n");
  30. exitNow = 1;
  31. return 1;
  32. }
  33. int AHandler(struct mg_connection *conn, void *cbdata) {
  34. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  35. mg_printf(conn, "<html><body>");
  36. mg_printf(conn, "<h2>This is the A handler!!!</h2>");
  37. mg_printf(conn, "</body></html>\n");
  38. return 1;
  39. }
  40. int ABHandler(struct mg_connection *conn, void *cbdata) {
  41. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  42. mg_printf(conn, "<html><body>");
  43. mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
  44. mg_printf(conn, "</body></html>\n");
  45. return 1;
  46. }
  47. int main(int argc, char *argv[]) {
  48. const char * options[] = { "document_root", DOCUMENT_ROOT,
  49. "listening_ports", PORT, 0 };
  50. struct mg_callbacks callbacks;
  51. struct mg_context *ctx;
  52. memset(&callbacks, 0, sizeof(callbacks));
  53. ctx = mg_start(&callbacks, 0, options);
  54. mg_set_request_handler(ctx,EXAMPLE_URI, ExampleHandler,0);
  55. mg_set_request_handler(ctx,EXIT_URI, ExitHandler,0);
  56. mg_set_request_handler(ctx,"/a", AHandler,0);
  57. mg_set_request_handler(ctx,"/a/b", ABHandler,0); // going out of order with this to see if it will work.
  58. printf("Browse files at http://localhost:%s/\n", PORT);
  59. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  60. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  61. while (!exitNow) {
  62. #ifdef _WIN32
  63. Sleep(1000);
  64. #else
  65. sleep(1);
  66. #endif
  67. }
  68. printf("Bye!\n");
  69. return 0;
  70. }