May 2019
Intermediate to advanced
478 pages
11h 21m
English
Our server code uses a series of if statements to determine the proper media type based only on the requested file's extension. This isn't a perfect solution, but it is a common one, and it works for our purposes.
The code to determine a file's media type is as follows:
/*web_server.c except*/const char *get_content_type(const char* path) { const char *last_dot = strrchr(path, '.'); if (last_dot) { if (strcmp(last_dot, ".css") == 0) return "text/css"; if (strcmp(last_dot, ".csv") == 0) return "text/csv"; if (strcmp(last_dot, ".gif") == 0) return "image/gif"; if (strcmp(last_dot, ".htm") == 0) return "text/html"; if (strcmp(last_dot, ".html") == 0) return "text/html"; if (strcmp(last_dot, ".ico") == 0) ...Read now
Unlock full access