Tuesday, February 19, 2013

My first web server

Hello readers,
I had been willing to develop a small and efficient web server hosted in a LAN for quick transfer of files. A couple of days ago I had some issues with my FTP client when I wanted to transfer something from my ubuntu machine. So I decided to write a simple HTTP server from scratch.

Operation is fairly simple, it binds itself to the port 80 and listens for connections. Once it receives a request, it checks for the presence of requested file. If found, it sends back the same file with HTTP 200 response. Otherwise sends HTTP 404 with the "notfound.htm" under server root directory. If no specific file is requested, it checks for the presence of "index.htm" in the requested directory (default being '/' ) and then serves index.htm page under that directory. The server serves only static pages and files.

It is relatively easy, less than 200 lines of code. Developed on ubuntu linux 12.04. Implements only HTTP Responses 200 and 404. The default server root is "/usr/local/www/". It requires root privileges for binding of socket to the port 80. Interrupts recognized are Ctrl+C for termination. I call it the 'ViServer'.

Please note that this piece of code was written for learning purposes only and you are likely to find it buggy. 

Here is the link to the source code:
https://docs.google.com/file/d/0B_oonTu4H_SIWTRJM3ljNm1KTUk/edit

 Use it for testing on your local machine only and never deploy it in untrusted LAN or worse, facing the internet!

Thank you! :)


1 comment:

  1. Haven't tested it. But just a small suggestion. Put up your source codes on http://github.com

    ReplyDelete