Fix buffer leak in HttpObjectEncoder when content is always empty #12828
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.
Motivation:
When the HttpObjectEncoder.encodeAndClose method encodes a response which content is always empty (as described in _HttpObjectEncoder. isContentAlwaysEmpty ), then the empty last http content or the empty FullHttpResponse is not closed.
An http response content is always empty for example when status code is INFORMATIONAL, is a 204, 205, 304 ... See for example here
Example scenario:
a DefaultHttpResponse is first encoded with status 204, in this case, the HttpObjectEncoder.encodeAndClose method will initialize its state to ST_CONTENT_ALWAYS_EMPTY, see here.
next, an EmptyLastHttpContent is encoded. So, the HttpObjectEncoder.encodeAndClose state being in ST_CONTENT_ALWAYS_EMPTY, then the encodeAndClose method switch statement will proceed here directly, which is not closing the EmptyLastHttpContent
Modification:
Move the code which was disposing the message from here to the case on ST_CONTENT_ALWAYS_EMPTY here.
Also added some test cases in HttpResponseEncoderTest.
Result:
Disposing the message to the case on ST_CONTENT_ALWAYS_EMPTY will continue to ensure that the message is closed when the state is ST_CONTENT_NON_CHUNK and when content length is zero (because the case fall-through, see here), and also ensures that any HttpContent is closed when state is ST_CONTENT_ALWAYS_EMPTY.