From bb07e58a9c59705b54164b06bcbe40c052880d90 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Mon, 7 Nov 2022 10:18:44 +0100 Subject: Support file reference in package message (#184) * Fix packed message parsing for absolute path * Always enable are sharing when opening files as IoBuffers. * Allow control over sending partial files as localfile ref * Check "AcceptFlags" field in RPC message for allowing localfile ref in reply * make oplog entry add operations ZEN_DEBUG level logs * changelog --- zenhttp/httpshared.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'zenhttp/httpshared.cpp') diff --git a/zenhttp/httpshared.cpp b/zenhttp/httpshared.cpp index f9aa3af82..a7dca5441 100644 --- a/zenhttp/httpshared.cpp +++ b/zenhttp/httpshared.cpp @@ -6,14 +6,14 @@ #include #include #include +#include #include #include +#include #include #include #include -#include - #include #include @@ -93,7 +93,10 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags) ResponseBuffers.push_back(std::move(RefBuffer)); }; - auto IsLocalRef = [](const CompositeBuffer& AttachmentBinary, CbAttachmentReferenceHeader& LocalRef, std::string& Path8) -> bool { + auto IsLocalRef = [](const CompositeBuffer& AttachmentBinary, + bool DenyPartialLocalReferences, + CbAttachmentReferenceHeader& LocalRef, + std::string& Path8) -> bool { const SharedBuffer& Segment = AttachmentBinary.GetSegments().front(); IoBufferFileReference Ref; const IoBuffer& SegmentBuffer = Segment.AsIoBuffer(); @@ -103,6 +106,11 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags) return false; } + if (DenyPartialLocalReferences && !SegmentBuffer.IsWholeFile()) + { + return false; + } + ExtendablePathBuilder<256> LocalRefFile; LocalRefFile.Append(std::filesystem::absolute(PathFromHandle(Ref.FileHandle))); Path8 = LocalRefFile.ToUtf8(); @@ -131,19 +139,20 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags) // segments to be marshaled at once bool MarshalByLocalRef = EnumHasAllFlags(Flags, FormatFlags::kAllowLocalReferences) && (Compressed.GetSegments().size() == 1); - + bool DenyPartialLocalReferences = EnumHasAllFlags(Flags, FormatFlags::kDenyPartialLocalReferences); CbAttachmentReferenceHeader LocalRef; std::string Path8; if (MarshalByLocalRef) { - MarshalByLocalRef = IsLocalRef(Compressed, LocalRef, Path8); + MarshalByLocalRef = IsLocalRef(Compressed, DenyPartialLocalReferences, LocalRef, Path8); } if (MarshalByLocalRef) { const bool IsCompressed = true; MarshalLocal(Path8, LocalRef, AttachmentHash, IsCompressed); + ZEN_DEBUG("Marshalled '{}' as file of {} bytes", Path8, Compressed.GetSize()); } else { @@ -171,19 +180,21 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags) IoHash AttachmentHash = IoHash::FromBLAKE3(AttachmentBuffer.GetRawHash()); bool MarshalByLocalRef = EnumHasAllFlags(Flags, FormatFlags::kAllowLocalReferences) && (AttachmentBinary.GetSegments().size() == 1); + bool DenyPartialLocalReferences = EnumHasAllFlags(Flags, FormatFlags::kDenyPartialLocalReferences); CbAttachmentReferenceHeader LocalRef; std::string Path8; if (MarshalByLocalRef) { - MarshalByLocalRef = IsLocalRef(AttachmentBinary, LocalRef, Path8); + MarshalByLocalRef = IsLocalRef(AttachmentBinary, DenyPartialLocalReferences, LocalRef, Path8); } if (MarshalByLocalRef) { const bool IsCompressed = false; MarshalLocal(Path8, LocalRef, AttachmentHash, IsCompressed); + ZEN_DEBUG("Marshalled '{}' as file of {} bytes", Path8, AttachmentBinary.GetSize()); } else { @@ -270,7 +281,7 @@ ParsePackageMessage(IoBuffer Payload, std::function= (sizeof(CbAttachmentReferenceHeader) + AttachRefHdr->AbsolutePathLength)); - std::filesystem::path Path{PathPointer}; + std::filesystem::path Path{std::u8string_view(PathPointer, AttachRefHdr->AbsolutePathLength)}; if (IoBuffer ChunkReference = IoBufferBuilder::MakeFromFile(Path, AttachRefHdr->PayloadByteOffset, AttachRefHdr->PayloadByteSize)) -- cgit v1.2.3