Skip to content

Commit

Permalink
C4HTTPClient::Uri: Refactor parsing so that all clonk-style URIs work
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed Nov 11, 2023
1 parent 8c8bef2 commit 83d8e7c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
57 changes: 41 additions & 16 deletions src/C4HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,20 @@ void C4HTTPClient::Uri::CURLUDeleter::operator()(CURLU * const uri)
C4HTTPClient::Uri::Uri(const std::string &serverAddress, const std::uint16_t port)
: uri{ThrowIfFailed(curl_url(), "curl_url failed")}
{
CURLUcode urlError{curl_url_set(uri.get(), CURLUPART_URL, serverAddress.c_str(), 0)};
if (urlError == CURLUE_UNSUPPORTED_SCHEME)
ThrowIfFailed(curl_url_set(uri.get(), CURLUPART_URL, serverAddress.c_str(), 0) == CURLUE_OK, "malformed URL");

if (port > 0)
{
urlError = curl_url_set(uri.get(), CURLUPART_URL, std::format("http://{}", serverAddress).c_str(), 0);
SetPort(port);
}
}

ThrowIfFailed(urlError == CURLUE_OK, "malformed URL");

C4HTTPClient::Uri::Uri(std::unique_ptr<CURLU, CURLUDeleter> uri, const std::uint16_t port)
: uri{std::move(uri)}
{
if (port > 0)
{
char *portString;
const auto errGetPort = curl_url_get(uri.get(), CURLUPART_PORT, &portString, 0);
curl_free(portString);
if (errGetPort == CURLUE_NO_PORT)
{
ThrowIfFailed(curl_url_set(uri.get(), CURLUPART_PORT, std::to_string(port).c_str(), 0) == CURLUE_OK, "curl_url_set PORT failed");
}
else
{
ThrowIfFailed(errGetPort == CURLUE_OK, "curl_url_get PORT failed");
}
SetPort(port);
}
}

Expand All @@ -101,6 +94,38 @@ std::string C4HTTPClient::Uri::GetUriAsString() const
return string;
}

C4HTTPClient::Uri C4HTTPClient::Uri::ParseOldStyle(const std::string &serverAddress, const std::uint16_t port)
{
std::unique_ptr<CURLU, CURLUDeleter> uri{ThrowIfFailed(curl_url(), "curl_url failed")};

CURLUcode result{curl_url_set(uri.get(), CURLUPART_URL, serverAddress.c_str(), 0)};

if (result == CURLUE_BAD_SCHEME || result == CURLUE_UNSUPPORTED_SCHEME)
{
result = curl_url_set(uri.get(), CURLUPART_URL, std::format("http://{}", serverAddress).c_str(), 0);
}

ThrowIfFailed(result == CURLUE_OK, "malformed URL");

return {std::move(uri), port};
}

void C4HTTPClient::Uri::SetPort(const std::uint16_t port)
{
char *portString;
const auto errGetPort = curl_url_get(uri.get(), CURLUPART_PORT, &portString, 0);
curl_free(portString);

if (errGetPort == CURLUE_NO_PORT)
{
ThrowIfFailed(curl_url_set(uri.get(), CURLUPART_PORT, std::to_string(port).c_str(), 0) == CURLUE_OK, "curl_url_set PORT failed");
}
else
{
ThrowIfFailed(errGetPort == CURLUE_OK, "curl_url_get PORT failed");
}
}

C4HTTPClient::C4HTTPClient()
: shareHandle{ThrowIfFailed(curl_share_init(), "curl_share_init failed")}
{
Expand Down
9 changes: 9 additions & 0 deletions src/C4HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ class C4HTTPClient
public:
Uri(const std::string &serverAddress, std::uint16_t port = 0);

private:
Uri(std::unique_ptr<CURLU, CURLUDeleter> uri, std::uint16_t port = 0);

public:
std::string GetServerAddress() const;
std::string GetUriAsString() const;

auto get() const { return uri.get(); }

public:
[[nodiscard]] static Uri ParseOldStyle(const std::string &serverAddress, std::uint16_t port = 0);

private:
void SetPort(std::uint16_t port);

private:
std::unique_ptr<CURLU, CURLUDeleter> uri;
};
Expand Down
2 changes: 1 addition & 1 deletion src/C4Network2Reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ void C4Network2HTTPClient::Clear()

bool C4Network2HTTPClient::SetServer(const std::string_view serverAddress, const std::uint16_t defaultPort) try
{
const C4HTTPClient::Uri uri{std::string{serverAddress}, defaultPort};
const auto uri = C4HTTPClient::Uri::ParseOldStyle(std::string{serverAddress}, defaultPort);
url = uri.GetUriAsString();
serverName = uri.GetServerAddress();
return true;
Expand Down

0 comments on commit 83d8e7c

Please sign in to comment.