aboutsummaryrefslogtreecommitdiff
path: root/zenhttp/httpclient.cpp
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/httpclient.cpp
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/httpclient.cpp')
-rw-r--r--zenhttp/httpclient.cpp92
1 files changed, 91 insertions, 1 deletions
diff --git a/zenhttp/httpclient.cpp b/zenhttp/httpclient.cpp
index 9d692271d..fb31e0a8b 100644
--- a/zenhttp/httpclient.cpp
+++ b/zenhttp/httpclient.cpp
@@ -2,12 +2,102 @@
#include <zenhttp/httpclient.h>
-#include <spdlog/spdlog.h>
+#include <zencore/compactbinarybuilder.h>
+#include <zencore/compactbinarypackage.h>
+#include <zencore/logging.h>
+#include <zencore/stream.h>
#include <doctest/doctest.h>
namespace zen {
+HttpClient::HttpClient(std::string_view BaseUri) : m_BaseUri(BaseUri)
+{
+}
+
+HttpClient::~HttpClient()
+{
+}
+
+void
+HttpClient::TransactPackage(std::string_view Url, CbPackage Package)
+{
+ cpr::Session Sess;
+ Sess.SetUrl(m_BaseUri + std::string(Url));
+
+ // First, list of offered chunks for filtering on the server end
+
+ std::vector<IoHash> AttachmentsToSend;
+ std::span<const CbAttachment> Attachments = Package.GetAttachments();
+
+ if (Attachments.empty() == false)
+ {
+ CbObjectWriter Writer;
+ Writer.BeginArray("offer");
+
+ for (const CbAttachment& Attachment : Attachments)
+ {
+ IoHash Hash = Attachment.GetHash();
+
+ Writer.AddHash(Hash);
+ }
+
+ Writer.EndArray();
+
+ MemoryOutStream MemOut;
+ BinaryWriter MemWriter(MemOut);
+ Writer.Save(MemWriter);
+
+ Sess.SetHeader({{"Content-Type", "application/x-ue-offer"}, {"UE-Session", "123456789012345678901234"}, {"UE-Request", "1"}});
+ Sess.SetBody(cpr::Body{(const char*)MemOut.Data(), MemOut.Size()});
+
+ cpr::Response FilterResponse = Sess.Post();
+
+ if (FilterResponse.status_code == 200)
+ {
+ IoBuffer ResponseBuffer(IoBuffer::Wrap, FilterResponse.text.data(), FilterResponse.text.size());
+ CbObject ResponseObject = LoadCompactBinaryObject(ResponseBuffer);
+
+ for (auto& Entry : ResponseObject["need"])
+ {
+ ZEN_ASSERT(Entry.IsHash());
+ AttachmentsToSend.push_back(Entry.AsHash());
+ }
+ }
+ }
+
+ CbPackage SendPackage;
+ SendPackage.SetObject(Package.GetObject(), Package.GetObjectHash());
+ ;
+
+ for (const IoHash& AttachmentCid : AttachmentsToSend)
+ {
+ const CbAttachment* Attachment = Package.FindAttachment(AttachmentCid);
+
+ if (Attachment)
+ {
+ SendPackage.AddAttachment(*Attachment);
+ }
+ else
+ {
+ // This should be an error -- server asked to have something we can't find
+ }
+ }
+
+ {
+ MemoryOutStream MemOut;
+ BinaryWriter MemWriter(MemOut);
+ SendPackage.Save(MemWriter);
+
+ Sess.SetHeader({{"Content-Type", "application/x-ue-cbpkg"}, {"UE-Session", "123456789012345678901234"}, {"UE-Request", "1"}});
+ Sess.SetBody(cpr::Body{(const char*)MemOut.Data(), MemOut.Size()});
+
+ cpr::Response FilterResponse = Sess.Post();
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////
+
TEST_CASE("httpclient")
{
using namespace std::literals;