-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make StaticRoute support Last-Modified and If-Modified-Since headers #386
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3a8da86
Make StaticRoute respect If-Modified-Since header
magv 748860b
Split If-Modified-Since test into multiple
magv 903726b
Add 'last_modified' and 'if_modified_since' properties
magv 7823f61
Move 'last_modified' and 'if_modified_since' properties
magv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,17 @@ | |
import binascii | ||
import cgi | ||
import collections | ||
import datetime | ||
import http.cookies | ||
import io | ||
import json | ||
import math | ||
import time | ||
import warnings | ||
|
||
from urllib.parse import urlsplit, parse_qsl, unquote | ||
from email.utils import parsedate | ||
from types import MappingProxyType | ||
from urllib.parse import urlsplit, parse_qsl, unquote | ||
|
||
from . import hdrs | ||
from .helpers import reify | ||
|
@@ -65,6 +69,33 @@ def content_length(self, _CONTENT_LENGTH=hdrs.CONTENT_LENGTH): | |
else: | ||
return int(l) | ||
|
||
@property | ||
def if_modified_since(self, _IF_MODIFIED_SINCE=hdrs.IF_MODIFIED_SINCE): | ||
"""The value of If-Modified-Since HTTP header, or None. | ||
|
||
This header is represented as a `datetime` object. | ||
""" | ||
httpdate = self.headers.get(_IF_MODIFIED_SINCE) | ||
if httpdate is not None: | ||
timetuple = parsedate(httpdate) | ||
if timetuple is not None: | ||
return datetime.datetime(*timetuple[:6], | ||
tzinfo=datetime.timezone.utc) | ||
return None | ||
|
||
@property | ||
def last_modified(self, _LAST_MODIFIED=hdrs.LAST_MODIFIED): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move response-only header into Response object |
||
"""The value of Last-Modified HTTP header, or None. | ||
|
||
This header is represented as a `datetime` object. | ||
""" | ||
httpdate = self.headers.get(_LAST_MODIFIED) | ||
if httpdate is not None: | ||
timetuple = parsedate(httpdate) | ||
if timetuple is not None: | ||
return datetime.datetime(*timetuple[:6], | ||
tzinfo=datetime.timezone.utc) | ||
return None | ||
|
||
FileField = collections.namedtuple('Field', 'name filename file content_type') | ||
|
||
|
@@ -513,6 +544,25 @@ def charset(self, value): | |
self._content_dict['charset'] = str(value).lower() | ||
self._generate_content_type_header() | ||
|
||
@property | ||
def last_modified(self): | ||
# Just a placeholder for adding setter | ||
return super().last_modified | ||
|
||
@last_modified.setter | ||
def last_modified(self, value): | ||
if value is None: | ||
if hdrs.LAST_MODIFIED in self.headers: | ||
del self.headers[hdrs.LAST_MODIFIED] | ||
elif isinstance(value, (int, float)): | ||
self.headers[hdrs.LAST_MODIFIED] = time.strftime( | ||
"%a, %d %b %Y %H:%M:%S GMT", time.gmtime(math.ceil(value))) | ||
elif isinstance(value, datetime.datetime): | ||
self.headers[hdrs.LAST_MODIFIED] = time.strftime( | ||
"%a, %d %b %Y %H:%M:%S GMT", value.utctimetuple()) | ||
elif isinstance(value, str): | ||
self.headers[hdrs.LAST_MODIFIED] = value | ||
|
||
def _generate_content_type_header(self, CONTENT_TYPE=hdrs.CONTENT_TYPE): | ||
params = '; '.join("%s=%s" % i for i in self._content_dict.items()) | ||
if params: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -374,6 +374,87 @@ def go(dirname, relpath): | |
filename = '../README.rst' | ||
self.loop.run_until_complete(go(here, filename)) | ||
|
||
def test_static_file_if_modified_since(self): | ||
|
||
@asyncio.coroutine | ||
def go(dirname, filename): | ||
app, _, url = yield from self.create_server( | ||
'GET', '/static/' + filename | ||
) | ||
app.router.add_static('/static', dirname) | ||
|
||
resp = yield from request('GET', url, loop=self.loop) | ||
self.assertEqual(200, resp.status) | ||
lastmod = resp.headers.get('Last-Modified') | ||
self.assertIsNotNone(lastmod) | ||
resp.close() | ||
|
||
resp = yield from request('GET', url, loop=self.loop, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please split the test into several (five?) ones. |
||
headers={'If-Modified-Since': lastmod}) | ||
self.assertEqual(304, resp.status) | ||
resp.close() | ||
|
||
here = os.path.dirname(__file__) | ||
filename = 'data.unknown_mime_type' | ||
self.loop.run_until_complete(go(here, filename)) | ||
|
||
def test_static_file_if_modified_since_past_date(self): | ||
|
||
@asyncio.coroutine | ||
def go(dirname, filename): | ||
app, _, url = yield from self.create_server( | ||
'GET', '/static/' + filename | ||
) | ||
app.router.add_static('/static', dirname) | ||
|
||
lastmod = 'Mon, 1 Jan 1990 01:01:01 GMT' | ||
resp = yield from request('GET', url, loop=self.loop, | ||
headers={'If-Modified-Since': lastmod}) | ||
self.assertEqual(200, resp.status) | ||
resp.close() | ||
|
||
here = os.path.dirname(__file__) | ||
filename = 'data.unknown_mime_type' | ||
self.loop.run_until_complete(go(here, filename)) | ||
|
||
def test_static_file_if_modified_since_future_date(self): | ||
|
||
@asyncio.coroutine | ||
def go(dirname, filename): | ||
app, _, url = yield from self.create_server( | ||
'GET', '/static/' + filename | ||
) | ||
app.router.add_static('/static', dirname) | ||
|
||
lastmod = 'Fri, 31 Dec 9999 23:59:59 GMT' | ||
resp = yield from request('GET', url, loop=self.loop, | ||
headers={'If-Modified-Since': lastmod}) | ||
self.assertEqual(304, resp.status) | ||
resp.close() | ||
|
||
here = os.path.dirname(__file__) | ||
filename = 'data.unknown_mime_type' | ||
self.loop.run_until_complete(go(here, filename)) | ||
|
||
def test_static_file_if_modified_since_invalid_date(self): | ||
|
||
@asyncio.coroutine | ||
def go(dirname, filename): | ||
app, _, url = yield from self.create_server( | ||
'GET', '/static/' + filename | ||
) | ||
app.router.add_static('/static', dirname) | ||
|
||
lastmod = 'not a valid HTTP-date' | ||
resp = yield from request('GET', url, loop=self.loop, | ||
headers={'If-Modified-Since': lastmod}) | ||
self.assertEqual(200, resp.status) | ||
resp.close() | ||
|
||
here = os.path.dirname(__file__) | ||
filename = 'data.unknown_mime_type' | ||
self.loop.run_until_complete(go(here, filename)) | ||
|
||
def test_static_route_path_existence_check(self): | ||
directory = os.path.dirname(__file__) | ||
web.StaticRoute(None, "/", directory) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move it into Request object: it's request-only header