main.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2021 Cesanta Software Limited
  2. // All rights reserved
  3. //
  4. // Example HTTP client.
  5. // Instead of buffering the whole HTTP response in memory and wait for the
  6. // MG_EV_HTTP_MSG event, this client catches MG_EV_HTTP_CHUNK events and
  7. // prints them. This is useful to download large content without spooling
  8. // everything to memory, or catch chunked content.
  9. //
  10. // You can change `s_url` from the command line by executing: ./example YOUR_URL
  11. //
  12. // To enable SSL/TLS for this client, build it like this:
  13. // make MBEDTLS=/path/to/your/mbedtls/installation
  14. // make OPENSSL=/path/to/your/openssl/installation
  15. #include "mongoose.h"
  16. // The very first web page in history. You can replace it from command line
  17. static const char *s_url = "http://info.cern.ch";
  18. // Print HTTP response and signal that we're done
  19. static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  20. if (ev == MG_EV_CONNECT) {
  21. // Connected to server. Extract host name from URL
  22. struct mg_str host = mg_url_host(s_url);
  23. // If s_url is https://, tell client connection to use TLS
  24. if (mg_url_is_ssl(s_url)) {
  25. struct mg_tls_opts opts = {.ca = "ca.pem", .srvname = host};
  26. mg_tls_init(c, &opts);
  27. }
  28. // Send request
  29. mg_printf(c,
  30. "GET %s HTTP/1.1\r\n"
  31. "Connection: keep-alive\r\n"
  32. "Keep-Alive: timeout=60\r\n"
  33. "Host: %.*s\r\n"
  34. "\r\n",
  35. mg_url_uri(s_url), (int) host.len, host.ptr);
  36. } else if (ev == MG_EV_HTTP_CHUNK) {
  37. struct mg_http_message *hm = (struct mg_http_message *) ev_data;
  38. fwrite(hm->chunk.ptr, 1, hm->chunk.len, stdout);
  39. // fprintf(stderr, "c %u\n", (unsigned) hm->chunk.len);
  40. mg_http_delete_chunk(c, hm);
  41. if (hm->chunk.len == 0) *(bool *) fn_data = true; // Last chunk
  42. } else if (ev == MG_EV_HTTP_MSG) {
  43. // Response is received. Print it
  44. struct mg_http_message *hm = (struct mg_http_message *) ev_data;
  45. fwrite(hm->body.ptr, 1, hm->body.len, stdout);
  46. c->is_closing = 1; // Tell mongoose to close this connection
  47. *(bool *) fn_data = true; // Tell event loop to stop
  48. } else if (ev == MG_EV_ERROR) {
  49. *(bool *) fn_data = true; // Error, tell event loop to stop
  50. }
  51. }
  52. int main(int argc, char *argv[]) {
  53. struct mg_mgr mgr; // Event manager
  54. bool done = false; // Event handler flips it to true
  55. const char *log_level = getenv("V"); // Log level
  56. if (log_level == NULL) log_level = "3"; // If not set, set to DEBUG
  57. mg_log_set(atoi(log_level)); // Set to 0 to disable debug log
  58. if (argc > 1) s_url = argv[1]; // Use URL from command line
  59. mg_mgr_init(&mgr); // Initialise event manager
  60. mg_http_connect(&mgr, s_url, fn, &done); // Create client connection
  61. while (!done) mg_mgr_poll(&mgr, 1000); // Infinite event loop
  62. mg_mgr_free(&mgr); // Free resources
  63. return 0;
  64. }