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 class ConflictingPage(Exception): |
| 54 pass |
| 55 |
| 56 def get_pages_and_localizable_files(source): |
| 57 for page, format in source.list_pages(): |
| 58 yield page |
| 59 for filename in source.list_localizable_files(): |
| 60 yield filename |
| 61 |
53 def get_data(path): | 62 def get_data(path): |
54 if source.has_static(path): | 63 if source.has_static(path): |
55 return source.read_static(path) | 64 return source.read_static(path) |
56 | 65 |
57 path = path.strip("/") | 66 path = path.strip("/") |
58 if path == "": | 67 if path == "": |
59 path = source.read_config().get("general", "defaultlocale") | 68 path = source.read_config().get("general", "defaultlocale") |
60 if "/" in path: | 69 if "/" in path: |
61 locale, page = path.split("/", 1) | 70 locale, requested_page = path.split("/", 1) |
62 else: | 71 else: |
63 locale, page = path, "" | 72 locale, requested_page = path, "" |
64 | 73 |
65 default_page = source.read_config().get("general", "defaultpage") | 74 default_page = source.read_config().get("general", "defaultpage") |
66 alternative_page = "/".join([page, default_page]).lstrip("/") | 75 alternative_page = "/".join([requested_page, default_page]).lstrip("/") |
| 76 |
| 77 found_page = None |
| 78 func = None |
| 79 args = None |
| 80 |
67 for format in converters.iterkeys(): | 81 for format in converters.iterkeys(): |
68 for p in (page, alternative_page): | 82 for page in (requested_page, alternative_page): |
69 if source.has_page(p, format): | 83 if source.has_page(page, format): |
70 return process_page(source, locale, p, format, "http://127.0.0.1:5000") | 84 if found_page: |
71 if source.has_localizable_file(locale, page): | 85 raise ConflictingPage("The requested page correpsonds to multiple file
s") |
72 return source.read_localizable_file(locale, page) | |
73 | 86 |
74 return None | 87 found_page = page |
| 88 func = process_page |
| 89 args = (source, locale, page, format, "http://127.0.0.1:5000") |
| 90 |
| 91 if source.has_localizable_file(locale, requested_page): |
| 92 if found_page: |
| 93 raise ConflictingPage("The requested page conflicts with a localizable fil
e") |
| 94 |
| 95 found_page = requested_page |
| 96 func = source.read_localizable_file |
| 97 args = (locale, requested_page) |
| 98 |
| 99 if not found_page: |
| 100 return None |
| 101 |
| 102 for page in get_pages_and_localizable_files(source): |
| 103 if page.startswith(found_page + "/") or found_page.startswith(page + "/"): |
| 104 raise ConflictingPage("The requested path conflicts with the path of anoth
er page") |
| 105 |
| 106 return func(*args) |
75 | 107 |
76 def show_error(start_response, status, **kwargs): | 108 def show_error(start_response, status, **kwargs): |
77 env = jinja2.Environment(autoescape=True) | 109 env = jinja2.Environment(autoescape=True) |
78 template = env.from_string(ERROR_TEMPLATE) | 110 template = env.from_string(ERROR_TEMPLATE) |
79 mime = "text/html; encoding=%s" % UNICODE_ENCODING | 111 mime = "text/html; encoding=%s" % UNICODE_ENCODING |
80 start_response(status, [("Content-Type", mime)]) | 112 start_response(status, [("Content-Type", mime)]) |
81 for fragment in template.stream(status=status, **kwargs): | 113 for fragment in template.stream(status=status, **kwargs): |
82 yield fragment.encode(UNICODE_ENCODING) | 114 yield fragment.encode(UNICODE_ENCODING) |
83 | 115 |
84 def handler(environ, start_response): | 116 def handler(environ, start_response): |
85 path = environ.get("PATH_INFO") | 117 path = environ.get("PATH_INFO") |
86 | 118 |
87 data = get_data(path) | 119 data = get_data(path) |
88 if data == None: | 120 if data is None: |
89 return show_error(start_response, "404 Not Found", uri=path) | 121 return show_error(start_response, "404 Not Found", uri=path) |
90 | 122 |
91 mime = mime_types.guess_type(path)[0] or "text/html" | 123 mime = mime_types.guess_type(path)[0] or "text/html" |
92 | 124 |
93 if isinstance(data, unicode): | 125 if isinstance(data, unicode): |
94 data = data.encode(UNICODE_ENCODING) | 126 data = data.encode(UNICODE_ENCODING) |
95 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) | 127 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) |
96 | 128 |
97 start_response("200 OK", [("Content-Type", mime)]) | 129 start_response("200 OK", [("Content-Type", mime)]) |
98 return [data] | 130 return [data] |
(...skipping 26 matching lines...) Expand all Loading... |
125 return app(environ, start_response) | 157 return app(environ, start_response) |
126 except Exception, e: | 158 except Exception, e: |
127 return show_error(start_response, "500 Internal Server Error", | 159 return show_error(start_response, "500 Internal Server Error", |
128 uri=environ.get("PATH_INFO"), error=e) | 160 uri=environ.get("PATH_INFO"), error=e) |
129 | 161 |
130 server = make_server(host, port, wrapper) | 162 server = make_server(host, port, wrapper) |
131 print " * Running on http://%s:%i/" % server.server_address | 163 print " * Running on http://%s:%i/" % server.server_address |
132 server.serve_forever() | 164 server.serve_forever() |
133 | 165 |
134 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) | 166 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) |
OLD | NEW |