Mercurial > piecrust2
annotate piecrust/serving/util.py @ 723:606f6d57b5df
routing: Cleanup URL routing and improve page matching.
* Add new types of route parameters for integers (int4, int2, int).
* Remove hard-coded hacks around converting year/month/day values.
* Make the blog post routes use the new typed parameters.
* Fix problems with matching routes with integer parameters when they can
get confused with a sub-page number.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 29 May 2016 20:19:28 -0700 |
parents | ab5c6a8ae90a |
children | dfd9f5ee4622 |
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 |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
8 from piecrust.app import PieCrust, apply_variant_and_values |
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.rendering import QualifiedPage |
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.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
|
11 from piecrust.sources.base import MODE_PARSING |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
12 from piecrust.sources.pageref import PageNotFoundError |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
13 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
|
14 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 logger = logging.getLogger(__name__) |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
19 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
|
20 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
|
21 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
|
22 app.config.set('server/is_serving', True) |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
23 return app |
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 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
26 class RequestedPage(object): |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
27 def __init__(self): |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
28 self.qualified_page = None |
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.page_num = 1 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
31 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
|
32 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
33 |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
34 def find_routes(routes, uri, is_sub_page=False): |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
35 """ Returns routes matching the given URL, but puts generator routes |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
36 at the end. |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
37 """ |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
38 res = [] |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
39 gen_res = [] |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
40 for route in routes: |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
41 metadata = route.matchUri(uri) |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
42 if metadata is not None: |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
43 if route.is_source_route: |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
44 res.append((route, metadata, is_sub_page)) |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
45 else: |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
46 gen_res.append((route, metadata, is_sub_page)) |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
47 return res + gen_res |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
48 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
49 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
50 def get_requested_page(app, req_path): |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
51 # 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
|
52 routes = find_routes(app.routes, req_path) |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
53 |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
54 # 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
|
55 # we try to also match the base URL (without the number). |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
56 req_path_no_num, page_num = split_sub_uri(app, req_path) |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
57 if page_num > 1: |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
58 routes += find_routes(app.routes, req_path_no_num, True) |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
59 |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
60 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
|
61 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
|
62 |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
63 req_page = RequestedPage() |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
64 for route, route_metadata, is_sub_page in routes: |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
65 try: |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
66 cur_req_path = req_path |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
67 if is_sub_page: |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
68 cur_req_path = req_path_no_num |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
69 |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
70 qp = _get_requested_page_for_route( |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
71 app, route, route_metadata, cur_req_path) |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
72 if qp is not None: |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
73 req_page.qualified_page = qp |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
74 req_page.req_path = cur_req_path |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
75 if is_sub_page: |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
76 req_page.page_num = page_num |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
77 break |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
78 except PageNotFoundError as nfe: |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
79 req_page.not_found_errors.append(nfe) |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
80 return req_page |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
81 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
82 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
83 def _get_requested_page_for_route(app, route, route_metadata, req_path): |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
84 if not route.is_generator_route: |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
85 source = app.getSource(route.source_name) |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
86 factory = source.findPageFactory(route_metadata, MODE_PARSING) |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
87 if factory is None: |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
88 raise PageNotFoundError( |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
89 "No path found for '%s' in source '%s'." % |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
90 (req_path, source.name)) |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
91 else: |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
92 factory = route.generator.getPageFactory(route_metadata) |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
93 if factory is None: |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
94 raise PageNotFoundError( |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
95 "No path found for '%s' in generator '%s'." % |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
96 (req_path, route.generator.name)) |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
97 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
98 # Build the page. |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
99 page = factory.buildPage() |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
100 qp = QualifiedPage(page, route, route_metadata) |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
101 return qp |
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 |
553
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 def load_mimetype_map(): |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 mimetype_map = {} |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 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
|
107 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
|
108 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
|
109 for line in f: |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 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
|
111 if len(tokens) > 1: |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 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
|
113 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
|
114 return mimetype_map |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 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
|
118 logger.debug("Serving %s" % path) |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 # 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
|
121 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
|
122 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
|
123 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
|
124 if etag in request.if_none_match: |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 response = Response() |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 response.status_code = 304 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 return response |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 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
|
130 response = Response(wrapper) |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 _, 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
|
132 response.set_etag(etag) |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 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
|
134 response.mimetype = mimetype_map.get( |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 ext.lstrip('.'), 'text/plain') |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 response.direct_passthrough = True |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 return response |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 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
|
141 content_type_map = { |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
142 'html': 'text/html', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 'xml': 'text/xml', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 'txt': 'text/plain', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 'text': 'text/plain', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 'css': 'text/css', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
147 'xhtml': 'application/xhtml+xml', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
148 'atom': 'application/atom+xml', # or 'text/xml'? |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 'rss': 'application/rss+xml', # or 'text/xml'? |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 'json': 'application/json'} |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
151 |