Skip to content

Building HTTP requests and responses using Java NIO/NIO2

License

Notifications You must be signed in to change notification settings

Oliver-Loeffler/NIOHttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NIOHttp

Build Status

Reading and writing HTTP request and response objects from and to java.nio.channels .

⚠️ This project has exploratory character and is in a very early stage of development.

Goals

  • Create a library/toolkit to write and read HTTP requests to and from java.nio.channels (Java's non-blocking IO)
  • Explore java.util.concurrentjava.util.concurrent API
  • Do not use external frameworks, integrate HTTP protocol and java.nio.channels using JDK 1.8+.

Boundary Conditions

  • zero external dependencies beside standard JDK except for unit & integration testing and build automation

Idea

First one need to connect a SocketStream to a URL such as http://www.raumzeitfalle.de/.

 
 URL url = new URL("http://www.raumzeitfalle.de/");
 InetSocketAddress address = new InetSocketAddress(url.getHost(), url.getDefaultPort());
 SocketChannel socketChannel = SocketChannel.open(address);

The server won't respond unless a request is sent (for example a GET request).

  writeGetRequestTo(socketChannel);
  

Even with bad requests server will respond, however, this may take time and response time is different. Here the static HttpResponseReader.fromChannel(...) method provides a FutureTask< Void > which can be executed by an ExecutorService. To collect the result, a Consumer< HttpResponse > must be provided. (https://github.com/Oliver-Loeffler/NIOHttp/blob/master/src/main/java/net/raumzeitfalle/niohttp/playground/FutureDemo.java)

 FutureTask<Void> futureTask = HttpResponseReader
 				.fromChannel(socketChannel,r -> System.out.println(r.responseHeader());
 		
 ExecutorService executor = Executors.newFixedThreadPool(1);
 executor.submit(futureTask);

Furthermore HttpResponseReader.fromChannel(...) returns a Stream of HttpResponse objects (https://github.com/Oliver-Loeffler/NIOHttp/blob/master/src/main/java/net/raumzeitfalle/niohttp/playground/StreamDemo.java)

Stream<HttpResponse> responseStream = HttpResponseReader.fromChannel(socketChannel); 
responseStream.findFirst().ifPresent(consumer);

This would enable (given appropriate HttpResponse capabilities), interesting ways of working with HttpResponses.

HttpResponseReader.fromChannel(socketChannel)
	    .filter( r -> r.isNotBadRequest() )
	    .findFirst().ifPresent(consumer);
	    

The current implementation is not yet fully functional here, as the HttpResponseReader only reads exactly one message from a channel. The response reading process has to be extended for continuous reading so that HttpResponse streaming will work.

Resources

The HTTP/1.1 Protocol

HTTP/2 Protocol

HTTP over TLS (transport layer security)

Java non-blocking IO (NIO and NIO2) and Networking

Other helpful references

About

Building HTTP requests and responses using Java NIO/NIO2

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages