Login

Tag "header"

Snippet List

Header view decorators

This file includes two Django view decorators `header` and `headers` that provide an easy way to set response headers. Also, because I have to work with a lot of cross domain requests, I include few shortcuts for convenience to set the Access-Control-Allow-Origin header appropriately.

  • views
  • view
  • decorator
  • headers
  • decorators
  • header
Read More
Author: ydm
  • 1
  • 1

Add get_addr() method to request object

I thought it would be useful to have a `get_addr()` method available on request objects, similar to the `get_host()` provided by Django. This middleware will add a `get_addr()` method to requests which uses the `X-Forwarded-For` header (useful if you're behind a proxy) if it's present and you have the `USE_X_FORWARDED_FOR` header set to `True` (default is `False`) and otherwise will use the `REMOTE_ADDR` environment variable. Note that if you are *not* behind a proxy and have `USE_X_FORWARDED_FOR` set to `True`, then clients can spoof their IP by simply setting the `X-Forwarded-For header`.

  • request
  • ip
  • header
  • address
  • client
  • remote-addr
  • x-forwarded-for
  • get-addr
Read More

Improved Accept middleware with webkit workaround

An accept middleware, which is based on the code of http://djangosnippets.org/snippets/1042/ but adds a workaround for the buggy accept header, sent from webkit browsers such as safari and chrome. The workaround affects any accept header, that has xml and (x)html in the best q, but also the xml mediatype at first place in the list. If this is the case, the header is rearanged, by shifting the xml mediatype to become the last element of the best quality entries in the header. If the workaround did manipulate the header, and there is a html entry in the list with lower quality as an xhtml entry that is also in the list (with best q), then the html entry is also raised in q to be one entry in front of xml.

  • middleware
  • accept
  • header
  • webkit
Read More

Improved Accept header middleware

A clean and simple implementation of parsing the Accept header. It places the result in request.accepted_types. Place this middleware anywhere in the chain, as all it does is add to the request object.

  • middleware
  • accept
  • header
Read More

4 snippets posted so far.