javascriptcssclibmagic

libmagic. text/plain instead of text/javascript text/css


I use libmagic to get the mime type of a file in a web interface of my project. I get text/plain mime type on css and js files.

For example chromium shows below warnings:

Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost:8000/jquery-ui.css".
Resource interpreted as Script but transferred with MIME type text/plain: "http://localhost:8000/jquery.timers.js".

Http dialog

alex@alex-laptop ~ $ nc localhost 8000
GET /ui.css HTTP/1.1


HTTP/1.1 200 OK
Connection: close
Content-Length: 699
Content-Type: text/plain; charset=us-ascii
Date: Wed, 19 Sep 2012 11:41:48 GMT

...

How can I fix this?


Solution

  • You'll need to do the same thing as Apache: consult a database of extensions for the MIME type, and, if you fail, consult libmagic.

    Text and XML files are often too generic for libmagic to figure out. libmagic is only meant to determine a file type by examining a few bytes, so CSS and JavaScript are insufficiently distinct to determine their exact types. In the case of XML, libmagic would have to have fairly sophisticated rules to determine if a file was XHTML, SVG, XHTML+SVG, or an XSLT that produces XHTML and/or SVG. That's beyond its scope.