From f54ed69a358d2dd059bd844c752f96e29e90e269 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 28 Oct 2012 09:06:55 +1300 Subject: [PATCH] Adjust serve() API. --- libpathod/app.py | 6 +++--- libpathod/language.py | 16 ++++++++-------- libpathod/pathoc.py | 4 ++-- libpathod/pathod.py | 6 +++--- test/test_language.py | 22 +++++++++++----------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/libpathod/app.py b/libpathod/app.py index 6cde801a0..6f12c0a23 100644 --- a/libpathod/app.py +++ b/libpathod/app.py @@ -129,16 +129,16 @@ def _preview(is_request): s = cStringIO.StringIO() args["pauses"] = r.preview_safe() - + c = app.config["pathod"].check_policy(r) if c: args["error"] = c return render(template, False, **args) if is_request: - r.serve(app.config["pathod"].request_settings, s, host="example.com") + r.serve(s, app.config["pathod"].request_settings, host="example.com") else: - r.serve(app.config["pathod"].request_settings, s) + r.serve(s, app.config["pathod"].request_settings) args["output"] = utils.escape_unprintables(s.getvalue()) return render(template, False, **args) diff --git a/libpathod/language.py b/libpathod/language.py index 311d51e5d..31239b744 100644 --- a/libpathod/language.py +++ b/libpathod/language.py @@ -605,7 +605,7 @@ class Message: actions.reverse() return [i.intermediate(settings) for i in actions] - def serve(self, settings, fp, request_host): + def serve(self, fp, settings, request_host): """ fp: The file pointer to write to. @@ -733,8 +733,8 @@ class CraftedRequest(Request): for i in tokens: i.accept(settings, self) - def serve(self, settings, fp, host): - d = Request.serve(self, settings, fp, host) + def serve(self, fp, settings, host): + d = Request.serve(self, fp, settings, host) d["spec"] = self.spec return d @@ -746,8 +746,8 @@ class CraftedResponse(Response): for i in tokens: i.accept(settings, self) - def serve(self, settings, fp): - d = Response.serve(self, settings, fp, None) + def serve(self, fp, settings): + d = Response.serve(self, fp, settings, None) d["spec"] = self.spec return d @@ -759,11 +759,11 @@ class PathodErrorResponse(Response): self.msg = LiteralGenerator(msg) self.body = LiteralGenerator("pathod error: " + (body or msg)) self.headers = [ - Header(ValueLiteral("Content-Type"), ValueLiteral("text/plain")), + Header(ValueLiteral("Content-Type"), ValueLiteral("text/plain")), ] - def serve(self, settings, fp): - d = Response.serve(self, settings, fp, None) + def serve(self, fp, settings): + d = Response.serve(self, fp, settings, None) d["internal"] = True return d diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index df291c59a..4e592a065 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -22,7 +22,7 @@ class Pathoc(tcp.TCPClient): language.FileAccessDenied. """ r = language.parse_request(self.settings, spec) - ret = r.serve(self.settings, self.wfile, self.host) + ret = r.serve(self.wfile, self.settings, self.host) self.wfile.flush() return http.read_response(self.rfile, r.method, None) @@ -68,7 +68,7 @@ class Pathoc(tcp.TCPClient): if showresp: self.rfile.start_log() try: - req = r.serve(self.settings, self.wfile, self.host) + req = r.serve(self.wfile, self.settings, self.host) self.wfile.flush() resp = http.read_response(self.rfile, r.method, None) except http.HttpError, v: diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 5d787c554..d80a90187 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -21,14 +21,14 @@ class PathodHandler(tcp.BaseHandler): c = self.server.check_policy(crafted) if c: err = language.PathodErrorResponse(c) - err.serve(self.server.request_settings, self.wfile) + err.serve(self.wfile, self.server.request_settings) log = dict( type = "error", msg = c ) return False, log - response_log = crafted.serve(self.server.request_settings, self.wfile) + response_log = crafted.serve(self.wfile, self.server.request_settings) log = dict( type = "crafted", request=request_log, @@ -106,7 +106,7 @@ class PathodHandler(tcp.BaseHandler): return self.serve_crafted(crafted, request_log) elif self.server.noweb: crafted = language.PathodErrorResponse("Access Denied") - crafted.serve(self.server.request_settings, self.wfile) + crafted.serve(self.wfile, self.server.request_settings) return False, dict(type = "error", msg="Access denied: web interface disabled") else: self.info("app: %s %s"%(method, path)) diff --git a/test/test_language.py b/test/test_language.py index 289f180c7..e5ec51d2d 100644 --- a/test/test_language.py +++ b/test/test_language.py @@ -206,7 +206,7 @@ class TestMisc: def test_internal_response(self): d = cStringIO.StringIO() s = language.PathodErrorResponse("foo") - s.serve({}, d) + s.serve(d, {}) class Test_Action: @@ -279,7 +279,7 @@ class TestInject: def test_serve(self): s = cStringIO.StringIO() r = language.parse_response({}, "400:i0,'foo'") - assert r.serve({}, s) + assert r.serve(s, {}) def test_spec(self): e = language.InjectAt.expr() @@ -344,7 +344,7 @@ class TestParseRequest: def test_render(self): s = cStringIO.StringIO() r = language.parse_request({}, "GET:'/foo'") - assert r.serve({}, s, "foo.com") + assert r.serve(s, {}, "foo.com") def test_str(self): r = language.parse_request({}, 'GET:"/foo"') @@ -479,15 +479,15 @@ class TestWriteValues: def test_write_values_after(self): s = cStringIO.StringIO() r = language.parse_response({}, "400:da") - r.serve({}, s) + r.serve(s, {}) s = cStringIO.StringIO() r = language.parse_response({}, "400:pa,0") - r.serve({}, s) + r.serve(s, {}) s = cStringIO.StringIO() r = language.parse_response({}, "400:ia,'xx'") - r.serve({}, s) + r.serve(s, {}) assert s.getvalue().endswith('xx') @@ -514,19 +514,19 @@ class TestResponse: def test_render(self): s = cStringIO.StringIO() r = language.parse_response({}, "400'msg'") - assert r.serve({}, s) + assert r.serve(s, {}) def test_raw(self): s = cStringIO.StringIO() r = language.parse_response({}, "400:b'foo'") - r.serve({}, s) + r.serve(s, {}) v = s.getvalue() assert "Content-Length" in v assert "Date" in v s = cStringIO.StringIO() r = language.parse_response({}, "400:b'foo':r") - r.serve({}, s) + r.serve(s, {}) v = s.getvalue() assert not "Content-Length" in v assert not "Date" in v @@ -534,7 +534,7 @@ class TestResponse: def test_length(self): def testlen(x): s = cStringIO.StringIO() - x.serve({}, s) + x.serve(s, {}) assert x.length({}, None) == len(s.getvalue()) testlen(language.parse_response({}, "400'msg'")) testlen(language.parse_response({}, "400'msg':h'foo'='bar'")) @@ -544,7 +544,7 @@ class TestResponse: def testlen(x, actions): s = cStringIO.StringIO() m = x.maximum_length({}, None) - x.serve({}, s) + x.serve(s, {}) assert m >= len(s.getvalue()) r = language.parse_response({}, "400'msg':b@100")