Added HTTPClient type.

This commit is contained in:
2024-08-20 00:28:12 +02:00
parent 99f5987f4b
commit 03f255a7d0
3 changed files with 148 additions and 17 deletions

View File

@@ -7,6 +7,9 @@
#include <memory>
#include <string>
#include <map>
#include "./socket.hpp"
#include "./url.hpp"
#include "../container/boxed_object.hpp"
#include "../io/stream.hpp"
namespace mijin
@@ -19,12 +22,11 @@ struct HTTPVersion
struct HTTPRequest
{
HTTPVersion version = {1, 1};
std::string address;
std::string method = "GET";
std::multimap<std::string, std::string> headers;
std::string body;
explicit HTTPRequest(std::string address_) noexcept : address(std::move(address_)) {}
};
struct HTTPResponse
@@ -50,6 +52,23 @@ private:
Task<StreamError> c_writeRequest(const HTTPRequest& request) 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)