Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize compressed documents correctly #16415

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using OrchardCore.Data.Documents;
Expand All @@ -12,15 +13,19 @@ namespace OrchardCore.Documents
/// </summary>
public class DefaultDocumentSerializer : IDocumentSerializer
{
public static readonly DefaultDocumentSerializer Instance = new();
private static readonly byte[] _gZipHeaderBytes = [0x1f, 0x8b];

private readonly JsonSerializerOptions _serializerOptions;

public DefaultDocumentSerializer()
public DefaultDocumentSerializer(JsonSerializerOptions serializerOptions)
{
_serializerOptions = serializerOptions;
}

public Task<byte[]> SerializeAsync<TDocument>(TDocument document, int compressThreshold = int.MaxValue) where TDocument : class, IDocument, new()
public Task<byte[]> SerializeAsync<TDocument>(TDocument document, int compressThreshold = int.MaxValue)
where TDocument : class, IDocument, new()
{
var data = Encoding.UTF8.GetBytes(JConvert.SerializeObject(document));
var data = JsonSerializer.SerializeToUtf8Bytes(document, _serializerOptions);
if (data.Length >= compressThreshold)
{
data = Compress(data);
Expand All @@ -29,22 +34,32 @@ public DefaultDocumentSerializer()
return Task.FromResult(data);
}

public Task<TDocument> DeserializeAsync<TDocument>(byte[] data) where TDocument : class, IDocument, new()
public Task<TDocument> DeserializeAsync<TDocument>(byte[] data)
where TDocument : class, IDocument, new()
{
if (IsCompressed(data))
{
data = Decompress(data);
}

var document = JConvert.DeserializeObject<TDocument>(Encoding.UTF8.GetString(data));
using var ms = new MemoryStream(data);

var document = JsonSerializer.Deserialize<TDocument>(ms, _serializerOptions);

return Task.FromResult(document);
}

private static readonly byte[] _gZipHeaderBytes = [0x1f, 0x8b];
internal static bool IsCompressed(byte[] data)
{
// Ensure data is at least as long as the GZip header
if (data.Length >= _gZipHeaderBytes.Length)
{
// Compare the header bytes.
return data.Take(_gZipHeaderBytes.Length).SequenceEqual(_gZipHeaderBytes);
}

internal static bool IsCompressed(byte[] data) =>
data.Length < _gZipHeaderBytes.Length && data[0..1] == _gZipHeaderBytes;
return false;
}

internal static byte[] Compress(byte[] data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Json;

namespace OrchardCore.Documents.Options
{
Expand All @@ -11,13 +12,20 @@ public class DocumentOptionsSetup : IConfigureNamedOptions<DocumentOptions>
public static readonly TimeSpan DefaultFailoverRetryLatency = TimeSpan.FromSeconds(30);

private readonly IShellConfiguration _shellConfiguration;
private readonly DocumentJsonSerializerOptions _documentJsonSerializerOptions;

public DocumentOptionsSetup(IShellConfiguration shellConfiguration)
private DefaultDocumentSerializer _defaultDocumentSerializer;

public DocumentOptionsSetup(
IShellConfiguration shellConfiguration,
IOptions<DocumentJsonSerializerOptions> documentJsonSerializerOptions)
{
_shellConfiguration = shellConfiguration;
_documentJsonSerializerOptions = documentJsonSerializerOptions.Value;
}

public void Configure(DocumentOptions options) => Configure(string.Empty, options);
public void Configure(DocumentOptions options)
=> Configure(string.Empty, options);

public void Configure(string name, DocumentOptions options)
{
Expand Down Expand Up @@ -46,7 +54,7 @@ public void Configure(string name, DocumentOptions options)
?? sharedConfig.SynchronizationLatency
?? TimeSpan.FromSeconds(1);

options.Serializer = DefaultDocumentSerializer.Instance;
options.Serializer = _defaultDocumentSerializer ??= new DefaultDocumentSerializer(_documentJsonSerializerOptions.SerializerOptions);

options.CompressThreshold = namedConfig.CompressThreshold;
if (options.CompressThreshold == 0)
Expand Down