Index: cms/bin/test_server.py |
=================================================================== |
--- a/cms/bin/test_server.py |
+++ b/cms/bin/test_server.py |
@@ -10,22 +10,21 @@ |
# 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 sys, os |
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", |
@@ -34,17 +33,17 @@ mime_types = { |
".jpg": "image/jpeg", |
".jpeg": "image/jpeg", |
} |
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") |
@@ -53,27 +52,40 @@ def get_data(path): |
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") |
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 handler(environ, start_response): |
+ path = environ.get("REQUEST_URI") or environ.get("PATH_INFO") |
Sebastian Noack
2015/03/21 12:50:18
Do we need that hack? As far as I know PATH_INFO i
Sebastian Noack
2015/03/23 11:31:10
We might actually want to use wsgiref.util.request
Wladimir Palant
2015/03/23 11:51:07
Done.
|
data = get_data(path) |
if data == None: |
flask.abort(404) |
root, ext = os.path.splitext(path) |
mime = mime_types.get(ext.lower(), "application/octet-stream") |
- return data, 200, {"Content-Type": mime} |
+ |
+ if isinstance(data, unicode): |
Sebastian Noack
2015/03/21 12:50:18
How about following?
import mimetypes
mime =
Wladimir Palant
2015/03/23 11:51:07
Normally, I would delegate that to a separate issu
|
+ data = data.encode("utf-8") |
+ |
+ 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): |
+ server = make_server(host, port, app) |
+ print " * Running on http://%s:%i/" % server.server_address |
+ server.serve_forever() |
+ |
+ run_simple("localhost", 5000, handler, use_reloader=True, use_debugger=True) |