Repository navigation
fix: bound servlet response validation cache #435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
visheshupadhyay
wants to merge
2
commits into
getyourguide:main
Choose a base branch
from
visheshupadhyay:fix/servlet-bounded-response-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
...java/com/getyourguide/openapi/validation/filter/BoundedContentCachingResponseWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| package com.getyourguide.openapi.validation.filter; | ||
|
|
||
| import jakarta.servlet.ServletOutputStream; | ||
| import jakarta.servlet.WriteListener; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStreamWriter; | ||
| import java.io.PrintWriter; | ||
| import java.io.UncheckedIOException; | ||
| import java.io.Writer; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.web.util.ContentCachingResponseWrapper; | ||
|
|
||
| /** | ||
| * Caches a response body up to a fixed limit. Once the limit is exceeded, it writes the cached bytes and all | ||
| * subsequent bytes directly to the underlying response. | ||
| */ | ||
| public class BoundedContentCachingResponseWrapper extends ContentCachingResponseWrapper { | ||
| private final int cacheLimitBytes; | ||
| private final ByteArrayOutputStream content = new ByteArrayOutputStream(); | ||
|
|
||
| private ServletOutputStream outputStream; | ||
| private PrintWriter writer; | ||
| private ResettableWriter writerDelegate; | ||
| private boolean contentOverflowed; | ||
| private boolean bodyCopied; | ||
|
|
||
| public BoundedContentCachingResponseWrapper(HttpServletResponse response, int cacheLimitBytes) { | ||
| super(response); | ||
| if (cacheLimitBytes < 1) { | ||
| throw new IllegalArgumentException("cacheLimitBytes must be positive"); | ||
| } | ||
| this.cacheLimitBytes = cacheLimitBytes; | ||
| } | ||
|
|
||
| public boolean isContentOverflowed() { | ||
| return contentOverflowed; | ||
| } | ||
|
|
||
| public void flushContent() { | ||
| flushWriter(); | ||
| } | ||
|
|
||
| @Override | ||
| public ServletOutputStream getOutputStream() { | ||
| if (writer != null) { | ||
| throw new IllegalStateException( | ||
| "This response is already being written as text. Continue using getWriter() instead of getOutputStream()." | ||
| ); | ||
| } | ||
| if (outputStream == null) { | ||
| outputStream = new ResponseServletOutputStream(); | ||
| } | ||
| return outputStream; | ||
| } | ||
|
|
||
| @Override | ||
| public PrintWriter getWriter() { | ||
| if (writer != null) { | ||
| return writer; | ||
| } | ||
| if (outputStream != null) { | ||
| throw new IllegalStateException( | ||
| "This response is already being written as bytes. Continue using getOutputStream() instead of getWriter()." | ||
| ); | ||
| } | ||
| var encoding = getCharacterEncoding(); | ||
| var charset = encoding != null ? Charset.forName(encoding) : StandardCharsets.ISO_8859_1; | ||
| outputStream = getOutputStream(); | ||
| writerDelegate = new ResettableWriter(outputStream, charset); | ||
| writer = new PrintWriter(writerDelegate); | ||
| return writer; | ||
| } | ||
|
|
||
| @Override | ||
| public void flushBuffer() throws IOException { | ||
| if (writer != null) { | ||
| writer.flush(); | ||
| } | ||
| if (contentOverflowed) { | ||
| getNativeResponse().flushBuffer(); | ||
| } | ||
| } | ||
|
|
||
| public byte[] getContentAsByteArray() { | ||
| flushWriter(); | ||
| return content.toByteArray(); | ||
| } | ||
|
|
||
| public ByteArrayInputStream getContentInputStream() { | ||
| return new ByteArrayInputStream(getContentAsByteArray()); | ||
| } | ||
|
|
||
| public int getContentSize() { | ||
| flushWriter(); | ||
| return content.size(); | ||
| } | ||
|
|
||
| public void copyBodyToResponse() throws IOException { | ||
| flushWriter(); | ||
| if (bodyCopied) { | ||
| return; | ||
| } | ||
| if (!contentOverflowed && content.size() > 0) { | ||
| var response = getNativeResponse(); | ||
| if (!response.isCommitted() && response.getHeader(HttpHeaders.TRANSFER_ENCODING) == null) { | ||
| response.setContentLength(content.size()); | ||
| } | ||
| getDelegateOutputStream().write(content.toByteArray()); | ||
| content.reset(); | ||
| } | ||
| bodyCopied = true; | ||
| } | ||
|
|
||
| @Override | ||
| public void resetBuffer() { | ||
| clearContent(); | ||
| getNativeResponse().resetBuffer(); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| clearContent(); | ||
| getNativeResponse().reset(); | ||
| } | ||
|
|
||
| @Override | ||
| public void sendError(int status) throws IOException { | ||
| resetBuffer(); | ||
| getNativeResponse().sendError(status); | ||
| } | ||
|
|
||
| @Override | ||
| public void sendError(int status, String message) throws IOException { | ||
| resetBuffer(); | ||
| getNativeResponse().sendError(status, message); | ||
| } | ||
|
|
||
| @Override | ||
| public void sendRedirect(String location) throws IOException { | ||
| resetBuffer(); | ||
| getNativeResponse().sendRedirect(location); | ||
| } | ||
|
|
||
| private void clearContent() { | ||
| if (writerDelegate != null) { | ||
| writerDelegate.reset(); | ||
| } | ||
| content.reset(); | ||
| contentOverflowed = false; | ||
| bodyCopied = false; | ||
| } | ||
|
|
||
| private HttpServletResponse getNativeResponse() { | ||
| return (HttpServletResponse) getResponse(); | ||
| } | ||
|
|
||
| private ServletOutputStream getDelegateOutputStream() throws IOException { | ||
| return getNativeResponse().getOutputStream(); | ||
| } | ||
|
|
||
| private void flushWriter() { | ||
| if (writer != null) { | ||
| writer.flush(); | ||
| } | ||
| } | ||
|
|
||
| private void write(byte[] bytes, int offset, int length) throws IOException { | ||
| if (contentOverflowed) { | ||
| getDelegateOutputStream().write(bytes, offset, length); | ||
| return; | ||
| } | ||
|
|
||
| if (length <= cacheLimitBytes - content.size()) { | ||
| content.write(bytes, offset, length); | ||
| return; | ||
| } | ||
|
|
||
| contentOverflowed = true; | ||
| var responseOutputStream = getDelegateOutputStream(); | ||
| content.writeTo(responseOutputStream); | ||
| content.reset(); | ||
| responseOutputStream.write(bytes, offset, length); | ||
| } | ||
|
|
||
| private class ResponseServletOutputStream extends ServletOutputStream { | ||
| @Override | ||
| public boolean isReady() { | ||
| try { | ||
| return getDelegateOutputStream().isReady(); | ||
| } catch (IOException exception) { | ||
| throw new UncheckedIOException(exception); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setWriteListener(WriteListener writeListener) { | ||
| try { | ||
| getDelegateOutputStream().setWriteListener(writeListener); | ||
| } catch (IOException exception) { | ||
| throw new UncheckedIOException(exception); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(int value) throws IOException { | ||
| write(new byte[] {(byte) value}); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] bytes, int offset, int length) throws IOException { | ||
| BoundedContentCachingResponseWrapper.this.write(bytes, offset, length); | ||
| } | ||
| } | ||
|
|
||
| private static class ResettableWriter extends Writer { | ||
| private final ServletOutputStream outputStream; | ||
| private final Charset charset; | ||
| private OutputStreamWriter delegate; | ||
|
|
||
| private ResettableWriter(ServletOutputStream outputStream, Charset charset) { | ||
| this.outputStream = outputStream; | ||
| this.charset = charset; | ||
| this.delegate = new OutputStreamWriter(outputStream, charset); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(char[] characters, int offset, int length) throws IOException { | ||
| delegate.write(characters, offset, length); | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() throws IOException { | ||
| delegate.flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| delegate.close(); | ||
| } | ||
|
|
||
| private void reset() { | ||
| delegate = new OutputStreamWriter(outputStream, charset); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.