mirror of
https://github.com/cesanta/mongoose.git
synced 2024-11-30 23:49:01 +08:00
rework fetch
This commit is contained in:
parent
5f1f5ffffd
commit
8c4cfc8101
@ -84,14 +84,21 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
}
|
}
|
||||||
struct fetch_data {
|
struct fetch_data {
|
||||||
char *buf;
|
char *buf;
|
||||||
int code, connected, closed;
|
const char *url;
|
||||||
|
int code, closed;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void fcb(struct mg_connection *c, int ev, void *ev_data) {
|
static void fcb(struct mg_connection *c, int ev, void *ev_data) {
|
||||||
struct fetch_data *fd = (struct fetch_data *) c->fn_data;
|
struct fetch_data *fd = (struct fetch_data *) c->fn_data;
|
||||||
if (ev == MG_EV_CONNECT) {
|
if (ev == MG_EV_CONNECT) {
|
||||||
MG_DEBUG(("CONNECT"));
|
MG_DEBUG(("CONNECT"));
|
||||||
fd->connected = 1;
|
if (mg_url_is_ssl(fd->url)) {
|
||||||
|
struct mg_tls_opts opts;
|
||||||
|
memset(&opts, 0, sizeof(opts)); // read CA from packed_fs
|
||||||
|
opts.name = mg_url_host(fd->url);
|
||||||
|
opts.ca = mg_unpacked("/data/ca.pem");
|
||||||
|
mg_tls_init(c, &opts);
|
||||||
|
}
|
||||||
} else if (ev == MG_EV_HTTP_MSG) {
|
} else if (ev == MG_EV_HTTP_MSG) {
|
||||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||||
snprintf(fd->buf, FETCH_BUF_SIZE, "%.*s", (int) hm->message.len, hm->message.buf);
|
snprintf(fd->buf, FETCH_BUF_SIZE, "%.*s", (int) hm->message.len, hm->message.buf);
|
||||||
@ -103,35 +110,20 @@ static void fcb(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
} else if (ev == MG_EV_CLOSE) {
|
} else if (ev == MG_EV_CLOSE) {
|
||||||
MG_DEBUG(("CLOSE"));
|
MG_DEBUG(("CLOSE"));
|
||||||
fd->closed = 1;
|
fd->closed = 1;
|
||||||
|
} else if (ev == MG_EV_READ) {
|
||||||
|
long bytes = *(long *) ev_data;
|
||||||
|
MG_DEBUG(("READ %d: %.*s", (int) bytes, (int) bytes, c->recv.buf));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int fetch(struct mg_mgr *mgr, char *buf, const char *url,
|
static int fetch(struct mg_mgr *mgr, char *buf, const char *url,
|
||||||
const char *fmt, ...) {
|
const char *fmt, ...) {
|
||||||
struct fetch_data fd = {buf, 0, 0, 0};
|
struct fetch_data fd = {buf, url, 0, 0};
|
||||||
int i;
|
int i;
|
||||||
struct mg_connection *c = NULL;
|
struct mg_connection *c = NULL;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
c = mg_http_connect(mgr, url, fcb, &fd);
|
c = mg_http_connect(mgr, url, fcb, &fd);
|
||||||
ASSERT(c != NULL);
|
ASSERT(c != NULL);
|
||||||
for (i = 0; i < 500 && !fd.connected && !fd.closed; i++) {
|
|
||||||
mg_mgr_poll(mgr, 0);
|
|
||||||
usleep(10000); // 10 ms. Slow down poll loop to ensure packet transit
|
|
||||||
}
|
|
||||||
if (fd.closed) return 0;
|
|
||||||
if (c != NULL && mg_url_is_ssl(url)) {
|
|
||||||
struct mg_tls_opts opts;
|
|
||||||
memset(&opts, 0, sizeof(opts)); // read CA from packed_fs
|
|
||||||
opts.name = mg_url_host(url);
|
|
||||||
opts.ca = mg_unpacked("/data/ca.pem");
|
|
||||||
if (strstr(url, "localhost") != NULL) {
|
|
||||||
// Local connection, use self-signed certificates
|
|
||||||
opts.ca = mg_unpacked("/certs/ca.crt");
|
|
||||||
// opts.cert = mg_str(s_tls_cert);
|
|
||||||
// opts.key = mg_str(s_tls_key);
|
|
||||||
}
|
|
||||||
mg_tls_init(c, &opts);
|
|
||||||
}
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
mg_vprintf(c, fmt, &ap);
|
mg_vprintf(c, fmt, &ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
@ -170,9 +162,27 @@ static void test_http_client(struct mg_mgr *mgr) {
|
|||||||
static struct mg_connection *s_conn;
|
static struct mg_connection *s_conn;
|
||||||
static char s_topic[16];
|
static char s_topic[16];
|
||||||
|
|
||||||
|
#define MQTT_URL "mqtt://broker.hivemq.com:1883" // MQTT broker URL
|
||||||
|
#if MG_TLS == MG_TLS_BUILTIN
|
||||||
|
#define MQTTS_URL "mqtts://mongoose.ws:8883" // HiveMQ does not do TLS1.3
|
||||||
|
#define MQTTS_CA mg_str(s_ca_cert)
|
||||||
|
static const char *s_ca_cert;
|
||||||
|
#elif MG_TLS
|
||||||
|
#define MQTTS_URL "mqtts://broker.hivemq.com:8883" // MQTT broker URL
|
||||||
|
#define MQTTS_CA mg_unpacked("/data/ca.pem")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
|
static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||||
if (ev == MG_EV_MQTT_OPEN) {
|
if (ev == MG_EV_CONNECT) {
|
||||||
// MQTT connect is successful
|
MG_DEBUG(("CONNECT"));
|
||||||
|
#if MG_TLS
|
||||||
|
struct mg_tls_opts opts = {.ca = MQTTS_CA,
|
||||||
|
.name = mg_url_host(MQTTS_URL)};
|
||||||
|
mg_tls_init(c, &opts);
|
||||||
|
#endif
|
||||||
|
} else if (ev == MG_EV_MQTT_OPEN) {
|
||||||
|
MG_DEBUG(("MQTT CONNECT"));
|
||||||
struct mg_mqtt_opts sub_opts;
|
struct mg_mqtt_opts sub_opts;
|
||||||
memset(&sub_opts, 0, sizeof(sub_opts));
|
memset(&sub_opts, 0, sizeof(sub_opts));
|
||||||
sub_opts.topic = mg_str(mg_random_str(s_topic, sizeof(s_topic)));
|
sub_opts.topic = mg_str(mg_random_str(s_topic, sizeof(s_topic)));
|
||||||
@ -194,16 +204,21 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
} else if (ev == MG_EV_CLOSE) {
|
} else if (ev == MG_EV_CLOSE) {
|
||||||
MG_DEBUG(("CLOSE"));
|
MG_DEBUG(("CLOSE"));
|
||||||
s_conn = NULL;
|
s_conn = NULL;
|
||||||
|
} else if (ev == MG_EV_ERROR) {
|
||||||
|
MG_ERROR(("%lu ERROR %s", c->id, (char *) ev_data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
|
static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
|
||||||
const char *url = "mqtt://broker.hivemq.com:1883";
|
|
||||||
bool passed = false;
|
bool passed = false;
|
||||||
struct mg_mqtt_opts opts;
|
struct mg_mqtt_opts opts;
|
||||||
memset(&opts, 0, sizeof(opts));
|
memset(&opts, 0, sizeof(opts));
|
||||||
opts.clean = true, opts.version = 4;
|
opts.clean = true, opts.version = 4;
|
||||||
s_conn = mg_mqtt_connect(mgr, url, &opts, mqtt_fn, &passed);
|
#if MG_TLS
|
||||||
|
s_conn = mg_mqtt_connect(mgr, MQTTS_URL, &opts, mqtt_fn, &passed);
|
||||||
|
#else
|
||||||
|
s_conn = mg_mqtt_connect(mgr, MQTT_URL, &opts, mqtt_fn, &passed);
|
||||||
|
#endif
|
||||||
ASSERT(s_conn != NULL);
|
ASSERT(s_conn != NULL);
|
||||||
for (int i = 0; i < 500 && s_conn != NULL && !s_conn->is_closing; i++) {
|
for (int i = 0; i < 500 && s_conn != NULL && !s_conn->is_closing; i++) {
|
||||||
mg_mgr_poll(mgr, 0);
|
mg_mgr_poll(mgr, 0);
|
||||||
@ -330,8 +345,8 @@ int main(void) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// RUN TESTS
|
// RUN TESTS
|
||||||
test_http_server(&mgr, mif.ip);
|
|
||||||
test_http_client(&mgr);
|
test_http_client(&mgr);
|
||||||
|
test_http_server(&mgr, mif.ip);
|
||||||
test_mqtt_connsubpub(&mgr);
|
test_mqtt_connsubpub(&mgr);
|
||||||
|
|
||||||
printf("SUCCESS. Total tests: %d\n", s_num_tests);
|
printf("SUCCESS. Total tests: %d\n", s_num_tests);
|
||||||
@ -343,3 +358,39 @@ int main(void) {
|
|||||||
close(fd);
|
close(fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if MG_TLS == MG_TLS_BUILTIN
|
||||||
|
static const char *s_ca_cert =
|
||||||
|
"-----BEGIN CERTIFICATE-----\n"
|
||||||
|
"MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n"
|
||||||
|
"TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n"
|
||||||
|
"cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n"
|
||||||
|
"WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n"
|
||||||
|
"ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n"
|
||||||
|
"MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n"
|
||||||
|
"h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+\n"
|
||||||
|
"0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U\n"
|
||||||
|
"A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW\n"
|
||||||
|
"T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+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sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n"
|
||||||
|
"jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n"
|
||||||
|
"oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n"
|
||||||
|
"4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n"
|
||||||
|
"mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n"
|
||||||
|
"emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n"
|
||||||
|
"-----END CERTIFICATE-----\n";
|
||||||
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user