Oblivious Sage
Oblivious Sage

Reputation: 3405

How to Generate the HTTP Content-Type Header in C?

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:

Upvotes: 3

Views: 3024

Answers (3)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215497

Probably the best approach is a lookup table based on extensions. The idea that a given file has a single "type" associated with it is just wrong. At least using extensions gives you a bit of power to control how the file contents are interpreted. For example if you wanted to show an example of how html source works, you could rename example.html to example.html.txt and have the client treat it as text/plain. If you just used a heuristic to determine that the file contents "are html", you'd be stuck.

Upvotes: 1

icktoofay
icktoofay

Reputation: 129109

Some systems contain a file called /etc/mime.types which contains a bunch of extensions and MIME type pairs.

See the documentation for such a file.

Upvotes: 1

Adam Mihalcin
Adam Mihalcin

Reputation: 14478

A file is only a bag of bytes, so there is no way to know for sure that a .html file doesn't contain C code, for example. You could delegate to the file command, which contains a lot of heuristics for determining these kinds of things based on the first few bytes of a file (scripts start with #!, for instance), but file still isn't foolproof. I would recommend the lookup table based on filenames.

Upvotes: 0

Related Questions