mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-30 12:50:58 +00:00
Refactor HTTP client
This commit is contained in:
1 parent
b0a5165b44
commit
aede72fa37
6 files changed
+42
-45
No files matched your search
@@ -1,5 +1,4 @@
|
|||||||
PROG ?= example
|
PROG ?= example
|
||||||
ARGS ?= http://www.ladyada.net/
|
|
||||||
MBEDTLS_DIR ?=
|
MBEDTLS_DIR ?=
|
||||||
|
|
||||||
ifeq "$(MBEDTLS_DIR)" ""
|
ifeq "$(MBEDTLS_DIR)" ""
|
||||||
|
|||||||
+26
-28
@@ -1,46 +1,44 @@
|
|||||||
// Copyright (c) 2020 Cesanta Software Limited
|
// Copyright (c) 2020 Cesanta Software Limited
|
||||||
// All rights reserved
|
// All rights reserved
|
||||||
//
|
//
|
||||||
// Example HTTP client. Usage:
|
// Example HTTP client. Connect to `s_url`, send request, wait for a response,
|
||||||
// make
|
// print the response and exit.
|
||||||
// ./example http://www.ladyada.net/
|
|
||||||
//
|
//
|
||||||
// To enable SSL/TLS for this client, build it like this:
|
// To enable SSL/TLS for this client, build it like this:
|
||||||
// make MBEDTLS_DIR=/path/to/your/mbedtls/installation
|
// make MBEDTLS_DIR=/path/to/your/mbedtls/installation
|
||||||
|
|
||||||
#include "mongoose.h"
|
#include "mongoose.h"
|
||||||
|
|
||||||
|
// static const char *s_url = "http://www.ladyada.net";
|
||||||
|
static const char *s_url = "https://github.com";
|
||||||
|
|
||||||
// Print HTTP response and signal that we're done
|
// Print HTTP response and signal that we're done
|
||||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||||
if (ev == MG_EV_HTTP_MSG) {
|
if (ev == MG_EV_CONNECT) {
|
||||||
|
// Connected to server
|
||||||
|
if (mg_url_is_ssl(s_url)) {
|
||||||
|
// If s_url is https://, tell client connection to use TLS
|
||||||
|
struct mg_tls_opts opts = {.ca = "ca.pem"};
|
||||||
|
mg_tls_init(c, &opts);
|
||||||
|
}
|
||||||
|
// Send request
|
||||||
|
mg_printf(c, "GET %s HTTP/1.0\r\n\r\n", mg_url_uri(s_url));
|
||||||
|
} else if (ev == MG_EV_HTTP_MSG) {
|
||||||
|
// Response is received. Print it
|
||||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||||
printf("%.*s", (int) hm->message.len, hm->message.ptr);
|
printf("%.*s", (int) hm->message.len, hm->message.ptr);
|
||||||
c->is_closing = 1;
|
c->is_closing = 1; // Tell mongoose to close this connection
|
||||||
*(bool *) fn_data = true;
|
*(bool *) fn_data = true; // Tell event loop to stop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(void) {
|
||||||
struct mg_mgr mgr; // Event manager
|
struct mg_mgr mgr; // Event manager
|
||||||
mg_mgr_init(&mgr); // Initialise event manager
|
bool done = false; // Event handler flips it to true
|
||||||
|
mg_log_set("3"); // Set to 0 to disable debug
|
||||||
if (argc != 2) {
|
mg_mgr_init(&mgr); // Initialise event manager
|
||||||
fprintf(stderr, "Usage: %s URL\n", argv[0]); // Print error
|
mg_http_connect(&mgr, s_url, fn, &done); // Create client connection
|
||||||
} else {
|
while (!done) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
||||||
bool done = false; // Event handler flips it to true
|
mg_mgr_free(&mgr); // Free resources
|
||||||
struct mg_connection *c = mg_http_connect(&mgr, argv[1], fn, &done);
|
|
||||||
if (c != NULL) {
|
|
||||||
mg_printf(c, "GET %s HTTP/1.0\r\n\r\n", mg_url_uri(argv[1]));
|
|
||||||
// If target URL is https://, tell client connection to use TLS
|
|
||||||
if (mg_url_is_ssl(argv[1])) {
|
|
||||||
struct mg_tls_opts opts = {.ca = "ca.pem"};
|
|
||||||
mg_tls_init(c, &opts);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (done == false) mg_mgr_poll(&mgr, 1000); // Event loop
|
|
||||||
}
|
|
||||||
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+8
-8
@@ -184,7 +184,7 @@ static size_t mg_dns_parse_name(const uint8_t *s, const uint8_t *e, size_t off,
|
|||||||
j += n;
|
j += n;
|
||||||
i += n + 1;
|
i += n + 1;
|
||||||
to[j] = '\0'; // Zero-terminate this chunk
|
to[j] = '\0'; // Zero-terminate this chunk
|
||||||
LOG(LL_DEBUG, ("-- %zu/%zu %zu %zu", i, e - s, j, n));
|
// LOG(LL_DEBUG, ("-- %zu/%zu %zu %zu", i, e - s, j, n));
|
||||||
}
|
}
|
||||||
if (tolen > 0) to[tolen - 1] = '\0'; // Make sure make sure it is nul-term
|
if (tolen > 0) to[tolen - 1] = '\0'; // Make sure make sure it is nul-term
|
||||||
return i;
|
return i;
|
||||||
@@ -2781,13 +2781,11 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
|
|||||||
if (mg_tls_handshake(c)) c->is_tls_hs = 0;
|
if (mg_tls_handshake(c)) c->is_tls_hs = 0;
|
||||||
#endif
|
#endif
|
||||||
} else if (c->is_connecting) {
|
} else if (c->is_connecting) {
|
||||||
if (c->is_readable || c->is_writable) {
|
if (c->is_readable || c->is_writable) connect_conn(c);
|
||||||
connect_conn(c);
|
|
||||||
}
|
|
||||||
} else if (c->is_tls_hs) {
|
} else if (c->is_tls_hs) {
|
||||||
if ((c->is_readable || c->is_writable) && mg_tls_handshake(c)) {
|
if ((c->is_readable || c->is_writable) && mg_tls_handshake(c)) {
|
||||||
c->is_tls_hs = 0;
|
c->is_tls_hs = 0;
|
||||||
mg_call(c, MG_EV_CONNECT, NULL);
|
if (c->is_connecting) mg_call(c, MG_EV_CONNECT, NULL);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (c->is_readable) read_conn(c, ll_read);
|
if (c->is_readable) read_conn(c, ll_read);
|
||||||
@@ -3003,11 +3001,11 @@ int mg_tls_handshake(struct mg_connection *c) {
|
|||||||
mbedtls_ssl_set_bio(&tls->ssl, &c->fd, mbedtls_net_send, mbedtls_net_recv, 0);
|
mbedtls_ssl_set_bio(&tls->ssl, &c->fd, mbedtls_net_send, mbedtls_net_recv, 0);
|
||||||
rc = mbedtls_ssl_handshake(&tls->ssl);
|
rc = mbedtls_ssl_handshake(&tls->ssl);
|
||||||
if (rc == 0) { // Success
|
if (rc == 0) { // Success
|
||||||
LOG(LL_DEBUG, ("%p OK", c->fd));
|
LOG(LL_DEBUG, ("%p succes", c->fd));
|
||||||
} else if (rc == MBEDTLS_ERR_SSL_WANT_READ ||
|
} else if (rc == MBEDTLS_ERR_SSL_WANT_READ ||
|
||||||
rc == MBEDTLS_ERR_SSL_WANT_WRITE) { // Still pending
|
rc == MBEDTLS_ERR_SSL_WANT_WRITE) { // Still pending
|
||||||
LOG(LL_VERBOSE_DEBUG, ("%p pending, %d%d %d (-%#x)", c->fd, c->is_connecting,
|
LOG(LL_VERBOSE_DEBUG, ("%p pending, %d%d %d (-%#x)", c->fd,
|
||||||
c->is_tls_hs, rc, -rc));
|
c->is_connecting, c->is_tls_hs, rc, -rc));
|
||||||
} else {
|
} else {
|
||||||
mg_error(c, "TLS handshake: -%#x", -rc); // Error
|
mg_error(c, "TLS handshake: -%#x", -rc); // Error
|
||||||
}
|
}
|
||||||
@@ -3096,6 +3094,7 @@ int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
|||||||
c->tls = tls;
|
c->tls = tls;
|
||||||
c->is_tls = 1;
|
c->is_tls = 1;
|
||||||
c->is_tls_hs = 1;
|
c->is_tls_hs = 1;
|
||||||
|
if (c->is_client && !c->is_connecting) mg_tls_handshake(c);
|
||||||
return 1;
|
return 1;
|
||||||
fail:
|
fail:
|
||||||
c->is_closing = 1;
|
c->is_closing = 1;
|
||||||
@@ -3223,6 +3222,7 @@ int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
|||||||
c->tls = tls;
|
c->tls = tls;
|
||||||
c->is_tls = 1;
|
c->is_tls = 1;
|
||||||
c->is_tls_hs = 1;
|
c->is_tls_hs = 1;
|
||||||
|
if (c->is_client && !c->is_connecting) mg_tls_handshake(c);
|
||||||
return 1;
|
return 1;
|
||||||
fail:
|
fail:
|
||||||
c->is_closing = 1;
|
c->is_closing = 1;
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ static size_t mg_dns_parse_name(const uint8_t *s, const uint8_t *e, size_t off,
|
|||||||
j += n;
|
j += n;
|
||||||
i += n + 1;
|
i += n + 1;
|
||||||
to[j] = '\0'; // Zero-terminate this chunk
|
to[j] = '\0'; // Zero-terminate this chunk
|
||||||
LOG(LL_DEBUG, ("-- %zu/%zu %zu %zu", i, e - s, j, n));
|
// LOG(LL_DEBUG, ("-- %zu/%zu %zu %zu", i, e - s, j, n));
|
||||||
}
|
}
|
||||||
if (tolen > 0) to[tolen - 1] = '\0'; // Make sure make sure it is nul-term
|
if (tolen > 0) to[tolen - 1] = '\0'; // Make sure make sure it is nul-term
|
||||||
return i;
|
return i;
|
||||||
|
|||||||
+2
-4
@@ -514,13 +514,11 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
|
|||||||
if (mg_tls_handshake(c)) c->is_tls_hs = 0;
|
if (mg_tls_handshake(c)) c->is_tls_hs = 0;
|
||||||
#endif
|
#endif
|
||||||
} else if (c->is_connecting) {
|
} else if (c->is_connecting) {
|
||||||
if (c->is_readable || c->is_writable) {
|
if (c->is_readable || c->is_writable) connect_conn(c);
|
||||||
connect_conn(c);
|
|
||||||
}
|
|
||||||
} else if (c->is_tls_hs) {
|
} else if (c->is_tls_hs) {
|
||||||
if ((c->is_readable || c->is_writable) && mg_tls_handshake(c)) {
|
if ((c->is_readable || c->is_writable) && mg_tls_handshake(c)) {
|
||||||
c->is_tls_hs = 0;
|
c->is_tls_hs = 0;
|
||||||
mg_call(c, MG_EV_CONNECT, NULL);
|
if (c->is_connecting) mg_call(c, MG_EV_CONNECT, NULL);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (c->is_readable) read_conn(c, ll_read);
|
if (c->is_readable) read_conn(c, ll_read);
|
||||||
|
|||||||
@@ -33,11 +33,11 @@ int mg_tls_handshake(struct mg_connection *c) {
|
|||||||
mbedtls_ssl_set_bio(&tls->ssl, &c->fd, mbedtls_net_send, mbedtls_net_recv, 0);
|
mbedtls_ssl_set_bio(&tls->ssl, &c->fd, mbedtls_net_send, mbedtls_net_recv, 0);
|
||||||
rc = mbedtls_ssl_handshake(&tls->ssl);
|
rc = mbedtls_ssl_handshake(&tls->ssl);
|
||||||
if (rc == 0) { // Success
|
if (rc == 0) { // Success
|
||||||
LOG(LL_DEBUG, ("%p OK", c->fd));
|
LOG(LL_DEBUG, ("%p succes", c->fd));
|
||||||
} else if (rc == MBEDTLS_ERR_SSL_WANT_READ ||
|
} else if (rc == MBEDTLS_ERR_SSL_WANT_READ ||
|
||||||
rc == MBEDTLS_ERR_SSL_WANT_WRITE) { // Still pending
|
rc == MBEDTLS_ERR_SSL_WANT_WRITE) { // Still pending
|
||||||
LOG(LL_VERBOSE_DEBUG, ("%p pending, %d%d %d (-%#x)", c->fd, c->is_connecting,
|
LOG(LL_VERBOSE_DEBUG, ("%p pending, %d%d %d (-%#x)", c->fd,
|
||||||
c->is_tls_hs, rc, -rc));
|
c->is_connecting, c->is_tls_hs, rc, -rc));
|
||||||
} else {
|
} else {
|
||||||
mg_error(c, "TLS handshake: -%#x", -rc); // Error
|
mg_error(c, "TLS handshake: -%#x", -rc); // Error
|
||||||
}
|
}
|
||||||
@@ -126,6 +126,7 @@ int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
|||||||
c->tls = tls;
|
c->tls = tls;
|
||||||
c->is_tls = 1;
|
c->is_tls = 1;
|
||||||
c->is_tls_hs = 1;
|
c->is_tls_hs = 1;
|
||||||
|
if (c->is_client && !c->is_connecting) mg_tls_handshake(c);
|
||||||
return 1;
|
return 1;
|
||||||
fail:
|
fail:
|
||||||
c->is_closing = 1;
|
c->is_closing = 1;
|
||||||
@@ -253,6 +254,7 @@ int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
|||||||
c->tls = tls;
|
c->tls = tls;
|
||||||
c->is_tls = 1;
|
c->is_tls = 1;
|
||||||
c->is_tls_hs = 1;
|
c->is_tls_hs = 1;
|
||||||
|
if (c->is_client && !c->is_connecting) mg_tls_handshake(c);
|
||||||
return 1;
|
return 1;
|
||||||
fail:
|
fail:
|
||||||
c->is_closing = 1;
|
c->is_closing = 1;
|
||||||
|
|||||||
Reference in new issue
Block a user