Index: cms/bin/test_server.py |
=================================================================== |
--- a/cms/bin/test_server.py |
+++ b/cms/bin/test_server.py |
@@ -10,70 +10,115 @@ |
# Adblock Plus is distributed in the hope that it will be useful, |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
# GNU General Public License for more details. |
# |
# You should have received a copy of the GNU General Public License |
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
-import sys, os, flask |
+import mimetypes |
+import os |
+import sys |
+ |
+import jinja2 |
+ |
from ..utils import process_page |
from ..sources import FileSource |
from ..converters import converters |
-app = flask.Flask("cms.bin.test_server") |
source = None |
-mime_types = { |
- "": "text/html; charset=utf-8", |
- ".htm": "text/html; charset=utf-8", |
- ".html": "text/html; charset=utf-8", |
- ".js": "application/javascript; charset=utf-8", |
- ".css": "text/css; charset=utf-8", |
- ".xml": "text/xml; charset=utf-8", |
- ".png": "image/png", |
- ".jpg": "image/jpeg", |
- ".jpeg": "image/jpeg", |
-} |
+ERROR_404 = """ |
+<html> |
+ <head> |
+ <title>404 Not Found</title> |
+ </head> |
+ <body> |
+ <h1>404 Not Found</h1> |
+ <p>No page found for the address {{uri}}.</p> |
+ </body> |
+</html>""" |
+ |
+ERROR_500 = """ |
Sebastian Noack
2015/03/23 12:06:47
How about using a common template for both error p
Wladimir Palant
2015/03/23 12:27:26
Done.
|
+<html> |
+ <head> |
+ <title>500 Internal Server Error</title> |
+ </head> |
+ <body> |
+ <h1>500 Internal Server Error</h1> |
+ <p>An error occurred while processing the request for {{uri}}:</p> |
+ <pre>{{error}}</pre> |
+ </body> |
+</html>""" |
+ |
+mime_types = mimetypes.MimeTypes() |
+mime_types.add_type("text/html", "") |
def get_data(path): |
if source.has_static(path): |
return source.read_static(path) |
- path = path.rstrip("/") |
+ path = path.strip("/") |
if path == "": |
path = source.read_config().get("general", "defaultlocale") |
if "/" in path: |
locale, page = path.split("/", 1) |
else: |
locale, page = path, "" |
default_page = source.read_config().get("general", "defaultpage") |
alternative_page = "/".join([page, default_page]).lstrip("/") |
for format in converters.iterkeys(): |
for p in (page, alternative_page): |
if source.has_page(p, format): |
- return process_page(source, locale, p, format, "http://127.0.0.1:5000").encode("utf-8") |
+ return process_page(source, locale, p, format, "http://127.0.0.1:5000") |
if source.has_localizable_file(locale, page): |
return source.read_localizable_file(locale, page) |
return None |
-@app.route("/", methods = ["GET"]) |
-@app.route("/<path:path>", methods = ["GET"]) |
-def show(path=""): |
+def show_template(start_response, status, template_data, **kwargs): |
+ env = jinja2.Environment(autoescape=True) |
+ template = env.from_string(template_data) |
+ start_response(status, [("Content-Type", "text/html; encoding=utf-8")]) |
+ return [template.render(**kwargs).encode("utf-8")] |
Sebastian Noack
2015/03/23 12:06:47
How about this?
for fragment in template.stream
Wladimir Palant
2015/03/23 12:27:26
Done.
|
+ |
+def handler(environ, start_response): |
+ path = environ.get("PATH_INFO") |
+ |
data = get_data(path) |
if data == None: |
- flask.abort(404) |
+ return show_template(start_response, "404 Not Found", ERROR_404, uri=path) |
- root, ext = os.path.splitext(path) |
- mime = mime_types.get(ext.lower(), "application/octet-stream") |
- return data, 200, {"Content-Type": mime} |
+ mime = mime_types.guess_type(path)[0] or "application/octet-stream" |
+ |
+ if isinstance(data, unicode): |
+ data = data.encode("utf-8") |
+ mime += "; charset=utf-8" |
Sebastian Noack
2015/03/23 12:06:47
Maybe we should add a (global) variable for the en
Wladimir Palant
2015/03/23 12:27:26
Done.
|
+ |
+ start_response("200 OK", [("Content-Type", mime)]) |
+ return [data] |
if __name__ == "__main__": |
if len(sys.argv) < 2: |
print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0] |
sys.exit(1) |
source = FileSource(sys.argv[1]) |
- app.run(debug=True) |
+ try: |
+ from werkzeug.serving import run_simple |
+ except ImportError: |
+ from wsgiref.simple_server import make_server |
+ def run_simple(host, port, app, **kwargs): |
+ def wrapper(environ, start_response): |
+ try: |
+ return app(environ, start_response) |
+ except Exception, e: |
+ return show_template(start_response, "500 Internal Server Error", |
+ ERROR_500, uri=environ.get("PATH_INFO"), error=e) |
+ |
+ server = make_server(host, port, wrapper) |
+ print " * Running on http://%s:%i/" % server.server_address |
+ server.serve_forever() |
+ |
+ run_simple("localhost", 5000, handler, use_reloader=True, use_debugger=True) |