How to add custom headers to static view?

53 views
Skip to first unread message

Marcin Raczyński

unread,
Jul 8, 2016, 1:25:45 PM7/8/16
to pylons-discuss
I have static view configured:
   
config.add_static_view(name='static', path='mypackage:assets')

I want every static file response has header "X-Content-Type-Options".

How can I do it?

Michael Merickel

unread,
Jul 8, 2016, 2:07:28 PM7/8/16
to Pylons
Easiest option is to just add a tween or NewResponse subscriber to your app that handles responses from that url prefix.

If you don't like hard-coding the url prefix, then another option is to use a route factory on the static view which can annotate the request. You can then use this info in a tween or NewResponse subscriber to determine whether to set the header. I have a gist of this method here https://gist.github.com/mmerickel/d9efb2ab358be75e6767 which you may find useful.

In the future I'd like to make the static view more extensible in this regard. In 1.8 we are considering adding support for view derivers that know about static views, so they can wrap them exclusively. You could then define a view deriver for static views which could affect only their responses with no performance impact on other views.

There are a couple other tickets open with respect to wrapping the static view response as well.



--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/f925aadb-8210-4841-8e32-c41abceb3164%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vincent Catalano

unread,
Jul 8, 2016, 2:08:20 PM7/8/16
to pylons-...@googlegroups.com
One way to do this would be to serve your static content using a view callable and add the X-Content-Type-Options header the response: http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/assets.html#advanced-serving-static-assets-using-a-view-callable

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/f925aadb-8210-4841-8e32-c41abceb3164%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Vincent Catalano
Software Engineer and Web Ninja,
(520).603.8944

Marcin Raczyński

unread,
Jul 8, 2016, 4:59:09 PM7/8/16
to pylons-discuss
Thanks Vincent!
What if I still want to use request.static_url() and have custom headers in static assets?

Marcin Raczyński

unread,
Jul 8, 2016, 5:08:50 PM7/8/16
to pylons-discuss

Thanks Michael a lot!
It is exactly what I was looking for. 
It would be nice to see changes in Pyramid which make such customizations more developer friendly.

Piotr Dobrogost

unread,
Jul 11, 2016, 10:41:12 AM7/11/16
to pylons-discuss
On Friday, July 8, 2016 at 8:07:28 PM UTC+2, Michael Merickel wrote:

In the future I'd like to make the static view more extensible in this regard. In 1.8 we are considering

Have you thought about giving user the ability to specify class used by add_static_view()?

Regards,
Piotr Dobrogost

Reply all
Reply to author
Forward
0 new messages