Assignment-1 Solution

$30.00 $24.00

  Static File Serving The web server should serve static files in the current working directory under the path prefix “/file/”. For example, when a client sends a request to get the path “/file/CS2105.txt”, the server should try to open and read the file with relative path “CS2105.txt”. If the entire file can be successfully…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

 

Static File Serving

The web server should serve static files in the current working directory under the path prefix “/file/”. For example, when a client sends a request to get the path “/file/CS2105.txt”, the server should try to open and read the file with relative path “CS2105.txt”. If the entire file can be successfully opened and read, the server returns code “200” and the file content as the content body, with “Content-Length” header properly sent. If the file does not exist, the server should return code “404”. Otherwise (e.g. the server fails to open the file for reading due to lack of permission), the server should return code “403”.

Your Task

You are given a folder containing multiple files for testing purpose. Write your program into only one file named WebServer.xxx where xxx is the corresponding file extension for respective language. Refer to Program Submission Section earlier for more details. To run the Web Server program on sunfire, use the following command:

python3 WebServer.py <port>

For example:

python3 WebServer.py 8000

You should choose a port number greater than 1023 because operating systems restrict the access to ports lower than 1024. If you get a BindException: Address already in use (or similar), try a different port number. Remember to configure your python3 alias and refer to assignment 0 if you forgot how to do it. Note: regardless of language you are using, the one and only one compulsory argument passed to the program should be the port number, and there’s no standard input.

To solve the task, you have to develop a web server program. A Web Server is just a TCP server that understands HTTP. It will

(1) create a TCP socket

(2) listen to the socket and accept a new TCP connection

(3) read HTTP request(s) from TCP connection and handle it/them, and finally

(4) send HTTP response(s) to the client through TCP.

 

Page 7 of 10

Detailed specification is described in previous sections named The Web Server, The HTTP-like Protocol, The Key-value Store and Static File Serving.

Following good OOP practise, suggested method/class names to be implemented are shown in bold font below. Students can choose different names for their methods as well.

1. The start method of the WebServer class should create a ServerSocket and listen to new connection requests. Once a TCP connection is established, this method should pass the connection socket to handleClientSocket method (please refer to lecture 3 notes for a demonstration of TCP ServerSocket and Socket).

2. handleClientSocket will then take all necessary steps to handle a HTTP request from a client. It should make use of the HttpRequest class. First, you have to read a HTTP request from the socket. Next, you should call parseRequest of the HttpRequest class to parse the request.

3. An instantiated object of the HttpRequest class represents a single incoming HTTP request. Thus, it is a good idea to create a new HttpRequest object for each request.

4. Once the request is parsed, Web Server should call formHttpResponse. This method inspects the HttpRequest and decide whether to send a 200 OK or a 404 response. formHttpResponse returns a byte array which can be sent to the client using sendHttpResponse.

Testing Your Solution

We will grade your submission on sunfire. Thus, it is a good idea to test your submission on sunfire before submission. In the past we had submissions that do not work on sunfire, e.g. because sunfire might have a different Java/python versions installed. Unless stated otherwise for individual tasks, you are allowed to use libraries installed in public folders of sunfire (e.g. /usr/lib) only.

You can test your Web Server in many different ways. For example, using the command line, or by manually sending requests to the Web Server. We also provide a simplified test case.

 

 

Page 8 of 10

Test case: We will use carefully crafted scripts to test the functionality of your submission. For your convenience, we provide a simplified and condensed version of our test cases for your own testing. We strongly recommend you to execute this test case before submission. Passing this test case does not guarantee full marks but failing it means something is wrong. To execute the test case, you need to

(1) open two SSH windows to sunfire

(2) run your Web Server in one window (refer to previous section)

(3) execute the following command in another window with port number the same as the

one passed to the WebServer:

bash test.sh <port>

For example,

bash test.sh 8000

Grading Rubric

1. The HTTP-like protocol (4 marks)

a) The server correctly understands all valid requests and sends valid responses only

b) The server supports persistent connection with pipelining as specified

c) The server returns correct status codes according to the specifications

d) The server supports different functions under different prefixes of the HTTP path

2. The key-value store (4 marks)

a) The server supports insertion and update of key-value pairs through the POST method

b) The server supports retrieval of value by key through the GET method

c) The server supports removal of key-value pairs through the DELETE method

3. Serving static files (2 marks)

a) The server serves static files with the path relative to the directory where the server

program is started

During grading, we will only transfer files that are sufficiently small (< 5 MB). Thus, it is safe to load the entire file into memory. Please refer to assignment 0 exercise 2 if you are unsure how to do it.

 

 

 

Page 9 of 10

Question & Answer

If you have any doubts on this assignment, please post your questions on IVLE forum or consult the teaching team. We are not supposed to debug programs for you, and we provide support for language-specific questions on a best-effort basis only. The intention of Q&A is to help clarify misconceptions or give you necessary directions.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Page 10 of 10

Assignment-1 Solution
$30.00 $24.00