mongoose/examples/http-client/main.c

73 lines
3.0 KiB
C
Raw Normal View History

2021-01-30 21:03:11 +08:00
// Copyright (c) 2021 Cesanta Software Limited
2020-12-10 21:26:05 +08:00
// All rights reserved
//
2020-12-18 22:29:30 +08:00
// Example HTTP client. Connect to `s_url`, send request, wait for a response,
// print the response and exit.
2021-01-30 21:03:11 +08:00
// You can change `s_url` from the command line by executing: ./example YOUR_URL
2020-12-10 21:26:05 +08:00
//
2023-03-29 22:46:40 +08:00
// To enable SSL/TLS, , see https://mongoose.ws/tutorials/tls/#how-to-build
2020-12-10 21:26:05 +08:00
#include "mongoose.h"
2021-01-30 21:03:11 +08:00
// The very first web page in history. You can replace it from command line
2021-09-13 20:47:49 +08:00
static const char *s_url = "http://info.cern.ch/";
static const char *s_post_data = NULL; // POST data
static const uint64_t s_timeout_ms = 1500; // Connect timeout in milliseconds
2020-12-18 22:29:30 +08:00
2020-12-10 21:26:05 +08:00
// Print HTTP response and signal that we're done
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
2022-03-20 22:20:47 +08:00
if (ev == MG_EV_OPEN) {
// Connection created. Store connect expiration time in c->data
*(uint64_t *) c->data = mg_millis() + s_timeout_ms;
2022-03-20 22:20:47 +08:00
} else if (ev == MG_EV_POLL) {
if (mg_millis() > *(uint64_t *) c->data &&
2022-03-20 22:20:47 +08:00
(c->is_connecting || c->is_resolving)) {
mg_error(c, "Connect timeout");
}
} else if (ev == MG_EV_CONNECT) {
2021-01-30 21:03:11 +08:00
// Connected to server. Extract host name from URL
2020-12-25 00:52:58 +08:00
struct mg_str host = mg_url_host(s_url);
if (mg_url_is_ssl(s_url)) {
struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
.name = mg_url_host(s_url)};
mg_tls_init(c, &opts);
}
2020-12-18 22:29:30 +08:00
// Send request
2022-03-20 22:20:47 +08:00
int content_length = s_post_data ? strlen(s_post_data) : 0;
2021-01-30 21:03:11 +08:00
mg_printf(c,
2022-03-15 22:14:36 +08:00
"%s %s HTTP/1.0\r\n"
2021-01-30 21:03:11 +08:00
"Host: %.*s\r\n"
2022-03-15 22:14:36 +08:00
"Content-Type: octet-stream\r\n"
"Content-Length: %d\r\n"
2021-01-30 21:03:11 +08:00
"\r\n",
2022-03-20 22:20:47 +08:00
s_post_data ? "POST" : "GET", mg_url_uri(s_url), (int) host.len,
2022-03-15 22:14:36 +08:00
host.ptr, content_length);
2022-03-20 22:20:47 +08:00
mg_send(c, s_post_data, content_length);
2020-12-18 22:29:30 +08:00
} else if (ev == MG_EV_HTTP_MSG) {
// Response is received. Print it
2020-12-10 21:26:05 +08:00
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
printf("%.*s", (int) hm->message.len, hm->message.ptr);
2023-08-18 03:45:10 +08:00
c->is_draining = 1; // Tell mongoose to close this connection
2020-12-18 22:29:30 +08:00
*(bool *) fn_data = true; // Tell event loop to stop
2021-01-30 21:03:11 +08:00
} else if (ev == MG_EV_ERROR) {
*(bool *) fn_data = true; // Error, tell event loop to stop
2020-12-10 21:26:05 +08:00
}
}
2021-01-30 21:03:11 +08:00
int main(int argc, char *argv[]) {
2022-06-10 19:33:30 +08:00
const char *log_level = getenv("LOG_LEVEL"); // Allow user to set log level
if (log_level == NULL) log_level = "4"; // Default is verbose
2022-03-20 22:20:47 +08:00
struct mg_mgr mgr; // Event manager
bool done = false; // Event handler flips it to true
if (argc > 1) s_url = argv[1]; // Use URL provided in the command line
2022-08-01 18:19:32 +08:00
mg_log_set(atoi(log_level)); // Set to 0 to disable debug
2022-03-20 22:20:47 +08:00
mg_mgr_init(&mgr); // Initialise event manager
2020-12-18 22:29:30 +08:00
mg_http_connect(&mgr, s_url, fn, &done); // Create client connection
while (!done) mg_mgr_poll(&mgr, 50); // Event manager loops until 'done'
2020-12-18 22:29:30 +08:00
mg_mgr_free(&mgr); // Free resources
2020-12-10 21:26:05 +08:00
return 0;
}