Skip to content

Commit

Permalink
#243026895: Fix the creation of stream only if needed for both Owin a…
Browse files Browse the repository at this point in the history
…nd MVC apps. (#99)

* Fix the creation of stream only if needed.

* user defined default for parsed content-length
  • Loading branch information
praves77 authored Nov 22, 2024
1 parent 0ab81ce commit ac5eec8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Moesif.Middleware/Moesif.Middleware.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Moesif.Middleware</id>
<version>3.1.0</version>
<version>3.1.1</version>
<title>MoesifMiddleware</title>
<authors>Moesif</authors>
<owners>Moesif</owners>
Expand Down
50 changes: 33 additions & 17 deletions Moesif.Middleware/NetCore/MoesifMiddlewareNetCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,27 @@ public void UpdateCompaniesBatch(List<Dictionary<string, object>> companyProfile
companyHelper.UpdateCompaniesBatch(client, companyProfiles, debug);
}

public void CreateStreamHelpers(HttpContext httpContext, out StreamHelper outputCaptureOwin)
{
outputCaptureOwin = null; // Buffering Owin response

// Check if we need to create memory stream., only if
// - logBody is enabled &&
// - response's content-length is less than maxBodySize
var resHeaders = loggerHelper.ToHeaders(httpContext.Response.Headers, debug);
int parsedRespContentLength = responseMaxBodySize - 1;
GetContentLengthAndEncoding(resHeaders, parsedRespContentLength, out parsedRespContentLength); // Get the content-length from response header if possible.
bool needToCreateStream = (logBody && parsedRespContentLength <= responseMaxBodySize) ;

// Create stream to Buffer Owin response
if (needToCreateStream)
{
var owinResponse = httpContext.Response;
outputCaptureOwin = new StreamHelper(owinResponse.Body);
owinResponse.Body = outputCaptureOwin;
}
}

public async Task Invoke(HttpContext httpContext)
{
#if MOESIF_INSTRUMENT
Expand Down Expand Up @@ -455,19 +476,10 @@ public async Task Invoke(HttpContext httpContext)

else
{
StreamHelper outputCaptureOwin = null;
// Create memory stream only if needed
// - logBody is enabled &&
// - response's content-length is less than maxBodySize
var resHeaders = loggerHelper.ToHeaders(httpContext.Response.Headers, debug);
int parsedRespContentLength = 1000;
GetContentLengthAndEncoding(resHeaders, out parsedRespContentLength); // Get the content-length from response header if possible.
if (logBody && parsedRespContentLength <= responseMaxBodySize)
{
var owinResponse = httpContext.Response;
outputCaptureOwin = new StreamHelper(owinResponse.Body);
owinResponse.Body = outputCaptureOwin;
}
// Create memory stream
StreamHelper outputCaptureOwin = null; // For buffering Owin response
CreateStreamHelpers(httpContext, out outputCaptureOwin);

await _next(httpContext);

#if MOESIF_INSTRUMENT
Expand Down Expand Up @@ -603,10 +615,9 @@ public async Task Invoke(HttpContext httpContext)
#endif
}

public static string GetContentLengthAndEncoding(Dictionary<string, string> headers, out int parsedContentLength)
public static string GetContentLengthAndEncoding(Dictionary<string, string> headers, int defaultLength, out int parsedContentLength)
{
string contentEncoding = "";
parsedContentLength = 100000;

if (headers != null)
{
Expand All @@ -615,6 +626,10 @@ public static string GetContentLengthAndEncoding(Dictionary<string, string> head
headers.TryGetValue("Content-Encoding", out contentEncoding);
int.TryParse(contentLength, out parsedContentLength);
}
else
{
parsedContentLength = defaultLength;
}

return contentEncoding;
}
Expand Down Expand Up @@ -650,11 +665,11 @@ public static string GetExceededBodyForBodySize(string prefix, int curBodySize,
// Get Content-Length and Content-Encoding
// string contentEncoding = "";
// string contentLength = "";
int parsedContentLength = 100000;
int parsedContentLength = requestMaxBodySize -1 ;
// reqHeaders.TryGetValue("Content-Encoding", out contentEncoding);
// reqHeaders.TryGetValue("Content-Length", out contentLength);
// int.TryParse(contentLength, out parsedContentLength);
string contentEncoding = GetContentLengthAndEncoding(reqHeaders, out parsedContentLength);
string contentEncoding = GetContentLengthAndEncoding(reqHeaders, parsedContentLength, out parsedContentLength);

// RequestBody
request.EnableBuffering(bufferThreshold: 1000000);
Expand Down Expand Up @@ -798,6 +813,7 @@ private async Task<EventResponseModel> FormatLambdaResponse(HttpContext httpCont

var originalResponseBodyStream = httpContext.Response.Body;
string responseBody = string.Empty;
// TODO : why new memory stream here???
using (var responseBodyStream = new MemoryStream())
{
httpContext.Response.Body = responseBodyStream; // Use the memory stream for the response
Expand Down
48 changes: 31 additions & 17 deletions Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,31 +254,42 @@ public void UpdateCompaniesBatch(List<Dictionary<string, object>> companyProfile
companyHelper.UpdateCompaniesBatch(client, companyProfiles, debug);
}

public async override Task Invoke(IOwinContext httpContext)
public void CreateStreamHelpers(IOwinContext httpContext, out StreamHelper outputCaptureMVC, out StreamHelper outputCaptureOwin)
{
// Buffering mvc reponse
StreamHelper outputCaptureMVC = null;
outputCaptureMVC = null; // Buffering MVC response
outputCaptureOwin = null; // Buffering Owin response

// Check if we need to create memory stream. Create it only if
// - logBody is enabled &&
// - response's content-length is less than maxBodySize
var resHeaders = loggerHelper.ToHeaders(httpContext.Response.Headers, debug);
int parsedResContentLength = responseMaxBodySize - 1;
GetContentLengthAndEncoding(resHeaders, parsedResContentLength, out parsedResContentLength); // Get the content-length from response header if possible.
bool needToCreateStream = (logBody && parsedResContentLength <= responseMaxBodySize) ;

// Buffering mvc response
HttpResponse httpResponse = HttpContext.Current?.Response;
if (httpResponse != null)
if (httpResponse != null && needToCreateStream)
{
outputCaptureMVC = new StreamHelper(httpResponse.Filter);
httpResponse.Filter = outputCaptureMVC;
}

// Buffering Owin response
StreamHelper outputCaptureOwin = null;
// Create memory stream only if needed
// - logBody is enabled &&
// - response's content-length is less than maxBodySize
var resHeaders = loggerHelper.ToHeaders(httpContext.Response.Headers, debug);
int parsedResContentLength = 1000;
GetContentLengthAndEncoding(resHeaders, out parsedResContentLength); // Get the content-length from response header if possible.
if (logBody && parsedResContentLength <= responseMaxBodySize)
// Create stream to buffer Owin response
if (needToCreateStream)
{
IOwinResponse owinResponse = httpContext.Response;
outputCaptureOwin = new StreamHelper(owinResponse.Body);
owinResponse.Body = outputCaptureOwin;
}
}

public override async Task Invoke(IOwinContext httpContext)
{
// Create memory stream to buffer response
StreamHelper outputCaptureMVC = null; // For buffering MVC response
StreamHelper outputCaptureOwin = null; // For buffering Owin response
CreateStreamHelpers(httpContext, out outputCaptureMVC, out outputCaptureOwin);

// Initialize Transaction Id
string transactionId = null;
Expand Down Expand Up @@ -396,10 +407,9 @@ public string GetClientIp(IOwinRequest request)
return ipAddress;
}

public static string GetContentLengthAndEncoding(Dictionary<string, string> headers, out int parsedContentLength)
public static string GetContentLengthAndEncoding(Dictionary<string, string> headers, int defaultLength, out int parsedContentLength)
{
string contentEncoding = "";
parsedContentLength = 100000;

if (headers != null)
{
Expand All @@ -408,6 +418,10 @@ public static string GetContentLengthAndEncoding(Dictionary<string, string> head
headers.TryGetValue("Content-Encoding", out contentEncoding);
int.TryParse(contentLength, out parsedContentLength);
}
else
{
parsedContentLength = defaultLength;
}

return contentEncoding;
}
Expand All @@ -433,12 +447,12 @@ public static string GetExceededBodyForBodySize(string prefix, int curBodySize,
// Get Content-Length and Content-Encoding
// string contentEncoding = "";
// string contentLength = "";
int parsedContentLength = 100000;
int parsedContentLength = requestMaxBodySize - 1;
// reqHeaders.TryGetValue("Content-Encoding", out contentEncoding);
// reqHeaders.TryGetValue("Content-Length", out contentLength);
// int.TryParse(contentLength, out parsedContentLength);
string requestContentType = request.ContentType;
string contentEncoding = GetContentLengthAndEncoding(reqHeaders, out parsedContentLength); // Get the content-length
string contentEncoding = GetContentLengthAndEncoding(reqHeaders, parsedContentLength, out parsedContentLength); // Get the content-length

// RequestBody
string body = null;
Expand Down
4 changes: 2 additions & 2 deletions Moesif.Middleware/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.1.0")]
[assembly: AssemblyFileVersion("3.1.0")]
[assembly: AssemblyVersion("3.1.1")]
[assembly: AssemblyFileVersion("3.1.1")]

0 comments on commit ac5eec8

Please sign in to comment.