serve_resource()

The serve_resource() function sends a connected client a requested resource. Our server expects all hosted files to be in a subdirectory called public. Ideally, our server should not allow access to any files outside of this public directory. However, as we shall see, enforcing this restriction may be more difficult than it first appears.

Our serve_resource() function takes as arguments a connected client and a requested resource path. The function begins as follows:

/*web_server.c except*/void serve_resource(struct client_info *client, const char *path) {    printf("serve_resource %s %s\n", get_client_address(client), path);

The connected client's IP address and the requested path are printed to aid in debugging. In a production ...

Get Hands-On Network Programming with C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.