How to use the whitenoise.middleware.WhiteNoiseMiddleware function in whitenoise

To help you get started, we’ve selected a few whitenoise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github evansd / whitenoise / tests / test_django_whitenoise.py View on Github external
def test_unversioned_file_not_cached_forever(server, static_files, _collect_static):
    url = settings.STATIC_URL + static_files.js_path
    response = server.get(url)
    assert response.content == static_files.js_content
    assert response.headers.get("Cache-Control") == "max-age={}, public".format(
        WhiteNoiseMiddleware.max_age
    )
github evansd / whitenoise / tests / test_django_whitenoise.py View on Github external
def test_versioned_file_cached_forever(server, static_files, _collect_static):
    url = storage.staticfiles_storage.url(static_files.js_path)
    response = server.get(url)
    assert response.content == static_files.js_content
    assert response.headers.get(
        "Cache-Control"
    ) == "max-age={}, public, immutable".format(WhiteNoiseMiddleware.FOREVER)
github Splawik / pytigon / pytigon / schserw / schmiddleware / whitenoise2.py View on Github external
def __init__(self, get_response=None, settings=settings):
        WhiteNoiseMiddleware.__init__(self, get_response, settings)
        if self.static_root:
            maps = {}
            for app in settings.INSTALLED_APPS:
                if isinstance(app, AppConfigMod):
                    app_path = os.path.abspath(os.path.dirname(app.module.__file__))
                    prj_path = os.path.split(app_path)[0]
                    prj_name = os.path.split(prj_path)[-1]
                    static_path = os.path.join(prj_path, "static", prj_name)
                    if prj_name not in maps:
                        maps[prj_name] = [static_path, self.static_prefix + prj_name + "/"]
            fs = None
            if default_storage.fs:
                for pos in default_storage.fs.mounts:
                    if pos[0] == '/static/':
                        fs = pos[1]
                        break
github Splawik / pytigon / pytigon / schserw / schmiddleware / whitenoise2.py View on Github external
import os
from django.conf import settings
from whitenoise.middleware import WhiteNoiseMiddleware
from pytigon_lib.schdjangoext.django_init import AppConfigMod
from django.core.files.storage import default_storage
from fs.osfs import OSFS
from django.http import HttpResponse, HttpResponseNotFound


class WhiteNoiseMiddleware2(WhiteNoiseMiddleware):
    def __init__(self, get_response=None, settings=settings):
        WhiteNoiseMiddleware.__init__(self, get_response, settings)
        if self.static_root:
            maps = {}
            for app in settings.INSTALLED_APPS:
                if isinstance(app, AppConfigMod):
                    app_path = os.path.abspath(os.path.dirname(app.module.__file__))
                    prj_path = os.path.split(app_path)[0]
                    prj_name = os.path.split(prj_path)[-1]
                    static_path = os.path.join(prj_path, "static", prj_name)
                    if prj_name not in maps:
                        maps[prj_name] = [static_path, self.static_prefix + prj_name + "/"]
            fs = None
            if default_storage.fs:
                for pos in default_storage.fs.mounts:
                    if pos[0] == '/static/':
github evansd / whitenoise / whitenoise / middleware.py View on Github external
def candidate_paths_for_url(self, url):
        if self.use_finders and url.startswith(self.static_prefix):
            path = finders.find(url[len(self.static_prefix) :])
            if path:
                yield path
        paths = super(WhiteNoiseMiddleware, self).candidate_paths_for_url(url)
        for path in paths:
            yield path
