Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ The following tables show the available configuration (sorted alphabetically):
| `logbook.client.predicate.exclude` | Exclude certain paths and methods for outgoing requests (overrides `logbook.client.predicate.include`) | `[]` |
| `logbook.filter.enabled` | Enable the [`LogbookFilter`](#servlet) | `true` |
| `logbook.filter.form-request-mode` | Determines how [form requests](#form-requests) are handled | `body` |
| `logbook.filter.max-buffer-size` | Maximum amount of request/response body data to buffer in memory before discarding capture and streaming directly | `-1` (disabled) |
| `logbook.filters.body.default-enabled` | Enables/disables default body filters that are collected by java.util.ServiceLoader | `true` |
| `logbook.format.style` | [Formatting style](#formatting) (`http`, `json`, `curl` or `splunk`) | `json` |
| `logbook.httpclient.decompress-response` | Enables/disables additional decompression process for HttpClient with gzip encoded body (to logging purposes only). This means extra decompression and possible performance impact. | `false` (disabled) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ static byte[] toByteArray(final InputStream in) throws IOException {
return out.toByteArray();
}

static byte[] toByteArray(final InputStream in, final long maxBytes) throws IOException {
if (maxBytes < 0 || maxBytes == Long.MAX_VALUE) {
return toByteArray(in);
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final byte[] buf = new byte[4096];
long totalRead = 0;
int bytesRead;

while (totalRead <= maxBytes && (bytesRead = in.read(buf, 0, (int) Math.min(buf.length, maxBytes + 1 - totalRead))) != -1) {
out.write(buf, 0, bytesRead);
totalRead += bytesRead;
}
return out.toByteArray();
}

static void copy(final InputStream from, final OutputStream to) throws IOException {
Objects.requireNonNull(from);
Objects.requireNonNull(to);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

final class LocalResponse extends HttpServletResponseWrapper implements HttpResponse {

private final AtomicReference<State> state = new AtomicReference<>(new Unbuffered());
private final AtomicReference<State> state;

private final String protocolVersion;

Expand Down Expand Up @@ -72,25 +72,31 @@ default byte[] getBody() {

}

@AllArgsConstructor
private static final class Unbuffered implements State {

private final long maxBufferSize;

@Override
public State with() {
return new Offering();
return new Offering(maxBufferSize);
}

}

@AllArgsConstructor
private static final class Offering implements State {

private final long maxBufferSize;

@Override
public State without() {
return new Unbuffered();
return new Unbuffered(maxBufferSize);
}

@Override
public State buffer(final ServletResponse response) throws IOException {
final Tee tee = new Tee(response.getOutputStream());
final Tee tee = new Tee(response.getOutputStream(), maxBufferSize);
return new Buffering(tee);
}

Expand Down Expand Up @@ -151,8 +157,13 @@ public State with() {
}

LocalResponse(final HttpServletResponse response, final String protocolVersion) {
this(response, protocolVersion, -1L);
}

LocalResponse(final HttpServletResponse response, final String protocolVersion, final long maxBufferSize) {
super(response);
this.protocolVersion = protocolVersion;
this.state = new AtomicReference<>(new Unbuffered(maxBufferSize));
}

@Override
Expand Down Expand Up @@ -241,9 +252,9 @@ private static class Tee {
private PrintWriter writer;
private byte[] bytes;

private Tee(final ServletOutputStream original) {
private Tee(final ServletOutputStream original, final long maxBufferSize) {
this.branch = new ByteArrayOutputStream();
this.output = new TeeServletOutputStream(original, branch);
this.output = new TeeServletOutputStream(original, branch, maxBufferSize);
}

ServletOutputStream getOutputStream() {
Expand All @@ -267,40 +278,71 @@ void flush() throws IOException {

byte[] getBytes() {
if (bytes == null) {
bytes = branch.toByteArray();
if (output.isExceeded()) {
bytes = new byte[0];
} else {
bytes = branch.toByteArray();
}
}
return bytes;
}
}

@AllArgsConstructor
private static class TeeServletOutputStream extends ServletOutputStream {

private final ServletOutputStream original;
private final OutputStream branch;
private final ByteArrayOutputStream branch;
private final long maxBufferSize;
@Getter
private volatile boolean exceeded;

TeeServletOutputStream(final ServletOutputStream original, final ByteArrayOutputStream branch, final long maxBufferSize) {
this.original = original;
this.branch = branch;
this.maxBufferSize = maxBufferSize;
this.exceeded = false;
}

@Override
public void write(final int b) throws IOException {
original.write(b);
branch.write(b);
if (!exceeded) {
if (maxBufferSize >= 0 && branch.size() >= maxBufferSize) {
exceeded = true;
branch.reset();
} else {
branch.write(b);
}
}
}

@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
original.write(b, off, len);
branch.write(b, off, len);
if (!exceeded) {
if (maxBufferSize >= 0 && (long) branch.size() + len > maxBufferSize) {
exceeded = true;
branch.reset();
} else {
branch.write(b, off, len);
}
}
}

@Override
public void flush() throws IOException {
original.flush();
branch.flush();
if (!exceeded) {
branch.flush();
}
}

@Override
public void close() throws IOException {
original.close();
branch.close();
if (!exceeded) {
branch.close();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public final class LogbookFilter implements HttpFilter {
@With
private final AsyncOnCompleteListenerWrapper asyncOnCompleteListenerWrapper;

@With
private final long maxBufferSize;

public LogbookFilter() {
this(Logbook.create());
}
Expand All @@ -54,14 +57,25 @@ public LogbookFilter(final Logbook logbook) {
}

public LogbookFilter(final Logbook logbook, @Nullable final Strategy strategy) {
this(logbook, strategy, FormRequestMode.fromProperties(), AsyncOnCompleteListenerWrapper.identity());
this(logbook, strategy, FormRequestMode.fromProperties(), AsyncOnCompleteListenerWrapper.identity(), maxBufferSizeFromProperties());
}

static long maxBufferSizeFromProperties() {
@Nullable String property = System.getProperty("logbook.servlet.max-buffer-size");
if (property == null) {
property = System.getProperty("logbook.filter.max-buffer-size");
}
if (property == null) {
return -1L;
}
return Long.parseLong(property.trim());
}

@Override
public void doFilter(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse,
final FilterChain chain) throws ServletException, IOException {
final RemoteRequest request = new RemoteRequest(httpRequest, formRequestMode);
final LocalResponse response = new LocalResponse(httpResponse, request.getProtocolVersion());
final RemoteRequest request = new RemoteRequest(httpRequest, formRequestMode, maxBufferSize);
final LocalResponse response = new LocalResponse(httpResponse, request.getProtocolVersion(), maxBufferSize);

final ResponseProcessingStage processing;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,21 @@ private static final class Unbuffered implements State {

private final FormRequestMode formRequestMode;
private final Charset charset;
private final long maxBufferSize;

@Override
public State with() {
return new Offering(formRequestMode, charset);
return new Offering(formRequestMode, charset, maxBufferSize);
}

@Override
public State without() {
return new Withouted(formRequestMode, charset);
return new Withouted(formRequestMode, charset, maxBufferSize);
}

@Override
public State buffer(final ServletRequest request) throws IOException {
return doBuffer(request, formRequestMode, charset);
return doBuffer(request, formRequestMode, charset, maxBufferSize);
}

}
Expand All @@ -195,10 +196,11 @@ private static final class Withouted implements State {

private final FormRequestMode formRequestMode;
private final Charset charset;
private final long maxBufferSize;

@Override
public State with() {
return new Offering(formRequestMode, charset);
return new Offering(formRequestMode, charset, maxBufferSize);
}

}
Expand All @@ -208,18 +210,23 @@ private static final class Offering implements State {

private final FormRequestMode formRequestMode;
private final Charset charset;
private final long maxBufferSize;

@Override
public State buffer(final ServletRequest request) throws IOException {
return doBuffer(request, formRequestMode, charset);
return doBuffer(request, formRequestMode, charset, maxBufferSize);
}
}

private static State doBuffer(final ServletRequest request, final FormRequestMode formRequestMode, final Charset charset) throws IOException {
private static State doBuffer(final ServletRequest request, final FormRequestMode formRequestMode, final Charset charset, final long maxBufferSize) throws IOException {
if (isFormRequest(request)) {
switch (formRequestMode) {
case PARAMETER:
return new Buffering(reconstructFormBody(request, charset));
final byte[] formBody = reconstructFormBody(request, charset);
if (maxBufferSize >= 0 && formBody.length > maxBufferSize) {
return new Passing();
}
return new Buffering(formBody);
case OFF:
return new Passing();
default:
Expand All @@ -229,7 +236,14 @@ private static State doBuffer(final ServletRequest request, final FormRequestMod
if (isMultipartRequest(request)) {
return new Passing();
}
return new Buffering(toByteArray(request.getInputStream()));
if (maxBufferSize >= 0 && request.getContentLengthLong() > maxBufferSize) {
return new Passing();
}
final byte[] bytes = toByteArray(request.getInputStream(), maxBufferSize);
if (maxBufferSize >= 0 && bytes.length > maxBufferSize) {
return new Exceeded(bytes);
}
return new Buffering(bytes);
}

private static boolean isFormRequest(final ServletRequest request) {
Expand Down Expand Up @@ -327,9 +341,37 @@ private static final class Passing implements State {

}

private static final class Exceeded implements State {

private final byte[] prefix;
private ServletInputStream stream;

Exceeded(final byte[] prefix) {
this.prefix = prefix;
}

@Override
public synchronized ServletInputStream getInputStream(final ServletRequest request) throws IOException {
if (stream == null) {
stream = new SequenceServletInputStream(prefix, request.getInputStream());
}
return stream;
}

@Override
public byte[] getBody() {
return new byte[0];
}

}

RemoteRequest(final HttpServletRequest request, final FormRequestMode formRequestMode) {
this(request, formRequestMode, -1L);
}

RemoteRequest(final HttpServletRequest request, final FormRequestMode formRequestMode, final long maxBufferSize) {
super(request);
this.state = new AtomicReference<>(new Unbuffered(formRequestMode, getCharset()));
this.state = new AtomicReference<>(new Unbuffered(formRequestMode, getCharset(), maxBufferSize));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public SecureLogbookFilter withAsyncOnCompleteListenerWrapper(
return new SecureLogbookFilter(filter.withAsyncOnCompleteListenerWrapper(asyncOnCompleteListenerWrapper));
}

public SecureLogbookFilter withMaxBufferSize(final long maxBufferSize) {
return new SecureLogbookFilter(filter.withMaxBufferSize(maxBufferSize));
}

@Override
public void doFilter(final HttpServletRequest request, final HttpServletResponse response,
final FilterChain chain) throws ServletException, IOException {
Expand Down
Loading
Loading