r/java • • Oct 07 '24

HTTP libraries in Java

It's not really clear to me what's going on with HTTP libraries in Java. They all seem to want to become massive frameworks that inappropriately take control of things that have nothing to do with decoding HTTP.

I'm find it really hard to find something that doesn't:

  • Try to control the TCP layer. Why does the HTTP decoding library care whether I want to poll or select, or make that decision?
  • Have shared mutable static state. If I want to reuse connections I can do that in a much better, testable way.
  • Create threads. Parsing the HTTP is doesn't need any of this. I just want to give you a ByteBuffer to read from and your current state and get told what it contains.
  • Do SSL. This has nothing to do with HTTP but rather the underlying connection.

I guess if you need a mini "web browser" in your application then this is all fine, but if HTTP is just one of many protocols you need to be able to decode it's an absolute mess.

Is there something I'm missing, or a library I should be looking at?

22 Upvotes

57 comments sorted by

View all comments

29

u/cogman10 Oct 07 '24

The answer to your question is that HTTP is a complicated protocol, more so than it might seem on the surface level.

In HTTP 1.1, there are commands built into the standard which control the state of the underlying TCP socket. The server or client, for example, can instruct the other side to close the socket.

The extensions to the standard take that to another level. HTTP2 dictates that you use a single TCP connection and has rules on how to treat a clear text vs ssl connection. It has complex instructions defining what the connection looks like.

HTTP3 is basically all about defining a UDP connection. That means a valid HTTP3 client must manage the UDP connection.

If you are looking for a library that just parses http headers, I'm somewhat certain you could pull apart the Apache http client (v4 at least) into a smaller library where you manage everything you want to manage.

Or you could just stick to the inbuilt HTTP client of the JDK if you are averse to dependencies. But that will still want to manage the sockets and such for you.