Problem Set 1 - zhttpto Web Server
The goals of this assignment are to:
Introduce the Rust programming language and get some experience writing code in Rust.
Preview several of the main concepts we will cover in this class including: processes, threats, memory management, and network protocols. You are not expected to understand everything we use in this assignment, but we hope this exposure will get you to start considering more deeply what is going on in computer systems.
Note that this assignment may require you to learn aspects of Rust that were not covered in class or in the tutorials (although the tutorials were designed to cover most of what you need to complete this assignment). You should be resourceful in searching for helpful documentation, but we encourage you to use the scheduled office hours and class IRC channel to get help.
Collaboration Policy. For this problem set, everyone should submit their own assignment. You should discuss the problems with others and are expected to help your classmates in ways that benefit their learning, but not provide answers thoughtlessly. You should attempt every problem on your own first before seeking help and must fully understand everything you submit as your own work.
Before continuing with this assignment, you should:
After finishing these steps, you should have a
ps1 directory that
include the starting
zhttpo.rs code for this assignment.
Make sure you have set up the repository and starting
code correctly. For these
problems, you should work in your
ps1 repository, and put all the code
in files in that directory.
A web server is a program that responds to HTTP requests. HTTP (Hypertext Transfer Protocol) is the protocol that specifies how web clients and servers should communicate with each other.
A simple web server can just open a socket to listen for requests, and send back a response when a request arrives. Tim Berners-Lee led the development of the first web server, starting in 1990.
The most popular web server today (and since 1996) is Apache, which is about 2.2 million lines of (mostly C) code. Your web server will provide some of the same functionalities as Apache, but will be much simpler!
Before working on the web server code, complete Part 2 of the Rust tutorial. (We won't check your code for the other exercises, but you should do them also.)
Complete the last exercise in the tutorial, implementing the joiner program that combines two message shares and outputs the result.
Making the Zhttpto Web Server
For this assignment, we have provided starting code for a miniscule web
zhttpto.rs (pronounced "zepto"). Its so small, it doesn't
yet even understand HTTP and responds to every request with the same web
page response. For this assignment, you will modify the web server code
to serve simple static pages.
rustc zhttpto.rs) and run (
./zhttpto on Unix) the server.
The server is listening on port 4414. Open a web browser to
http://localhost:4414. If the server is running correctly, you should
see a welcoming response page. (We will got more into how network
sockets work and the design of network protocols like HTTP later in the
In the shell that is running the server, you should see text output showing the messages received by the server. The most interesting line is the User-Agent, which is what your web browser is telling the server. Typical User-Agent strings incorporate a history of web browser development. For example, the request from my Firefox browser includes: User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:22.0) Gecko/20100101 Firefox/22.0.
For the short answer (prose) questions, you should create a file
answers.md in your
ps1 repository. The
.md suffix is for
Markdown, a convenient
way of generating HTML content. It is not necessary to do any fancy
formatting, though, you can just use plain text for your answers unless
more is necessary for clarity. The first lines of your
file should be:
Title: Problem Set 1 Answers Author: <your name>
Copy the User-Agent string reported by your browser. Explain as many of the things in that string as you can.
Take a look at the provided code in
zhttpto.rs. For the rest of the
problem set, you will be modifying that code.
For the next problem, your goal is to add a counter to the server that increments with each request.
If you do this in a straightforward way, by declaring a mutable static variable you are likely to encounter a compile-time errors like this:
zhttpto.rs:50:27: 50:40 error: use of mutable static requires unsafe function or block zhttpto.rs:50 visitor_count += 1; ^~~~~~~~~~~~~ zhttpto.rs:59:50: 59:63 error: use of mutable static requires unsafe function or block zhttpto.rs:59 </body></html>\r\n", visitor_count); ^~~~~~~~~~~~~
Speculate on why Rust thinks it is unsafe to modify a global variable like this.
One of the major themes of this course is how to use concurrency safely, and we will study some of the potential problems caused by mutable shared memory as well as different strategies for solving them. For now, you can just live dangerously and use an unsafe block around the dangerous code.
Modify the server so it maintains a count of the number of requests, and adds a message to the response that includes a count of the number of requests. You should see the number increase each time your reload the page.
Even with the counter, our web server is not very useful! For the rest of this assignment, your goal is to modify the web server to serve requested static pages.
If the first line of the incoming request matches
GET /<path> HTTP/1.1
<path> is a non-empty file system path. Your server should
respond by sending the contents of the file
is the current working directory (where you started the web server) in
If the incoming request contains an empty path, your server should respond as it did in the previous problem.
Modify the server to respond with requested files, as described above.
As noted above, your zhttpto server is ridiculously insecure, and will happily serve any file that is requested. We'll talk a lot more later in class about how to improve security, but for now, you should make some simple improvements to your server to restrict the files it will serve.
Modify the server to only serve files that have .html
extensions and that are in the directory where the server is started
(including any subirectories of that directory). If a requests asks for
a file that is not permitted, your server should respond with an error
page (this should use the 403 HTTP response code in place of the 200 in
the normal response; if you are feeling humorous, you can use
Use this form to submit your problem set.
We will return to the web server in Problem Set 3, where you'll learn more about network protocols, threads, and managing resources to improve the functionality and performance of your web server.