Mercurial > piecrust2
annotate piecrust/serving/util.py @ 852:4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
* Everything is a `ContentSource`, including assets directories.
* Most content sources are subclasses of the base file-system source.
* A source is processed by a "pipeline", and there are 2 built-in pipelines,
one for assets and one for pages. The asset pipeline is vaguely functional,
but the page pipeline is completely broken right now.
* Rewrite the baking process as just running appropriate pipelines on each
content item. This should allow for better parallelization.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 17 May 2017 00:11:48 -0700 |
parents | 673979a5d548 |
children | f070a4fc033c |
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 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
8 from piecrust.page import QualifiedPage, 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) |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
20 return app |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
21 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
22 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
23 class RequestedPage(object): |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
24 def __init__(self): |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
25 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
|
26 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
|
27 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
|
28 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
29 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
30 def find_routes(routes, uri, sub_num=1): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
31 """ 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
|
32 """ |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
33 res = [] |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
34 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
|
35 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
|
36 if route_params is not None: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
37 res.append((route, route_params, sub_num)) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
38 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
|
39 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
40 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
41 def get_requested_page(app, req_path): |
732
dfd9f5ee4622
serve: Fix some problems with trailing slashes.
Ludovic Chabant <ludovic@chabant.com>
parents:
723
diff
changeset
|
42 # 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
|
43 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
|
44 if req_path != root_url: |
732
dfd9f5ee4622
serve: Fix some problems with trailing slashes.
Ludovic Chabant <ludovic@chabant.com>
parents:
723
diff
changeset
|
45 req_path = req_path.rstrip('/') |
dfd9f5ee4622
serve: Fix some problems with trailing slashes.
Ludovic Chabant <ludovic@chabant.com>
parents:
723
diff
changeset
|
46 |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
47 # 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
|
48 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
|
49 |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
50 # 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
|
51 # 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
|
52 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
|
53 if page_num > 1: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
54 routes += find_routes(app.routes, req_path_no_num, page_num) |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
55 |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
56 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
|
57 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
|
58 |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
59 req_page = RequestedPage() |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
60 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
|
61 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
|
62 if route_sub_num > 1: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
63 cur_req_path = req_path_no_num |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
64 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
65 qp = _get_requested_page_for_route(app, route, route_params, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
66 route_sub_num) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
67 if qp is not None: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
68 req_page.qualified_page = qp |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
69 req_page.req_path = cur_req_path |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
70 break |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
71 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
72 req_page.not_found_errors.append(PageNotFoundError( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
73 "No path found for '%s' in source '%s'." % |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
74 (cur_req_path, route.source_name))) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
75 |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
76 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
|
77 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
78 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
79 def _get_requested_page_for_route(app, route, route_params, sub_num): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
80 source = app.getSource(route.source_name) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
81 item = source.findContent(route_params) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
82 if item is None: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
83 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
|
84 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
85 # Build the page. |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
86 page = app.getPage(item) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
771
diff
changeset
|
87 qp = QualifiedPage(page, route, route_params, 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
|
88 return qp |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
89 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
90 |
553
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 def load_mimetype_map(): |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 mimetype_map = {} |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 for line in f: |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 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
|
98 if len(tokens) > 1: |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 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
|
100 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
|
101 return mimetype_map |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 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
|
105 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
|
106 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 # 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 response = Response() |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 response.status_code = 304 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
114 return response |
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 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
|
117 response = Response(wrapper) |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 _, 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
|
119 response.set_etag(etag) |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 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
|
121 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
|
122 ext.lstrip('.'), 'text/plain') |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 response.direct_passthrough = True |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 return response |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 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
|
128 content_type_map = { |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 'html': 'text/html', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 'xml': 'text/xml', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 'txt': 'text/plain', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
132 'text': 'text/plain', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 'css': 'text/css', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 'xhtml': 'application/xhtml+xml', |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 '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
|
136 '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
|
137 'json': 'application/json'} |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 |