Changes between Initial Version and Version 1 of Ticket #29925, comment 4


Ignore:
Timestamp:
Nov 8, 2018, 9:38:37 AM (6 years ago)
Author:
Jaap Roes

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #29925, comment 4

    initial v1  
    55
    66class Http2ServerPushRedirectMiddleware:
     7    redirect_status_codes = {301, 302}
     8
    79    def __init__(self, get_response):
    810        self.get_response = get_response
    911
     12    def should_preload(self, request, response):
     13        return (
     14            request.is_secure
     15            and response.status_code in self.redirect_status_codes
     16            and hasattr(response, 'url')
     17            and not response.has_header('Link')
     18        )
     19
    1020    def __call__(self, request):
    1121        response = self.get_response(request)
    12         if request.is_secure and response.status_code in {301, 302} and hasattr(response, 'url'):
     22        if self.should_preload(request, response):
    1323            url = response.url
    1424            if is_safe_url(url, allowed_hosts={request.get_host()}, require_https=True):
    1525                response['Link'] = f'<{url}>; rel=preload'
    16         return response
     26    return response
    1727}}}
    1828
Back to Top