aboutsummaryrefslogtreecommitdiff
path: root/zenhttp/include
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-09-13 10:07:30 +0200
committerStefan Boberg <[email protected]>2021-09-13 10:07:30 +0200
commit1d6aeff046a8f9f3df564163b56e927096decc39 (patch)
tree368dcb95585fbef37314b9a05d7e77d95a76e844 /zenhttp/include
parentAdded CbPackageOffer content type (diff)
downloadzen-1d6aeff046a8f9f3df564163b56e927096decc39.tar.xz
zen-1d6aeff046a8f9f3df564163b56e927096decc39.zip
Implemented generic CbPackage attachments filtering
Package transmission will also need to be updated (up next) for the new scheme to be effective
Diffstat (limited to 'zenhttp/include')
-rw-r--r--zenhttp/include/zenhttp/httpclient.h20
-rw-r--r--zenhttp/include/zenhttp/httpserver.h70
2 files changed, 62 insertions, 28 deletions
diff --git a/zenhttp/include/zenhttp/httpclient.h b/zenhttp/include/zenhttp/httpclient.h
index 4a266e59e..10829a58c 100644
--- a/zenhttp/include/zenhttp/httpclient.h
+++ b/zenhttp/include/zenhttp/httpclient.h
@@ -4,17 +4,33 @@
#include "zenhttp.h"
-#include <zencore/string.h>
-#include <gsl/gsl-lite.hpp>
+#include <zencore/windows.h>
+
+// For some reason, these don't seem to stick, so we disable the warnings
+//# define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING 1
+//# define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS 1
+#pragma warning(push)
+#pragma warning(disable : 4004)
+#pragma warning(disable : 4996)
+#include <cpr/cpr.h>
+#pragma warning(pop)
namespace zen {
+class CbPackage;
+
/** Asynchronous HTTP client implementation for Zen use cases
*/
class HttpClient
{
public:
+ HttpClient(std::string_view BaseUri);
+ ~HttpClient();
+
+ void TransactPackage(std::string_view Url, CbPackage Package);
+
private:
+ std::string m_BaseUri;
};
} // namespace zen
diff --git a/zenhttp/include/zenhttp/httpserver.h b/zenhttp/include/zenhttp/httpserver.h
index b8b63ac4d..f859eb038 100644
--- a/zenhttp/include/zenhttp/httpserver.h
+++ b/zenhttp/include/zenhttp/httpserver.h
@@ -6,8 +6,10 @@
#include <zencore/enumflags.h>
#include <zencore/iobuffer.h>
+#include <zencore/iohash.h>
#include <zencore/refcount.h>
#include <zencore/string.h>
+#include <zencore/uid.h>
#include <functional>
#include <gsl/gsl-lite.hpp>
@@ -26,8 +28,10 @@ class CbPackage;
class StringBuilderBase;
std::string_view MapContentTypeToString(HttpContentType ContentType);
+HttpContentType ParseContentType(const std::string_view& ContentTypeString);
+const char* ReasonStringForHttpResultCode(int HttpCode);
-enum class HttpVerb
+enum class HttpVerb : uint8_t
{
kGet = 1 << 0,
kPut = 1 << 1,
@@ -179,7 +183,6 @@ public:
[[nodiscard]] inline std::string_view RelativeUri() const { return m_UriUtf8; } // Returns URI without service prefix
[[nodiscard]] inline std::string_view QueryString() const { return m_QueryStringUtf8; }
- inline bool IsHandled() const { return m_IsHandled; }
struct QueryParams
{
@@ -210,25 +213,16 @@ public:
inline HttpContentType RequestContentType() { return m_ContentType; }
inline HttpContentType AcceptContentType() { return m_AcceptType; }
- const char* HeaderAccept() const;
- const char* HeaderAcceptEncoding() const;
- const char* HeaderContentType() const;
- const char* HeaderContentEncoding() const;
- inline uint64_t HeaderContentLength() const { return m_ContentLength; }
+ inline uint64_t ContentLength() const { return m_ContentLength; }
+ virtual Oid SessionId() const;
+ virtual uint32_t RequestId() const;
- void SetSuppressResponseBody() { m_SuppressBody = true; }
+ inline bool IsHandled() const { return !!(m_Flags & kIsHandled); }
+ inline bool SuppressBody() const { return !!(m_Flags & kSuppressBody); }
+ inline void SetSuppressResponseBody() { m_Flags |= kSuppressBody; }
- // Asynchronous operations
-
- /** Read POST/PUT payload
-
- This will return a null buffer if the contents are not fully available yet, and the handler should
- at that point return - another completion request will be issued once the contents have been received
- fully.
-
- NOTE: in practice, via the http.sys implementation this always operates synchronously. This should
- be updated to provide fully asynchronous operation for better scalability on shared instances
- */
+ /** Read POST/PUT payload for request body, which is always available without delay
+ */
virtual IoBuffer ReadPayload() = 0;
ZENCORE_API CbObject ReadPayloadObject();
@@ -236,6 +230,9 @@ public:
/** Respond with payload
+ No data will have been sent when any of these functions return. Instead, the response will be transmitted
+ asynchronously, after returning from a request handler function.
+
Note that this is destructive in the sense that the IoBuffer instances referred to by Blobs will be
moved into our response handler array where they are kept alive, in order to reduce ref-counting storms
*/
@@ -249,14 +246,34 @@ public:
void WriteResponse(HttpResponseCode ResponseCode, HttpContentType ContentType, IoBuffer Blob);
protected:
- bool m_IsHandled = false;
- bool m_SuppressBody = false;
+ enum
+ {
+ kIsHandled = 1 << 0,
+ kSuppressBody = 1 << 1,
+ kHaveRequestId = 1 << 2,
+ kHaveSessionId = 1 << 3,
+ };
+
+ mutable uint32_t m_Flags = 0;
HttpVerb m_Verb = HttpVerb::kGet;
- uint64_t m_ContentLength = ~0ull;
HttpContentType m_ContentType = HttpContentType::kBinary;
HttpContentType m_AcceptType = HttpContentType::kUnknownContentType;
- ExtendableStringBuilder<256> m_UriUtf8;
- ExtendableStringBuilder<256> m_QueryStringUtf8;
+ uint64_t m_ContentLength = ~0ull;
+ ExtendableStringBuilder<128> m_UriUtf8;
+ ExtendableStringBuilder<128> m_QueryStringUtf8;
+ mutable uint32_t m_RequestId = ~uint32_t(0);
+ mutable Oid m_SessionId = Oid::Zero;
+
+ inline void SetIsHandled() { m_Flags |= kIsHandled; }
+};
+
+class IHttpPackageHandler : public RefCounted
+{
+public:
+ virtual void FilterOffer(std::vector<IoHash>& OfferCids) = 0;
+ virtual void OnBeginChunks() = 0;
+ virtual IoBuffer CreateTarget(const IoHash& Cid, uint64_t StorageSize) = 0;
+ virtual void OnEndChunks() = 0;
};
/**
@@ -272,8 +289,9 @@ public:
HttpService() = default;
virtual ~HttpService() = default;
- virtual const char* BaseUri() const = 0;
- virtual void HandleRequest(HttpServerRequest& HttpServiceRequest) = 0;
+ virtual const char* BaseUri() const = 0;
+ virtual void HandleRequest(HttpServerRequest& HttpServiceRequest) = 0;
+ virtual Ref<IHttpPackageHandler> HandlePackageRequest(HttpServerRequest& HttpServiceRequest);
// Internals