HTTP server from scratch: Configuration

This post is the conclusion of HTTP server from scratch. Previous post about modularization can be found here. In this post we will make our webserver somewhat more configurable. We will add one more switch to the server.properties to point to a external directory that will be used to serve the documents from. Since it is a good idea to support compression of the documents that we send, we will add support to gzip compression and finally, we will update our build configuration to generate a property distribution. ...

May 18, 2019 · 5 min · shiva

HTTP server from scratch: Modularization

This is a continuation in the series of post to build a static HTTP server from scratch. Previous post about Unit testing can be found here. In this post we are going to refactor the code a little bit to make it somewhat more modular and configurable. Modular in the sense, break out from a single class into smaller classes based on functionality. Finally remove hard-coded configurations. Break it down At this point our entire code resides in one class HttpServer. We will break this down into two, an Interface Http and HttpRequestHandler. The interface Http is going to hold some of the constants like, status codes and header names and each instance of HttpRequestHandler will service one incoming request. After the refactoring our main class HttpServer will read the server configuration, based on the configuration create a ServerSocket and listen to requests. When a request arrives, creates an instance of HttpRequestHandler and spawn a new thread to process the request. ...

March 17, 2019 · 3 min · shiva

HTTP server from scratch: Unit testing

This is a part two in the series to build a web server from scratch without external dependencies. Part one can be found here Now that we have minimal setup working, next area of focus is testing. If we were to follow Test Driven Development (TDD), test cases would have to be written first before any working code. But it is never too late to write some test cases. When it comes to testing streams like in this case, the easy way is using ByteArrayInputStream and ByteArrayOutputStream. Feed it a byte array or get back a byte array and verify. To help with the unit testing we will be using JUnit and Mockito. I know these are external dependencies, but these are development only dependencies and does not affect the production output. ...

February 23, 2019 · 3 min · shiva

HTTP server from scratch

Almost everything available on the Internet is served by web server. There are plenty of web server implementations like Apache, Nginx, Express, etc. But ever wondered how to implement a web server from scratch without any external dependencies? I wondered about that and this post is the result of that little experiment using Java. Goals Before listing the goals, this post assumes that you have some knowledge of HTTP. Mozilla has a very good overview here. Now the goals ...

February 9, 2019 · 4 min · shiva