Skip to content

Commit

Permalink
Merge pull request #38 from LykkeCity/BufferingHelperFix
Browse files Browse the repository at this point in the history
Fixed net.core 3.0 compatibility issue for MiddlewareApplicationBuild…
  • Loading branch information
starkmsu committed Mar 25, 2020
2 parents c5c6633 + 06f138a commit a56947d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
50 changes: 50 additions & 0 deletions src/Lykke.Common.ApiLibrary/Middleware/BufferingHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;

namespace Lykke.Common.ApiLibrary.Middleware
{
internal static class BufferingHelper
{
private const int DefaultBufferThreshold = 1024 * 30;

private static string _tempDirectory;

public static HttpRequest EnableRewind(this HttpRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));

var body = request.Body;
if (!body.CanSeek)
{
var fileStream = new FileBufferingReadStream(body, DefaultBufferThreshold, null, GetTempDirectory);
request.Body = fileStream;
request.HttpContext.Response.RegisterForDispose(fileStream);
}

return request;
}

private static string GetTempDirectory()
{
if (_tempDirectory != null)
return _tempDirectory;

// Look for folders in the following order.
var temp = Environment.GetEnvironmentVariable("ASPNETCORE_TEMP") // ASPNETCORE_TEMP - User set temporary location.
?? Path.GetTempPath(); // Fall back.

if (!Directory.Exists(temp))
{
// TODO: ???
throw new DirectoryNotFoundException(temp);
}

_tempDirectory = temp;

return _tempDirectory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using JetBrains.Annotations;
using Lykke.Common.Log;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -88,7 +87,7 @@ public static void UseLykkeMiddleware(
app.UseMiddleware<ClientErrorHandlerMiddleware>(logFactory);
}
}

/// <summary>
/// Configure application to use forwarded headers
/// </summary>
Expand All @@ -99,10 +98,10 @@ public static void UseLykkeForwardedHeaders(this IApplicationBuilder app)
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost
};

forwardingOptions.KnownNetworks.Clear(); //its loopback by default
forwardingOptions.KnownProxies.Clear();

app.UseForwardedHeaders(forwardingOptions);
}
}
Expand Down

0 comments on commit a56947d

Please sign in to comment.