Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the content type header in response for a particular file type in Pyramid web framework

Tags:

python

pyramid

I am using pyramid web framework to build a website. I keep getting this warning in chrome console:

Resource interpreted as Font but transferred with MIME type application/octet-stream: "http:static/images/fonts/font.woff".

How do I get rid of this warning message?

I have configured static files to be served using add_static_view

I can think of a way to do this by adding a subscriber function for responses that checks if the path ends in .woff and setting the response header to application/x-font-woff. But it does not look like a clean solution. Is there a way to tell Pyramid to do it through some setting.

like image 349
Ranjith Ramachandra Avatar asked Feb 19 '23 23:02

Ranjith Ramachandra


2 Answers

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

mimetypes.guess_type(path, strict=False)

The module looks in the Windows registry if on that platform, and in the following locations 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 extend one of those files, or create your own file and add it to the module using the .init() function.

The file format is simple, just list the mimetype, then some whitespace, then a space-separated list of extensions:

application/x-font-woff     woff
like image 58
Martijn Pieters Avatar answered Feb 27 '23 09:02

Martijn Pieters


Simply add this following code where your Pyramid web app gets initialized.

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

For instance, I have added it in my webapp.py file, which gets called the first time the server gets hit with a request.

like image 44
kashiB Avatar answered Feb 27 '23 11:02

kashiB