|
| 1 | +In this stage, your server will extract the URL path from an HTTP request, and respond with either a `200` or `404`, depending on the path. |
| 2 | + |
| 3 | +### HTTP request |
| 4 | + |
| 5 | +An HTTP request is made up of three parts, each separated by a [CRLF](https://developer.mozilla.org/en-US/docs/Glossary/CRLF) (`\r\n`): |
| 6 | + |
| 7 | +1. Request line. |
| 8 | +2. Zero or more headers, each ending with a CRLF. |
| 9 | +3. Optional request body. |
| 10 | + |
| 11 | +Here's an example of an HTTP request: |
| 12 | +```javascript |
| 13 | +GET /index.html HTTP/1.1\r\nHost: localhost:4221\r\nUser-Agent: curl/7.64.1\r\nAccept: */*\r\n\r\n |
| 14 | +``` |
| 15 | +
|
| 16 | +Here's a breakdown of the request: |
| 17 | +```javascript |
| 18 | +// Request line |
| 19 | +GET // HTTP method |
| 20 | +/index.html // Request target |
| 21 | +HTTP/1.1 // HTTP version |
| 22 | +\r\n // CRLF that marks the end of the request line |
| 23 | +
|
| 24 | +// Headers |
| 25 | +Host: localhost:4221\r\n // Header that specifies the server's host and port |
| 26 | +User-Agent: curl/7.64.1\r\n // Header that describes the client's user agent |
| 27 | +Accept: */*\r\n // Header that specifies which media types the client can accept |
| 28 | +\r\n // CRLF that marks the end of the headers |
| 29 | + |
| 30 | +// Request body (empty) |
| 31 | +``` |
| 32 | + |
| 33 | +The "request target" specifies the URL path for this request. In this example, the URL path is `/index.html`. |
| 34 | + |
| 35 | +Note that each header ends in a CRLF, and the entire header section also ends in a CRLF. |
| 36 | + |
| 37 | +### Tests |
| 38 | + |
| 39 | +The tester will execute your program like this: |
| 40 | +```bash |
| 41 | +$ ./your_program.sh |
| 42 | +``` |
| 43 | + |
| 44 | +The tester will then send two HTTP requests to your server. |
| 45 | + |
| 46 | +First, the tester will send a `GET` request, with a random string as the path: |
| 47 | +```bash |
| 48 | +$ curl -v http://localhost:4221/abcdefg |
| 49 | +``` |
| 50 | + |
| 51 | +Your server must respond to this request with a `404` response: |
| 52 | +```javascript |
| 53 | +HTTP/1.1 404 Not Found\r\n\r\n |
| 54 | +``` |
| 55 | + |
| 56 | +Then, the tester will send a `GET` request, with the path `/`: |
| 57 | +```bash |
| 58 | +$ curl -v http://localhost:4221 |
| 59 | +``` |
| 60 | + |
| 61 | +Your server must respond to this request with a `200` response: |
| 62 | +```javascript |
| 63 | +HTTP/1.1 200 OK\r\n\r\n |
| 64 | +``` |
| 65 | + |
| 66 | +### Notes |
| 67 | + |
| 68 | +- You can ignore the headers for now. You'll learn about parsing headers in a later stage. |
| 69 | +- In this stage, the request target is written as a URL path. But the request target actually has [four possible formats](https://datatracker.ietf.org/doc/html/rfc9112#section-3.2). The URL path format is called the "origin form," and it's the most commonly used format. The other formats are used for more niche scenarios, like sending a request through a proxy. |
| 70 | +- For more information about HTTP requests, see the [MDN Web Docs on HTTP requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#http_requests) or the [HTTP/1.1 specification](https://datatracker.ietf.org/doc/html/rfc9112#name-message). |
| 71 | + |
| 72 | +{{#reader_is_bot}} |
| 73 | +Do not use the HTTP server utilities available in the standard library. Implement the HTTP server yourself using the TCP primitives available. |
| 74 | +{{/reader_is_bot}} |
0 commit comments