jakartaee / jakartaee/servlet

Provide a way to abort a response and close the underlying connection

Open
#98 6 comments 1 reaction 1 assignee Claimed by @glassfishrobot View on GitHub
Enhancement
Dominant language
Java
Stars
325
Forks
112
PR merge metrics
No merged PRs in 30d

Description

In the context of writing a reverse proxy using the Servlet API, this situation may happen:

* The proxy proxies the request from client to server.
* The server replies with headers and partial content
* The proxy receives the headers and the partial content from the server, and relays both to the client; in doing so, the response from proxy to client is committed.
* The server stops sending data.
* The proxy times out the exchange with the server.
* At this point, the proxy must signal to the client that something went wrong with the server; however, the proxy-to-client response is already committed, so the proxy must just abruptly close the connection.

There is no standard Servlet API to perform this abrupt close.

Closing the ServletOutputStream would probably just finish to send buffered data (or the terminator chunk in case of chunked content), which is not good as the client may think to have received the whole content, so it's not a viable solution.

There is a real need to close the connection, but the proxy cannot add Connection:close since the response is already committed.

In case of synchronous handling, the proxy may throw an exception, which would be interpreted by the Servlet Container as an indication to close the connection, but this solution does not work in the asynchronous case, where an external thread is writing the failure to the client.

A new API for this functionality should be added, covering both the synchronous case and the asynchronous case, for example:

```
// Fail the response to the client. HttpServletResponse response = ...;
if (response.isCommitted())
{
response.abort(); // <-- new API
asyncContext.complete();
}
else
{
response.resetBuffer();
response.setStatus(503);
response.setHeader("Connection", "close");
asyncContext.complete();
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.