admin管理员组

文章数量:1391947

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?

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?

Share Improve this question edited May 24, 2015 at 20:39 Jonathan Leffler 756k145 gold badges951 silver badges1.3k bronze badges asked Sep 19, 2012 at 12:14 alex_acalex_ac 1291 silver badge8 bronze badges 2
  • Why do you use libmagic for this? The mime-types of .css and .js are well-defined, you don't need to guess them (and that's pretty much what libmagic does). – Joachim Sauer Commented Sep 19, 2012 at 13:23
  • I use a lot of files with different types. I don't want to hardcode their mime types. – alex_ac Commented Sep 19, 2012 at 13:57
Add a ment  | 

2 Answers 2

Reset to default 4

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.

Not sure what you are doing to need this.

Normally you would know which files are what by extension, directory etc.

Easiest would be to check extension – but guess that, for some reason, is not an option.

You can make a custom function to parse and detect ecma and css:

if (mime==text/plain) { mime = my_parser(data); }

You can use a validator and check if it validates as css or js.

You can make your own magic pattern file where you test for what you want – rather error-prone tho;

For example (very simplified)…

File my.magic:

0        search/4096    =function\ 
>&0      search/128     =)
>>&0     search/128     ={              JavaScript
!:mime   application/javascript

0        search/4096    ={
>&0      search/512     =transition-    CSS3
!:mime   text/css

0        search/4096    ={
>&0      search/512     =background:    CSS
!:mime   text/css

0        search/4096    ={
>&0      search/512     =color:         CSS
!:mime   text/css

Then pile it:

$ file -C -m my.magic

And use:

$ file -im my.magic.mgc some_file

本文标签: libmagic textplain instead of textjavascript textcssStack Overflow