Index: trunk/tools/osm-tools/cgi-bin/export |
— | — | @@ -14,7 +14,10 @@ |
15 | 15 | import tempfile |
16 | 16 | import resource |
17 | 17 | import yaml |
| 18 | +import md5 |
| 19 | +import datetime |
18 | 20 | |
| 21 | + |
19 | 22 | # |
20 | 23 | # Config data |
21 | 24 | # |
— | — | @@ -41,11 +44,22 @@ |
42 | 45 | resource.setrlimit(resource.RLIMIT_AS,(as_min, as_max)) |
43 | 46 | |
44 | 47 | # Routine to output HTTP headers |
45 | | -def output_headers(content_type, length = 0): |
| 48 | +def output_headers(content_type, length = 0, etag = 0): |
46 | 49 | 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 | + |
47 | 60 | if length: |
48 | 61 | print "Content-Length: %d" % length |
49 | 62 | print "" |
| 63 | + |
50 | 64 | |
51 | 65 | # Routine to output the contents of a file |
52 | 66 | def output_file(file): |
— | — | @@ -135,8 +149,16 @@ |
136 | 150 | if form.getvalue("format") == "png": |
137 | 151 | image = mapnik.Image(map.width, map.height) |
138 | 152 | 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()) |
141 | 163 | print png |
142 | 164 | else: |
143 | 165 | output_error("Unknown format '%s'" % form.getvalue("format")) |
Index: trunk/tools/osm-tools/cgi-bin/export-config.yml |
— | — | @@ -18,4 +18,7 @@ |
19 | 19 | maptypes: |
20 | 20 | - osm-like |
21 | 21 | |
| 22 | +caching: |
| 23 | + max-age: 604800 |
| 24 | + |
22 | 25 | locales: /sql/misc-data/wikipedia-languages.yml |