Java HTTP/2 Server Socket
Asked Answered
P

2

8

I want to get server sockets working for HTTP/2 in Java, preferably TLS/https.

I've got a TLS server socket working fine, but browsers will only talk HTTP/1.1 to it. If I understand correctly, you need ALPN to get a HTTP/2 browser to connect to your TLS socket and start running HTTP/2 on it; browsers won't ask for upgrade to HTTP/2 on https. It seems Java8 does not do ALPN so far. Maybe there is some other way to coerce browsers to do HTTP/2, at least non-TLS.

So, anybody know how to make a Java server socket for HTTP/2?

Phira answered 1/4, 2015 at 19:27 Comment(4)
Why don't you check out existing implementations like Jetty, Netty and Undertow? See http2 implementationsIncurvate
@DonghwanKim Yes, I have looked at two of them. ISTR that one of them adds ALPN to Java SSL sockets via some 'SSLExtension', IIRC. That could be a way to get ALPN for Java, and thus be able to have browser connect with 'h2' HTTP/2. Might be too much work, I don't know. I might just wait and hope that Java eventually gets ALPN in some update.Phira
Java won't get ALPN until at least JDK 9, which is slated for late 2016 or 2017. Meanwhile, you can use Jetty's ALPN implementation, or better yet, use Jetty (or other servers as suggested) directly rather than doing your own HTTP/2 implementation using ServerSocket.Burger
@Burger Great! One can simply tell the JVM to use 'path_to_alpn_boot_jar' at VM boot. That's actually a good answer -- hint, hint. Also, thanks for the APLN timeline for Java.Phira
B
12

Java won't get ALPN until at least JDK 9, which is slated for late 2016 or 2017.

Meanwhile, you can use Jetty's ALPN implementation, or better yet, use Jetty (or other servers as suggested) directly rather than doing your own HTTP/2 implementation using ServerSocket.

[Disclaimer, I am a Jetty committer] Jetty 9.3 has great support for HTTP/2, including HTTP/2 Push.

Burger answered 6/4, 2015 at 8:22 Comment(2)
Definitely use a container (I now use Undertow, but Jetty is also solid). There's no reason to reinvent the entire stack, especially since Spring MVC or Jersey will handle all of the plumbing for you.Ferrick
@chrylis I can understand Jonas N very well, I also found this page looking for a way to make my webserver support HTTP/2, because it can be fun and you can also learn a lot from that about how HTTP works etc. But anyway thanks for the info on Jetty's ALPN implementation!Snappish
F
1

FYI, Java Dev Team is preparing ALPN for Java 9. Hopefully as you can see this issue, https://bugs.openjdk.java.net/browse/JDK-8062848,

ALPN support may be backported to JDK 8 so an implementation is needed that does not introduce any new Java SE APIs. This may require creating something in a com.oracle.ssl.net package or via System Properties.

Faltboat answered 11/6, 2015 at 7:45 Comment(1)
Does use of Jetty ALPN and support for HTTP/2 in JDK < 9 require the use of SSL and an https connection? All the examples I've seen use https but was wondering if it can be done over regular http as well?Backstage

© 2022 - 2024 — McMap. All rights reserved.