Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8211437

java.net.http.HttpClient hangs on 204 reply without Content-length 0

    Details

    • Subcomponent:
    • Resolved In Build:
      b17
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Backports

        Description

        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


          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  michaelm Michael McMahon
                  Reporter:
                  webbuggrp Webbug Group
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  5 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: