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

孤街醉人 提交于 2020-01-04 04:13:10

问题


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-1
  • Content-Type: image/png
  • Content-Type: application/x-gzip
  • Content-Type: application/pdf

回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9137732/how-to-generate-the-http-content-type-header-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!