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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 env = jinja2.Environment(autoescape=True) | 94 env = jinja2.Environment(autoescape=True) |
95 template = env.from_string(ERROR_TEMPLATE) | 95 template = env.from_string(ERROR_TEMPLATE) |
96 mime = "text/html; encoding=%s" % UNICODE_ENCODING | 96 mime = "text/html; encoding=%s" % UNICODE_ENCODING |
97 start_response(status, [("Content-Type", mime)]) | 97 start_response(status, [("Content-Type", mime)]) |
98 for fragment in template.stream(status=status, **kwargs): | 98 for fragment in template.stream(status=status, **kwargs): |
99 yield fragment.encode(UNICODE_ENCODING) | 99 yield fragment.encode(UNICODE_ENCODING) |
100 | 100 |
101 def handler(environ, start_response): | 101 def handler(environ, start_response): |
102 path = environ.get("PATH_INFO") | 102 path = environ.get("PATH_INFO") |
103 | 103 |
| 104 # First check for a relevant URL handler in sitescripts |
| 105 try: |
| 106 from sitescripts.web import get_handler |
| 107 dynamic_handler = get_handler(path) |
| 108 except (KeyError, ImportError): |
| 109 pass |
| 110 else: |
| 111 return dynamic_handler(environ, start_response) |
| 112 |
| 113 # Otherwise serve a static file / page |
104 data = get_data(path) | 114 data = get_data(path) |
105 if data is None: | 115 if data is None: |
106 return show_error(start_response, "404 Not Found", uri=path) | 116 return show_error(start_response, "404 Not Found", uri=path) |
107 | 117 |
108 mime = mimetypes.guess_type(path)[0] or "text/html" | 118 mime = mimetypes.guess_type(path)[0] or "text/html" |
109 | 119 |
110 if isinstance(data, unicode): | 120 if isinstance(data, unicode): |
111 data = data.encode(UNICODE_ENCODING) | 121 data = data.encode(UNICODE_ENCODING) |
112 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) | 122 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) |
113 | 123 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 return app(environ, start_response) | 162 return app(environ, start_response) |
153 except Exception, e: | 163 except Exception, e: |
154 return show_error(start_response, "500 Internal Server Error", | 164 return show_error(start_response, "500 Internal Server Error", |
155 uri=environ.get("PATH_INFO"), error=e) | 165 uri=environ.get("PATH_INFO"), error=e) |
156 | 166 |
157 server = make_server(host, port, wrapper, ThreadedWSGIServer) | 167 server = make_server(host, port, wrapper, ThreadedWSGIServer) |
158 print " * Running on http://%s:%i/" % server.server_address | 168 print " * Running on http://%s:%i/" % server.server_address |
159 server.serve_forever() | 169 server.serve_forever() |
160 | 170 |
161 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) | 171 run("localhost", 5000, handler, use_reloader=True, use_debugger=True) |
OLD | NEW |