Handling cache with ETag and If-None-Match headers.

This commit is contained in:
Ozbolt Menegatti 2019-09-17 10:35:14 +02:00
parent 3dc2ca0ebc
commit 44915ea014

View File

@ -13,8 +13,8 @@ then $LOCATION$ gets the value of http://example.com/myplugin
import sys
import os.path
import mimetypes
import hashlib
from base64 import b64encode
import string
import random
import redis
from flask import Flask, Response, request
@ -56,6 +56,10 @@ def check_cache(full_path):
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>")
def plugin_file(plugin, path):
full_path = "{}/{}".format(plugin, path)
@ -83,11 +87,11 @@ def plugin_file(plugin, path):
resp = Response(result, mimetype=mt)
resp.headers.add('Access-Control-Allow-Origin', '*')
resp.headers.add('ETag', generate_etag())
resp.status_code = status_code
# return everything if cached response is not allowed
if 'Cache-Control' in request.headers:
if request.headers['Cache-Control'] in ('no-cache', 'max-age=0'):
if 'If-None-Match' not in request.headers:
resp.status_code = 200
return resp