aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp
diff options
context:
space:
mode:
authorLiam Mitchell <[email protected]>2025-08-21 23:58:51 +0000
committerLiam Mitchell <[email protected]>2025-08-21 23:58:51 +0000
commit33209bd6931f49362dfc2d62c6cb6b87a42c99e1 (patch)
treecfc7914634088b3f4feac2d4cec0b5650dfdcc3c /src/zenhttp
parentFix changelog merge issues (diff)
parentavoid new in static IoBuffer (#472) (diff)
downloadzen-33209bd6931f49362dfc2d62c6cb6b87a42c99e1.tar.xz
zen-33209bd6931f49362dfc2d62c6cb6b87a42c99e1.zip
Merge remote-tracking branch 'origin/main' into de/zen-service-command
Diffstat (limited to 'src/zenhttp')
-rw-r--r--src/zenhttp/httpclient.cpp7
-rw-r--r--src/zenhttp/httpclientauth.cpp23
-rw-r--r--src/zenhttp/include/zenhttp/httpclient.h23
-rw-r--r--src/zenhttp/include/zenhttp/httpclientauth.h3
-rw-r--r--src/zenhttp/packageformat.cpp2
5 files changed, 48 insertions, 10 deletions
diff --git a/src/zenhttp/httpclient.cpp b/src/zenhttp/httpclient.cpp
index a2d323b5e..30a2bfc65 100644
--- a/src/zenhttp/httpclient.cpp
+++ b/src/zenhttp/httpclient.cpp
@@ -1628,12 +1628,13 @@ HttpClient::Response::ErrorMessage(std::string_view Prefix) const
}
else if (StatusCode != HttpResponseCode::ImATeapot && (int)StatusCode)
{
- return fmt::format("{}{}HTTP error {} {} ({})",
+ std::string TextResponse = ToText();
+ return fmt::format("{}{}HTTP error {} {}{}",
Prefix,
Prefix.empty() ? ""sv : ": "sv,
(int)StatusCode,
zen::ToString(StatusCode),
- ToText());
+ TextResponse.empty() ? ""sv : fmt::format(" ({})", TextResponse));
}
else
{
@@ -1646,7 +1647,7 @@ HttpClient::Response::ThrowError(std::string_view ErrorPrefix)
{
if (!IsSuccess())
{
- throw std::runtime_error(ErrorMessage(ErrorPrefix));
+ throw HttpClientError(ErrorMessage(ErrorPrefix), Error.has_value() ? Error.value().ErrorCode : 0, StatusCode);
}
}
diff --git a/src/zenhttp/httpclientauth.cpp b/src/zenhttp/httpclientauth.cpp
index 39efe1d0c..62e1b77bc 100644
--- a/src/zenhttp/httpclientauth.cpp
+++ b/src/zenhttp/httpclientauth.cpp
@@ -89,14 +89,25 @@ namespace zen { namespace httpclientauth {
static HttpClientAccessToken GetOidcTokenFromExe(const std::filesystem::path& OidcExecutablePath,
std::string_view CloudHost,
- bool Unattended)
+ bool Unattended,
+ bool Quiet)
{
Stopwatch Timer;
CreateProcOptions ProcOptions;
+ if (Quiet)
+ {
+ ProcOptions.StdoutFile = std::filesystem::temp_directory_path() / fmt::format(".zen-auth-output-{}", Oid::NewOid());
+ }
const std::filesystem::path AuthTokenPath(std::filesystem::temp_directory_path() / fmt::format(".zen-auth-{}", Oid::NewOid()));
- auto _ = MakeGuard([AuthTokenPath]() { RemoveFile(AuthTokenPath); });
+ auto _ = MakeGuard([AuthTokenPath, &ProcOptions]() {
+ RemoveFile(AuthTokenPath);
+ if (!ProcOptions.StdoutFile.empty())
+ {
+ RemoveFile(ProcOptions.StdoutFile);
+ }
+ });
const std::string ProcArgs = fmt::format("{} --AuthConfigUrl {} --OutFile {} --Unattended={}",
OidcExecutablePath,
@@ -164,13 +175,15 @@ namespace zen { namespace httpclientauth {
}
std::optional<std::function<HttpClientAccessToken()>> CreateFromOidcTokenExecutable(const std::filesystem::path& OidcExecutablePath,
- std::string_view CloudHost)
+ std::string_view CloudHost,
+ bool Quiet)
{
- HttpClientAccessToken InitialToken = GetOidcTokenFromExe(OidcExecutablePath, CloudHost, false);
+ HttpClientAccessToken InitialToken = GetOidcTokenFromExe(OidcExecutablePath, CloudHost, /* Unattended */ false, Quiet);
if (InitialToken.IsValid())
{
return [OidcExecutablePath = std::filesystem::path(OidcExecutablePath),
CloudHost = std::string(CloudHost),
+ Quiet,
InitialToken]() mutable {
if (InitialToken.IsValid())
{
@@ -178,7 +191,7 @@ namespace zen { namespace httpclientauth {
InitialToken = {};
return Result;
}
- return GetOidcTokenFromExe(OidcExecutablePath, CloudHost, true);
+ return GetOidcTokenFromExe(OidcExecutablePath, CloudHost, /* Unattended */ true, Quiet);
};
}
return {};
diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h
index c991a71ea..50bd5b53a 100644
--- a/src/zenhttp/include/zenhttp/httpclient.h
+++ b/src/zenhttp/include/zenhttp/httpclient.h
@@ -57,6 +57,29 @@ struct HttpClientSettings
uint8_t RetryCount = 0;
};
+class HttpClientError : public std::runtime_error
+{
+public:
+ using _Mybase = runtime_error;
+
+ HttpClientError(const std::string& Message, int Error, HttpResponseCode ResponseCode)
+ : _Mybase(Message)
+ , m_Error(Error)
+ , m_ResponseCode(ResponseCode)
+ {
+ }
+
+ HttpClientError(const char* Message, int Error, HttpResponseCode ResponseCode)
+ : _Mybase(Message)
+ , m_Error(Error)
+ , m_ResponseCode(ResponseCode)
+ {
+ }
+
+ const int m_Error = 0;
+ const HttpResponseCode m_ResponseCode = HttpResponseCode::ImATeapot;
+};
+
class HttpClient
{
public:
diff --git a/src/zenhttp/include/zenhttp/httpclientauth.h b/src/zenhttp/include/zenhttp/httpclientauth.h
index 5b9b9d305..32d00f87f 100644
--- a/src/zenhttp/include/zenhttp/httpclientauth.h
+++ b/src/zenhttp/include/zenhttp/httpclientauth.h
@@ -27,7 +27,8 @@ namespace httpclientauth {
std::function<HttpClientAccessToken()> CreateFromDefaultOpenIdProvider(AuthMgr& AuthManager);
std::optional<std::function<HttpClientAccessToken()>> CreateFromOidcTokenExecutable(const std::filesystem::path& OidcExecutablePath,
- std::string_view CloudHost);
+ std::string_view CloudHost,
+ bool Quiet);
} // namespace httpclientauth
} // namespace zen
diff --git a/src/zenhttp/packageformat.cpp b/src/zenhttp/packageformat.cpp
index 9d423ecbc..0b7848f79 100644
--- a/src/zenhttp/packageformat.cpp
+++ b/src/zenhttp/packageformat.cpp
@@ -576,7 +576,7 @@ ParsePackageMessage(IoBuffer Payload, std::function<IoBuffer(const IoHash&, uint
if (!MalformedAttachments.empty())
{
- StringBuilder<1024> SB;
+ ExtendableStringBuilder<1024> SB;
SB << (uint64_t)MalformedAttachments.size() << " malformed attachments in package message:\n";
for (const auto& It : MalformedAttachments)
{