Simple Web Server
Due: 5:00am, Friday, October 11, 2024
Get the Assignment Code
Login to your university Unix account and copy the assignment code to a location under your home directory:
The starter code for this assignment is on the Linux server here:
/export/home/public/schwesin/cpsc328/assignments/project4
Description
The purpose of this assignment is to create a web server that can respond to simple HTTP GET requests. For example, given a request of:
GET /file.txt HTTP/1.1\r\n
Host: localhost\r\n
Connection: close\r\n
\r\n
Your server must parse the request to get the file path for the request and
then construct an appropriate HTTP response. The directory that the server
should serve files from is determined by the -d
command line argument. Note
that your server must ensure that files are only served from the given
directory and its subdirectories. It is a security risk if a client can obtain
files from arbitrary ancestor directories. If a client requests such a file,
then the response code should be “404 Not Found”.
The form of the response will be similar to this:
HTTP/1.1 200 OK\r\n
Content-Type: text/plain\r\n
Content-Length: 5\r\n
Connection: close\r\n
\r\n
Hello
Here is the criteria for the response:
- The response code will either be “200 OK” or “404 Not Found”
- The
Content-Type
will either betext/html
ortext/plain
; this will be based on the file extension of the requested file. - The
Content-Length
is computed from the contents of the file
Here is the general flow of the program:
Create a socket: this is a combination of
getaddrinfo
andsocket
and the value of the-p
command line argument.Bind the socket with
bind
.Listen on the socket with
listen
.Loop forever:
accept new connections with
accept
.Receive the HTTP response with
recv
; in this case the client is waiting for a response (the client socket will not be closed), so you need to read until the end of the headers.Create the HTTP response and send it to the client.
Close the socket returned from
accept
withclose
Specifications
The name of the compiled program must be
webserver
.The input to the program must be in the form of command line arguments as follows:
-p <string>
(required) port-d <string>
directory to serve files from (default ./)-v
verbose: print the HTTP request that was received
The following rules must be added to the provided Makefile:
- Add a rule to build an executable named
webserver
. This must be the default action of the Makefile. - Add a rule called
clean
to remove any temporary files and thewebserver
executable.
- Add a rule to build an executable named
If the implementation language is Python, the
socket.create_connection
andsocket.create_server
functions are forbidden and theurllib
module is forbidden.Note: when testing make sure to use 5-digit or greater port numbers.
Turning in the Assignment
To turn in the assignment, execute the submit script by running the command
make submit
from within the project4
directory.
Grading Criteria
- Consistent coding style
- Modular design
- Correct implementation of the specification
- Correct error checking of library calls
Note: If the submission includes material that was not covered in class and the material is not properly cited, then you will receive a failing grade for this assignment.