-
Bug
-
Resolution: Fixed
-
P4
-
11, 12
-
b17
-
generic
-
generic
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8211445 | 11.0.2 | Michael McMahon | P4 | Resolved | Fixed | b02 |
A DESCRIPTION OF THE PROBLEM :
According to RFC 7230, a 204 reply should not have Content-length header:
"A server MUST NOT send a Content-Length header field in any response with a status code of 1xx (Informational) or 204 (No Content)."
However, if no such header is present on a 204 reply, an HTTP request made with java.net.http.HttpClient will hang waiting for more content.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1 - Create a webserver that replies with HTTP status code 204 and does not set the Content-length header to 0 (following the RFC).
2 - Use java.net.http.HttpClient to request a resource from such server.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Obtain a valid response.
ACTUAL -
Client hangs waiting for more content to read.
---------- BEGIN SOURCE ----------
I've used Spark Java (sparkjava.com) for the webserver as com.sun.net.httpserver.HttpServer does not conform with the RFC and always returns the Content-length (reported in another issue).
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import static spark.Spark.*;
public class TestClient {
public static void main(String[] args) throws IOException, InterruptedException {
port(8000);
get("/", (req, res) -> {
res.status(204);
return "";
});
awaitInitialization();
var request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8000/"))
.GET()
.build();
final var res = HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
System.out.println(res.statusCode());
stop();
awaitStop();
}
}
---------- END SOURCE ----------
FREQUENCY : always
According to RFC 7230, a 204 reply should not have Content-length header:
"A server MUST NOT send a Content-Length header field in any response with a status code of 1xx (Informational) or 204 (No Content)."
However, if no such header is present on a 204 reply, an HTTP request made with java.net.http.HttpClient will hang waiting for more content.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1 - Create a webserver that replies with HTTP status code 204 and does not set the Content-length header to 0 (following the RFC).
2 - Use java.net.http.HttpClient to request a resource from such server.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Obtain a valid response.
ACTUAL -
Client hangs waiting for more content to read.
---------- BEGIN SOURCE ----------
I've used Spark Java (sparkjava.com) for the webserver as com.sun.net.httpserver.HttpServer does not conform with the RFC and always returns the Content-length (reported in another issue).
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import static spark.Spark.*;
public class TestClient {
public static void main(String[] args) throws IOException, InterruptedException {
port(8000);
get("/", (req, res) -> {
res.status(204);
return "";
});
awaitInitialization();
var request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8000/"))
.GET()
.build();
final var res = HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
System.out.println(res.statusCode());
stop();
awaitStop();
}
}
---------- END SOURCE ----------
FREQUENCY : always
- backported by
-
JDK-8211445 java.net.http.HttpClient hangs on 204 reply without Content-length 0
- Resolved
- duplicates
-
JDK-8211858 java.net.http.HttpClient fails to timely recognize 204 response
- Closed
- relates to
-
JDK-8218662 Allow 204 responses with Content-Length:0
- Closed
-
JDK-8211420 com.sun.net.httpserver.HttpServer returns Content-length header for 204 response code
- Closed
-
JDK-8216974 HttpConnection not returned to the pool after 204 response
- Closed