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

Fix support of webassembly #911

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 4 additions & 12 deletions Minio/DataModel/Result/ResponseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public sealed class ResponseResult : IDisposable
private string content;
private ReadOnlyMemory<byte> contentBytes;
private bool disposed;
private Stream stream;

public ResponseResult(HttpRequestMessage request, HttpResponseMessage response)
public ResponseResult(HttpRequestMessage request, HttpResponseMessage response, Stream stream = null)
{
Request = request;
Response = response;
ContentStream = stream;
}

public ResponseResult(HttpRequestMessage request, Exception exception)
Expand All @@ -56,14 +56,7 @@ public HttpStatusCode StatusCode
}
}

public Stream ContentStream
{
get
{
if (Response is null) return null;
return stream ??= Response.Content.ReadAsStream();
}
}
public Stream ContentStream { get; }//if (Response is null) return null;//return stream ??= Response.Content.ReadAsStream();

public ReadOnlyMemory<byte> ContentBytes
{
Expand Down Expand Up @@ -118,13 +111,12 @@ public void Dispose()
{
if (disposed) return;

stream?.Dispose();
ContentStream?.Dispose();
Request?.Dispose();
Response?.Dispose();

content = null;
contentBytes = null;
stream = null;

disposed = true;
}
Expand Down
5 changes: 2 additions & 3 deletions Minio/MinioClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,10 @@ public static IMinioClient Build(this IMinioClient minioClient)
minioClient.Config.Endpoint = string.Format(CultureInfo.InvariantCulture, "{0}://{1}", scheme, host);
else
minioClient.Config.Endpoint = host;

var httpClientHandler = new HttpClientHandler { Proxy = minioClient.Config.Proxy };

minioClient.Config.HttpClient ??= minioClient.Config.Proxy is null
? new HttpClient()
: new HttpClient(httpClientHandler);
: new HttpClient(new HttpClientHandler { Proxy = minioClient.Config.Proxy });
_ = minioClient.Config.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent",
minioClient.Config.FullUserAgent);
minioClient.Config.HttpClient.Timeout = TimeSpan.FromMinutes(30);
Expand Down
11 changes: 10 additions & 1 deletion Minio/RequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ private static async Task<ResponseResult> ExecuteTaskCoreAsync(this IMinioClient
var response = await minioClient.Config.HttpClient.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);
responseResult = new ResponseResult(request, response);
var statusCode = response.StatusCode;
var memoryStream = new MemoryStream();
if (statusCode == HttpStatusCode.OK)
{
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
}

responseResult = new ResponseResult(request, response, memoryStream);
Comment on lines +99 to +108
Copy link
Collaborator

@ebozduman ebozduman Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the previous review comments, this part needs to be cleaned up as responseResult has the ContentStream already set up.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also run dotnet regitlint for formatting problems.

if (requestMessageBuilder.ResponseWriter is not null)
requestMessageBuilder.ResponseWriter(responseResult.ContentStream);
if (requestMessageBuilder.FunctionResponseWriter is not null)
Expand Down
63 changes: 59 additions & 4 deletions Minio/V4Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,69 @@ private ReadOnlySpan<byte> GenerateSigningKey(string region, DateTime signingDat
/// <param name="content">Bytes to be hmac computed</param>
/// <returns>Computed hmac of input content</returns>
private ReadOnlySpan<byte> SignHmac(ReadOnlySpan<byte> key, ReadOnlySpan<byte> content)
{
return ComputeHmac256(key.ToArray(), content.ToArray());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code is the cause of the Functional test failure caught during the build tests.

You need to debug the code and resolve the issue.
When I reverted this change and run the Functional tests with the code suggested in the previous review comments, Functional tests ran successfully.

//#if NETSTANDARD
// using var hmac = new HMACSHA256(key.ToArray());
// hmac.Initialize();
// return hmac.ComputeHash(content.ToArray());
//#else
// return HMACSHA256.HashData(key, content);
//#endif
}

private byte[] ComputeHmac256(byte[] key, byte[] content)
{
var hKey = key;
if (hKey.Length > 64)
{
hKey = ComputeSha256(hKey);
}
else if (hKey.Length < 64)
{
var data = new byte[64];
Array.Copy(hKey.ToArray(), data, hKey.Length);
hKey = (data);
}

var innerKeyPad = new byte[hKey.Length];
for (var i = 0; i < hKey.Length; i++)
{
innerKeyPad[i] = (byte)(hKey[i] ^ 0x36);
}

var innerBody = new byte[content.Length + innerKeyPad.Length];
Array.Copy(innerKeyPad, innerBody, innerKeyPad.Length);
Array.Copy(content,0, innerBody, innerKeyPad.Length, content.Length);
var innerHash = ComputeSha256(innerBody);

var outerKeyPad = new byte[hKey.Length];
for (var i = 0; i < hKey.Length; i++)
{
outerKeyPad[i] = (byte)(hKey[i] ^ 0x5c);
}

var outerBody = new byte[innerHash.Length + outerKeyPad.Length];
Array.Copy(outerKeyPad, outerBody, outerKeyPad.Length);
Array.Copy(innerHash, 0, outerBody, outerKeyPad.Length, innerHash.Length);
var outerHash = ComputeSha256(outerBody);
return outerHash;
}
/// <summary>
/// Compute sha256 checksum.
/// </summary>
/// <param name="body">Bytes body</param>
/// <returns>Bytes of sha256 checksum</returns>
private byte[] ComputeSha256(byte[] body)
{
#if NETSTANDARD
using var hmac = new HMACSHA256(key.ToArray());
hmac.Initialize();
return hmac.ComputeHash(content.ToArray());
using var sha = SHA256.Create();
var hash
= sha.ComputeHash(body.ToArray());
#else
return HMACSHA256.HashData(key, content);
var hash = SHA256.HashData(body);
#endif
return hash;
}

/// <summary>
Expand Down
Loading