Question: Using linux write a C++ program that implements a basic HTTP server. If this is done properly, you should be able to connect to it

Using linux write a C++ program that implements a basic HTTP server. If this is done properly, you should be able to connect to it through a web browser, but this will not be one of the criteria for grading.

The program will essentially be a loop that goes on forever (until it is killed), waiting for a client to connect. When a client connects, it accepts that connection, calls fork to make a child process, and handles communication from that client in the child process. The parent process should continue to wait for more connections, accepting them and forking as necessary. The program will accept two command line parameters:

1. the port number on which the server will be listening

2. the path to a directory that will serve as the root directory of the web server For example:

% ./z123456 9001 www

The requests received by the program will be of the form:

GET path

where the path is the path, relative to the directory specified as the second command line parameter, of the file that is being requested. There are several rules on what can constitute a valid path, and they are as follows:

it must begin with a /

it may contain additional / path separators to access subdirectories

a single / character refers to the directory specified in command line

a trailing / in the pathname can be ignored if the path refers to a directory

any data in the request beyond the path should be ignored

it may not contain the substring .. If the path refers to a directory, then:

if the file called index.html exists in that directory, send the contents of that file to the client

if not, send a list of the files in the specified directory to the client (do not include files that start with .) If the path refers to a file, then the content of the file should be sent to the client.

Error Checking

If the command line arguments are incomplete, or if the path to the web servers root is invalid, print an error message to stderr and exit with an error code. If any of the system calls fail, the program should use perror to report what happened and exit with an error code. If the path in the GET request is invalid, or if a file or directory cannot be accessed, then an appropriate error message should be sent to the client to notify them.

Other Notes

This is a simple TCP server. If you need a tool to test it, you can use the telnet command to connect to your server at the specified port. Type your command and hit enter, and the client should display what the server returns.

% telnet localhost 9001 Connected to localhost.

Escape character is '^]'. GET /

fileOne.html fileTwo.html

Connection closed by foreign host.

% telnet localhost 9001 Connected to localhost.

Escape character is '^]'.

GET fileOne

Error: fileOne not found

Connection closed by foreign host.

% telnet localhost 9001 Connected to localhost.

Escape character is '^]'.

GET fileOne.html

... contents of file ...

Connection closed by foreign host.

Make sure that the assignment is contained in a single file called z123456.cxx,

Make sure that your program compiles, links, and runs properly

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!