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

HttpServer: regression in HttpExchange after JDK-7026262

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 17, 21, 25
    • core-libs
    • None
    • generic
    • generic

      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();
              }
          }
      }

      Before JDK-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.

            michaelm Michael McMahon
            evergizova Ekaterina Vergizova
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: