Skip to content

Commit

Permalink
If-Modified-Since
Browse files Browse the repository at this point in the history
  • Loading branch information
SkywalkerSpace committed Aug 31, 2020
1 parent 3422554 commit 60119c8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
9 changes: 8 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from seatable_thumbnail.thumbnail import Thumbnail
from seatable_thumbnail.http_request import HTTPRequest
from seatable_thumbnail.http_response import gen_error_response, \
gen_text_response, gen_thumbnail_response
gen_text_response, gen_thumbnail_response, gen_cache_response

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -33,6 +33,13 @@ async def __call__(self, scope, receive, send):

# ===== thumbnail =====
elif 'thumbnail/' in request.url:
# cache
if request.headers.get('if-modified-since'):
response_start, response_body = gen_cache_response()
await send(response_start)
await send(response_body)
return

# check
try:
validator = ThumbnailValidator(request)
Expand Down
13 changes: 13 additions & 0 deletions seatable_thumbnail/http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ def __init__(self, **scope):
self.parse()

def parse(self):
self.parse_headers()
self.parse_url()
self.parse_query_dict()

def parse_headers(self):
raw_headers = self.headers
headers = {}
for item in raw_headers:
k = item[0].decode().lower()
v = item[1].decode()
if k in headers:
headers[k].append(v)
else:
headers[k] = [v]
self.headers = headers

def parse_url(self):
self.url = self.path[len(settings.URL_PREFIX):]

Expand Down
12 changes: 10 additions & 2 deletions seatable_thumbnail/http_response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from seatable_thumbnail.constants import TEXT_CONTENT_TYPE, THUMBNAIL_CONTENT_TYPE
from seatable_thumbnail.constants import TEXT_CONTENT_TYPE, THUMBNAIL_CONTENT_TYPE, \
EMPTY_BYTES


def gen_response_start(status, content_type):
Expand Down Expand Up @@ -32,11 +33,18 @@ def gen_text_response(text):
return response_start, response_body


def gen_cache_response():
response_start = gen_response_start(304, TEXT_CONTENT_TYPE)
response_body = gen_response_body(EMPTY_BYTES)

return response_start, response_body


def gen_thumbnail_response(thumbnail, last_modified):
response_start = gen_response_start(200, THUMBNAIL_CONTENT_TYPE)
response_body = gen_response_body(thumbnail)

# Cache-Control
# cache
if thumbnail:
response_start['headers'].append([b'Cache-Control', b'public'])
response_start['headers'].append([b'Cache-Control', b'max-age=86400'])
Expand Down

0 comments on commit 60119c8

Please sign in to comment.