#29925: Redirect with HTTP/2 Server Push
-------------------------------------+-------------------------------------
     Reporter:  Jaap Roes            |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  HTTP handling        |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  http2 server push    |             Triage Stage:  Accepted
  redirects                          |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Jaap Roes):

 After contemplating for a while on how to implement this nicely, I came to
 the conclusion that the easiest/neatest way to do this would be to just
 introduce a new middleware:

 {{{#!python
 from django.utils.http import is_safe_url

 class Http2ServerPushRedirectMiddleware:
     def __init__(self, get_response):
         self.get_response = get_response

     def __call__(self, request):
         response = self.get_response(request)
         if request.is_secure and response.status_code in {301, 302} and
 hasattr(response, 'url'):
             url = response.url
             if is_safe_url(url, allowed_hosts={request.get_host()},
 require_https=True):
                 response['Link'] = f'<{url}>; rel=preload'
         return response
 }}}

 This works as long as this middleware is placed before `CommonMiddleware`.

 I've updated the [https://github.com/leukeleu/django-push-redirect test
 project] to use this approach as well.

 Are you still interested in having a middleware like this in Django core?
 Releasing this as a 3rd party package could be a sufficient solution as
 well.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29925#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.5306d286d79b34034da3968722ceb24c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to