java.net.http.HttpClient: invalid exception when bad status line is returned

Dmitry Sivachenko trtrmitya at gmail.com
Fri Jan 18 20:57:49 UTC 2019


Hello, 

I am tasting java.net.http.HttpClient with  

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.

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.

What do you think?

Thanks.

Here is a sample test program:

package ru.mitya.test;

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/net-dev/attachments/20190118/89196a7c/attachment-0001.html>


More information about the net-dev mailing list