The goal of this project is to further understand the mechanism of how HTTP networking programming works. As a web programming, the HTTP server is the gateway to the world therefore understanding how HTTP Request and Response are handled is crucial for debugging, for performance improvement and string parsing using the core library.
The other goal is to have zero or minimum dependency mainly rely on Java's core library.
- Changing to virtual thread, and about to handle different files, such as images, JSON, etc.
- Adding tests
This is not a production/commercial/industry grade project, it is a gateway for curious programmers, like myself, to learn about the how to build HTTP server from scratch in Java.
Client -> ServerSocket -> Socket (each Client) -> BufferedReader -> Parsing Messages -> OutputStream -> Client
This project is based on Joshua Davies' post on mini HTTP server (see in reference), from there I have refactored some of the code to parse string faster.
The HTTP server is made of Socket, Reader, and Writer,they are the core parts for the communication between Client and Server. Socket is let us listen and reply to client, Reader is the reads the Requests, and Writer writers the Response
We will be using Java's ServerSocket as the base to open the gate and handle incoming Requests, by that we will need to have port number, similar to Express.js, that is also connecting to Socket under the hood.
Then for the actual client requests will be using another Socket connection from there we will spawn a Thread to handle the actual Request, Response.
Before we implement the handle for client socket, we will create Reader for incoming messages, all the messages coming in a string, and there is special format known as the HTTP Messages.
From there we are able to get the HTTP methods, URI, path parameter (if any), we will use StringTokenizer from the Java util library to dissect, and use while loop to get all reat of the information from the HTTP Message
Finally, we also need a BufferedReader to turn all the body of the Request for further processing.
Another crucial part of HTTP Server is responding different kind of messages or protocol such as JSON, to response we are rely on the socket's output stream, here will respond accord to the Request
The Socket handler, we are leveraging on the multithreading capability of Java, the socket handler act as a worker, whenever there is a new connection it will spawn a Thread to handle in the coming traffics.
During the development, I have also learnt how to create a logger from scratch, where I based on the idea of JojoZhang's (reference) LogUtils, and I made a wrapper around the Java util Logger.
Although Apple and Google are removing Cookies, but it is an important relic, from Joshua's blog post on making a mini HTTP Server he explains how a Cookies is constructed .
Still from Joshua's post he talked about how to set up a SSL connetion for a HTTP Server, this is something I have never dream of learn until now, but for now I have omitted it in future it will be included in this project.
JojoZhang's HTTP server - Learn the part of creating a Logger