From 0aff101694398584073a65e043256e039218bac3 Mon Sep 17 00:00:00 2001 From: James Graham Date: Wed, 16 Jul 2014 14:28:02 +0100 Subject: [PATCH 1/2] Fix url rewriter to deal with absolute URLs and those with query strings. --- wptserve/server.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/wptserve/server.py b/wptserve/server.py index 80d8094e5b6e86..3348d061e1692d 100644 --- a/wptserve/server.py +++ b/wptserve/server.py @@ -15,7 +15,7 @@ from request import Server, Request from response import Response from router import Router -import routes +import routes as default_roots from utils import HTTPException @@ -91,12 +91,16 @@ def rewrite(self, request_handler): :param request_handler: BaseHTTPRequestHandler for which to rewrite the request. """ - if request_handler.path in self.rules: - methods, destination = self.rules[request_handler.path] + split_url = urlparse.urlsplit(request_handler.path) + if split_url.path in self.rules: + methods, destination = self.rules[split_url.path] if "*" in methods or request_handler.command in methods: logger.debug("Rewriting request path %s to %s" % (request_handler.path, destination)) - request_handler.path = destination + new_url = list(split_url) + new_url[2] = destination + new_url = urlparse.urlunsplit(new_url) + request_handler.path = new_url class WebTestServer(ThreadingMixIn, BaseHTTPServer.HTTPServer): @@ -275,7 +279,7 @@ class WebTestHttpd(object): HTTP server designed for testing scenarios. Takes a router class which provides one method get_handler which takes a Request - and returns a handler function." + and returns a handler function. .. attribute:: host @@ -305,10 +309,13 @@ class WebTestHttpd(object): def __init__(self, host="127.0.0.1", port=8000, server_cls=None, handler_cls=WebTestRequestHandler, use_ssl=False, certificate=None, router_cls=Router, - doc_root=os.curdir, routes=routes.routes, + doc_root=os.curdir, routes=None, rewriter_cls=RequestRewriter, bind_hostname=True, rewrites=None, config=None): + if routes is None: + routes = default_routes.routes + self.host = host self.router = router_cls(doc_root, routes) From e5ec62b14fa8dc9fd19b6ec284bc39a236b19fda Mon Sep 17 00:00:00 2001 From: James Graham Date: Wed, 16 Jul 2014 14:54:09 +0100 Subject: [PATCH 2/2] fixup! Fix url rewriter to deal with absolute URLs and those with query strings. --- wptserve/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wptserve/server.py b/wptserve/server.py index 3348d061e1692d..c1cafd07b05733 100644 --- a/wptserve/server.py +++ b/wptserve/server.py @@ -15,7 +15,7 @@ from request import Server, Request from response import Response from router import Router -import routes as default_roots +import routes as default_routes from utils import HTTPException