| Left: | ||
| Right: |
| 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-2013 Eyeo GmbH | 4 # Copyright (C) 2006-2013 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 13 matching lines...) Expand all Loading... | |
| 24 app = flask.Flask("sitescripts.web.bin.test_server") | 24 app = flask.Flask("sitescripts.web.bin.test_server") |
| 25 source = None | 25 source = None |
| 26 | 26 |
| 27 def get_data(path): | 27 def get_data(path): |
| 28 if source.has_static(path): | 28 if source.has_static(path): |
| 29 return source.read_static(path) | 29 return source.read_static(path) |
| 30 | 30 |
| 31 path = path.rstrip("/") | 31 path = path.rstrip("/") |
| 32 if path == "": | 32 if path == "": |
| 33 path = source.read_config().get("general", "defaultlocale") | 33 path = source.read_config().get("general", "defaultlocale") |
| 34 if not "/" in path: | 34 if "/" not in path: |
|
Sebastian Noack
2013/10/29 11:04:17
In Python there is a "not in" operator, that shoul
| |
| 35 path = "%s/%s" % (path, source.read_config().get("general", "defaultpage")) | 35 path = "%s/%s" % (path, source.read_config().get("general", "defaultpage")) |
| 36 | 36 |
| 37 locale, page = path.split("/", 1) | 37 locale, page = path.split("/", 1) |
| 38 for format in converters.iterkeys(): | 38 for format in converters.iterkeys(): |
| 39 if source.has_page(page, format): | 39 if source.has_page(page, format): |
| 40 return process_page(source, locale, page, format).encode("utf-8") | 40 return process_page(source, locale, page, format).encode("utf-8") |
| 41 if source.has_localizable_file(locale, page): | 41 if source.has_localizable_file(locale, page): |
| 42 return source.read_localizable_file(locale, page) | 42 return source.read_localizable_file(locale, page) |
| 43 | 43 |
| 44 return None | 44 return None |
| 45 | 45 |
| 46 @app.route("/", methods = ["GET"]) | 46 @app.route("/", methods = ["GET"]) |
| 47 @app.route("/<path:path>", methods = ["GET"]) | 47 @app.route("/<path:path>", methods = ["GET"]) |
| 48 def show(path=""): | 48 def show(path=""): |
| 49 data = get_data(path) | 49 data = get_data(path) |
| 50 if data == None: | 50 if data == None: |
| 51 flask.abort(404) | 51 flask.abort(404) |
| 52 | 52 |
| 53 root, ext = os.path.splitext(path) | 53 root, ext = os.path.splitext(path) |
| 54 if ext == ".js": | 54 if ext == ".js": |
| 55 mime = "text/js; charset=utf-8" | 55 mime = "application/javascript; charset=utf-8" |
|
Sebastian Noack
2013/10/29 11:04:17
This isn't the mime type for javascript, "applicat
Wladimir Palant
2013/11/04 09:49:21
Actually, application/x-javascript.
Sebastian Noack
2013/11/04 13:28:18
See http://tools.ietf.org/html/rfc4329#section-7.2
Wladimir Palant
2013/11/04 16:11:35
Interesting, wasn't aware of it. Apparently, nginx
| |
| 56 elif ext == ".css": | 56 elif ext == ".css": |
| 57 mime = "text/css; charset=utf-8" | 57 mime = "text/css; charset=utf-8" |
| 58 elif ext == ".png": | 58 elif ext == ".png": |
| 59 mime = "image/png" | 59 mime = "image/png" |
| 60 elif ext == "": | 60 elif ext == "": |
| 61 mime = "text/html; charset=utf-8" | 61 mime = "text/html; charset=utf-8" |
| 62 else: | 62 else: |
| 63 mime = "application/octet-stream" | 63 mime = "application/octet-stream" |
| 64 return data, 200, {"Content-Type": mime} | 64 return data, 200, {"Content-Type": mime} |
| 65 | 65 |
| 66 if __name__ == "__main__": | 66 if __name__ == "__main__": |
| 67 setupStderr() | 67 setupStderr() |
| 68 if len(sys.argv) < 2: | 68 if len(sys.argv) < 2: |
| 69 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0] | 69 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0] |
| 70 sys.exit(1) | 70 sys.exit(1) |
| 71 | 71 |
| 72 source = FileSource(sys.argv[1]) | 72 source = FileSource(sys.argv[1]) |
| 73 | 73 |
| 74 # Make sure to "fix" argv to ensure that restart can succeed | 74 # Make sure to "fix" argv to ensure that restart can succeed |
| 75 sys.argv[0:1] = ["-m", "sitescripts.web.bin.test_server"] | 75 sys.argv[0:1] = ["-m", "sitescripts.web.bin.test_server"] |
| 76 | 76 |
| 77 app.run(debug=True) | 77 app.run(debug=True) |
| LEFT | RIGHT |