On this page:
web_  server

Serving the Web

For this homework set, you will implement a rudimentary web server in Rust.

The objective of this homework is to get to know Rust’s concurrency model and constructs.

Deadline Friday 13 March NOON.


The purpose of web_server is to respond to the single command of HTTP 0.9, the GET command, which has the following shape:

  GET <path_to_file> HTTP

That is, it is the literal world GET, followed by a blank space, followed by a Unix-style path to a file, followed by another blank space and the literal token HTTP. The following line is a blank line.

In return to a valid GET command, the web server spawns a task that retrieves the command, records it in a log file, and generates a response. For this assignment, the following four forms of responses are appropriate:
  • an 200 OK status, which starts a reply that serves the specified file;

  • a 400 Bad Request, which indicates that the command is not a properly formatted GET command;

  • a 403 Forbidden, which rejects a command because it specifies a file that is off-limits;

  • a 404 Not Found, which informs the client that the specified file does not exist.

Each response is preceded by HTTP/1.0 plus a blank space.

The complete header of a 200 OK response is formatted as follows:

  HTTP/1.0 200 OK


  Content-type: text/<plain_or_html>

  Content-Length: <number_of_bytes_sent>

plus a blank line. To keep things simple, the <plain_or_html> property is either html for files whose suffix is .html or plain for all others.

The remainder of a 200-message is the content of the specified file.

A path specification <path_to_file> must start with / and is interpreted after concatenating it with the server’s root path:
  • if the resulting path points to a file, the file is served with a 200-message unless its permissions do not allow so.

  • if the resulting path points to a directory, it is interpreted as pointing to one of these files: index.html, index.shtml, and index.txt. The first file found is served assuming it is accessible. Otherwise the path triggers a 404-message.

  • otherwise the server responds with an error message.

Run your web server on localhost (, port 8000. To explore its workings, point your web browser to http://localhost:8000/main.rs (assuming you are running it out of the src directory) or use telnet:

  $ telnet 8080


  Connected to localhost.

  Escape character is '^]'.

  GET /main.rs HTTP/1.0


The result should be a response of this shape:

  HTTP/1.0 200 OK


  Content-type: text/plain

  Content-Length: 2034...


  // This module implements a simplistic HTTP server.


  use std::io;

  use std::io::{TcpListener,TcpStream};

  use std::io::{Listener,Acceptor};

  use std::thread::Thread;

  use std::io::net::tcp;



Do not try to cover additional HTTP commands. The purpose of the assignment is to prepare the Running the T project.