How to set a content type header in response to a specific file type in the Pyramid web infrastructure

I use the pyramid framework to create a website. I keep getting this warning in the chrome console:

The resource is interpreted as a font, but is transmitted using an application of the MIME / octet stream type: "http: static / images / fonts / font.woff".

How do I get rid of this warning?

I set up static files for maintenance using add_static_view

I can come up with a way to do this by adding a subscriber function for answers that checks if the path ends in .woff and sets the response header to application/x-font-woff . But this does not seem to be a clean solution. Is there any way to tell the Pyramid to do this through some settings.

+4
source share
2 answers

Pyramid uses the standard mimetypes module to guess the type of mimetype based on the extension. It calls:

 mimetypes.guess_type(path, strict=False) 

The module scans the Windows registry, if on this platform and in the following places for mimetype lists:

 knownfiles = [ "/etc/mime.types", "/etc/httpd/mime.types", # Mac OS X "/etc/httpd/conf/mime.types", # Apache "/etc/apache/mime.types", # Apache 1 "/etc/apache2/mime.types", # Apache 2 "/usr/local/etc/httpd/conf/mime.types", "/usr/local/lib/netscape/mime.types", "/usr/local/etc/httpd/conf/mime.types", # Apache 1.2 "/usr/local/etc/mime.types", # Apache 1.3 ] 

You can either expand one of these files, or create your own file and add it to the module using the .init() function.

The file format is simple, just specify the mimetype type, then a space, then a list of extensions separated by spaces:

 application/x-font-woff woff 
+6
source

Just add this following code where your Pyramid web application gets initialized.

import mimetypes mimetypes.add_type('application/x-font-woff', '.woff')

For example, I added it to my webapp.py file, which is called upon the first request to the server.

+1
source

Source: https://habr.com/ru/post/1437751/


All Articles