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

com.sun.net.httpserver.HttpServer returns Content-length header for 204 response code

XMLWordPrintable

    • b15
    • generic
    • generic
    • Verified

        A DESCRIPTION OF THE PROBLEM :
        This violates RFC7230:
        - "A server MUST NOT send a Content-Length header field in any response
           with a status code of 1xx (Informational) or 204 (No Content)."

        and com.sun.net.httpserver.HttpExchange#sendResponseHeaders JavaDocs:
        - "if {@literal <= -1}, then no response body length is specified and no response body may be written."

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the attached source code and then:

        curl -i "http://localhost:8000/"

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        HTTP/1.1 204 No Content
        Date: Sun, 30 Sep 2018 17:08:29 GMT
        ACTUAL -
        HTTP/1.1 204 No Content
        Date: Sun, 30 Sep 2018 17:08:29 GMT
        Content-length: 0

        ---------- BEGIN SOURCE ----------
        import com.sun.net.httpserver.HttpExchange;
        import com.sun.net.httpserver.HttpHandler;
        import com.sun.net.httpserver.HttpServer;

        import java.io.IOException;
        import java.net.InetSocketAddress;

        public class Test {
            public static void main(String[] args) throws Exception {
                HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
                server.createContext("/", new MyHandler());
                server.setExecutor(null);
                server.start();
            }

            static class MyHandler implements HttpHandler {
                @Override
                public void handle(HttpExchange t) throws IOException {
                    t.sendResponseHeaders(204, -1);
                }
            }
        }

        ---------- END SOURCE ----------

        FREQUENCY : always


              michaelm Michael McMahon
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: