Fix http client not continuing requests in case of failure to connect
parent
36c276358b
commit
b7b2adfa32
|
@ -62,6 +62,7 @@ struct http_co_t
|
||||||
void run_cb_and_clear();
|
void run_cb_and_clear();
|
||||||
void start_connection();
|
void start_connection();
|
||||||
void close_connection();
|
void close_connection();
|
||||||
|
void next_request();
|
||||||
void handle_events();
|
void handle_events();
|
||||||
void handle_connect_result();
|
void handle_connect_result();
|
||||||
void submit_read();
|
void submit_read();
|
||||||
|
@ -169,6 +170,7 @@ void http_co_t::send_request(const std::string & host, const std::string & reque
|
||||||
close_connection();
|
close_connection();
|
||||||
parsed = { .error = "HTTP request timed out" };
|
parsed = { .error = "HTTP request timed out" };
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
|
next_request();
|
||||||
stackout();
|
stackout();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -274,6 +276,7 @@ void http_co_t::start_connection()
|
||||||
struct sockaddr_storage addr;
|
struct sockaddr_storage addr;
|
||||||
if (!string_to_addr(host.c_str(), 1, 80, &addr))
|
if (!string_to_addr(host.c_str(), 1, 80, &addr))
|
||||||
{
|
{
|
||||||
|
close_connection();
|
||||||
parsed = { .error = "Invalid address: "+host };
|
parsed = { .error = "Invalid address: "+host };
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
stackout();
|
stackout();
|
||||||
|
@ -282,6 +285,7 @@ void http_co_t::start_connection()
|
||||||
peer_fd = socket(addr.ss_family, SOCK_STREAM, 0);
|
peer_fd = socket(addr.ss_family, SOCK_STREAM, 0);
|
||||||
if (peer_fd < 0)
|
if (peer_fd < 0)
|
||||||
{
|
{
|
||||||
|
close_connection();
|
||||||
parsed = { .error = std::string("socket: ")+strerror(errno) };
|
parsed = { .error = std::string("socket: ")+strerror(errno) };
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
stackout();
|
stackout();
|
||||||
|
@ -296,6 +300,7 @@ void http_co_t::start_connection()
|
||||||
close_connection();
|
close_connection();
|
||||||
parsed = { .error = std::string("connect: ")+strerror(errno) };
|
parsed = { .error = std::string("connect: ")+strerror(errno) };
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
|
next_request();
|
||||||
stackout();
|
stackout();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -329,6 +334,7 @@ void http_co_t::handle_events()
|
||||||
{
|
{
|
||||||
close_connection();
|
close_connection();
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
|
next_request();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -350,6 +356,7 @@ void http_co_t::handle_connect_result()
|
||||||
close_connection();
|
close_connection();
|
||||||
parsed = { .error = std::string("connect: ")+strerror(result) };
|
parsed = { .error = std::string("connect: ")+strerror(result) };
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
|
next_request();
|
||||||
stackout();
|
stackout();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -389,6 +396,7 @@ again:
|
||||||
close_connection();
|
close_connection();
|
||||||
parsed = { .error = std::string("sendmsg: ")+strerror(errno) };
|
parsed = { .error = std::string("sendmsg: ")+strerror(errno) };
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
|
next_request();
|
||||||
stackout();
|
stackout();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -438,6 +446,7 @@ void http_co_t::submit_read()
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
parsed = { .error = std::string("recvmsg: ")+strerror(-res) };
|
parsed = { .error = std::string("recvmsg: ")+strerror(-res) };
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
|
next_request();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -488,6 +497,7 @@ bool http_co_t::handle_read()
|
||||||
close_connection();
|
close_connection();
|
||||||
parsed = { .error = "Response has neither Connection: close, nor Transfer-Encoding: chunked nor Content-Length headers" };
|
parsed = { .error = "Response has neither Connection: close, nor Transfer-Encoding: chunked nor Content-Length headers" };
|
||||||
run_cb_and_clear();
|
run_cb_and_clear();
|
||||||
|
next_request();
|
||||||
stackout();
|
stackout();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -552,24 +562,25 @@ bool http_co_t::handle_read()
|
||||||
response_callback = NULL;
|
response_callback = NULL;
|
||||||
parsed = {};
|
parsed = {};
|
||||||
if (!keepalive)
|
if (!keepalive)
|
||||||
{
|
|
||||||
close_connection();
|
close_connection();
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
state = HTTP_CO_KEEPALIVE;
|
state = HTTP_CO_KEEPALIVE;
|
||||||
if (keepalive_queue.size() > 0)
|
next_request();
|
||||||
{
|
|
||||||
auto next = keepalive_queue[0];
|
|
||||||
keepalive_queue.erase(keepalive_queue.begin(), keepalive_queue.begin()+1);
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
stackout();
|
stackout();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void http_co_t::next_request()
|
||||||
|
{
|
||||||
|
if (keepalive_queue.size() > 0)
|
||||||
|
{
|
||||||
|
auto next = keepalive_queue[0];
|
||||||
|
keepalive_queue.erase(keepalive_queue.begin(), keepalive_queue.begin()+1);
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t stoull_full(const std::string & str, int base)
|
uint64_t stoull_full(const std::string & str, int base)
|
||||||
{
|
{
|
||||||
if (isspace(str[0]))
|
if (isspace(str[0]))
|
||||||
|
|
Loading…
Reference in New Issue