Skip to content

Commit

Permalink
Use sendError() rather than working directly with Coyote response
Browse files Browse the repository at this point in the history
The primary benefit is that the standard error page mechanism is
invoked.
  • Loading branch information
markt-asf committed Nov 8, 2023
1 parent f752e64 commit 700d582
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions java/org/apache/catalina/connector/InputBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ boolean isBlocking() {
*
* @throws IOException An underlying IOException occurred
*/
@SuppressWarnings("deprecation")
@Override
public int realReadBytes() throws IOException {
if (closed) {
Expand All @@ -310,28 +309,29 @@ public int realReadBytes() throws IOException {
try {
return coyoteRequest.doRead(this);
} catch (BadRequestException bre) {
// Set flag used by asynchronous processing to detect errors on non-container threads
coyoteRequest.setErrorException(bre);
// In synchronous processing, this exception may be swallowed by the application so set error flags here.
coyoteRequest.setAttribute(RequestDispatcher.ERROR_EXCEPTION, bre);
coyoteRequest.getResponse().setStatus(400);
coyoteRequest.getResponse().setError();
// Make the exception visible to the application
handleReadException(bre);
throw bre;
} catch (IOException ioe) {
// Set flag used by asynchronous processing to detect errors on non-container threads
coyoteRequest.setErrorException(ioe);
// In synchronous processing, this exception may be swallowed by the application so set error flags here.
coyoteRequest.setAttribute(RequestDispatcher.ERROR_EXCEPTION, ioe);
coyoteRequest.getResponse().setStatus(400);
coyoteRequest.getResponse().setError();
handleReadException(ioe);
// Any other IOException on a read is almost always due to the remote client aborting the request.
// Make the exception visible to the application
throw new ClientAbortException(ioe);
}
}


private void handleReadException(Exception e) throws IOException {
// Set flag used by asynchronous processing to detect errors on non-container threads
coyoteRequest.setErrorException(e);
// In synchronous processing, this exception may be swallowed by the application so set error flags here.
Request request = (Request) coyoteRequest.getNote(CoyoteAdapter.ADAPTER_NOTES);
Response response = request.getResponse();
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, e);
response.sendError(400);
}


public int readByte() throws IOException {
throwIfClosed();

Expand Down

0 comments on commit 700d582

Please sign in to comment.