So I’m working on a networking assignment to produce a basic HTTP/1.0 web server in C. I have most of it figured out, but one of the requirements is that it properly populate the Content-Type field in the header, and I can’t seem to find any way to do this automatically.
I’m already using fstat() to get the file size and when it was last modified, and I noticed that also includes a “st_objtype” field, but after some research it looks like this is just the AS/400 object type (which is obviously not what I need) and stat() & lstat() appear to do essentially the same thing as fstat().
Is there any way in C to automatically generate a string with the HTTP-style file type for a given file, or do I just need to make a big list of types and plug the correct value into the header based on the ending of the requested file (.txt, .html, .png, etc)?
Some examples of the Content-Type field for various files I checked:
Content-Type: text/html; charset=ISO-8859-1Content-Type: image/pngContent-Type: application/x-gzipContent-Type: application/pdf
Some systems contain a file called
/etc/mime.typeswhich contains a bunch of extensions and MIME type pairs.See the documentation for such a file.