aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zenhttp')
-rw-r--r--src/zenhttp/auth/authmgr.cpp4
-rw-r--r--src/zenhttp/httpclient.cpp52
-rw-r--r--src/zenhttp/include/zenhttp/httpclient.h8
-rw-r--r--src/zenhttp/servers/httpasio.cpp22
-rw-r--r--src/zenhttp/servers/httpparser.cpp11
-rw-r--r--src/zenhttp/servers/httpplugin.cpp14
-rw-r--r--src/zenhttp/servers/httpsys.cpp24
-rw-r--r--src/zenhttp/transports/asiotransport.cpp2
-rw-r--r--src/zenhttp/transports/winsocktransport.cpp2
9 files changed, 96 insertions, 43 deletions
diff --git a/src/zenhttp/auth/authmgr.cpp b/src/zenhttp/auth/authmgr.cpp
index 18568a21d..a520e8fd1 100644
--- a/src/zenhttp/auth/authmgr.cpp
+++ b/src/zenhttp/auth/authmgr.cpp
@@ -295,7 +295,7 @@ private:
}
}
}
- catch (std::exception& Err)
+ catch (const std::exception& Err)
{
ZEN_ERROR("(de)serialize state FAILED, reason '{}'", Err.what());
@@ -367,7 +367,7 @@ private:
ZEN_WARN("save auth state FAILED, reason '{}'", Reason.value());
}
}
- catch (std::exception& Err)
+ catch (const std::exception& Err)
{
ZEN_WARN("serialize state FAILED, reason '{}'", Err.what());
}
diff --git a/src/zenhttp/httpclient.cpp b/src/zenhttp/httpclient.cpp
index 9811e5814..262785a0a 100644
--- a/src/zenhttp/httpclient.cpp
+++ b/src/zenhttp/httpclient.cpp
@@ -51,16 +51,6 @@ AsCprBody(const IoBuffer& Obj)
return cpr::Body((const char*)Obj.GetData(), Obj.GetSize());
}
-static cpr::Body
-AsCprBody(const CompositeBuffer& Buffers)
-{
- SharedBuffer Buffer = Buffers.Flatten();
-
- // This is super inefficient, should be fixed
- std::string String{(const char*)Buffer.GetData(), Buffer.GetSize()};
- return cpr::Body{std::move(String)};
-}
-
//////////////////////////////////////////////////////////////////////////
static HttpClient::Response
@@ -221,10 +211,15 @@ struct HttpClient::Impl : public RefCounted
CprSession->SetReadCallback({});
return Result;
}
- inline cpr::Response Post()
+ inline cpr::Response Post(std::optional<cpr::ReadCallback>&& Read = {})
{
+ if (Read)
+ {
+ CprSession->SetReadCallback(std::move(Read.value()));
+ }
cpr::Response Result = CprSession->Post();
ZEN_TRACE("POST {}", Result);
+ CprSession->SetReadCallback({});
return Result;
}
inline cpr::Response Delete()
@@ -384,7 +379,7 @@ public:
m_FileHandle = nullptr;
}
}
- catch (std::exception& Ex)
+ catch (const std::exception& Ex)
{
ZEN_ERROR("Failed deleting temp file {}. Reason '{}'", m_FileHandle, Ex.what());
}
@@ -724,6 +719,12 @@ HttpClient::Post(std::string_view Url, const KeyValueMap& AdditionalHeader, cons
HttpClient::Response
HttpClient::Post(std::string_view Url, const IoBuffer& Payload, const KeyValueMap& AdditionalHeader)
{
+ return Post(Url, Payload, Payload.GetContentType(), AdditionalHeader);
+}
+
+HttpClient::Response
+HttpClient::Post(std::string_view Url, const IoBuffer& Payload, ZenContentType ContentType, const KeyValueMap& AdditionalHeader)
+{
ZEN_TRACE_CPU("HttpClient::PostWithPayload");
return CommonResponse(DoWithRetry(
@@ -732,7 +733,7 @@ HttpClient::Post(std::string_view Url, const IoBuffer& Payload, const KeyValueMa
m_Impl->AllocSession(m_BaseUri, Url, m_ConnectionSettings, AdditionalHeader, {}, m_SessionId, GetAccessToken());
Sess->SetBody(AsCprBody(Payload));
- Sess->UpdateHeader({HeaderContentType(Payload.GetContentType())});
+ Sess->UpdateHeader({HeaderContentType(ContentType)});
return Sess.Post();
},
m_ConnectionSettings.RetryCount));
@@ -758,17 +759,30 @@ HttpClient::Post(std::string_view Url, CbObject Payload, const KeyValueMap& Addi
HttpClient::Response
HttpClient::Post(std::string_view Url, CbPackage Pkg, const KeyValueMap& AdditionalHeader)
{
- ZEN_TRACE_CPU("HttpClient::PostPackage");
+ return Post(Url, zen::FormatPackageMessageBuffer(Pkg), ZenContentType::kCbPackage, AdditionalHeader);
+}
+
+HttpClient::Response
+HttpClient::Post(std::string_view Url, const CompositeBuffer& Payload, ZenContentType ContentType, const KeyValueMap& AdditionalHeader)
+{
+ ZEN_TRACE_CPU("HttpClient::Post");
return CommonResponse(DoWithRetry(
[&]() {
- CompositeBuffer Message = zen::FormatPackageMessageBuffer(Pkg);
-
+ uint64_t SizeLeft = Payload.GetSize();
+ CompositeBuffer::Iterator BufferIt = Payload.GetIterator(0);
+ auto ReadCallback = [&Payload, &BufferIt, &SizeLeft](char* buffer, size_t& size, intptr_t) {
+ size = Min<size_t>(size, SizeLeft);
+ MutableMemoryView Data(buffer, size);
+ Payload.CopyTo(Data, BufferIt);
+ SizeLeft -= size;
+ return true;
+ };
Impl::Session Sess =
m_Impl->AllocSession(m_BaseUri, Url, m_ConnectionSettings, AdditionalHeader, {}, m_SessionId, GetAccessToken());
- Sess->SetBody(AsCprBody(Message));
- Sess->UpdateHeader({HeaderContentType(ZenContentType::kCbPackage)});
- return Sess.Post();
+ Sess->UpdateHeader({HeaderContentType(ContentType)});
+
+ return Sess.Post(cpr::ReadCallback(gsl::narrow<cpr::cpr_off_t>(Payload.GetSize()), ReadCallback));
},
m_ConnectionSettings.RetryCount));
}
diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h
index f3559f214..8318e3679 100644
--- a/src/zenhttp/include/zenhttp/httpclient.h
+++ b/src/zenhttp/include/zenhttp/httpclient.h
@@ -149,8 +149,16 @@ public:
[[nodiscard]] Response Delete(std::string_view Url, const KeyValueMap& AdditionalHeader = {});
[[nodiscard]] Response Post(std::string_view Url, const KeyValueMap& AdditionalHeader = {}, const KeyValueMap& Parameters = {});
[[nodiscard]] Response Post(std::string_view Url, const IoBuffer& Payload, const KeyValueMap& AdditionalHeader = {});
+ [[nodiscard]] Response Post(std::string_view Url,
+ const IoBuffer& Payload,
+ ZenContentType ContentType,
+ const KeyValueMap& AdditionalHeader = {});
[[nodiscard]] Response Post(std::string_view Url, CbObject Payload, const KeyValueMap& AdditionalHeader = {});
[[nodiscard]] Response Post(std::string_view Url, CbPackage Payload, const KeyValueMap& AdditionalHeader = {});
+ [[nodiscard]] Response Post(std::string_view Url,
+ const CompositeBuffer& Payload,
+ ZenContentType ContentType,
+ const KeyValueMap& AdditionalHeader = {});
[[nodiscard]] Response Upload(std::string_view Url, const IoBuffer& Payload, const KeyValueMap& AdditionalHeader = {});
[[nodiscard]] Response Upload(std::string_view Url,
const CompositeBuffer& Payload,
diff --git a/src/zenhttp/servers/httpasio.cpp b/src/zenhttp/servers/httpasio.cpp
index de71eb0a7..cddbe1ae2 100644
--- a/src/zenhttp/servers/httpasio.cpp
+++ b/src/zenhttp/servers/httpasio.cpp
@@ -476,7 +476,15 @@ HttpServerConnection::HandleRequest()
{
Service->HandleRequest(Request);
}
- catch (std::system_error& SystemError)
+ catch (const AssertException& AssertEx)
+ {
+ // Drop any partially formatted response
+ Request.m_Response.reset();
+
+ ZEN_ERROR("Caught assert exception while handling request: {}", AssertEx.FullDescription());
+ Request.WriteResponse(HttpResponseCode::InternalServerError, HttpContentType::kText, AssertEx.FullDescription());
+ }
+ catch (const std::system_error& SystemError)
{
// Drop any partially formatted response
Request.m_Response.reset();
@@ -491,14 +499,14 @@ HttpServerConnection::HandleRequest()
Request.WriteResponse(HttpResponseCode::InternalServerError, HttpContentType::kText, SystemError.what());
}
}
- catch (std::bad_alloc& BadAlloc)
+ catch (const std::bad_alloc& BadAlloc)
{
// Drop any partially formatted response
Request.m_Response.reset();
Request.WriteResponse(HttpResponseCode::InsufficientStorage, HttpContentType::kText, BadAlloc.what());
}
- catch (std::exception& ex)
+ catch (const std::exception& ex)
{
// Drop any partially formatted response
Request.m_Response.reset();
@@ -958,7 +966,11 @@ HttpAsioServerImpl::Start(uint16_t Port, bool ForceLooopback, int ThreadCount)
{
m_IoService.run();
}
- catch (std::exception& e)
+ catch (const AssertException& AssertEx)
+ {
+ ZEN_ERROR("Assert caught in asio event loop: {}", AssertEx.FullDescription());
+ }
+ catch (const std::exception& e)
{
ZEN_ERROR("Exception caught in asio event loop: '{}'", e.what());
}
@@ -1075,7 +1087,7 @@ HttpAsioServer::Close()
{
m_Impl->Stop();
}
- catch (std::exception& ex)
+ catch (const std::exception& ex)
{
ZEN_WARN("Caught exception stopping http asio server: {}", ex.what());
}
diff --git a/src/zenhttp/servers/httpparser.cpp b/src/zenhttp/servers/httpparser.cpp
index 0a1c5686a..b848a5243 100644
--- a/src/zenhttp/servers/httpparser.cpp
+++ b/src/zenhttp/servers/httpparser.cpp
@@ -372,7 +372,12 @@ HttpRequestParser::OnMessageComplete()
ResetState();
return 0;
}
- catch (std::system_error& SystemError)
+ catch (const AssertException& AssertEx)
+ {
+ ZEN_WARN("Assert caught when processing http request: {}", AssertEx.FullDescription());
+ return 1;
+ }
+ catch (const std::system_error& SystemError)
{
if (IsOOM(SystemError.code()))
{
@@ -389,13 +394,13 @@ HttpRequestParser::OnMessageComplete()
ResetState();
return 1;
}
- catch (std::bad_alloc& BadAlloc)
+ catch (const std::bad_alloc& BadAlloc)
{
ZEN_WARN("out of memory when processing http request: '{}'", BadAlloc.what());
ResetState();
return 1;
}
- catch (std::exception& Ex)
+ catch (const std::exception& Ex)
{
ZEN_ERROR("failed processing http request: '{}'", Ex.what());
ResetState();
diff --git a/src/zenhttp/servers/httpplugin.cpp b/src/zenhttp/servers/httpplugin.cpp
index 4a2615133..09cd76f3e 100644
--- a/src/zenhttp/servers/httpplugin.cpp
+++ b/src/zenhttp/servers/httpplugin.cpp
@@ -386,7 +386,7 @@ HttpPluginConnectionHandler::HandleRequest()
{
Service->HandleRequest(Request);
}
- catch (std::system_error& SystemError)
+ catch (const std::system_error& SystemError)
{
// Drop any partially formatted response
Request.m_Response.reset();
@@ -401,14 +401,14 @@ HttpPluginConnectionHandler::HandleRequest()
Request.WriteResponse(HttpResponseCode::InternalServerError, HttpContentType::kText, SystemError.what());
}
}
- catch (std::bad_alloc& BadAlloc)
+ catch (const std::bad_alloc& BadAlloc)
{
// Drop any partially formatted response
Request.m_Response.reset();
Request.WriteResponse(HttpResponseCode::InsufficientStorage, HttpContentType::kText, BadAlloc.what());
}
- catch (std::exception& ex)
+ catch (const std::exception& ex)
{
// Drop any partially formatted response
Request.m_Response.reset();
@@ -691,13 +691,13 @@ HttpPluginServerImpl::Initialize(int BasePort, std::filesystem::path DataDir)
{
Plugin->Initialize(this);
}
- catch (std::exception& Ex)
+ catch (const std::exception& Ex)
{
ZEN_WARN("exception caught during plugin initialization: {}", Ex.what());
}
}
}
- catch (std::exception& ex)
+ catch (const std::exception& ex)
{
ZEN_WARN("Caught exception starting http plugin server: {}", ex.what());
}
@@ -723,7 +723,7 @@ HttpPluginServerImpl::Close()
{
Plugin->Shutdown();
}
- catch (std::exception& Ex)
+ catch (const std::exception& Ex)
{
ZEN_WARN("exception caught during plugin shutdown: {}", Ex.what());
}
@@ -733,7 +733,7 @@ HttpPluginServerImpl::Close()
m_Plugins.clear();
}
- catch (std::exception& ex)
+ catch (const std::exception& ex)
{
ZEN_WARN("Caught exception stopping http plugin server: {}", ex.what());
}
diff --git a/src/zenhttp/servers/httpsys.cpp b/src/zenhttp/servers/httpsys.cpp
index 4b812a127..2b97e3f25 100644
--- a/src/zenhttp/servers/httpsys.cpp
+++ b/src/zenhttp/servers/httpsys.cpp
@@ -873,7 +873,12 @@ HttpAsyncWorkRequest::AsyncWorkItem::Execute()
new HttpMessageResponseRequest(Tx, 500, "Response generated but no request handler scheduled"sv));
}
}
- catch (std::exception& Ex)
+ catch (const AssertException& AssertEx)
+ {
+ return (void)Tx.IssueNextRequest(
+ new HttpMessageResponseRequest(Tx, 500, fmt::format("Assert thrown in async work: '{}", AssertEx.FullDescription())));
+ }
+ catch (const std::exception& Ex)
{
return (void)Tx.IssueNextRequest(
new HttpMessageResponseRequest(Tx, 500, fmt::format("Exception thrown in async work: {}", Ex.what())));
@@ -1485,7 +1490,11 @@ HttpSysTransaction::IssueNextRequest(HttpSysRequestHandler* NewCompletionHandler
ZEN_WARN("IssueRequest() failed: {}", ErrorCode.message());
}
- catch (std::exception& Ex)
+ catch (const AssertException& AssertEx)
+ {
+ ZEN_ERROR("Assert thrown in IssueNextRequest(): {}", AssertEx.FullDescription());
+ }
+ catch (const std::exception& Ex)
{
ZEN_ERROR("exception caught in IssueNextRequest(): {}", Ex.what());
}
@@ -1995,7 +2004,12 @@ InitialRequestHandler::HandleCompletion(ULONG IoResult, ULONG_PTR NumberOfBytesT
// Unable to route
return new HttpMessageResponseRequest(Transaction(), 404, "No suitable route found"sv);
}
- catch (std::system_error& SystemError)
+ catch (const AssertException& AssertEx)
+ {
+ ZEN_ERROR("Caught assert exception while handling request: {}", AssertEx.FullDescription());
+ return new HttpMessageResponseRequest(Transaction(), (uint16_t)HttpResponseCode::InternalServerError, AssertEx.FullDescription());
+ }
+ catch (const std::system_error& SystemError)
{
if (IsOOM(SystemError.code()) || IsOOD(SystemError.code()))
{
@@ -2005,11 +2019,11 @@ InitialRequestHandler::HandleCompletion(ULONG IoResult, ULONG_PTR NumberOfBytesT
ZEN_ERROR("Caught system error exception while handling request: {}", SystemError.what());
return new HttpMessageResponseRequest(Transaction(), (uint16_t)HttpResponseCode::InternalServerError, SystemError.what());
}
- catch (std::bad_alloc& BadAlloc)
+ catch (const std::bad_alloc& BadAlloc)
{
return new HttpMessageResponseRequest(Transaction(), (uint16_t)HttpResponseCode::InsufficientStorage, BadAlloc.what());
}
- catch (std::exception& ex)
+ catch (const std::exception& ex)
{
ZEN_ERROR("Caught exception while handling request: '{}'", ex.what());
return new HttpMessageResponseRequest(Transaction(), (uint16_t)HttpResponseCode::InternalServerError, ex.what());
diff --git a/src/zenhttp/transports/asiotransport.cpp b/src/zenhttp/transports/asiotransport.cpp
index a9a782821..96a15518c 100644
--- a/src/zenhttp/transports/asiotransport.cpp
+++ b/src/zenhttp/transports/asiotransport.cpp
@@ -426,7 +426,7 @@ AsioTransportPlugin::Initialize(TransportServer* ServerInterface)
{
m_IoService.run();
}
- catch (std::exception& e)
+ catch (const std::exception& e)
{
ZEN_ERROR("exception caught in asio event loop: {}", e.what());
}
diff --git a/src/zenhttp/transports/winsocktransport.cpp b/src/zenhttp/transports/winsocktransport.cpp
index 7407c55dd..8c82760bb 100644
--- a/src/zenhttp/transports/winsocktransport.cpp
+++ b/src/zenhttp/transports/winsocktransport.cpp
@@ -309,7 +309,7 @@ SocketTransportPluginImpl::Initialize(TransportServer* ServerInterface)
{
Connection->HandleConnection();
}
- catch (std::exception& Ex)
+ catch (const std::exception& Ex)
{
ZEN_WARN("exception caught in connection loop: {}", Ex.what());
}