RequestServer: Move some cURL utilities to their own file

This will allow more easily using these from other files. This also lets
us hide the Windows.h header necessity in a single location, instead of
needing to remember to include it everywhre we would otherwise include
<curl/curl.h>.
This commit is contained in:
Timothy Flynn 2025-10-23 15:21:33 -04:00 committed by Andreas Kling
parent 7450da5556
commit 1216a2f952
6 changed files with 91 additions and 60 deletions

View file

@ -8,6 +8,7 @@ set(SOURCES
Cache/DiskCache.cpp
Cache/Utilities.cpp
ConnectionFromClient.cpp
CURL.cpp
Resolver.cpp
WebSocketImplCurl.cpp
)

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Enumerate.h>
#include <RequestServer/CURL.h>
namespace RequestServer {
ByteString build_curl_resolve_list(DNS::LookupResult const& dns_result, StringView host, u16 port)
{
StringBuilder resolve_opt_builder;
resolve_opt_builder.appendff("{}:{}:", host, port);
for (auto const& [i, addr] : enumerate(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 (i > 0)
resolve_opt_builder.append(',');
resolve_opt_builder.append(formatted_address);
}
dbgln_if(REQUESTSERVER_DEBUG, "RequestServer: Resolve list: {}", resolve_opt_builder.string_view());
return resolve_opt_builder.to_byte_string();
}
Requests::NetworkError curl_code_to_network_error(int code)
{
switch (code) {
case CURLE_COULDNT_RESOLVE_HOST:
return Requests::NetworkError::UnableToResolveHost;
case CURLE_COULDNT_RESOLVE_PROXY:
return Requests::NetworkError::UnableToResolveProxy;
case CURLE_COULDNT_CONNECT:
return Requests::NetworkError::UnableToConnect;
case CURLE_OPERATION_TIMEDOUT:
return Requests::NetworkError::TimeoutReached;
case CURLE_TOO_MANY_REDIRECTS:
return Requests::NetworkError::TooManyRedirects;
case CURLE_SSL_CONNECT_ERROR:
return Requests::NetworkError::SSLHandshakeFailed;
case CURLE_PEER_FAILED_VERIFICATION:
return Requests::NetworkError::SSLVerificationFailed;
case CURLE_URL_MALFORMAT:
return Requests::NetworkError::MalformedUrl;
case CURLE_BAD_CONTENT_ENCODING:
return Requests::NetworkError::InvalidContentEncoding;
default:
return Requests::NetworkError::Unknown;
}
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
// Include this header instead of <curl/curl.h>. Only include this header from .cpp files.
#include <AK/ByteString.h>
#include <AK/Platform.h>
#include <AK/StringView.h>
#include <LibDNS/Resolver.h>
#include <LibRequests/NetworkError.h>
#if defined(AK_OS_WINDOWS)
# include <AK/Windows.h> // Needed because curl.h includes winsock2.h
#endif
#include <curl/curl.h>
namespace RequestServer {
ByteString build_curl_resolve_list(DNS::LookupResult const& dns_result, StringView host, u16 port);
Requests::NetworkError curl_code_to_network_error(int code);
}

View file

@ -17,18 +17,12 @@
#include <LibTextCodec/Decoder.h>
#include <LibWebSocket/ConnectionInfo.h>
#include <LibWebSocket/Message.h>
#include <RequestServer/CURL.h>
#include <RequestServer/Cache/DiskCache.h>
#include <RequestServer/ConnectionFromClient.h>
#include <RequestServer/Resolver.h>
#include <RequestServer/WebSocketImplCurl.h>
#ifdef AK_OS_WINDOWS
// needed because curl.h includes winsock2.h
# include <AK/Windows.h>
#endif
#include <curl/curl.h>
namespace RequestServer {
static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;
@ -38,26 +32,6 @@ static long s_connect_timeout_seconds = 90L;
Optional<DiskCache> g_disk_cache;
ByteString build_curl_resolve_list(DNS::LookupResult const& dns_result, StringView host, u16 port)
{
StringBuilder resolve_opt_builder;
resolve_opt_builder.appendff("{}:{}:", host, port);
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);
}
dbgln_if(REQUESTSERVER_DEBUG, "RequestServer: Resolve list: {}", resolve_opt_builder.string_view());
return resolve_opt_builder.to_byte_string();
}
struct ConnectionFromClient::ActiveRequest : public Weakable<ActiveRequest> {
CURLM* multi { nullptr };
CURL* easy { nullptr };
@ -674,32 +648,6 @@ void ConnectionFromClient::issue_network_request(i32 request_id, ByteString meth
}
#endif
static Requests::NetworkError map_curl_code_to_network_error(CURLcode const& code)
{
switch (code) {
case CURLE_COULDNT_RESOLVE_HOST:
return Requests::NetworkError::UnableToResolveHost;
case CURLE_COULDNT_RESOLVE_PROXY:
return Requests::NetworkError::UnableToResolveProxy;
case CURLE_COULDNT_CONNECT:
return Requests::NetworkError::UnableToConnect;
case CURLE_OPERATION_TIMEDOUT:
return Requests::NetworkError::TimeoutReached;
case CURLE_TOO_MANY_REDIRECTS:
return Requests::NetworkError::TooManyRedirects;
case CURLE_SSL_CONNECT_ERROR:
return Requests::NetworkError::SSLHandshakeFailed;
case CURLE_PEER_FAILED_VERIFICATION:
return Requests::NetworkError::SSLVerificationFailed;
case CURLE_URL_MALFORMAT:
return Requests::NetworkError::MalformedUrl;
case CURLE_BAD_CONTENT_ENCODING:
return Requests::NetworkError::InvalidContentEncoding;
default:
return Requests::NetworkError::Unknown;
}
}
static Requests::RequestTimingInfo get_timing_info_from_curl_easy_handle(CURL* easy_handle)
{
/*

View file

@ -75,8 +75,6 @@ private:
ByteString m_alt_svc_cache_path;
};
// FIXME: Find a good home for this
ByteString build_curl_resolve_list(DNS::LookupResult const&, StringView host, u16 port);
constexpr inline uintptr_t websocket_private_tag = 0x1;
}

View file

@ -4,13 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ConnectionFromClient.h"
#include <AK/Windows.h>
#include <RequestServer/CURL.h>
#include <RequestServer/ConnectionFromClient.h>
#include <RequestServer/WebSocketImplCurl.h>
#include <curl/curl.h>
namespace RequestServer {
NonnullRefPtr<WebSocketImplCurl> WebSocketImplCurl::create(CURLM* multi_handle)