Skip to content

Commit

Permalink
Setting requestMaxBodySize and responseMaxBodySize as separate user c…
Browse files Browse the repository at this point in the history
…onfigurable variables
  • Loading branch information
bakennedy committed Nov 18, 2024
1 parent 3cc9353 commit 1137f93
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,16 @@ Override the following parameters, if needed.
| -- | -- | -- | -- |
| No | boolean | true | Whether to log request and response body to Moesif. |

#### `maxBodySize`
| Required | Type | Default Value | Description |
| -- | -- | -- | -- |
| No | Number | 10000 | The maximum request or response body size in bytes to log when sending the data to Moesif. |
#### `requestMaxBodySize`
| Required | Type | Default Value | Description |
| -- | -- |---------------|----------------------------------------------------------------------------------------------|
| No | Number | 1,048,576 | The maximum request body size in bytes to log when sending the data to Moesif. Default 1 MiB |

#### `responseMaxBodySize`
| Required | Type | Default Value | Description |
| -- | -- |---------------|---------------------------------------------------------------------------------|
| No | Number | 1,048,576 | The maximum response body size in bytes to log when sending the data to Moesif. Default 1 MiB |


### Interface Methods
Override following methods, if needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class MoesifConfiguration {
public int queueSize = 1000000; // maximum queue capacity to hold events.
public int retry = 0; // how many times to retry, if fails to post events.ß
public int updateConfigTime = 5 * 60; // in seconds - time to update app config periodically.
public long maxBodySize = 1024 * 1024; // in bytes - max body size to log.
// Maximum size of request/response body to be logged. Events with a larger body will still be logged as normal,
// but the body contents are replaced with an indicator that the body was too large
public long requestMaxBodySize = 1024 * 1024; // in bytes
public long responseMaxBodySize = 1024 * 1024; // in bytes

public String identifyUser(HttpServletRequest request, HttpServletResponse response) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ private EventRequestModel getEventRequestModel(LoggingHttpServletRequestWrapper
eventRequestBuilder.transferEncoding(bodyWrapper.transferEncoding);
}
if (requestWrapper.bodySkipped) {
eventRequestBuilder.body(BodyHandler.getLargeBodyError(requestWrapper.contentLength, config.maxBodySize));
eventRequestBuilder.body(BodyHandler.getLargeBodyError(requestWrapper.contentLength, config.requestMaxBodySize));
}
}

Expand All @@ -500,7 +500,7 @@ private EventResponseModel getEventResponseModel(LoggingHttpServletResponseWrapp
eventResponseBuilder.transferEncoding(bodyWrapper.transferEncoding);
}
if (responseWrapper.bodySkipped) {
eventResponseBuilder.body(BodyHandler.getLargeBodyError(responseWrapper.contentLength, config.maxBodySize));
eventResponseBuilder.body(BodyHandler.getLargeBodyError(responseWrapper.contentLength, config.requestMaxBodySize));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class LoggingHttpServletRequestWrapper extends HttpServletRequestWrapper
public boolean bodySkipped = false;
public long contentLength = 0;
private byte[] content;
private MoesifConfiguration config;
private final MoesifConfiguration config;

public LoggingHttpServletRequestWrapper(HttpServletRequest request, MoesifConfiguration config) {
super(request);
Expand Down Expand Up @@ -51,7 +51,7 @@ private void updateContentLength(long length) {
private boolean shouldSkipBody() {
readContentLength();
// should skip if we are not logging body by config or content length is greater than max body size
return !BodyHandler.logBody || contentLength > config.maxBodySize;
return !BodyHandler.logBody || contentLength > config.requestMaxBodySize;
}

@Override
Expand All @@ -70,14 +70,14 @@ public String getContent() {
}

if (this.parameterMap.isEmpty()) {
content = boundedStreamToArray(delegate.getInputStream(), config.maxBodySize);
content = boundedStreamToArray(delegate.getInputStream(), config.requestMaxBodySize);
if (content == null) {
bodySkipped = true;
return null;
}
} else {
content = getContentFromParameterMap(this.parameterMap);
if (content.length > config.maxBodySize) {
if (content.length > config.requestMaxBodySize) {
bodySkipped = true;
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void readContentLength() {
private boolean shouldSkipBody() {
readContentLength();
// should skip if we are not logging body by config or content length is greater than max body size
return !BodyHandler.logBody || contentLength > config.maxBodySize;
return !BodyHandler.logBody || contentLength > config.responseMaxBodySize;
}

public String getContent() {
Expand Down Expand Up @@ -142,7 +142,7 @@ public void setWriteListener(WriteListener writeListener) {
public void write(int b) throws IOException {
outputStream.write(b);
if (!bufferExceeded) {
if (baos.size() < config.maxBodySize) {
if (baos.size() < config.responseMaxBodySize) {
baos.write(b);
} else {
bufferExceeded = true;
Expand All @@ -156,7 +156,7 @@ public void write(int b) throws IOException {
public void write(byte[] b, int off, int len) throws IOException {
outputStream.write(b, off, len);
if (!bufferExceeded) {
if (baos.size() + len <= config.maxBodySize) {
if (baos.size() + len <= config.responseMaxBodySize) {
baos.write(b, off, len);
} else {
bufferExceeded = true;
Expand Down

0 comments on commit 1137f93

Please sign in to comment.