hello.c 879 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "mongoose.h"
  4. static void *callback(enum mg_event event,
  5. struct mg_connection *conn,
  6. const struct mg_request_info *request_info) {
  7. if (event == MG_NEW_REQUEST) {
  8. const char *content = "hello from mongoose!";
  9. mg_printf(conn,
  10. "HTTP/1.1 200 OK\r\n"
  11. "Content-Type: text/plain\r\n"
  12. "Content-Length: %d\r\n" // Always set Content-Length
  13. "\r\n"
  14. "%s",
  15. strlen(content),
  16. content);
  17. // Mark as processed
  18. return "";
  19. } else {
  20. return NULL;
  21. }
  22. }
  23. int main(void) {
  24. struct mg_context *ctx;
  25. const char *options[] = {"listening_ports", "8080", NULL};
  26. ctx = mg_start(&callback, NULL, options);
  27. getchar(); // Wait until user hits "enter"
  28. mg_stop(ctx);
  29. return 0;
  30. }