Skip to content
This repository
Browse code

add docstring for web.modified; enhance function slightly

  • Loading branch information...
commit d12fbe85b88941542eb928fb4950cc12cda5fc03 1 parent 18ff819
Aaron Swartz aaronsw authored anandology committed

Showing 1 changed file with 20 additions and 1 deletion. Show diff stats Hide diff stats

  1. +20 1 web/http.py
21 web/http.py
@@ -43,6 +43,23 @@ def lastmodified(date_obj):
43 43 web.header('Last-Modified', net.httpdate(date_obj))
44 44
45 45 def modified(date=None, etag=None):
  46 + """
  47 + Checks to see if the page has been modified since the version in the
  48 + requester's cache.
  49 +
  50 + When you publish pages, you can include `Last-Modified` and `ETag`
  51 + with the date the page was last modified and an opaque token for
  52 + the particular version, respectively. When readers reload the page,
  53 + the browser sends along the modification date and etag value for
  54 + the version it has in its cache. If the page hasn't changed,
  55 + the server can just return `304 Not Modified` and not have to
  56 + send the whole page again.
  57 +
  58 + This function takes the last-modified date `date` and the ETag `etag`
  59 + and checks the headers to see if they match. If they do, it returns
  60 + `True` and sets the response status to `304 Not Modified`. It also
  61 + sets `Last-Modified and `ETag` output headers.
  62 + """
46 63 n = set(x.strip('" ') for x in web.ctx.env.get('HTTP_IF_NONE_MATCH', '').split(','))
47 64 m = net.parsehttpdate(web.ctx.env.get('HTTP_IF_MODIFIED_SINCE', '').split(';')[0])
48 65 validate = False
@@ -54,8 +71,10 @@ def modified(date=None, etag=None):
54 71 # HTTP dates don't have sub-second precision
55 72 if date-datetime.timedelta(seconds=1) <= m:
56 73 validate = True
57   -
  74 +
58 75 if validate: web.ctx.status = '304 Not Modified'
  76 + lastmodified(date)
  77 + web.header('ETag', '"' + etag + '"')
59 78 return not validate
60 79
61 80 def write(cgi_response):

0 comments on commit d12fbe8

Please sign in to comment.
Something went wrong with that request. Please try again.