Handling cache with ETag and If-None-Match headers.
This commit is contained in:
parent
3dc2ca0ebc
commit
44915ea014
|
@ -13,8 +13,8 @@ then $LOCATION$ gets the value of http://example.com/myplugin
|
||||||
import sys
|
import sys
|
||||||
import os.path
|
import os.path
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import hashlib
|
import string
|
||||||
from base64 import b64encode
|
import random
|
||||||
|
|
||||||
import redis
|
import redis
|
||||||
from flask import Flask, Response, request
|
from flask import Flask, Response, request
|
||||||
|
@ -56,6 +56,10 @@ def check_cache(full_path):
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
|
||||||
|
def generate_etag(N=12):
|
||||||
|
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<plugin>/<path:path>")
|
@app.route("/<plugin>/<path:path>")
|
||||||
def plugin_file(plugin, path):
|
def plugin_file(plugin, path):
|
||||||
full_path = "{}/{}".format(plugin, path)
|
full_path = "{}/{}".format(plugin, path)
|
||||||
|
@ -83,12 +87,12 @@ def plugin_file(plugin, path):
|
||||||
|
|
||||||
resp = Response(result, mimetype=mt)
|
resp = Response(result, mimetype=mt)
|
||||||
resp.headers.add('Access-Control-Allow-Origin', '*')
|
resp.headers.add('Access-Control-Allow-Origin', '*')
|
||||||
|
resp.headers.add('ETag', generate_etag())
|
||||||
resp.status_code = status_code
|
resp.status_code = status_code
|
||||||
|
|
||||||
# return everything if cached response is not allowed
|
# return everything if cached response is not allowed
|
||||||
if 'Cache-Control' in request.headers:
|
if 'If-None-Match' not in request.headers:
|
||||||
if request.headers['Cache-Control'] in ('no-cache', 'max-age=0'):
|
resp.status_code = 200
|
||||||
resp.status_code = 200
|
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user