After JDK-7026262 fix, the behavior of HttpExchange changed when calling sendResponseHeaders(N, -1) first and then reading the InputStream obtained from getRequestBody():
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) {
try {
t.sendResponseHeaders(200, -1);
InputStream is = t.getRequestBody();
byte[] in = is.readAllBytes();
System.out.print(new String(in));
t.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
BeforeJDK-7026262, the request body InputStream could be read successfully.
After the fix, it throws "java.io.IOException: Stream is closed" because ExchangeImpl.sendResponseHeaders() now calls ExchangeImpl.close() if contentLen is -1.
HttpServerTest.java reproducer is attached.
Although reading the request body after calling sendResponseHeaders() is not a typical life-cycle pattern according to the HttpExchange specification, it does not appear to be explicitly prohibited.
Moreover, it still works when sendResponseHeaders() is called with a non-negative contentLen.
A simple workaround is to read the request body before sending response headers.
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) {
try {
t.sendResponseHeaders(200, -1);
InputStream is = t.getRequestBody();
byte[] in = is.readAllBytes();
System.out.print(new String(in));
t.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Before
After the fix, it throws "java.io.IOException: Stream is closed" because ExchangeImpl.sendResponseHeaders() now calls ExchangeImpl.close() if contentLen is -1.
HttpServerTest.java reproducer is attached.
Although reading the request body after calling sendResponseHeaders() is not a typical life-cycle pattern according to the HttpExchange specification, it does not appear to be explicitly prohibited.
Moreover, it still works when sendResponseHeaders() is called with a non-negative contentLen.
A simple workaround is to read the request body before sending response headers.
- relates to
-
JDK-7026262 HttpServer: improve handling of finished HTTP exchanges
-
- Closed
-