Skip to content

Commit

Permalink
Fix VerifyUploadHandler to use async
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsh committed Mar 21, 2020
1 parent 980bd7e commit 999f835
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion NetCoreServer/GenericHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task Invoke(HttpContext context)

if (path.Equals(new PathString("/verifyupload.ashx")))
{
VerifyUploadHandler.Invoke(context);
await VerifyUploadHandler.InvokeAsync(context);
return;
}

Expand Down
9 changes: 5 additions & 4 deletions NetCoreServer/Handlers/VerifyUploadHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace NetCoreServer
{
public class VerifyUploadHandler
{
public static void Invoke(HttpContext context)
public static async Task InvokeAsync(HttpContext context)
{
// Report back original request method verb.
context.Response.Headers.Add("X-HttpRequest-Method", context.Request.Method);
Expand All @@ -26,7 +27,7 @@ public static void Invoke(HttpContext context)
}

// Get request body.
byte[] requestBodyBytes = ReadAllRequestBytes(context);
byte[] requestBodyBytes = await ReadAllRequestBytesAsync(context);

// Check MD5 checksum for non-empty request body.
if (requestBodyBytes.Length > 0)
Expand Down Expand Up @@ -64,14 +65,14 @@ public static void Invoke(HttpContext context)
}
}

private static byte[] ReadAllRequestBytes(HttpContext context)
private static async Task<byte[]> ReadAllRequestBytesAsync(HttpContext context)
{
Stream requestStream = context.Request.Body;
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = requestStream.Read(buffer, 0, buffer.Length)) > 0)
while ((read = await requestStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
Expand Down

0 comments on commit 999f835

Please sign in to comment.