Skip to content
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

EOFException in HttpMessageConverterExtractor while extracting data of a empty GZiped response #28613

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.web.client;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
Expand Down Expand Up @@ -96,14 +98,20 @@ public boolean hasEmptyMessageBody() throws IOException {
}
}
else {
this.pushbackInputStream = new PushbackInputStream(body);
int b = this.pushbackInputStream.read();
if (b == -1) {
return true;
try {
this.pushbackInputStream = new PushbackInputStream(body);
int b = this.pushbackInputStream.read();
if (b == -1) {
return true;
}
else {
this.pushbackInputStream.unread(b);
return false;
}
}
else {
this.pushbackInputStream.unread(b);
return false;
catch (EOFException ex) {
this.pushbackInputStream = new PushbackInputStream(new ByteArrayInputStream(new byte[0]));
return true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.zip.GZIPInputStream;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -114,6 +116,22 @@ void emptyMessageBody() throws IOException {
assertThat(result).isNull();
}

@Test
void emptyLazyGzipMessageBody() throws IOException {
given(response.getStatusCode()).willReturn(HttpStatus.BAD_REQUEST);
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getBody()).willReturn(new InputStream() {
@Override
public int read() throws IOException {
// Simulates lazy gzip stream reading
return new GZIPInputStream(new ByteArrayInputStream("".getBytes())).read();
}
});

Object result = extractor.extractData(response);
assertThat(result).isNull();
}

@Test // gh-22265
void nullMessageBody() throws IOException {
given(response.getStatusCode()).willReturn(HttpStatus.OK);
Expand Down