embedded_c.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. {
  20. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  21. mg_printf(conn, "<html><body>");
  22. mg_printf(conn, "<h2>This is example text!!!</h2>");
  23. mg_printf(conn, "<p>To exit <a href=\"%s\">click here</a></p>",
  24. EXIT_URI);
  25. mg_printf(conn, "</body></html>\n");
  26. return 1;
  27. }
  28. int ExitHandler(struct mg_connection *conn, void *cbdata)
  29. {
  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 = 1;
  33. return 1;
  34. }
  35. int AHandler(struct mg_connection *conn, void *cbdata)
  36. {
  37. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  38. mg_printf(conn, "<html><body>");
  39. mg_printf(conn, "<h2>This is the A handler!!!</h2>");
  40. mg_printf(conn, "</body></html>\n");
  41. return 1;
  42. }
  43. int ABHandler(struct mg_connection *conn, void *cbdata)
  44. {
  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 AB handler!!!</h2>");
  48. mg_printf(conn, "</body></html>\n");
  49. return 1;
  50. }
  51. int FooHandler(struct mg_connection *conn, void *cbdata)
  52. {
  53. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  54. mg_printf(conn, "<html><body>");
  55. mg_printf(conn, "<h2>This is the Foo handler!!!</h2>");
  56. mg_printf(conn, "</body></html>\n");
  57. return 1;
  58. }
  59. int main(int argc, char *argv[])
  60. {
  61. const char * options[] = { "document_root", DOCUMENT_ROOT,
  62. "listening_ports", PORT, 0
  63. };
  64. struct mg_callbacks callbacks;
  65. struct mg_context *ctx;
  66. memset(&callbacks, 0, sizeof(callbacks));
  67. ctx = mg_start(&callbacks, 0, options);
  68. mg_set_request_handler(ctx,EXAMPLE_URI, ExampleHandler,0);
  69. mg_set_request_handler(ctx,EXIT_URI, ExitHandler,0);
  70. mg_set_request_handler(ctx,"/a", AHandler,0);
  71. mg_set_request_handler(ctx,"/a/b", ABHandler,0);
  72. mg_set_request_handler( ctx, "**.foo$", FooHandler,0);
  73. printf("Browse files at http://localhost:%s/\n", PORT);
  74. printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
  75. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  76. while (!exitNow) {
  77. #ifdef _WIN32
  78. Sleep(1000);
  79. #else
  80. sleep(1);
  81. #endif
  82. }
  83. printf("Bye!\n");
  84. return 0;
  85. }