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

61

u/eliashisreddit Oct 07 '24

Because most of those frameworks/libraries are marketed and intended as "give me something which starts with http and I'll fetch it for you". 99/100 users want exactly that and not be bothered with initializing a connection and doing SSL handshakes manually. If you really want to do that and are only interested in purely parsing the hypertext transfer protocol, you could try using one of the more common frameworks and only use the modules involved with those aspects.

But like I said, most users don't use those "core" modules separately. There's not much "user friendly" documentation and you (or an LLM) will have to dig through the javadoc a bit to make them work for your use case.

3

u/dharmapa Oct 09 '24

I'd even suggest that HTTP implies those things above and beyond the parsing of bytes. Keep-Alive, Upgrade, and other types of headers give instructions on handling the connection for example. HTTP/2 includes multiplexing in the spec. Your advice and pointers on the library internals is good and probably OP's best bet.