Skip to content

Commit

Permalink
Let dotnet handle contentLength
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartdohmann committed Feb 8, 2024
1 parent b02d18e commit d975d58
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
11 changes: 5 additions & 6 deletions dotnet/Vaas/src/Vaas/Vaas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,12 @@ public async Task<VaasVerdict> ForUrlAsync(Uri uri, CancellationToken cancellati

public async Task<VaasVerdict> ForStreamAsync(
Stream stream,
long contentLength,
CancellationToken cancellationToken,
Dictionary<string, string>? verdictRequestAttributes = null
)
{
if (stream == null || contentLength <= 0)
throw new VaasClientException("Stream or content length was null.");
if (stream == null)
throw new VaasClientException("Stream was null.");

var verdictResponse = await ForStreamRequestAsync(
new VerdictRequestForStream(SessionId ?? throw new InvalidOperationException())
Expand All @@ -216,14 +215,14 @@ public async Task<VaasVerdict> ForStreamAsync(
}

var response = WaitForResponseAsync(verdictResponse.Guid);
await UploadStream(stream, contentLength, verdictResponse.Url, verdictResponse.UploadToken, cancellationToken);
await UploadStream(stream, verdictResponse.Url, verdictResponse.UploadToken, cancellationToken);

return new VaasVerdict(await response);
}

private async Task UploadStream(Stream stream, long contentLength, string url, string token, CancellationToken cancellationToken)
private async Task UploadStream(Stream stream, string url, string token, CancellationToken cancellationToken)
{
using var requestContent = new StreamContent(stream, (int)contentLength);
using var requestContent = new StreamContent(stream);
using var requestMessage = new HttpRequestMessage(HttpMethod.Put, url);
requestMessage.Content = requestContent;
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(token);
Expand Down
14 changes: 4 additions & 10 deletions dotnet/Vaas/test/Vaas.Test/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,9 @@ public async Task ForStream_WithEicarString_ReturnsMalicious()
var eicarBytes = System.Text.Encoding.UTF8.GetBytes("X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*");
targetStream.Write(eicarBytes, 0, eicarBytes.Length);
targetStream.Position = 0;
var contentLength = targetStream.Length;

// Act
var verdict = await vaas.ForStreamAsync(targetStream, contentLength, CancellationToken.None);
var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None);

// Assert
Assert.Equal(Verdict.Malicious, verdict.Verdict);
Expand All @@ -208,10 +207,9 @@ public async Task ForStream_WithCleanString_ReturnsClean()
var cleanBytes = System.Text.Encoding.UTF8.GetBytes("This is a clean file");
targetStream.Write(cleanBytes, 0, cleanBytes.Length);
targetStream.Position = 0;
var contentLength = targetStream.Length;

// Act
var verdict = await vaas.ForStreamAsync(targetStream, contentLength, CancellationToken.None);
var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None);

// Assert
Assert.Equal(Verdict.Clean, verdict.Verdict);
Expand All @@ -225,11 +223,9 @@ public async Task ForStream_WithCleanUrl_ReturnsClean()
var url = new Uri("https://raw.githubusercontent.com/GDATASoftwareAG/vaas/main/Readme.md");
var response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, url), CancellationToken.None);
var targetStream = await response.Content.ReadAsStreamAsync();
var contentLength = response.Content.Headers.ContentLength;
Assert.NotNull(contentLength);

// Act
var verdict = await vaas.ForStreamAsync(targetStream, (long)contentLength, CancellationToken.None);
var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None);

// Assert
Assert.Equal(Verdict.Clean, verdict.Verdict);
Expand All @@ -243,11 +239,9 @@ public async Task ForStream_WithEicarUrl_ReturnsEicar()
var url = new Uri("https://secure.eicar.org/eicar.com.txt");
var response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, url), CancellationToken.None);
var targetStream = await response.Content.ReadAsStreamAsync();
var contentLength = response.Content.Headers.ContentLength;
Assert.NotNull(contentLength);

// Act
var verdict = await vaas.ForStreamAsync(targetStream, (long)contentLength, CancellationToken.None);
var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None);

// Assert
Assert.Equal(Verdict.Malicious, verdict.Verdict);
Expand Down

0 comments on commit d975d58

Please sign in to comment.