A simple webserver implemented in Java.
Description
This is a simple filebased webserver implemented in Java that supports GET and HEAD requests. Based on the provided arguments to the jar file, the webserver will start on a port, with n number of threads and with a file or directory that will be used for response.
The args that are provided to the jar file are checked to be valid, so we need a valid port, a reasonable number of threads and a file or directory that exists or it's not empty. For files please use .html or .txt otherwise an error message is provided.
So we have the server, a pool of threads via the **ExecutorService** and after that we are going to receive and parse the Request. Based on the request we can check the protocol, the headers and body. If the request is parsed properly and we have a valid HTTP request then we go to the Response part. For the response part we verify that the requested file exists and stuff. If everything is alright we are preparing the response and respond to the client. For response we need to add the proper headers, body, content type, content length etc. We support both, files and html responses.
There is also a part in this implementation where I tried to add some support for HTTP/1.1 keep-alive. I added a mechanism where at first I will check the request headers to see if Connection was provided. If this header was provided I will add the header to the response with the same value. The values for this header are keep-alive and close. I have also added a timeout for our server for the InputStream associated with the socket.
Examples
Start the webserver using this terminal command :
java -cp webserver.jar server.Main 8080 9 index.html
OR
java -cp webserver.jar server.Main 8080 9 webfiles
If you start with index.html only this file will be delivered on path root /. If you start with webfiles then all files that are in this folder will be delivered. You can also play with the args because there are some validations that need to be met.
For help try
java -cp webserver.jar server.Main -h (or --help)
More samples
java -cp webserver.jar server.Main 8080 9 empty // empty folder validation
java -cp webserver.jar server.Main 8080 9 index.pdf // pdf not supported
java -cp webserver.jar server.Main 8080 9 text.txt // supports text
java -cp webserver.jar server.Main 8080 15 empty // no more than 10 thread
java -cp webserver.jar server.Main 808080880 9 empty // port not valid
When started with a folder, for example webfiles, if you access in browser path / the index.html will be delivered as default. Also there is a check for pdf files, when the client tries to access /index.pdf an error page is provided with an informative message. Same when the requested file doesn't exist
FYI This webserver is also able to provide pdf files, I added this restriction / check for more fun :) .
How to start and access the webserver
See the examples section in order to start the program
After the program will start, select your favorite web browser and go to http://localhost:8080. You can do the same also with curl
- http://localhost:8080
- http://localhost:8080/index.html
- http://localhost:8080/text.txt
- http://localhost:8080/blabla
- http://localhost:8080/index.pdf
- curl -X POST -I http://localhost:8080 , in order to see 501 Not implemented, POST is not supported
Reference links http://www.infinitepartitions.com/cgi-bin/showarticle.cgi?article=art078 https://github.com/ibogomolov/WebServer http://www.santhoshreddymandadi.com/java/simple-multithreaded-web-server-java.html https://www.net.t-labs.tu-berlin.de/teaching/computer_networking/ap01.htm