github mozilla / normandy / normandy / base / middleware.py View on Github external
Makes RemoteUserMiddleware customizable via settings.
    """

    @property
    def header(self):
        """
        Name of request header to grab username from.

        This will be the key as used in the request.META dictionary. To
        reference HTTP headers, this value should be all upper case and
        prefixed with "HTTP_".
        """
        return settings.OIDC_REMOTE_AUTH_HEADER


class NormandyWhiteNoiseMiddleware(WhiteNoiseMiddleware):
    def is_immutable_file(self, path, url):
        """
        Determine whether given URL represents an immutable file (i.e. a
        file with a hash of its contents as part of its name) which can
        therefore be cached forever
        """
        if not url.startswith(self.static_prefix):
            return False
        filename = os.path.basename(url)
        # Check if the filename ends with either 20 or 32 hex digits, and then an extension
        # 20 is for normal hashed content, like JS or CSS files, which use "[name].[hash].[ext]"
        # 32 is for content addressed files, like images or fonts, which use "[hash].[ext]"
        match = re.match(r"^.*([a-f0-9]{20}|[a-f0-9]{32})\.[\w\d]+$", filename)
        return bool(match)

github mozilla / treeherder / treeherder / middleware.py View on Github external
"script-src 'self' 'unsafe-eval' 'report-sample'",
    # The unsafe-inline is required for react-select's use of emotion (CSS in JS). See bug 1507903.
    # The Google entries are required for IFV's use of the Open Sans font from their CDN.
    "style-src 'self' 'unsafe-inline' 'report-sample' https://fonts.googleapis.com",
    "font-src 'self' https://fonts.gstatic.com",
    # The `data:` is required for images that were inlined by webpack's url-loader (as an optimisation).
    "img-src 'self' data:",
    "connect-src 'self' https://community-tc.services.mozilla.com https://firefox-ci-tc.services.mozilla.com https://*.taskcluster-artifacts.net https://taskcluster-artifacts.net https://*.taskcluster.net https://treestatus.mozilla-releng.net https://bugzilla.mozilla.org https://auth.mozilla.auth0.com",
    # Required since auth0-js performs session renewals in an iframe.
    "frame-src 'self' https://auth.mozilla.auth0.com",
    "report-uri {}".format(reverse('csp-report')),
]
CSP_HEADER = '; '.join(CSP_DIRECTIVES)


class CustomWhiteNoise(WhiteNoiseMiddleware):
    """
    Extends WhiteNoiseMiddleware with two additional features:
    1) Adds a `Content-Security-Policy` header to all static file responses.
    2) Allows WhiteNoise to recognise Neutrino-generated hashed filenames as "immutable",
       so that WhiteNoise will then set long Cache-Control max-age headers for them.

    For the stock functionality provided by WhiteNoiseMiddleware see:
    http://whitenoise.evans.io/
    """

    # Matches Neutrino's style of hashed filename URLs, eg:
    #   /assets/index.1d85033a.js
    #   /assets/2.379789df.css.map
    #   /assets/fontawesome-webfont.af7ae505.woff2
    IMMUTABLE_FILE_RE = re.compile(r'^/assets/.*\.[a-f0-9]{8}\..*')
github iodide-project / iodide / server / middleware.py View on Github external
# this functionality cribbed from treeherder:
# https://github.com/mozilla/treeherder/blob/2501fbc10ad4ec8da6cb4d1a49472f866659ed64/treeherder/middleware.py

import re

from whitenoise.middleware import WhiteNoiseMiddleware


class CustomWhiteNoise(WhiteNoiseMiddleware):
    """
    Adds two additional features to WhiteNoise:

    1) Serving index pages for directory paths (such as the site root).
    2) Setting long max-age headers for bundled js files
    """

    # Matches webpack's style of chunk filenames. eg:
    # index.f03882a6258f16fceb70.bundle.js
    IMMUTABLE_FILE_RE = re.compile(r'\.[a-f0-9]{16,}\.bundle\.(js|css)$')

    def is_immutable_file(self, path, url):
        """Support webpack bundle filenames when setting long max-age headers."""
        if self.IMMUTABLE_FILE_RE.search(url):
            return True
        # Otherwise fall back to the default method, so we catch filenames in the
github metakermit / django-spa / spa / middleware.py View on Github external
# -*- coding: utf-8 -*-

import os

from django.conf import settings
from django.urls import is_valid_path
from django.urls.exceptions import Resolver404
from whitenoise.middleware import WhiteNoiseMiddleware


class SPAMiddleware(WhiteNoiseMiddleware):
    """Adds support for serving a single-page app (SPA)
    with frontend routing on /

    """
    index_name = 'static/index.html'

    def process_request(self, request):
        # First try to serve the static files (on /static/ and on /)
        # which is relatively fast as files are stored in a self.files dict
        if self.autorefresh:  # debug mode
            static_file = self.find_file(request.path_info)
        else:  # from the collected static files
            static_file = self.files.get(request.path_info)
        if static_file is not None:
            return self.serve(static_file, request)
        else: