| LEFT | RIGHT |
| 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): | 53 def get_page(path): |
| 54 pass | 54 path = path.strip("/") |
| 55 if path == "": |
| 56 path = source.read_config().get("general", "defaultlocale") |
| 57 if "/" in path: |
| 58 locale, page = path.split("/", 1) |
| 59 else: |
| 60 locale, page = path, "" |
| 55 | 61 |
| 56 def get_pages_and_localizable_files(source): | 62 default_page = source.read_config().get("general", "defaultpage") |
| 57 for page, format in source.list_pages(): | 63 alternative_page = "/".join([page, default_page]).lstrip("/") |
| 58 yield page | 64 |
| 59 for filename in source.list_localizable_files(): | 65 for format in converters.iterkeys(): |
| 60 yield filename | 66 for p in (page, alternative_page): |
| 67 if source.has_page(p, format): |
| 68 return (p, process_page(source, locale, p, format, "http://127.0.0.1:500
0")) |
| 69 if source.has_localizable_file(locale, page): |
| 70 return (page, source.read_localizable_file(locale, page)) |
| 71 |
| 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 |
| 61 | 83 |
| 62 def get_data(path): | 84 def get_data(path): |
| 63 if source.has_static(path): | 85 if source.has_static(path): |
| 64 return source.read_static(path) | 86 return source.read_static(path) |
| 65 | 87 |
| 66 path = path.strip("/") | 88 page, data = get_page(path) |
| 67 if path == "": | 89 if page and has_conflicting_pages(page): |
| 68 path = source.read_config().get("general", "defaultlocale") | 90 raise Exception("The requested page conflicts with another page") |
| 69 if "/" in path: | 91 return data |
| 70 locale, requested_page = path.split("/", 1) | |
| 71 else: | |
| 72 locale, requested_page = path, "" | |
| 73 | |
| 74 default_page = source.read_config().get("general", "defaultpage") | |
| 75 alternative_page = "/".join([requested_page, default_page]).lstrip("/") | |
| 76 | |
| 77 found_page = None | |
| 78 func = None | |
| 79 args = None | |
| 80 | |
| 81 for format in converters.iterkeys(): | |
| 82 for page in (requested_page, alternative_page): | |
| 83 if source.has_page(page, format): | |
| 84 if found_page: | |
| 85 raise ConflictingPage("The requested page correpsonds to multiple file
s") | |
| 86 | |
| 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) | |
| 107 | 92 |
| 108 def show_error(start_response, status, **kwargs): | 93 def show_error(start_response, status, **kwargs): |
| 109 env = jinja2.Environment(autoescape=True) | 94 env = jinja2.Environment(autoescape=True) |
| 110 template = env.from_string(ERROR_TEMPLATE) | 95 template = env.from_string(ERROR_TEMPLATE) |
| 111 mime = "text/html; encoding=%s" % UNICODE_ENCODING | 96 mime = "text/html; encoding=%s" % UNICODE_ENCODING |
| 112 start_response(status, [("Content-Type", mime)]) | 97 start_response(status, [("Content-Type", mime)]) |
| 113 for fragment in template.stream(status=status, **kwargs): | 98 for fragment in template.stream(status=status, **kwargs): |
| 114 yield fragment.encode(UNICODE_ENCODING) | 99 yield fragment.encode(UNICODE_ENCODING) |
| 115 | 100 |
| 116 def handler(environ, start_response): | 101 def handler(environ, start_response): |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 return app(environ, start_response) | 142 return app(environ, start_response) |
| 158 except Exception, e: | 143 except Exception, e: |
| 159 return show_error(start_response, "500 Internal Server Error", | 144 return show_error(start_response, "500 Internal Server Error", |
| 160 uri=environ.get("PATH_INFO"), error=e) | 145 uri=environ.get("PATH_INFO"), error=e) |
| 161 | 146 |
| 162 server = make_server(host, port, wrapper) | 147 server = make_server(host, port, wrapper) |
| 163 print " * Running on http://%s:%i/" % server.server_address | 148 print " * Running on http://%s:%i/" % server.server_address |
| 164 server.serve_forever() | 149 server.serve_forever() |
| 165 | 150 |
| 166 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) | 151 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) |
| LEFT | RIGHT |