annotate piecrust/serving/util.py @ 1145:e94737572542

serve: Fix an issue where false positive matches were rendered as the requested page. Now we try to render the page, but also try to detect for the most common "empty" pages.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 05 Jun 2018 22:08:51 -0700
parents c8518f5cedbb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
553
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import hashlib
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import logging
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import datetime
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from werkzeug.wrappers import Response
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 from werkzeug.wsgi import wrap_file
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
8 from piecrust.page import PageNotFoundError
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
9 from piecrust.routing import RouteNotFoundError
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
10 from piecrust.uriutil import split_sub_uri
553
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 logger = logging.getLogger(__name__)
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
16 def get_app_for_server(appfactory, root_url='/'):
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
17 app = appfactory.create()
575
657384f08ca3 serve: Make it possible to preview pages with a custom root URL.
Ludovic Chabant <ludovic@chabant.com>
parents: 555
diff changeset
18 app.config.set('site/root', root_url)
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
19 app.config.set('server/is_serving', True)
862
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
20 # We'll serve page assets directly from where they are.
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
21 app.config.set('site/asset_url_format', root_url + '_asset/%path%')
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
22 return app
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
23
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
24
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
25 class RequestedPage(object):
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
26 def __init__(self):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
27 self.page = None
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
28 self.sub_num = 1
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
29 self.req_path = None
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
30 self.not_found_errors = []
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
31
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
32
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 916
diff changeset
33 def find_routes(routes, uri, decomposed_uri=None):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
34 """ Returns routes matching the given URL.
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
35 """
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 916
diff changeset
36 sub_num = 0
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 916
diff changeset
37 uri_no_sub = None
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 916
diff changeset
38 if decomposed_uri is not None:
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 916
diff changeset
39 uri_no_sub, sub_num = decomposed_uri
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 916
diff changeset
40
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
41 res = []
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
42
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
43 for route in routes:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
44 route_params = route.matchUri(uri)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
45 if route_params is not None:
863
01458d3b646b routing: Properly order routes by pass when matching them to the request.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
46 res.append((route, route_params, 1))
01458d3b646b routing: Properly order routes by pass when matching them to the request.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
47
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
48 if sub_num > 1:
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
49 for route in routes:
863
01458d3b646b routing: Properly order routes by pass when matching them to the request.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
50 route_params = route.matchUri(uri_no_sub)
01458d3b646b routing: Properly order routes by pass when matching them to the request.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
51 if route_params is not None:
01458d3b646b routing: Properly order routes by pass when matching them to the request.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
52 res.append((route, route_params, sub_num))
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
53
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
54 return res
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
55
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
56
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
57 def get_requested_pages(app, req_path):
732
dfd9f5ee4622 serve: Fix some problems with trailing slashes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
58 # Remove the trailing slash to simplify how we parse URLs.
771
673979a5d548 internal: Don't strip the trailing slash when we get the root URL exactly.
Ludovic Chabant <ludovic@chabant.com>
parents: 732
diff changeset
59 root_url = app.config.get('site/root')
673979a5d548 internal: Don't strip the trailing slash when we get the root URL exactly.
Ludovic Chabant <ludovic@chabant.com>
parents: 732
diff changeset
60 if req_path != root_url:
732
dfd9f5ee4622 serve: Fix some problems with trailing slashes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
61 req_path = req_path.rstrip('/')
dfd9f5ee4622 serve: Fix some problems with trailing slashes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
62
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
63 # Try to find what matches the requested URL.
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
64 # It could also be a sub-page (i.e. the URL ends with a page number), so
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
65 # we try to also match the base URL (without the number).
863
01458d3b646b routing: Properly order routes by pass when matching them to the request.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
66 req_path_no_sub, sub_num = split_sub_uri(app, req_path)
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 916
diff changeset
67 routes = find_routes(app.routes, req_path, (req_path_no_sub, sub_num))
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
68 if len(routes) == 0:
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
69 raise RouteNotFoundError("Can't find route for: %s" % req_path)
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
70
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
71 req_pages = []
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
72 not_founds = []
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
73
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
74 for route, route_params, route_sub_num in routes:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
75 cur_req_path = req_path
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
76 if route_sub_num > 1:
863
01458d3b646b routing: Properly order routes by pass when matching them to the request.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
77 cur_req_path = req_path_no_sub
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
78
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
79 page = _get_requested_page_for_route(app, route, route_params)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
80 if page is not None:
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
81 req_page = RequestedPage()
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
82 req_page.page = page
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
83 req_page.sub_num = route_sub_num
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
84 req_page.req_path = cur_req_path
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
85 req_pages.append(req_page)
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
86 else:
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
87 not_founds.append(PageNotFoundError(
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
88 "No path found for '%s' in source '%s'." %
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
89 (cur_req_path, route.source_name)))
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
90
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
91 return req_pages, not_founds
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
92
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
93
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
94 def get_requested_page(app, req_path):
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
95 req_pages, not_founds = get_requested_pages(app, req_path)
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
96 if req_pages:
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
97 return req_pages[0]
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
98
1145
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
99 nfrp = RequestedPage()
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
100 nfrp.not_found_errors = not_founds
e94737572542 serve: Fix an issue where false positive matches were rendered as the requested page.
Ludovic Chabant <ludovic@chabant.com>
parents: 1095
diff changeset
101 return nfrp
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
102
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
103
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
104 def _get_requested_page_for_route(app, route, route_params):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 771
diff changeset
105 source = app.getSource(route.source_name)
989
8adc27285d93 bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
106 item = source.findContentFromRoute(route_params)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
107 if item is not None:
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
108 return app.getPage(source, item)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
109 return None
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
110
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
111
553
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 def load_mimetype_map():
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 mimetype_map = {}
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 sep_re = re.compile(r'\s+')
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 path = os.path.join(os.path.dirname(__file__), 'mime.types')
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 with open(path, 'r') as f:
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 for line in f:
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 tokens = sep_re.split(line)
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 if len(tokens) > 1:
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 for t in tokens[1:]:
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 mimetype_map[t] = tokens[0]
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 return mimetype_map
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 def make_wrapped_file_response(environ, request, path):
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 # Check if we can return a 304 status code.
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 mtime = os.path.getmtime(path)
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 etag_str = '%s$$%s' % (path, mtime)
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 etag = hashlib.md5(etag_str.encode('utf8')).hexdigest()
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 if etag in request.if_none_match:
1095
c8518f5cedbb serve: Do some more useful debug logging when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents: 989
diff changeset
131 logger.debug("Serving %s [no download, E-Tag matches]" % path)
553
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 response = Response()
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 response.status_code = 304
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 return response
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135
1095
c8518f5cedbb serve: Do some more useful debug logging when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents: 989
diff changeset
136 logger.debug("Serving %s [full download]" % path)
553
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 wrapper = wrap_file(environ, open(path, 'rb'))
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 response = Response(wrapper)
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 _, ext = os.path.splitext(path)
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 response.set_etag(etag)
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 response.last_modified = datetime.datetime.fromtimestamp(mtime)
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 response.mimetype = mimetype_map.get(
916
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
143 ext.lstrip('.'), 'text/plain')
553
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 response.direct_passthrough = True
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 return response
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 mimetype_map = load_mimetype_map()
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149 content_type_map = {
916
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
150 'html': 'text/html',
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
151 'xml': 'text/xml',
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
152 'txt': 'text/plain',
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
153 'text': 'text/plain',
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
154 'css': 'text/css',
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
155 'xhtml': 'application/xhtml+xml',
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
156 'atom': 'application/atom+xml', # or 'text/xml'?
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
157 'rss': 'application/rss+xml', # or 'text/xml'?
84ce51430346 pep8: Fix indenting.
Ludovic Chabant <ludovic@chabant.com>
parents: 863
diff changeset
158 'json': 'application/json'}
553
cc6f3dbe3048 serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159