OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 <p>An error occurred while processing the request for {{uri}}:</p> | 43 <p>An error occurred while processing the request for {{uri}}:</p> |
44 <pre>{{error}}</pre> | 44 <pre>{{error}}</pre> |
45 {% endif %} | 45 {% endif %} |
46 </body> | 46 </body> |
47 </html>""" | 47 </html>""" |
48 | 48 |
49 # Create our own instance, the default one will introduce "random" host-specific | 49 # Create our own instance, the default one will introduce "random" host-specific |
50 # behavior by parsing local config files. | 50 # behavior by parsing local config files. |
51 mime_types = mimetypes.MimeTypes() | 51 mime_types = mimetypes.MimeTypes() |
52 | 52 |
53 def get_data(path): | 53 def get_page(path): |
54 if source.has_static(path): | |
55 return source.read_static(path) | |
56 | |
57 path = path.strip("/") | 54 path = path.strip("/") |
58 if path == "": | 55 if path == "": |
59 path = source.read_config().get("general", "defaultlocale") | 56 path = source.read_config().get("general", "defaultlocale") |
60 if "/" in path: | 57 if "/" in path: |
61 locale, page = path.split("/", 1) | 58 locale, page = path.split("/", 1) |
62 else: | 59 else: |
63 locale, page = path, "" | 60 locale, page = path, "" |
64 | 61 |
65 default_page = source.read_config().get("general", "defaultpage") | 62 default_page = source.read_config().get("general", "defaultpage") |
66 alternative_page = "/".join([page, default_page]).lstrip("/") | 63 alternative_page = "/".join([page, default_page]).lstrip("/") |
| 64 |
67 for format in converters.iterkeys(): | 65 for format in converters.iterkeys(): |
68 for p in (page, alternative_page): | 66 for p in (page, alternative_page): |
69 if source.has_page(p, format): | 67 if source.has_page(p, format): |
70 return process_page(source, locale, p, format, "http://127.0.0.1:5000") | 68 return (p, process_page(source, locale, p, format, "http://127.0.0.1:500
0")) |
71 if source.has_localizable_file(locale, page): | 69 if source.has_localizable_file(locale, page): |
72 return source.read_localizable_file(locale, page) | 70 return (page, source.read_localizable_file(locale, page)) |
73 | 71 |
74 return None | 72 return (None, None) |
| 73 |
| 74 def has_conflicting_pages(page): |
| 75 pages = [p for p, _ in source.list_pages()] |
| 76 pages.extend(source.list_localizable_files()) |
| 77 |
| 78 if pages.count(page) > 1: |
| 79 return True |
| 80 if any(p.startswith(page + "/") or page.startswith(p + "/") for p in pages): |
| 81 return True |
| 82 return False |
| 83 |
| 84 def get_data(path): |
| 85 if source.has_static(path): |
| 86 return source.read_static(path) |
| 87 |
| 88 page, data = get_page(path) |
| 89 if page and has_conflicting_pages(page): |
| 90 raise Exception("The requested page conflicts with another page") |
| 91 return data |
75 | 92 |
76 def show_error(start_response, status, **kwargs): | 93 def show_error(start_response, status, **kwargs): |
77 env = jinja2.Environment(autoescape=True) | 94 env = jinja2.Environment(autoescape=True) |
78 template = env.from_string(ERROR_TEMPLATE) | 95 template = env.from_string(ERROR_TEMPLATE) |
79 mime = "text/html; encoding=%s" % UNICODE_ENCODING | 96 mime = "text/html; encoding=%s" % UNICODE_ENCODING |
80 start_response(status, [("Content-Type", mime)]) | 97 start_response(status, [("Content-Type", mime)]) |
81 for fragment in template.stream(status=status, **kwargs): | 98 for fragment in template.stream(status=status, **kwargs): |
82 yield fragment.encode(UNICODE_ENCODING) | 99 yield fragment.encode(UNICODE_ENCODING) |
83 | 100 |
84 def handler(environ, start_response): | 101 def handler(environ, start_response): |
85 path = environ.get("PATH_INFO") | 102 path = environ.get("PATH_INFO") |
86 | 103 |
87 data = get_data(path) | 104 data = get_data(path) |
88 if data == None: | 105 if data is None: |
89 return show_error(start_response, "404 Not Found", uri=path) | 106 return show_error(start_response, "404 Not Found", uri=path) |
90 | 107 |
91 mime = mime_types.guess_type(path)[0] or "text/html" | 108 mime = mime_types.guess_type(path)[0] or "text/html" |
92 | 109 |
93 if isinstance(data, unicode): | 110 if isinstance(data, unicode): |
94 data = data.encode(UNICODE_ENCODING) | 111 data = data.encode(UNICODE_ENCODING) |
95 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) | 112 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) |
96 | 113 |
97 start_response("200 OK", [("Content-Type", mime)]) | 114 start_response("200 OK", [("Content-Type", mime)]) |
98 return [data] | 115 return [data] |
(...skipping 26 matching lines...) Expand all Loading... |
125 return app(environ, start_response) | 142 return app(environ, start_response) |
126 except Exception, e: | 143 except Exception, e: |
127 return show_error(start_response, "500 Internal Server Error", | 144 return show_error(start_response, "500 Internal Server Error", |
128 uri=environ.get("PATH_INFO"), error=e) | 145 uri=environ.get("PATH_INFO"), error=e) |
129 | 146 |
130 server = make_server(host, port, wrapper) | 147 server = make_server(host, port, wrapper) |
131 print " * Running on http://%s:%i/" % server.server_address | 148 print " * Running on http://%s:%i/" % server.server_address |
132 server.serve_forever() | 149 server.serve_forever() |
133 | 150 |
134 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) | 151 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) |
OLD | NEW |