GET request
Table of contents
Files
Implement handle_files_request
to handle HTTP GET
requests for files. You will need to call serve_file
accordingly. You should also be able to handle requests to files in subdirectories of the files directory (e.g. GET /images/hero.jpg
).
- If the file denoted by path exists, call
serve_file
on it. Read the contents of the file and write it to the client socket.- Make sure you set the correct
Content-Length
HTTP header. The value of this header should be the size of the HTTP response body, measured in bytes. For example,Content-Length: 7810
. You can usesnprintf
to convert an integer into a string. - You must use the
read
andwrite
syscalls for this assignment. Any implementations usingfread
orfwrite
will not earn any credit. This is purely for pedagogical reasons; we want you to be comfortable with the fact that low-level I/O may or may not perform the entire operation on all the bytes requested.
- Make sure you set the correct
- Else, serve a
404 Not Found
response (the HTTP body is optional) to the client. There are many things that can go wrong during an HTTP request, but we only expect you to support the404 Not Found
error message for a non-existent file. After finishing this part, curling forindex.html
should output the contents of the fileindex.html
.
Directories
Implement handle_files_request
to handle HTTP GET
requests for both files and directories.
You will now need to determine if
path
inhandle_files_request
refers to a file or a directory. Thestat
syscall and theS_ISDIR
orS_ISREG
macros will be useful for this purpose. After finding out ifpath
is a file or a directory, you will need to callserve_file
orserve_directory
accordingly.- If the directory contains an
index.html
file, respond with a200 OK
and the full contents of theindex.html
file. You may not assume that directory requests will have a trailing slash in the query string.- The
http_format_index
function inlibhttp.c
may be useful.
- The
- If the directory does not contain an
index.html
file, respond with an HTML page containing links to all of the immediate children of the directory (similar tols -1
), as well as a link to the parent directory.- The
http_format_href
function inlibhttp.c
may be useful. - To list the contents of a directory, good functions to use are
opendir
andreaddir
.
- The
If the directory does not exist, serve a
404 Not Found
response to the client.You don’t need to worry about extra slashes in your links (e.g.
//files///a.jpg
is perfectly fine). Both the file system and your web browser are tolerant of it.You do not need to handle file system objects other than files and directories (i.e. you do not need to handle symbolic links, pipes, or special files).
Remember to close the client socket before returning from the
handle_files_request
function.- Make helper functions to reuse similar code when you can. It will make your code easier to debug!
After finishing this part, curling for the root directory /
should output the contents of the file index.html
. All tests for Basic Server tests should pass on the autograder.