gesomax / httplib2

Automatically exported from code.google.com/p/httplib2
0 stars 0 forks source link

Negative Caching #83

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I think it's not standards conform, but sometimes it is desireable to cache
remote errors like 404 etc. Below is a runtime patch that wraps
_entry_disposition() so that it uses the Cache-Control header also for
errnous responses.

Beware:
- you must set a 'max-age' header for this to work and
- this code does not filter the Status codes, so it breaks HTTP auth if
used without changes.

# enable caching of negative http responses/bad resultsimport time, email,
calendar
_entry_disposition = httplib2._entry_disposition
def _entry_disposition2(response_headers, request_headers):
    retval = _entry_disposition(response_headers, request_headers)
    cc = httplib2._parse_cache_control(request_headers)
    if retval == 'STALE' and cc.has_key('max-age'):
        # apply cc-max-age to negative results, too
        max_age = int(cc['max-age'])
        now = httplib2.time.time()
        date =
calendar.timegm(email.Utils.parsedate_tz(response_headers['date']))
        current_age = max(0, now - date)
        if current_age < max_age:
            retval = 'FRESH'
    return retval
httplib2._entry_disposition = _entry_disposition2

Original issue reported on code.google.com by benjamin...@gmail.com on 23 Dec 2009 at 11:47

GoogleCodeExporter commented 8 years ago

Original comment by joe.gregorio@gmail.com on 15 Feb 2011 at 4:44