I'm creating a web server to practice my programming skills/improve my knowledge, I'd say it's from scratch but I'm using Java sockets which handle a big deal of complexity.
My challenge then is creating an implementation of the HTTP protocol to handle the communication between server and clients.
A request message from a client to a server includes, within the first line of that message, the method to be applied to the resource, the identifier of the resource, and the protocol version in use.
The first line of the request message must include:
- Method (GET, POST, HEAD, etc.)
- Request-URI (to be affected by the method)
- HTTP-Version (protocol version, in this case HTTP/1.1)
- CRLF (US-ASCII CR, carriage return (13), US-ASCII LF, linefeed (10), in other words a line terminator or newline)
The elements should be separated by SP characters (US-ASCII SP, space (32)).
After receiving and interpreting a request message, a server responds with an HTTP response message.
The response should begin with:
- HTTP-Version (same as above, HTTP/1.1)
- Status-Code (3-digit number that represents the result of processing the request, see full definitions)
- Reason-Phrase (short textual description of the status code)
- CRLF (line terminator)
And again, the elements must be separated by the defined SP characters.
These are the basic elements of HTTP requests and responses, the web server implementation gets more complicated as you support different media types, streaming, concurrent connections, etc.
In order to validate that our web server adheres to the HTTP protocol, we can use a suite of acceptance tests called Cob Spec. The tests were written using a framework called FitNesse which runs in your browser as a wiki. It's very easy to use and the installation is fairly straightforward, just go through the instructions in the project Read Me. At the time of writing I just had to clone a couple of GitHub repos, and have Java and Ruby installed.
Once you run Cob Spec you can access the tests suite in your browser, which lists the different test cases. You'll have to edit a couple of variables (from the website) in order to run them.
To run a test case go to "HttpTestSuite" from the main menu and navigate through the list, then click "Test". Otherwise click "Suite" to run everything.
Cob Spec will start your server on each test case (given a command you specify in your variables, e.g.
/path/to/server/bin/run), then it will send different requests to your server and expect the appropriate response.
Here's a screenshot of a test passing for a simple
In this example, the test passes when it receives an HTTP response with a status code of 200 (OK).