Added HTTPClient type.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
|
|
||||||
#include "./http.hpp"
|
#include "./http.hpp"
|
||||||
|
|
||||||
|
#include <format>
|
||||||
|
|
||||||
#include "../util/iterators.hpp"
|
#include "../util/iterators.hpp"
|
||||||
#include "../util/string.hpp"
|
#include "../util/string.hpp"
|
||||||
|
|
||||||
@@ -69,23 +71,14 @@ Task<StreamError> HTTPStream::c_writeRequest(const mijin::HTTPRequest& request)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MIJIN_HTTP_WRITE(request.method);
|
MIJIN_HTTP_WRITE(std::format("{} {} HTTP/{}.{}\n", request.method, request.address, request.version.major, request.version.minor));
|
||||||
MIJIN_HTTP_WRITE(" ");
|
|
||||||
MIJIN_HTTP_WRITE(request.address);
|
|
||||||
MIJIN_HTTP_WRITE(" HTTP/1.0\n");
|
|
||||||
for (const auto& [key, value] : moreHeaders)
|
for (const auto& [key, value] : moreHeaders)
|
||||||
{
|
{
|
||||||
MIJIN_HTTP_WRITE(key);
|
MIJIN_HTTP_WRITE(std::format("{}: {}\n", key, value));
|
||||||
MIJIN_HTTP_WRITE(": ");
|
|
||||||
MIJIN_HTTP_WRITE(value);
|
|
||||||
MIJIN_HTTP_WRITE("\n");
|
|
||||||
}
|
}
|
||||||
for (const auto& [key, value] : request.headers)
|
for (const auto& [key, value] : request.headers)
|
||||||
{
|
{
|
||||||
MIJIN_HTTP_WRITE(key);
|
MIJIN_HTTP_WRITE(std::format("{}: {}\n", key, value));
|
||||||
MIJIN_HTTP_WRITE(": ");
|
|
||||||
MIJIN_HTTP_WRITE(value);
|
|
||||||
MIJIN_HTTP_WRITE("\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MIJIN_HTTP_WRITE("\n");
|
MIJIN_HTTP_WRITE("\n");
|
||||||
@@ -163,7 +156,106 @@ Task<StreamResult<HTTPResponse>> HTTPStream::c_readResponse() noexcept
|
|||||||
|
|
||||||
co_return response;
|
co_return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
co_return error;
|
||||||
|
}
|
||||||
|
if (!request.headers.contains("connection"))
|
||||||
|
{
|
||||||
|
request.headers.emplace("connection", "keep-alive");
|
||||||
|
}
|
||||||
|
StreamResult<HTTPResponse> response = co_await stream_->c_request(request);
|
||||||
|
if (response.isError())
|
||||||
|
{
|
||||||
|
disconnect();
|
||||||
|
if (const StreamError error = createSocket(address, port, https); error != StreamError::SUCCESS)
|
||||||
|
{
|
||||||
|
co_return error;
|
||||||
|
}
|
||||||
|
response = co_await stream_->c_request(request);
|
||||||
|
}
|
||||||
|
co_return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
Task<StreamResult<HTTPResponse>> HTTPClient::c_request(const URL& url, HTTPRequest request) noexcept
|
||||||
|
{
|
||||||
|
if (url.getHost().empty())
|
||||||
|
{
|
||||||
|
co_return StreamError::UNKNOWN_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint16_t port = url.getPort();
|
||||||
|
bool https = false;
|
||||||
|
if (equalsIgnoreCase(url.getScheme(), "http"))
|
||||||
|
{
|
||||||
|
port = (port != 0) ? port : 80;
|
||||||
|
}
|
||||||
|
else if (equalsIgnoreCase(url.getScheme(), "https"))
|
||||||
|
{
|
||||||
|
port = (port != 0) ? port : 443;
|
||||||
|
https = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
co_return StreamError::UNKNOWN_ERROR;
|
||||||
|
}
|
||||||
|
Optional<ip_address_t> ipAddress = ipAddressFromString(url.getHost());
|
||||||
|
// TODO: lookup host
|
||||||
|
if (ipAddress.empty())
|
||||||
|
{
|
||||||
|
co_return StreamError::UNKNOWN_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!request.headers.contains("host"))
|
||||||
|
{
|
||||||
|
request.headers.emplace("host", url.getHost());
|
||||||
|
}
|
||||||
|
request.address = url.getPathQueryFragment();
|
||||||
|
if (request.address.empty())
|
||||||
|
{
|
||||||
|
request.address = "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
co_return co_await c_request(*ipAddress, port, https, std::move(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
void HTTPClient::disconnect() noexcept
|
||||||
|
{
|
||||||
|
if (socket_ == nullptr)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stream_.destroy();
|
||||||
|
socket_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
StreamError HTTPClient::createSocket(ip_address_t address, std::uint16_t port, bool https) noexcept
|
||||||
|
{
|
||||||
|
if (socket_ != nullptr && address == lastIP_ && port == lastPort_ && https == lastWasHttps_)
|
||||||
|
{
|
||||||
|
return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
lastIP_ = address;
|
||||||
|
lastPort_ = port;
|
||||||
|
lastWasHttps_ = https;
|
||||||
|
socket_ = std::move(newSocket);
|
||||||
|
stream_.construct(socket_->getStream());
|
||||||
|
return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef MIJIN_HTTP_WRITE
|
#undef MIJIN_HTTP_WRITE
|
||||||
#undef MIJIN_HTTP_READLINE
|
#undef MIJIN_HTTP_CHECKREAD
|
||||||
|
#undef MIJIN_HTTP_READLINE
|
||||||
@@ -7,6 +7,9 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include "./socket.hpp"
|
||||||
|
#include "./url.hpp"
|
||||||
|
#include "../container/boxed_object.hpp"
|
||||||
#include "../io/stream.hpp"
|
#include "../io/stream.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
@@ -19,12 +22,11 @@ struct HTTPVersion
|
|||||||
|
|
||||||
struct HTTPRequest
|
struct HTTPRequest
|
||||||
{
|
{
|
||||||
|
HTTPVersion version = {1, 1};
|
||||||
std::string address;
|
std::string address;
|
||||||
std::string method = "GET";
|
std::string method = "GET";
|
||||||
std::multimap<std::string, std::string> headers;
|
std::multimap<std::string, std::string> headers;
|
||||||
std::string body;
|
std::string body;
|
||||||
|
|
||||||
explicit HTTPRequest(std::string address_) noexcept : address(std::move(address_)) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct HTTPResponse
|
struct HTTPResponse
|
||||||
@@ -50,6 +52,23 @@ private:
|
|||||||
Task<StreamError> c_writeRequest(const HTTPRequest& request) noexcept;
|
Task<StreamError> c_writeRequest(const HTTPRequest& request) noexcept;
|
||||||
Task<StreamResult<HTTPResponse>> c_readResponse() noexcept;
|
Task<StreamResult<HTTPResponse>> c_readResponse() noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class HTTPClient
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Socket> socket_;
|
||||||
|
mijin::BoxedObject<HTTPStream> stream_;
|
||||||
|
ip_address_t lastIP_;
|
||||||
|
std::uint16_t lastPort_ = 0;
|
||||||
|
bool lastWasHttps_ = false;
|
||||||
|
public:
|
||||||
|
~HTTPClient() noexcept { disconnect(); }
|
||||||
|
Task<StreamResult<HTTPResponse>> c_request(ip_address_t address, std::uint16_t port, bool https, HTTPRequest request) noexcept;
|
||||||
|
Task<StreamResult<HTTPResponse>> c_request(const URL& url, HTTPRequest request = {}) noexcept;
|
||||||
|
void disconnect() noexcept;
|
||||||
|
private:
|
||||||
|
StreamError createSocket(ip_address_t address, std::uint16_t port, bool https) noexcept;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !defined(MIJIN_NET_HTTP_HPP_INCLUDED)
|
#endif // !defined(MIJIN_NET_HTTP_HPP_INCLUDED)
|
||||||
|
|||||||
@@ -265,16 +265,25 @@ mijin::Task<StreamError> TCPStream::c_readRaw(std::span<std::uint8_t> buffer, co
|
|||||||
MIJIN_ASSERT(isOpen(), "Socket is not open.");
|
MIJIN_ASSERT(isOpen(), "Socket is not open.");
|
||||||
setAsync(true);
|
setAsync(true);
|
||||||
|
|
||||||
|
if (buffer.empty())
|
||||||
|
{
|
||||||
|
co_return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
const long bytesRead = osRecv(handle_, buffer, readFlags(options));
|
const long bytesRead = osRecv(handle_, buffer, readFlags(options));
|
||||||
if (bytesRead >= 0)
|
if (bytesRead > 0)
|
||||||
{
|
{
|
||||||
if (outBytesRead != nullptr) {
|
if (outBytesRead != nullptr) {
|
||||||
*outBytesRead = static_cast<std::size_t>(bytesRead);
|
*outBytesRead = static_cast<std::size_t>(bytesRead);
|
||||||
}
|
}
|
||||||
co_return StreamError::SUCCESS;
|
co_return StreamError::SUCCESS;
|
||||||
}
|
}
|
||||||
|
else if (bytesRead == 0)
|
||||||
|
{
|
||||||
|
co_return StreamError::CONNECTION_CLOSED;
|
||||||
|
}
|
||||||
else if (errno != EAGAIN)
|
else if (errno != EAGAIN)
|
||||||
{
|
{
|
||||||
co_return translateErrno();
|
co_return translateErrno();
|
||||||
@@ -286,14 +295,25 @@ mijin::Task<StreamError> TCPStream::c_readRaw(std::span<std::uint8_t> buffer, co
|
|||||||
mijin::Task<StreamError> TCPStream::c_writeRaw(std::span<const std::uint8_t> buffer)
|
mijin::Task<StreamError> TCPStream::c_writeRaw(std::span<const std::uint8_t> buffer)
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(isOpen(), "Socket is not open.");
|
MIJIN_ASSERT(isOpen(), "Socket is not open.");
|
||||||
|
|
||||||
|
if (buffer.empty())
|
||||||
|
{
|
||||||
|
co_return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
setAsync(true);
|
setAsync(true);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (osSend(handle_, buffer, 0) >= 0)
|
const long bytesSent = osSend(handle_, buffer, 0);
|
||||||
|
if (bytesSent == static_cast<long>(buffer.size()))
|
||||||
{
|
{
|
||||||
co_return StreamError::SUCCESS;
|
co_return StreamError::SUCCESS;
|
||||||
}
|
}
|
||||||
|
else if (bytesSent == 0)
|
||||||
|
{
|
||||||
|
co_return StreamError::CONNECTION_CLOSED;
|
||||||
|
}
|
||||||
else if (errno != EAGAIN)
|
else if (errno != EAGAIN)
|
||||||
{
|
{
|
||||||
co_return translateErrno();
|
co_return translateErrno();
|
||||||
|
|||||||
Reference in New Issue
Block a user