r53669 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53668‎ | r53669 | r53670 >
Date:07:19, 23 July 2009
Author:avar
Status:deferred
Tags:
Comment:
Add caching header / Etag support, patch by Kai Krueger
Modified paths:
  • /trunk/tools/osm-tools/cgi-bin/export (modified) (history)
  • /trunk/tools/osm-tools/cgi-bin/export-config.yml (modified) (history)

Diff [purge]

Index: trunk/tools/osm-tools/cgi-bin/export
@@ -14,7 +14,10 @@
1515 import tempfile
1616 import resource
1717 import yaml
 18+import md5
 19+import datetime
1820
 21+
1922 #
2023 # Config data
2124 #
@@ -41,11 +44,22 @@
4245 resource.setrlimit(resource.RLIMIT_AS,(as_min, as_max))
4346
4447 # Routine to output HTTP headers
45 -def output_headers(content_type, length = 0):
 48+def output_headers(content_type, length = 0, etag = 0):
4649 print "Content-Type: %s" % content_type
 50+
 51+ #Add caching headers
 52+ print "Cache-Control: max-age=%i" % config['caching']['max-age']
 53+ today = datetime.datetime.utcnow()
 54+ delta = datetime.timedelta(days=(config['caching']['max-age']/86400), seconds = ((config['caching']['max-age']%86400)))
 55+ expire = today + delta
 56+ print "Expires: %s GMT" % expire.strftime("%a, %d %b %Y %H:%M:%S")
 57+ if etag:
 58+ print "Etag : \"%s\"" % etag
 59+
4760 if length:
4861 print "Content-Length: %d" % length
4962 print ""
 63+
5064
5165 # Routine to output the contents of a file
5266 def output_file(file):
@@ -135,8 +149,16 @@
136150 if form.getvalue("format") == "png":
137151 image = mapnik.Image(map.width, map.height)
138152 mapnik.render(map, image)
139 - png = image.tostring("png")
140 - output_headers("image/png", len(png))
 153+ png = image.tostring("png")
 154+ etag = md5.new(png)
 155+
 156+ etagquotes = "\"%s\"" % etag.hexdigest()
 157+ if os.environ.has_key('HTTP_IF_NONE_MATCH'):
 158+ if (os.environ['HTTP_IF_NONE_MATCH'] == etagquotes):
 159+ print "Status: 304 Not Modified"
 160+ exit
 161+
 162+ output_headers("image/png", len(png),etag.hexdigest())
141163 print png
142164 else:
143165 output_error("Unknown format '%s'" % form.getvalue("format"))
Index: trunk/tools/osm-tools/cgi-bin/export-config.yml
@@ -18,4 +18,7 @@
1919 maptypes:
2020 - osm-like
2121
 22+caching:
 23+ max-age: 604800
 24+
2225 locales: /sql/misc-data/wikipedia-languages.yml

Status & tagging log