|
| 1 | +export module CppUtils.Network.Http.Client; |
| 2 | + |
| 3 | +import std; |
| 4 | +import CppUtils.Network.Socket; |
| 5 | +import CppUtils.Network.Client; |
| 6 | +import CppUtils.Network.DNS; |
| 7 | +import CppUtils.Network.Http.Common; |
| 8 | + |
| 9 | +export namespace CppUtils::Network::Http |
| 10 | +{ |
| 11 | + class Client final |
| 12 | + { |
| 13 | + public: |
| 14 | + inline auto setHostname(std::string hostname) -> void |
| 15 | + { |
| 16 | + m_hostname = std::move(hostname); |
| 17 | + } |
| 18 | + |
| 19 | + [[nodiscard]] inline auto connect(std::string_view hostname, std::uint16_t port = 80) -> std::expected<void, std::error_code> |
| 20 | + { |
| 21 | + setHostname(std::string{hostname}); |
| 22 | + m_port = port; |
| 23 | + const auto ipAddresses = DNS::resolve(hostname); |
| 24 | + if (std::empty(ipAddresses)) |
| 25 | + return std::unexpected{std::make_error_code(std::errc::host_unreachable)}; |
| 26 | + return connect(ipAddresses, port); |
| 27 | + } |
| 28 | + |
| 29 | + [[nodiscard]] inline auto connect(const std::vector<std::string>& ipAddresses, std::uint16_t port) -> std::expected<void, std::error_code> |
| 30 | + { |
| 31 | + auto lastError = std::error_code{}; |
| 32 | + for (const auto& ip : ipAddresses) |
| 33 | + if (auto result = connectIP(ip, port); result.has_value()) |
| 34 | + return {}; |
| 35 | + else |
| 36 | + lastError = result.error(); |
| 37 | + return std::unexpected{lastError}; |
| 38 | + } |
| 39 | + |
| 40 | + [[nodiscard]] inline auto connectIP(std::string_view ip, std::uint16_t port) -> std::expected<void, std::error_code> |
| 41 | + { |
| 42 | + const auto domain = ip.find(':') != std::string_view::npos ? Socket::Domain::IPV6 : Socket::Domain::IPV4; |
| 43 | + m_connection.emplace(domain, Socket::Type::TCP); |
| 44 | + if (auto result = m_connection->connect(ip, port); not result) |
| 45 | + { |
| 46 | + m_connection.reset(); |
| 47 | + return std::unexpected{result.error()}; |
| 48 | + } |
| 49 | + return {}; |
| 50 | + } |
| 51 | + |
| 52 | + [[nodiscard]] inline auto send(Request request) -> std::expected<Response, std::error_code> |
| 53 | + { |
| 54 | + if (not m_connection.has_value()) |
| 55 | + if (auto result = connect(m_hostname, m_port); not result) |
| 56 | + return std::unexpected{result.error()}; |
| 57 | + |
| 58 | + if (std::ranges::none_of(request.headers, [](const auto& header) { return header.name == "Host"; })) |
| 59 | + request.headers.push_back(Header{"Host", m_hostname}); |
| 60 | + |
| 61 | + if (not m_connection.value().send(static_cast<std::string>(request))) |
| 62 | + { |
| 63 | + if (auto reconnectResult = connect(m_hostname, m_port); not reconnectResult) |
| 64 | + return std::unexpected{reconnectResult.error()}; |
| 65 | + |
| 66 | + if (auto retryResult = m_connection.value().send(static_cast<std::string>(request)); not retryResult) |
| 67 | + return std::unexpected{retryResult.error()}; |
| 68 | + } |
| 69 | + return readResponse(); |
| 70 | + } |
| 71 | + |
| 72 | + [[nodiscard]] inline auto get(std::string_view path) -> std::expected<Response, std::error_code> |
| 73 | + { |
| 74 | + auto request = Request{}; |
| 75 | + request.method = Method::Get; |
| 76 | + request.path = std::string{path}; |
| 77 | + return send(std::move(request)); |
| 78 | + } |
| 79 | + |
| 80 | + private: |
| 81 | + [[nodiscard]] inline auto readResponse() -> std::expected<Response, std::error_code> |
| 82 | + { |
| 83 | + return readMessage<Response>( |
| 84 | + [this](std::size_t size) { return m_connection.value().receive<std::string>(size); }, |
| 85 | + [this](std::string_view buffer) { return parseHeaders(buffer); }); |
| 86 | + } |
| 87 | + |
| 88 | + [[nodiscard]] inline auto parseHeaders(std::string_view serializedHeaders) -> Response |
| 89 | + { |
| 90 | + return parseMessage(serializedHeaders, [](std::string_view statusLine) { |
| 91 | + auto response = Response{}; |
| 92 | + if (auto firstSpace = statusLine.find(' '); firstSpace != std::string_view::npos) |
| 93 | + { |
| 94 | + if (auto secondSpace = statusLine.find(' ', firstSpace + 1); secondSpace != std::string_view::npos) |
| 95 | + { |
| 96 | + auto codeString = statusLine.substr(firstSpace + 1, secondSpace - firstSpace - 1); |
| 97 | + auto code = 0uz; |
| 98 | + std::from_chars(std::data(codeString), std::data(codeString) + std::size(codeString), code); |
| 99 | + response.status = static_cast<Status>(code); |
| 100 | + } |
| 101 | + } |
| 102 | + return response; |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + std::string m_hostname; |
| 107 | + std::uint16_t m_port = 80; |
| 108 | + std::optional<CppUtils::Network::Client> m_connection; |
| 109 | + }; |
| 110 | +} |
0 commit comments