RequestServer+LibDNS: Don't .await() the DNS lookup promise

...and make sure it will eventually complete (or fail) by adding a
timeout retry sequence.

Fixes an issue where RequestServer would stick around after exit,
waiting for piled up DNS requests for a long time.
This commit is contained in:
Ali Mohammad Pur 2024-11-25 01:04:29 +01:00 committed by Andreas Kling
commit ff311c1560
Notes: github-actions[bot] 2024-11-25 10:47:30 +00:00
2 changed files with 151 additions and 112 deletions

View file

@ -344,120 +344,118 @@ void ConnectionFromClient::start_request(i32 request_id, ByteString const& metho
}
auto host = url.serialized_host().value().to_byte_string();
auto dns_promise = m_resolver->dns.lookup(host, DNS::Messages::Class::IN, Array { DNS::Messages::ResourceType::A, DNS::Messages::ResourceType::AAAA }.span());
auto resolve_result = dns_promise->await();
if (resolve_result.is_error()) {
dbgln("StartRequest: DNS lookup failed for '{}': {}", host, resolve_result.error());
async_request_finished(request_id, 0, Requests::NetworkError::UnableToResolveHost);
return;
}
m_resolver->dns.lookup(host, DNS::Messages::Class::IN, Array { DNS::Messages::ResourceType::A, DNS::Messages::ResourceType::AAAA }.span())
->when_rejected([this, request_id](auto const& error) {
dbgln("StartRequest: DNS lookup failed: {}", error);
async_request_finished(request_id, 0, Requests::NetworkError::UnableToResolveHost);
})
.when_resolved([this, request_id, host, url, method, request_body, request_headers, proxy_data](auto const& dns_result) {
if (dns_result->records().is_empty()) {
dbgln("StartRequest: DNS lookup failed for '{}'", host);
async_request_finished(request_id, 0, Requests::NetworkError::UnableToResolveHost);
return;
}
auto dns_result = resolve_result.release_value();
if (dns_result->records().is_empty()) {
dbgln("StartRequest: DNS lookup failed for '{}'", host);
async_request_finished(request_id, 0, Requests::NetworkError::UnableToResolveHost);
return;
}
auto* easy = curl_easy_init();
if (!easy) {
dbgln("StartRequest: Failed to initialize curl easy handle");
return;
}
auto* easy = curl_easy_init();
if (!easy) {
dbgln("StartRequest: Failed to initialize curl easy handle");
return;
}
auto fds_or_error = Core::System::pipe2(O_NONBLOCK);
if (fds_or_error.is_error()) {
dbgln("StartRequest: Failed to create pipe: {}", fds_or_error.error());
return;
}
auto fds_or_error = Core::System::pipe2(O_NONBLOCK);
if (fds_or_error.is_error()) {
dbgln("StartRequest: Failed to create pipe: {}", fds_or_error.error());
return;
}
auto fds = fds_or_error.release_value();
auto writer_fd = fds[1];
auto reader_fd = fds[0];
async_request_started(request_id, IPC::File::adopt_fd(reader_fd));
auto fds = fds_or_error.release_value();
auto writer_fd = fds[1];
auto reader_fd = fds[0];
async_request_started(request_id, IPC::File::adopt_fd(reader_fd));
auto request = make<ActiveRequest>(*this, m_curl_multi, easy, request_id, writer_fd);
request->url = url.to_string().value();
auto request = make<ActiveRequest>(*this, m_curl_multi, easy, request_id, writer_fd);
request->url = url.to_string().value();
auto set_option = [easy](auto option, auto value) {
auto result = curl_easy_setopt(easy, option, value);
if (result != CURLE_OK) {
dbgln("StartRequest: Failed to set curl option: {}", curl_easy_strerror(result));
return false;
}
return true;
};
auto set_option = [easy](auto option, auto value) {
auto result = curl_easy_setopt(easy, option, value);
if (result != CURLE_OK) {
dbgln("StartRequest: Failed to set curl option: {}", curl_easy_strerror(result));
return false;
}
return true;
};
set_option(CURLOPT_PRIVATE, request.ptr());
set_option(CURLOPT_PRIVATE, request.ptr());
if (!g_default_certificate_path.is_empty())
set_option(CURLOPT_CAINFO, g_default_certificate_path.characters());
if (!g_default_certificate_path.is_empty())
set_option(CURLOPT_CAINFO, g_default_certificate_path.characters());
set_option(CURLOPT_ACCEPT_ENCODING, "gzip, deflate, br");
set_option(CURLOPT_URL, url.to_string().value().to_byte_string().characters());
set_option(CURLOPT_PORT, url.port_or_default());
set_option(CURLOPT_CONNECTTIMEOUT, s_connect_timeout_seconds);
set_option(CURLOPT_ACCEPT_ENCODING, "gzip, deflate, br");
set_option(CURLOPT_URL, url.to_string().value().to_byte_string().characters());
set_option(CURLOPT_PORT, url.port_or_default());
set_option(CURLOPT_CONNECTTIMEOUT, s_connect_timeout_seconds);
bool did_set_body = false;
bool did_set_body = false;
if (method == "GET"sv) {
set_option(CURLOPT_HTTPGET, 1L);
} else if (method.is_one_of("POST"sv, "PUT"sv, "PATCH"sv, "DELETE"sv)) {
request->body = request_body;
set_option(CURLOPT_POSTFIELDSIZE, request->body.size());
set_option(CURLOPT_POSTFIELDS, request->body.data());
did_set_body = true;
} else if (method == "HEAD") {
set_option(CURLOPT_NOBODY, 1L);
}
set_option(CURLOPT_CUSTOMREQUEST, method.characters());
if (method == "GET"sv) {
set_option(CURLOPT_HTTPGET, 1L);
} else if (method.is_one_of("POST"sv, "PUT"sv, "PATCH"sv, "DELETE"sv)) {
request->body = request_body;
set_option(CURLOPT_POSTFIELDSIZE, request->body.size());
set_option(CURLOPT_POSTFIELDS, request->body.data());
did_set_body = true;
} else if (method == "HEAD") {
set_option(CURLOPT_NOBODY, 1L);
}
set_option(CURLOPT_CUSTOMREQUEST, method.characters());
set_option(CURLOPT_FOLLOWLOCATION, 0);
set_option(CURLOPT_FOLLOWLOCATION, 0);
struct curl_slist* curl_headers = nullptr;
struct curl_slist* curl_headers = nullptr;
// NOTE: CURLOPT_POSTFIELDS automatically sets the Content-Type header.
// Set it to empty if the headers passed in don't contain a content type.
if (did_set_body && !request_headers.contains("Content-Type"))
curl_headers = curl_slist_append(curl_headers, "Content-Type:");
// NOTE: CURLOPT_POSTFIELDS automatically sets the Content-Type header.
// Set it to empty if the headers passed in don't contain a content type.
if (did_set_body && !request_headers.contains("Content-Type"))
curl_headers = curl_slist_append(curl_headers, "Content-Type:");
for (auto const& header : request_headers.headers()) {
auto header_string = ByteString::formatted("{}: {}", header.name, header.value);
curl_headers = curl_slist_append(curl_headers, header_string.characters());
}
set_option(CURLOPT_HTTPHEADER, curl_headers);
for (auto const& header : request_headers.headers()) {
auto header_string = ByteString::formatted("{}: {}", header.name, header.value);
curl_headers = curl_slist_append(curl_headers, header_string.characters());
}
set_option(CURLOPT_HTTPHEADER, curl_headers);
// FIXME: Set up proxy if applicable
(void)proxy_data;
// FIXME: Set up proxy if applicable
(void)proxy_data;
set_option(CURLOPT_WRITEFUNCTION, &on_data_received);
set_option(CURLOPT_WRITEDATA, reinterpret_cast<void*>(request.ptr()));
set_option(CURLOPT_WRITEFUNCTION, &on_data_received);
set_option(CURLOPT_WRITEDATA, reinterpret_cast<void*>(request.ptr()));
set_option(CURLOPT_HEADERFUNCTION, &on_header_received);
set_option(CURLOPT_HEADERDATA, reinterpret_cast<void*>(request.ptr()));
set_option(CURLOPT_HEADERFUNCTION, &on_header_received);
set_option(CURLOPT_HEADERDATA, reinterpret_cast<void*>(request.ptr()));
StringBuilder resolve_opt_builder;
resolve_opt_builder.appendff("{}:{}:", host, url.port_or_default());
auto first = true;
for (auto& addr : dns_result->cached_addresses()) {
auto formatted_address = addr.visit(
[&](IPv4Address const& ipv4) { return ipv4.to_byte_string(); },
[&](IPv6Address const& ipv6) { return MUST(ipv6.to_string()).to_byte_string(); });
if (!first)
resolve_opt_builder.append(',');
first = false;
resolve_opt_builder.append(formatted_address);
}
StringBuilder resolve_opt_builder;
resolve_opt_builder.appendff("{}:{}:", host, url.port_or_default());
auto first = true;
for (auto& addr : dns_result->cached_addresses()) {
auto formatted_address = addr.visit(
[&](IPv4Address const& ipv4) { return ipv4.to_byte_string(); },
[&](IPv6Address const& ipv6) { return MUST(ipv6.to_string()).to_byte_string(); });
if (!first)
resolve_opt_builder.append(',');
first = false;
resolve_opt_builder.append(formatted_address);
}
auto formatted_address = resolve_opt_builder.to_byte_string();
g_dns_cache.set(host, formatted_address);
curl_slist* resolve_list = curl_slist_append(nullptr, formatted_address.characters());
curl_easy_setopt(easy, CURLOPT_RESOLVE, resolve_list);
auto formatted_address = resolve_opt_builder.to_byte_string();
g_dns_cache.set(host, formatted_address);
curl_slist* resolve_list = curl_slist_append(nullptr, formatted_address.characters());
curl_easy_setopt(easy, CURLOPT_RESOLVE, resolve_list);
auto result = curl_multi_add_handle(m_curl_multi, easy);
VERIFY(result == CURLM_OK);
auto result = curl_multi_add_handle(m_curl_multi, easy);
VERIFY(result == CURLM_OK);
m_active_requests.set(request_id, move(request));
m_active_requests.set(request_id, move(request));
});
}
static Requests::NetworkError map_curl_code_to_network_error(CURLcode const& code)