Skip to content

Commit

Permalink
Better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Beej Jorgensen committed Mar 7, 2018
1 parent 0bbdb4d commit 38d6fcb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,38 @@ Don't worry: the networking code is already written.

### Main Goals

1. Add parsing of the first line of the HTTP request header that arrives. It
will be in the `request` array.
1. Add parsing of the first line of the HTTP request header that arrives in the
`handle_http_request()` function. It will be in the `request` array.

Read the three components from the first line of the HTTP header. Hint:
`sscanf()`.

Decide which handler to call based on the request type (`GET`, `POST`) and
the path (`/`, `/d20`, etc.)
Right after that, call the appropriate handler based on the request type
(`GET`, `POST`) and the path (`/`, `/d20`, etc.) You can start by just
checking for `/` and add the others later as you get to them. Hint:
`strcmp()`. Another hint: `strcmp()` returns `0 if the strings are the
_same_!

2. Implement the `get_root()` handler. This will call `send_response()`.
If you can't find an appropriate handler, call `resp_404()` instead to give them a "404 Not Found" response.

2. Implement the `get_root()` handler. This will call `send_response()`. If you
need a hint as to what the `send_response()` call should look like, check out
the usage of it in `resp_404()`, just above there.

> The `fd` variable that is passed widely around to all the functions holds a
> _file descriptor_. It's just a number use to represent an open
> communications path. Usually they point to regular files on disk, but in
> the case it points to an open _socket_ network connection. All of the code
> to create and use `fd` has been written already, but we still need to pass
> it around to the points it is used.
3. Implement `send_response()`. Hint: `sprintf()`, `strlen()` for computing
content length.
content length. `sprintf()` also returns the total number of bytes in the
result string, which might be helpful.

> The HTTP `Content-Length` header only includes the length of the body, not
> the header. But the `response_length` variable used by `send()` is the
> total length of both header and body.
4. Implement the `get_d20()` handler. Hint: `srand()` with `time(NULL)`,
`rand()`.
Expand Down
5 changes: 4 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ void handle_http_request(int fd)

// !!!! IMPLEMENT ME
// Get the request type and path from the first line
// Hint: sscanf()!

// !!!! IMPLEMENT ME (stretch goal)
// find_end_of_header()
// call the appropriate handler functions, above, with the incoming data
}
Expand Down Expand Up @@ -337,7 +340,7 @@ int main(void)
// newfd is a new socket descriptor for the new connection.
// listenfd is still listening for new connections.

// !!!! IMPLEMENT ME
// !!!! IMPLEMENT ME (stretch goal)
// Convert this to be multiprocessed with fork()

handle_http_request(newfd);
Expand Down

0 comments on commit 38d6fcb

Please sign in to comment.