Skip to content

Commit

Permalink
Allow BadMessageException to propagate unwrapped through HttpInput (#…
Browse files Browse the repository at this point in the history
…12571)

Allow BadMessageException to propagate unwrapped through HttpInput. Whilst Jetty handling will correctly unwrap it, 3rd party handling may not.
  • Loading branch information
gregw authored Nov 27, 2024
1 parent db8bb7a commit dd2c253
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ private Content intercept(Content content) throws IOException
{
return _interceptor.readFrom(content);
}
catch (RuntimeException | Error x)
{
throw x;
}
catch (Throwable x)
{
IOException failure = new IOException("Bad content", x);
Expand Down Expand Up @@ -1146,6 +1150,8 @@ public int noContent() throws IOException
{
if (_error instanceof IOException)
throw (IOException)_error;
if (_error instanceof RuntimeException)
throw (RuntimeException)_error;
throw new IOException(_error);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
String text = new String(data, 0, 1024, Charset.defaultCharset());

for (int i = 0; i < 9; i++)
endp.addInput("400\r\n" + text + "\r\n");
endp.addInput(Integer.toHexString(text.length()) + ";\r\n" + text + "\r\n");

HttpTester.Response response = HttpTester.parseResponse(endp.getResponse());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
Expand All @@ -63,9 +64,10 @@
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -110,10 +112,10 @@ public void handle(String target, Request jettyRequest, HttpServletRequest reque
// Throw immediately from the interceptor.
jettyRequest.getHttpInput().addInterceptor(content ->
{
throw new RuntimeException();
throw new BadMessageException();
});

assertThrows(IOException.class, () -> IO.readBytes(request.getInputStream()));
assertThrows(BadMessageException.class, () -> IO.readBytes(request.getInputStream()));
serverLatch.countDown();
response.setStatus(HttpStatus.NO_CONTENT_204);
}
Expand Down Expand Up @@ -159,7 +161,7 @@ public void handle(String target, Request jettyRequest, HttpServletRequest reque
throw new RuntimeException();
});

assertThrows(IOException.class, () -> IO.readBytes(request.getInputStream()));
assertThrows(RuntimeException.class, () -> IO.readBytes(request.getInputStream()));
serverLatch.countDown();
response.setStatus(HttpStatus.NO_CONTENT_204);
}
Expand Down Expand Up @@ -362,7 +364,7 @@ public void onDataAvailable()
// Now the interceptor should throw, but isReady() should not.
if (input.isReady())
{
assertThrows(IOException.class, () -> assertEquals(bytes[0], input.read()));
assertThrows(RuntimeException.class, () -> assertEquals(bytes[0], input.read()));
readFailureLatch.countDown();
response.setStatus(HttpStatus.NO_CONTENT_204);
asyncContext.complete();
Expand Down Expand Up @@ -431,7 +433,7 @@ public void onAllDataRead()
@Override
public void onError(Throwable error)
{
assertSame(failure, error.getCause());
assertThat(failure, anyOf(sameInstance(error), sameInstance(error.getCause())));
response.setStatus(HttpStatus.NO_CONTENT_204);
asyncContext.complete();
}
Expand Down

0 comments on commit dd2c253

Please sign in to comment.