How to get the content-type of a file

前端 未结 2 399
北恋
北恋 2021-01-28 00:43

I am implementing a HTTP/1.0 server that processes GET or HEAD request.

I\'ve finished Date, Last-Modified, and

相关标签:
2条回答
  • 2021-01-28 01:07

    It depends how sophisticated you want to be.

    If the files in question are all properly named and there are only several types to handle, having a switch based file suffix is sufficient. Going to the extreme case, making the right decision no matter what the file is would probably require either duplicating the functionality of Unix file command or running it on file in question (and then translating the output to the proper Content-Type).

    0 讨论(0)
  • 2021-01-28 01:18

    You could either look at the file extension (which is what most web servers do -- see e.g. the /etc/mime.types file; or you could use libmagic to automatically determine the content type by looking at the first few bytes of the file.

    0 讨论(0)
提交回复
热议问题