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

java.net.http.HttpClient: wrong exception type when bad status line is received

    XMLWordPrintable

Details

    • b02
    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "11.0.1" 2018-10-16
      OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.1+13)
      OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.1+13, mixed mode)

      on Mac OS X 10.14.2.

      A DESCRIPTION OF THE PROBLEM :
      Consider the case when server responds with bad (invalid format) status line: "HTTP/1.1 FOO"

      The following IllegalArgumentException is thrown:

      Exception in thread "main" java.lang.IllegalArgumentException: For input string: "FOO"
      at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:551)
      at java.net.http/jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:119)
      at ru.mitya.test.App.main(App.java:14)
      Caused by: java.lang.NumberFormatException: For input string: "FOO"
      at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
      at java.base/java.lang.Integer.parseInt(Integer.java:652)
      at java.base/java.lang.Integer.parseInt(Integer.java:770)
      at java.net.http/jdk.internal.net.http.Http1HeaderParser.readStatusLineFeed(Http1HeaderParser.java:197)
      at java.net.http/jdk.internal.net.http.Http1HeaderParser.parse(Http1HeaderParser.java:124)
      at java.net.http/jdk.internal.net.http.Http1Response$HeadersReader.handle(Http1Response.java:672)
      at java.net.http/jdk.internal.net.http.Http1Response$HeadersReader.handle(Http1Response.java:603)
      at java.net.http/jdk.internal.net.http.Http1Response$Receiver.accept(Http1Response.java:594)
      at java.net.http/jdk.internal.net.http.Http1Response$HeadersReader.tryAsyncReceive(Http1Response.java:650)
      at java.net.http/jdk.internal.net.http.Http1AsyncReceiver.flush(Http1AsyncReceiver.java:228)
      at java.net.http/jdk.internal.net.http.common.SequentialScheduler$SynchronizedRestartableTask.run(SequentialScheduler.java:175)
      at java.net.http/jdk.internal.net.http.common.SequentialScheduler$CompleteRestartableTask.run(SequentialScheduler.java:147)
      at java.net.http/jdk.internal.net.http.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:198)
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
      at java.base/java.lang.Thread.run(Thread.java:834)


      instead of the expected ProtocolException:

      Exception in thread "main" java.io.IOException: Invalid status line: "HTTP/1.1 FOO"
      at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:565)
      at java.net.http/jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:119)
      at ru.mitya.test.App.main(App.java:14)
      Caused by: java.net.ProtocolException: Invalid status line: "HTTP/1.1 FOO"
      <...>

      The suggested fix would be to catch IllegalArgumentException thrown by Integer.parseInt() in readStatusLineFeed() and throw
      ProtocolException instead of it.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      import java.net.URI;
      import java.net.http.HttpClient;
      import java.net.http.HttpRequest;
      import java.net.http.HttpResponse;

      public class App {
          public static void main(String[] args) throws Exception {
              HttpClient client = HttpClient.newBuilder().build();
              HttpRequest req = HttpRequest.newBuilder(URI.create("http://localhost:8000/"))
                      .version(HttpClient.Version.HTTP_1_1)
                      .build();
              HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandlers.ofString());
          }
      }

      Run this program, server should respond with "HTTP/1.1 FOO"

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      ProtocolException
      ACTUAL -
      IllegalArgumentException

      FREQUENCY : always


      Attachments

        Activity

          People

            jpai Jaikiran Pai
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: