Skip to content

Commit 4f4d2ef

Browse files
praves77keyur9
andauthored
#188545343 : Added support for maxBodySize and fixed logBody (#97)
* [preNet60] #188545343 : Added support for maxBodySize and fixed logBody * Add missing init maxBodySize options. * Use latest base API and updated README for NET6 support version. --------- Co-authored-by: Keyur <keyur.kv@gmail.com>
1 parent a5cc647 commit 4f4d2ef

File tree

8 files changed

+338
-844
lines changed

8 files changed

+338
-844
lines changed

Moesif.Middleware/Helpers/Compression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Moesif.Middleware.Helpers
99
{
1010
class Compression
1111
{
12-
public static async Task<string> UncompressStream(Stream memoryStream, string contentEncoding, int bufferSize)
12+
public static async Task<string> UncompressStream(Stream memoryStream, string contentEncoding, int bufferSize, bool logBody)
1313
{
14-
if (!memoryStream.CanRead)
14+
if (!logBody || !memoryStream.CanRead)
1515
{
1616
return null;
1717
}

Moesif.Middleware/Moesif.Middleware.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>Moesif.Middleware</id>
5-
<version>1.5.1</version>
5+
<version>1.5.2</version>
66
<title>MoesifMiddleware</title>
77
<authors>Moesif</authors>
88
<owners>Moesif</owners>
@@ -28,7 +28,7 @@
2828
</group>
2929
<group targetFramework="net461">
3030
<dependency id="Microsoft.Owin" version="4.2.2" />
31-
<dependency id="Moesif.Api" version="2.0.8" />
31+
<dependency id="Moesif.Api" version="2.0.9" />
3232
<dependency id="System.ValueTuple" version="4.5.0" />
3333
<dependency id="Newtonsoft.Json" version="13.0.2" />
3434
<dependency id="System.IdentityModel.Tokens.Jwt" version="6.35.0" />

Moesif.Middleware/NetCore/Helpers/LoggerHelper.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,23 @@ public Dictionary<string, string> ToHeaders(IHeaderDictionary headers, bool deb
154154
return copyHeaders;
155155
}
156156

157-
public async Task<string> GetRequestContents(string bodyAsText, HttpRequest request, string contentEncoding, int parsedContentLength, bool debug)
157+
public string SanitizeRequestBodyForMaxSize(string bodyString, int maxBodySize)
158+
{
159+
if (!string.IsNullOrWhiteSpace(bodyString) && bodyString.Length > maxBodySize)
160+
{
161+
// if request body, set the message.
162+
var jsonObject = new { msg = $"Exceeded-Max-Body-Size {bodyString.Length}" };
163+
bodyString = ApiHelper.JsonSerialize(jsonObject);
164+
}
165+
166+
return bodyString;
167+
}
168+
169+
public async Task<string> GetRequestContents(string bodyAsText, HttpRequest request, string contentEncoding, int parsedContentLength, bool debug, bool logBody)
158170
{
159171
try
160172
{
161-
bodyAsText = await Compression.UncompressStream(request.Body, contentEncoding, parsedContentLength);
173+
bodyAsText = await Compression.UncompressStream(request.Body, contentEncoding, parsedContentLength, logBody);
162174
request.Body.Position = 0;
163175
}
164176
catch (Exception)

Moesif.Middleware/NetCore/MoesifMiddlewareNetCore.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
using Moesif.Api.Exceptions;
1313
using System.Threading;
1414
using System.Collections.Concurrent;
15+
using System.Net.Http;
1516
using Moesif.Middleware.Helpers;
1617
using Moesif.Middleware.Models;
1718
using Microsoft.Extensions.Logging;
19+
using Newtonsoft.Json;
1820

1921
#if NETCORE
2022
using Microsoft.AspNetCore.Http;
@@ -75,6 +77,8 @@ public class MoesifMiddlewareNetCore
7577
public bool debug = false;
7678

7779
public bool logBody;
80+
public int requestMaxBodySize = 1000;
81+
public int responseMaxBodySize = 1000;
7882

7983
public bool isLambda;
8084

@@ -136,6 +140,8 @@ public MoesifMiddlewareNetCore(RequestDelegate next, Dictionary<string, object>
136140
debug = loggerHelper.GetConfigBoolValues(moesifOptions, "LocalDebug", false);
137141
client = new MoesifApiClient(moesifOptions["ApplicationId"].ToString(), APP_VERSION, debug);
138142
logBody = loggerHelper.GetConfigBoolValues(moesifOptions, "LogBody", true);
143+
requestMaxBodySize = loggerHelper.GetConfigIntValues(moesifOptions, "RequestMaxBodySize", 100000);
144+
responseMaxBodySize = loggerHelper.GetConfigIntValues(moesifOptions, "ResponseMaxBodySize", 100000);
139145
isLambda = loggerHelper.GetConfigBoolValues(moesifOptions, "IsLambda", false);
140146
_next = next;
141147
//config = AppConfig.getDefaultAppConfig();
@@ -576,6 +582,14 @@ public async Task Invoke(HttpContext httpContext)
576582
#endif
577583
}
578584

585+
public static string GetExceededBodyForBodySize(string prefix, int curBodySize, int maxBodySize)
586+
{
587+
object payload = new { msg = $"{prefix}.body.length {curBodySize} exceeded {prefix}MaxBodySize of {maxBodySize}" };
588+
string bodyPayload = ApiHelper.JsonSerialize(payload);
589+
590+
return bodyPayload;
591+
}
592+
579593
private async Task<(EventRequestModel, String)> FormatRequest(HttpRequest request, string transactionId)
580594
{
581595
#if MOESIF_INSTRUMENT
@@ -611,7 +625,25 @@ public async Task Invoke(HttpContext httpContext)
611625
setHeaderEnableBufferingTime = stopwatch.ElapsedMilliseconds;
612626
stopwatch.Restart();
613627
#endif
614-
bodyAsText = await loggerHelper.GetRequestContents(bodyAsText, request, contentEncoding, parsedContentLength, debug);
628+
// Check if body exceeded max size supported or no body content
629+
if (parsedContentLength == 0)
630+
{
631+
bodyAsText = null;
632+
}
633+
else if (parsedContentLength > requestMaxBodySize)
634+
{
635+
bodyAsText = GetExceededBodyForBodySize("request", parsedContentLength, requestMaxBodySize);
636+
}
637+
else
638+
{
639+
bodyAsText = await loggerHelper.GetRequestContents(bodyAsText, request, contentEncoding, parsedContentLength, debug, logBody);
640+
}
641+
642+
// Check if body exceeded max size supported
643+
if (!string.IsNullOrWhiteSpace(bodyAsText) && bodyAsText.Length > requestMaxBodySize)
644+
{
645+
bodyAsText = GetExceededBodyForBodySize("request", bodyAsText.Length, requestMaxBodySize);
646+
}
615647
#if MOESIF_INSTRUMENT
616648
getRequestContent = stopwatch.ElapsedMilliseconds;
617649
stopwatch.Restart();
@@ -683,6 +715,11 @@ private EventResponseModel FormatResponse(HttpResponse response, StreamHelper st
683715
string contentEncoding = "";
684716
rspHeaders.TryGetValue("Content-Encoding", out contentEncoding);
685717
string text = stream.ReadStream(contentEncoding);
718+
// Check if response body exceeded max size supported
719+
if (!string.IsNullOrWhiteSpace(text) && text.Length > responseMaxBodySize)
720+
{
721+
text = GetExceededBodyForBodySize("response", text.Length, responseMaxBodySize);
722+
}
686723
// Serialize Response body
687724
responseWrapper = loggerHelper.Serialize(text, response.ContentType, logBody, debug);
688725
}

Moesif.Middleware/NetFramework/Helpers/LoggerHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ public string GetOutputFilterStreamContents(StreamHelper filter, string contentE
142142
return null;
143143
}
144144

145-
public async Task<string> GetRequestContents(IOwinRequest request, string contentEncoding, int bufferSize, bool disableStreamOverride)
145+
public async Task<string> GetRequestContents(IOwinRequest request, string contentEncoding, int bufferSize, bool disableStreamOverride, bool logBody)
146146
{
147-
if (request == null || request.Body == null || !request.Body.CanRead)
147+
if (!logBody || request == null || request.Body == null || !request.Body.CanRead)
148148
{
149149
return string.Empty;
150150
}
@@ -158,7 +158,7 @@ public async Task<string> GetRequestContents(IOwinRequest request, string conte
158158
else {
159159
request.Body = memoryStream;
160160
}
161-
return await Compression.UncompressStream(memoryStream, contentEncoding, bufferSize);
161+
return await Compression.UncompressStream(memoryStream, contentEncoding, bufferSize, logBody);
162162
}
163163

164164
public void LogDebugMessage(bool debug, String msg)

Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class MoesifMiddlewareNetFramework : OwinMiddleware
6363
public string apiVersion;
6464

6565
public bool logBody;
66+
public int requestMaxBodySize = 1000;
67+
public int responseMaxBodySize = 1000;
6668

6769
private AutoResetEvent configEvent;
6870

@@ -100,6 +102,8 @@ public MoesifMiddlewareNetFramework(OwinMiddleware next, Dictionary<string, obje
100102
debug = loggerHelper.GetConfigBoolValues(moesifOptions, "LocalDebug", false);
101103
client = new MoesifApiClient(moesifOptions["ApplicationId"].ToString(), "moesif-netframework/1.5.1", debug);
102104
logBody = loggerHelper.GetConfigBoolValues(moesifOptions, "LogBody", true);
105+
requestMaxBodySize = loggerHelper.GetConfigIntValues(moesifOptions, "RequestMaxBodySize", 100000);
106+
responseMaxBodySize = loggerHelper.GetConfigIntValues(moesifOptions, "ResponseMaxBodySize", 100000);
103107
isBatchingEnabled = loggerHelper.GetConfigBoolValues(moesifOptions, "EnableBatching", true); // Enable batching
104108
disableStreamOverride = loggerHelper.GetConfigBoolValues(moesifOptions, "DisableStreamOverride", false); // Reset Request Body position
105109
batchSize = loggerHelper.GetConfigIntValues(moesifOptions, "BatchSize", 200); // Batch Size
@@ -267,7 +271,7 @@ public async override Task Invoke(IOwinContext httpContext)
267271
EventRequestModel request;
268272

269273
// Prepare Moeif Event Request Model
270-
(request, transactionId) = await ToRequest(httpContext.Request, transactionId);
274+
(request, transactionId) = await ToRequest(httpContext.Request, transactionId, logBody, requestMaxBodySize);
271275

272276
// Add Transaction Id to the Response Header
273277
if (!string.IsNullOrEmpty(transactionId))
@@ -363,8 +367,16 @@ private String getUserId(IOwinContext httpContext, EventRequestModel request)
363367
return userId;
364368
}
365369
}
370+
371+
public static string GetExceededBodyForBodySize(string prefix, int curBodySize, int maxBodySize)
372+
{
373+
object payload = new { msg = $"{prefix}.body.length {curBodySize} exceeded {prefix}MaxBodySize of {maxBodySize}" };
374+
string bodyPayload = ApiHelper.JsonSerialize(payload);
375+
376+
return bodyPayload;
377+
}
366378

367-
private async Task<(EventRequestModel, String)> ToRequest(IOwinRequest request, string transactionId)
379+
private async Task<(EventRequestModel, String)> ToRequest(IOwinRequest request, string transactionId, bool logBody, int maxBodySize)
368380
{
369381
// Request headers
370382
var reqHeaders = loggerHelper.ToHeaders(request.Headers, debug);
@@ -380,7 +392,24 @@ private String getUserId(IOwinContext httpContext, EventRequestModel request)
380392
int.TryParse(contentLength, out parsedContentLength);
381393
try
382394
{
383-
body = await loggerHelper.GetRequestContents(request, contentEncoding, parsedContentLength, disableStreamOverride);
395+
// Check if body exceeded max size supported or no body content
396+
if (parsedContentLength == 0)
397+
{
398+
body = null;
399+
}
400+
else if (parsedContentLength > requestMaxBodySize)
401+
{
402+
body = GetExceededBodyForBodySize("request", parsedContentLength, requestMaxBodySize);
403+
}
404+
else
405+
{
406+
body = await loggerHelper.GetRequestContents(request, contentEncoding, parsedContentLength, disableStreamOverride, logBody);
407+
}
408+
// Check if body exceeded max size supported
409+
if (!string.IsNullOrWhiteSpace(body) && body.Length > requestMaxBodySize)
410+
{
411+
body = GetExceededBodyForBodySize("request", body.Length, requestMaxBodySize);
412+
}
384413
}
385414
catch
386415
{
@@ -424,7 +453,12 @@ private EventResponseModel ToResponse(IOwinResponse response, StreamHelper outpu
424453
string contentEncoding = "";
425454
rspHeaders.TryGetValue("Content-Encoding", out contentEncoding);
426455

427-
var body = loggerHelper.GetOutputFilterStreamContents(outputStream, contentEncoding, logBody);
456+
var body = loggerHelper.GetOutputFilterStreamContents(outputStream, contentEncoding, logBody);
457+
// Check if body exceeded max size supported
458+
if (!string.IsNullOrWhiteSpace(body) && body.Length > responseMaxBodySize)
459+
{
460+
body = GetExceededBodyForBodySize("response", body.Length, responseMaxBodySize);
461+
}
428462
var bodyWrapper = loggerHelper.Serialize(body, response.ContentType, logBody);
429463

430464
// Add Transaction Id to Response Header

Moesif.Middleware/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
// You can specify all the values or you can default the Build and Revision Numbers
2424
// by using the '*' as shown below:
2525
// [assembly: AssemblyVersion("1.0.*")]
26-
[assembly: AssemblyVersion("1.5.1")]
27-
[assembly: AssemblyFileVersion("1.5.1")]
26+
[assembly: AssemblyVersion("1.5.2")]
27+
[assembly: AssemblyFileVersion("1.5.2")]

0 commit comments

Comments
 (0)