mirror of
https://github.com/cesanta/mongoose.git
synced 2025-07-30 17:36:14 +08:00
Fix #1445 - send error to a resolving connection when DNS error happens
This commit is contained in:
parent
5fc284722f
commit
9fd80fd136
@ -293,6 +293,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
|||||||
} else if (ev == MG_EV_CLOSE) {
|
} else if (ev == MG_EV_CLOSE) {
|
||||||
for (d = s_reqs; d != NULL; d = tmp) {
|
for (d = s_reqs; d != NULL; d = tmp) {
|
||||||
tmp = d->next;
|
tmp = d->next;
|
||||||
|
mg_error(d->c, "DNS error");
|
||||||
mg_dns_free(d);
|
mg_dns_free(d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3232,7 +3233,9 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||||
mg_event_handler_t fn, void *fn_data) {
|
mg_event_handler_t fn, void *fn_data) {
|
||||||
struct mg_connection *c = NULL;
|
struct mg_connection *c = NULL;
|
||||||
if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) {
|
if (url == NULL || url[0] == '\0') {
|
||||||
|
LOG(LL_ERROR, ("null url"));
|
||||||
|
} else if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) {
|
||||||
LOG(LL_ERROR, ("OOM"));
|
LOG(LL_ERROR, ("OOM"));
|
||||||
} else {
|
} else {
|
||||||
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
||||||
|
@ -183,6 +183,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
|||||||
} else if (ev == MG_EV_CLOSE) {
|
} else if (ev == MG_EV_CLOSE) {
|
||||||
for (d = s_reqs; d != NULL; d = tmp) {
|
for (d = s_reqs; d != NULL; d = tmp) {
|
||||||
tmp = d->next;
|
tmp = d->next;
|
||||||
|
mg_error(d->c, "DNS error");
|
||||||
mg_dns_free(d);
|
mg_dns_free(d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -361,7 +361,9 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||||
mg_event_handler_t fn, void *fn_data) {
|
mg_event_handler_t fn, void *fn_data) {
|
||||||
struct mg_connection *c = NULL;
|
struct mg_connection *c = NULL;
|
||||||
if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) {
|
if (url == NULL || url[0] == '\0') {
|
||||||
|
LOG(LL_ERROR, ("null url"));
|
||||||
|
} else if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) {
|
||||||
LOG(LL_ERROR, ("OOM"));
|
LOG(LL_ERROR, ("OOM"));
|
||||||
} else {
|
} else {
|
||||||
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
||||||
|
@ -1152,6 +1152,23 @@ static void fn1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|||||||
(void) c;
|
(void) c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_dns_timeout(const char *dns_server_url, const char *errstr) {
|
||||||
|
// Test timeout
|
||||||
|
struct mg_mgr mgr;
|
||||||
|
char buf[100] = "";
|
||||||
|
int i;
|
||||||
|
mg_mgr_init(&mgr);
|
||||||
|
mgr.dns4.url = dns_server_url;
|
||||||
|
mgr.dnstimeout = 10;
|
||||||
|
LOG(LL_DEBUG, ("opening dummy DNS listener..."));
|
||||||
|
mg_listen(&mgr, mgr.dns4.url, NULL, NULL); // Just discard our queries
|
||||||
|
mg_http_connect(&mgr, "http://google.com", fn1, buf);
|
||||||
|
for (i = 0; i < 50 && buf[0] == '\0'; i++) mg_mgr_poll(&mgr, 1);
|
||||||
|
mg_mgr_free(&mgr);
|
||||||
|
LOG(LL_DEBUG, ("buf: [%s]", buf));
|
||||||
|
ASSERT(strcmp(buf, errstr) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
static void test_dns(void) {
|
static void test_dns(void) {
|
||||||
struct mg_dns_message dm;
|
struct mg_dns_message dm;
|
||||||
// txid flags numQ numA numAP numOP
|
// txid flags numQ numA numAP numOP
|
||||||
@ -1171,21 +1188,9 @@ static void test_dns(void) {
|
|||||||
ASSERT(mg_dns_parse(data, sizeof(data), &dm) == 1);
|
ASSERT(mg_dns_parse(data, sizeof(data), &dm) == 1);
|
||||||
ASSERT(strcmp(dm.name, "") == 0);
|
ASSERT(strcmp(dm.name, "") == 0);
|
||||||
|
|
||||||
{
|
test_dns_timeout("udp://127.0.0.1:12345", "DNS timeout");
|
||||||
// Test timeout
|
test_dns_timeout("", "resolver");
|
||||||
struct mg_mgr mgr;
|
test_dns_timeout("tcp://0.0.0.0:0", "DNS error");
|
||||||
char buf[100] = "";
|
|
||||||
int i;
|
|
||||||
mg_mgr_init(&mgr);
|
|
||||||
mgr.dns4.url = "udp://127.0.0.1:12345";
|
|
||||||
mgr.dnstimeout = 10;
|
|
||||||
LOG(LL_DEBUG, ("opening dummy DNS listener..."));
|
|
||||||
mg_listen(&mgr, mgr.dns4.url, NULL, NULL); // Just discard our queries
|
|
||||||
mg_http_connect(&mgr, "http://google.com", fn1, buf);
|
|
||||||
for (i = 0; i < 50 && buf[0] == '\0'; i++) mg_mgr_poll(&mgr, 1);
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
ASSERT(strcmp(buf, "DNS timeout") == 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_util(void) {
|
static void test_util(void) {
|
||||||
|
Loading…
Reference in New Issue
Block a user