embedded_c.c 3.3 KB

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