diff options
| author | Aldo Cortesi <aldo@nullcube.com> | 2014-10-25 08:18:39 +1300 |
|---|---|---|
| committer | Aldo Cortesi <aldo@nullcube.com> | 2014-10-25 08:18:39 +1300 |
| commit | 5aadf92767614b7bd8e2ef677085410ac359e5e8 (patch) | |
| tree | 560778bba2ac6a2e6cc7a0b69bb042987ef67435 /libpathod | |
| parent | 3de68da3adf15f445e40412fdde4b94857640166 (diff) | |
| download | mitmproxy-5aadf92767614b7bd8e2ef677085410ac359e5e8.tar.gz mitmproxy-5aadf92767614b7bd8e2ef677085410ac359e5e8.tar.bz2 mitmproxy-5aadf92767614b7bd8e2ef677085410ac359e5e8.zip | |
Nicer way to specify patterns read for file - just use a path
Diffstat (limited to 'libpathod')
| -rw-r--r-- | libpathod/app.py | 4 | ||||
| -rw-r--r-- | libpathod/cmdline.py | 33 | ||||
| -rw-r--r-- | libpathod/language.py | 8 | ||||
| -rw-r--r-- | libpathod/pathoc.py | 4 | ||||
| -rw-r--r-- | libpathod/pathod.py | 6 | ||||
| -rw-r--r-- | libpathod/templates/docs_lang.html | 8 |
6 files changed, 38 insertions, 25 deletions
diff --git a/libpathod/app.py b/libpathod/app.py index fb1d6a2d..1a54f712 100644 --- a/libpathod/app.py +++ b/libpathod/app.py @@ -121,9 +121,9 @@ def make_app(noapi): try: if is_request: - r = language.parse_request(app.config["pathod"].request_settings, spec) + r = language.parse_request(spec) else: - r = language.parse_response(app.config["pathod"].request_settings, spec) + r = language.parse_response(spec) except language.ParseException, v: args["syntaxerror"] = str(v) args["marked"] = v.marked() diff --git a/libpathod/cmdline.py b/libpathod/cmdline.py index 6d1573f1..2c6e094e 100644 --- a/libpathod/cmdline.py +++ b/libpathod/cmdline.py @@ -1,7 +1,8 @@ #!/usr/bin/env python import argparse -import sys import os +import os.path +import sys from . import pathoc, pathod, version, utils from netlib import http_uastrings @@ -50,9 +51,11 @@ def go_pathoc(): ) parser.add_argument( 'request', type=str, nargs="+", - help='Request specification' + help=""" + Request specification, or path to a file containing a request + specifcation + """ ) - group = parser.add_argument_group( 'SSL', ) @@ -141,6 +144,16 @@ def go_pathoc(): args.connect_to = parts else: args.connect_to = None + + reqs = [] + for r in args.request: + if os.path.exists(r): + data = open(r).read() + reqs.append(data) + else: + reqs.append(r) + args.request = reqs + pathoc.main(args) @@ -174,7 +187,10 @@ def go_pathod(): type=str, action="append", metavar="ANCHOR", - help='Add an anchor. Specified as a string with the form pattern=pagespec' + help=""" + Add an anchor. Specified as a string with the form pattern=pagespec, or + pattern=filepath + """ ) parser.add_argument( "-c", dest='craftanchor', default="/p/", type=str, @@ -310,5 +326,14 @@ def go_pathod(): parser.error(v) args.sizelimit = sizelimit + anchors = [] + for patt, spec in anchors: + if os.path.exists(spec): + data = open(spec).read() + anchors.append((patt, data)) + else: + anchors.append((patt, spec)) + args.anchors = anchors + pathod.main(args) diff --git a/libpathod/language.py b/libpathod/language.py index 286a1a8e..002c8205 100644 --- a/libpathod/language.py +++ b/libpathod/language.py @@ -961,7 +961,7 @@ def read_file(settings, s): return file(s, "rb").read() -def parse_response(settings, s): +def parse_response(s): """ May raise ParseException or FileAccessDenied """ @@ -969,15 +969,13 @@ def parse_response(settings, s): s = s.decode("ascii") except UnicodeError: raise ParseException("Spec must be valid ASCII.", 0, 0) - if s.startswith(FILESTART): - s = read_file(settings, s) try: return Response(Response.expr().parseString(s, parseAll=True)) except pp.ParseException, v: raise ParseException(v.msg, v.line, v.col) -def parse_request(settings, s): +def parse_request(s): """ May raise ParseException or FileAccessDenied """ @@ -985,8 +983,6 @@ def parse_request(settings, s): s = s.decode("ascii") except UnicodeError: raise ParseException("Spec must be valid ASCII.", 0, 0) - if s.startswith(FILESTART): - s = read_file(settings, s) try: return Request(Request.expr().parseString(s, parseAll=True)) except pp.ParseException, v: diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 9ff03eca..e534bba5 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -80,7 +80,7 @@ class Pathoc(tcp.TCPClient): May raise language.ParseException, netlib.http.HttpError or language.FileAccessDenied. """ - r = language.parse_request(self.settings, spec) + r = language.parse_request(spec) language.serve(r, self.wfile, self.settings, self.address.host) self.wfile.flush() ret = list(http.read_response(self.rfile, r.method.string(), None)) @@ -115,7 +115,7 @@ class Pathoc(tcp.TCPClient): Returns True if we have a non-ignored response. """ try: - r = language.parse_request(self.settings, spec) + r = language.parse_request(spec) except language.ParseException, v: print >> fp, "Error parsing request spec: %s"%v.msg print >> fp, v.marked() diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 37b07bb6..92e5b2db 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -169,7 +169,7 @@ class PathodHandler(tcp.BaseHandler): for i in self.server.anchors: if i[0].match(path): self.info("crafting anchor: %s" % path) - aresp = language.parse_response(self.server.request_settings, i[1]) + aresp = language.parse_response(i[1]) again, retlog["response"] = self.serve_crafted(aresp) return again, retlog @@ -177,7 +177,7 @@ class PathodHandler(tcp.BaseHandler): spec = urllib.unquote(path)[len(self.server.craftanchor):] self.info("crafting spec: %s" % spec) try: - crafted = language.parse_response(self.server.request_settings, spec) + crafted = language.parse_response(spec) except language.ParseException, v: self.info("Parse error: %s" % v.msg) crafted = language.make_error_response( @@ -299,7 +299,7 @@ class Pathod(tcp.TCPServer): except re.error: raise PathodError("Invalid regex in anchor: %s" % i[0]) try: - language.parse_response(self.request_settings, i[1]) + language.parse_response(i[1]) except language.ParseException, v: raise PathodError("Invalid page spec in anchor: '%s', %s" % (i[1], str(v))) self.anchors.append((arex, i[1])) diff --git a/libpathod/templates/docs_lang.html b/libpathod/templates/docs_lang.html index aef12a8d..7cb3fc5f 100644 --- a/libpathod/templates/docs_lang.html +++ b/libpathod/templates/docs_lang.html @@ -183,14 +183,6 @@ </div> -<section id="specifying_requests"> - <div class="page-header"> - <h1>Executing specs from file</h1> - </div> - - <pre class="example">+./path/to/spec</pre> - -</section> <section id="specifying_requests"> <div class="page-header"> |
