diff --git a/Moesif.Middleware/Moesif.Middleware.nuspec b/Moesif.Middleware/Moesif.Middleware.nuspec index 9315191..42367d1 100644 --- a/Moesif.Middleware/Moesif.Middleware.nuspec +++ b/Moesif.Middleware/Moesif.Middleware.nuspec @@ -2,7 +2,7 @@ Moesif.Middleware - 3.1.0 + 3.1.1 MoesifMiddleware Moesif Moesif diff --git a/Moesif.Middleware/NetCore/MoesifMiddlewareNetCore.cs b/Moesif.Middleware/NetCore/MoesifMiddlewareNetCore.cs index 7057632..4ea5ea7 100644 --- a/Moesif.Middleware/NetCore/MoesifMiddlewareNetCore.cs +++ b/Moesif.Middleware/NetCore/MoesifMiddlewareNetCore.cs @@ -369,6 +369,27 @@ public void UpdateCompaniesBatch(List> 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 @@ -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 @@ -603,10 +615,9 @@ public async Task Invoke(HttpContext httpContext) #endif } - public static string GetContentLengthAndEncoding(Dictionary headers, out int parsedContentLength) + public static string GetContentLengthAndEncoding(Dictionary headers, int defaultLength, out int parsedContentLength) { string contentEncoding = ""; - parsedContentLength = 100000; if (headers != null) { @@ -615,6 +626,10 @@ public static string GetContentLengthAndEncoding(Dictionary head headers.TryGetValue("Content-Encoding", out contentEncoding); int.TryParse(contentLength, out parsedContentLength); } + else + { + parsedContentLength = defaultLength; + } return contentEncoding; } @@ -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); @@ -798,6 +813,7 @@ private async Task 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 diff --git a/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs b/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs index ac90dcb..6865918 100644 --- a/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs +++ b/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs @@ -254,31 +254,42 @@ public void UpdateCompaniesBatch(List> 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; @@ -396,10 +407,9 @@ public string GetClientIp(IOwinRequest request) return ipAddress; } - public static string GetContentLengthAndEncoding(Dictionary headers, out int parsedContentLength) + public static string GetContentLengthAndEncoding(Dictionary headers, int defaultLength, out int parsedContentLength) { string contentEncoding = ""; - parsedContentLength = 100000; if (headers != null) { @@ -408,6 +418,10 @@ public static string GetContentLengthAndEncoding(Dictionary head headers.TryGetValue("Content-Encoding", out contentEncoding); int.TryParse(contentLength, out parsedContentLength); } + else + { + parsedContentLength = defaultLength; + } return contentEncoding; } @@ -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; diff --git a/Moesif.Middleware/Properties/AssemblyInfo.cs b/Moesif.Middleware/Properties/AssemblyInfo.cs index dd89541..a2ad50d 100644 --- a/Moesif.Middleware/Properties/AssemblyInfo.cs +++ b/Moesif.Middleware/Properties/AssemblyInfo.cs @@ -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")]