A program that serves web objects from an existing directory to HTTP clients over the Internet using TCP. Watch a GIF of me interacting with it below!
- Supports HTTP
GET
requests. - Operates in non-persistent HTTP mode; once the object is served, the server closes the underlying TCP connection.
- Handles multiple connections simultaneously using multi-threading; spawns a new thread and creates a new
Socket
for each incoming TCP connection request (unlike UDP, where a single socket is used for all incoming requests and all messages are demultiplexed to the same socket). See the following image:
In the above image, you see a service process P1
open three different Socket
s for three different connecting clients; every client's segments are demultiplexed to a different Socket
at P1
.
- To prevent non-responsive clients from hogging server resources, if the server does not receive an HTTP message from the
client after the initial 3-way handshake, the server closes the connection and sends an error message with status code
408
. Note that this only occurs if the client is connecting usingtelnet
or certain other application layer protocols. With something like a browser, the handshake is automatically (ie. implicitly) followed by an HTTP request. - After server is shutdown, waits reasonable amount of time for current requests to be serviced before terminating.
- If the server can recover from exceptions (ie. the exception only affected a worker thread), then the server continues with normal execution. Otherwise, it terminates.
- The root directory of the web server, where the objects are located, is specified as a command line input parameter. If the object path in
the
GET
request is/object-path
, then the file containing the object is located on the absolute pathserver-root/object-path
in the file system, whereserver-root
is the root directory of the web server. -p <port_number>
specifies the server's port; default is2025
-t <idle_connection_timeout>
specifies the time after which the server closes the TCP connection in milli-seconds; default is0
(which means infinity, ie. idle connections are not closed)-r <server-root>
is the root directory of the web server (where all its HTTP objects are located); default is the current directory (directory in which program is ran)quit
is typed in the system terminal to shut the server down.- Sends responses with HTTP version
HTTP/1.1
; only returns responses with the following status codes/phrases:200 OK 400 Bad Request 404 Not Found 408 Request Timeout
- Assumes all header lines in the HTTP request are formatted correctly; only an error in the HTTP request line can trigger a
400 Bad Request
error. A properly formatted request line consists of three mandatory parts which are separated by one or more spaces, as follows:GET /object-path HTTP/1.1
. The commandGET
and protocolHTTP/1.1
are fixed, while theobject-path
is optional. If noobject-path
is provided, ie. the request only specifies "/", thenindex.html
is assumed by default.
-
Start the server by compiling all files and then running
ServerDriver.java
, as follows:javac *.java java ServerDriver -p <port_number> -t <idle_connection_timeout> -r <server_root>