Skip to content

hrishikeshathalye/MyServer

Repository files navigation

MyServer

🌐

A minimal, multithreaded HTTP/1.1 compliant Webserver

License: MIT made-with-python Version

Table of Contents

🎯 Features

✔️ Supports 5 common HTTP methods - GET, POST, PUT, DELETE and HEAD
✔️ Multithreaded
✔️ Configuration options
✔️ Level based logging support
✔️ Support for cookies
✔️ Automated basic testing

🎯 Installation And Usage

  1. Install all required packages using "pip3 install -r requirements.txt"
  2. Start the server using "python3 server.py port_no" , where port_no can be any valid port number. Not giving a port number will cause the server socket to bind to any available port. To bind to port numbers < 1024 prefixing the command with sudo is required.
  3. To stop or restart the server just type "stop" or "restart" into the terminal window where the server is running and press enter. A thread that continuously keeps waiting for input takes this input and stops/restarts the server.
  4. Server configuration options are available in the myserver.conf file in the conf directory.
  5. Access Log, Error Log and Post Data Logs are located in the log directory. If not already present the server will create them. (Being a general purpose web server, default behaviout for POST is to log POST data to PostDataLog.log)

Once the server has started, localhost:port_no can be accessed from the browser to access the sample default website hosted on the server.

🎯 Config

Locate the file myserver.conf in the conf directory. The meanings of the various configuration options are as follows:

  1. DocumentRoot : The folder where get requests are served from and where PUT requests store data. The value against this filed will indicate the location of the document root relative to the working directory of the project.

  2. ErrorPages : Custom error pages can be put into this directory, error indicating status codes (Eg: 400, 404, etc) will be sent with body as pages put in this directory. If this directory does not contain status codes for some errors, a blank body will be sent for those.

  3. MaxSimultaneousConnections : Maximum number of connections that can be handled at once, connections more than this will queue and if queue size is full no connection can be made. If set to 1, the server will essentially behave like a single threaded server.

  4. QueueSize : The maximum number of connections that will be queued once maximum simultaenous connections have been reached

  5. RequestTimeout : Timeout for requests. Since the server is capable of handling requests made line-by-line (telnet like fashion), this indicates the maximum amount of time the server will wait before terminating the connection once inactivity is detected. The timer resets after each line typed by the client, if request is being made line-by-line. This ensures the server isn't being kept hanging by a client who has initiated a connection but is taking too long to request.

  6. ServerCloseTimeout : Timeout to wait for requests to complete once a "stop" is given. If all requests are served before this timeout, server will stop immediately. else it will wait for a maximum time specified here, if requests do not complete within this time, the server still closes the connection immediately. This takes care of hanging threads and sets a hard limit on the timeout that the server will wait for.

  7. LogDir : Directory where log files are stored. (log by default)

  8. ErrorLog : Name of the error log file name. (error.log by default)

  9. AccessLog : Name of access log file name. (access.log by default)

  10. PostDataLog : Name of file where log of POST data is stored. (postData.log by default)

  11. Log Level : Only errors of severity equal to and above this level will be logged into error.log. Can be one of - CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET (NOTSET by default)

🎯 Testing

Run "python3 multithreadTest.py n" using the terminal where n specifies the number of stress test threads. Individual stress tests will spawn n threads to test each method. Combination stress tests will spawn number of threads that approximately sum to n. Do not start the server beforehand, the testing program will itself start the server before starting testing.

🎯 Logs

Log Formats:

  1. Access Log :
<IpAddr-of-client> - - <date> <request-line> <response-status-code> <size-of-response-body-in-bytes> <referer> <user-agent> <value of set cookie header in response> <value of cookie header in request>
  1. Post Data Log :
<datetime> - <post_data_as_bytestring>

🎯 Acknowledgements