More SSL stuff (still doesn't work :/).

This commit is contained in:
2024-08-27 19:52:08 +02:00
parent 0be34a845a
commit a43f92fb58
7 changed files with 156 additions and 28 deletions

View File

@@ -6,6 +6,10 @@
#include "../util/iterators.hpp"
#include "../util/string.hpp"
#if defined(MIJIN_ENABLE_OPENSSL)
#include "./ssl.hpp"
#endif
#define MIJIN_HTTP_WRITE(text) \
do \
{ \
@@ -160,7 +164,12 @@ Task<StreamResult<HTTPResponse>> HTTPStream::c_readResponse() noexcept
Task<StreamResult<HTTPResponse>> HTTPClient::c_request(ip_address_t address, std::uint16_t port, bool https,
HTTPRequest request) noexcept
{
if (const StreamError error = createSocket(address, port, https); error != StreamError::SUCCESS)
std::string hostname;
if (auto it = request.headers.find("host"); it != request.headers.end())
{
hostname = it->second;
}
if (const StreamError error = createSocket(address, hostname, port, https); error != StreamError::SUCCESS)
{
co_return error;
}
@@ -172,7 +181,7 @@ Task<StreamResult<HTTPResponse>> HTTPClient::c_request(ip_address_t address, std
if (response.isError())
{
disconnect();
if (const StreamError error = createSocket(address, port, https); error != StreamError::SUCCESS)
if (const StreamError error = createSocket(address, hostname, port, https); error != StreamError::SUCCESS)
{
co_return error;
}
@@ -242,7 +251,7 @@ void HTTPClient::disconnect() noexcept
socket_ = nullptr;
}
StreamError HTTPClient::createSocket(ip_address_t address, std::uint16_t port, bool https) noexcept
StreamError HTTPClient::createSocket(ip_address_t address, const std::string& hostname, std::uint16_t port, bool https) noexcept
{
if (socket_ != nullptr && address == lastIP_ && port == lastPort_ && https == lastWasHttps_)
{
@@ -250,17 +259,36 @@ StreamError HTTPClient::createSocket(ip_address_t address, std::uint16_t port, b
}
disconnect();
MIJIN_ASSERT(!https, "HTTPS not supported yet.");
std::unique_ptr<TCPSocket> newSocket = std::make_unique<TCPSocket>();
if (const StreamError error = newSocket->open(address, port); error != StreamError::SUCCESS)
{
return error;
}
socket_ = std::move(newSocket);
if (!https)
{
sslStream_.reset();
stream_.construct(socket_->getStream());
}
else
{
#if defined(MIJIN_ENABLE_OPENSSL)
std::unique_ptr<SSLStream> sslStream = std::make_unique<SSLStream>();
if (const StreamError error = sslStream->open(socket_->getStream(), hostname); error != StreamError::SUCCESS)
{
return error;
}
sslStream_ = std::move(sslStream);
stream_.construct(*sslStream_);
#else
return StreamError::NOT_SUPPORTED;
#endif
}
lastIP_ = address;
lastPort_ = port;
lastWasHttps_ = https;
socket_ = std::move(newSocket);
stream_.construct(socket_->getStream());
return StreamError::SUCCESS;
}
}