CSE 130Fall 2019Section 01Assignment 1
Due: Sunday, October 27 at 9:00PM
Goals
The goal for Assignment 1 is to implement a simple singlethreaded HTTP server. The server will respond to simple GET and PUT commands to read and write respectively files named by 27character ASCII names. The server will persistently store files in a directory on the server, so it can be restarted or otherwise run on a directory that already has files. As usual, you must have a design document and writeup along with your README.md in your git. Your code must build httpserver using make.
Programming assignment: HTTP server Design document
Before writing code for this assignment, as with every other assignment, you must write up a design document. Your design document must be called DESIGN.pdf, and must be in PDF you can easily convert other document formats, including plain text, to PDF.
Your design should describe the design of your code in enough detail that a knowledgeable programmer could duplicate your work. This includes descriptions of the data structures you use, nontrivial algorithms and formulas, and a description of each function with its purpose, inputs, outputs, and assumptions it makes about inputs or outputs.
Write your design document before you start writing code. Itll make writing code a lot easier. Also, if you want help with your code, the first thing were going to ask for is your design document. Were happy to help you with the design, but we cant debug code without a design any more than you can.
Program functionality
You may not use standard libraries for HTTP; you have to implement this yourself. You may use standard networking and file system system calls, but not any FILEcalls except for printing to the screen e.g., error messages. Note that string functions like sprintf and sscanf arent FILEcalls.
Your code may be either C or C, but all source files must have a .cpp suffix and be compiled by clang with no errors or warnings using the following flags: clang stdgnu11 Wall Wextra Wpedantic Wshadow
HTTP protocol
The HTTP protocol is used by clients and servers for a significant amount of web communication. Its designed to be simple, easy to parse in code, and easy to read by a human. Your server will need to send and receive files via http, so the best approach may be
2019 Ethan L. Miller, Faisal Nawab
to reuse your code for copying data between file descriptors which you should be wellacquainted with after asgn0!.
Your server will need to be able to parse simple HTTP headers; youre encouraged to use string functions but not FILEfunctions to do this. The http protocol that you need to implement is very simple. The client sends a request to the server that either asks to send a file from client to server PUT or fetch a file from server to client GET.
An HTTP header consists of one or more lines of ASCII text, followed by a blank empty line. The first line of the header is a single line specifying the action. A PUT header looks like this note the blank line at the end:
The newlines are encoded as rn, and the data being sent immediately follows the header. The sent data may include any bytes of data, including NUL 0. The ContentLength header line is optional; the client may send it to indicate how much data follows the header. if its included, the server should stop reading data after the specified number of bytes, and look for another header if the client hasnt closed the connection. If theres no ContentLength header, the server copies data until the client closes the connection read reads endoffile. For the above example, the name that the server will bind to the data is ABCDEFabcdef012345XYZxyzmm.
For GET, the header looks like this. Theres no ContentLength header because the client doesnt know the length of the content again, notice the blank line at the end:
All valid resource names in this assignment must be 27 ASCII characters long, and must consist only of the upper and lower case letters of the English alphabet 52 characters, the digits 09 10 characters, and dashand underscore , for a total of 64 possible characters that may be used. If a request includes an invalid name, the server must fail the request and respond accordingly.
The server must respond to a PUT or GET with a response, which is a response header optionally followed by data. An example response header looks like this:
The 200 is a status code200 means OK. The OK message is an informational description of the code. For example, the 404 status code could say File not found. The server must fill in the appropriate status code and message. For a response to a GET request and other requestsclarification added on Oct 18, the server must provide a ContentLength: line in the header, similar to the one shown in the PUT request. The header is followed by a blank line
PUT ABCDEFabcdef012345XYZxyzmm HTTP1.1 ContentLength: 460
GET ABCDEFarqdeXYZxyzf012345ab HTTP1.1
HTTP1.1 200 OKrn
2019 Ethan L. Miller, Faisal Nawab
and, for a GET response, by the data that the client has requested. As before, the data may include any data, including NUL bytes.
You can find a list of HTTP status codes at: https:en.wikipedia.orgwikiListofHTTPstatuscodes.
The only status codes youll need to implement are 200 OK, 201 Created, 400 Bad Request, 403 Forbidden, 404 Not Found, and 500 Internal Server Error. You may use additional status codes if you like, but these are the only required ones. Look at the link above to determine when to use each one.
Your server will need to be able to handle malformed and erroneous requests and respond appropriately, without crashing. Note that a bad name is not the same thing as a valid name that doesnt correspond to an existing file. You may assume that a header will be no longer than 4 KiB, though the data that follows it for a PUT may be much longer. Similarly, response headers will be less than 4 KiB, but the data may be much longer.
HTTP server
Your server binary must be called httpserver. Your HTTP server is a singlethreaded server that will listen on a userspecified port and respond to HTTP PUT and GET requests on that port. The address to listen to and the port number are specified on the command line. Yes, it is necessary to specify the server address, since your computer has multiple Internet addresses, including localhost.
The first argument to httpserver is the address of the HTTP server to contact, which may be specified as a hostname or an IP address; your software must handle either one. The second, optional, argument to httpserver is the port number on which to listen. If theres no second argument, assume the standard HTTP port, port 80.
Your server will use the directory in which its run to write2 files that are PUT, and read2 files for which a GET request is made. All file IO for user data must be done via read and write.
Testing your code
You should test your code on your own system. You can run the server on localhost using a port number above 1024 e.g., 8888. Come up with requests you can make of your server, and try them using curl1. See if this works! curl is very reliable, so errors are likely to involve your code.
You might also consider cloning a new copy of your repository from GitLabUCSC to a clean directory to see if it builds properly, and runs as you expect. Thats an easy way to tell if your repository has all of the right files in it. You can then delete the newlycloned copy of the directory on your local machine once youre done with it.
README and Writeup
As for previous assignments, your repository must include README.md and W RITEUP.pdf. The README.md file should be short, and contain any instructions necessary for running your code. You should also list limitations or issues in README.md, telling a user if there are any known issues with your code.
2019 Ethan L. Miller, Faisal Nawab
Your WRITEUP.pdf is where youll describe the testing you did on your program and answer any short questions the assignment might ask. The testing can be unit testing testing of individual functions or smaller pieces of the program or wholesystem testing, which involves running your code in particular scenarios.
For Assignment 1, please answer the following question:
What happens in your implementation if, during a PUT with a ContentLength, the connection was closed, ending the communication early? This extra concern was not present in your implementation of dog. Why not? Hint: this is an example of complexity being added by an extension of requirements in this case, data transfer over a network.
Submitting your assignment
All of your files for Assignment 1 must be in the asgn1 directory in your git. When you push your repository to GitLabUCSC, make sure to include the following:
There are no bad files in the asgn1 i.e., object files.
Your assignment builds in asgn1 make to produce httpserver.
All required files source files, DESIGN.pdf, README.md, WRITEUP.pdf are present in asgn1.
Hints
Start early on the design. This is a more difficult program than dog!
Youll need to use at least the system calls socket, bind, listen, accept, connect, send, recv, open, read, write, close. The last four calls should be familiar from Assignment 0, and send and recv are very similar to write and read, respectively. You might also want to investigate dprintf3 for printing to a file descriptor and sscanf3 for parsing data in a string i.e., a buffer. You should read the man pages or other documentation for these functions. Dont worry about the complexity of opening a socket; well discuss it in section. You may not use any calls for operating on files or network sockets other than those above.
Test your server using an existing Web client we recommend curl1. Make sure you test error conditions as well as normal operation.
Aggressively check for and report errors via a response. If your server runs into a problem well into sending data in response to a GET, you may not be able to send an error
UPDATE ON OCTOBER 16: added this part to avoid confusion. We do need you to submit the source files for your programs. We only require you to submit the httpserver implementation. You are NOT required to write a client program see the hints for how to test with a curl client
2019 Ethan L. Miller, Faisal Nawab
header the header may have been sent long ago. Instead, you should just close the connection. Normally, however, responses are your servers way of notifying the client of an error.
Your commit must contain the following files:
README.md
DESIGN.pdf
Makefile
WRITEUP.pdf
source files for the server see update above
It may not contain any .o files. You may, if you wish, include the source files for your DESIGN.pdf andor WRITEUP.pdf in your repo, but you dont have to. After running make, your directory must contain httpserver. Your source files must be .cpp files and the corresponding headers, if needed.
If you need help, use online documentation such as man pages and documentation on Grading
Makefiles. If you still need help, ask the course staff.
As with all of the assignments in this class, we will be grading you on all of the material you turn in, with the approximate distribution of points as follows: design document 35; coding practices 15; functionality 40; writeup 10.
Your code must compile to be graded. A submission that cannot compile will receive a maximum grade of 5.
2019 Ethan L. Miller, Faisal Nawab
Reviews
There are no reviews yet.