diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-06-24 16:38:32 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-06-24 16:38:32 +1200 |
commit | 4fc64ac04ffbec8e3a51ea3f7a129f17530ee3ef (patch) | |
tree | 377ad164d9bb860ddba4fc1b1134828f38feebed | |
parent | e6aa9ff67582c00d2de03f65b47dd776cd602fb7 (diff) | |
download | mitmproxy-4fc64ac04ffbec8e3a51ea3f7a129f17530ee3ef.tar.gz mitmproxy-4fc64ac04ffbec8e3a51ea3f7a129f17530ee3ef.tar.bz2 mitmproxy-4fc64ac04ffbec8e3a51ea3f7a129f17530ee3ef.zip |
Enable anchors on command line.
-rw-r--r-- | doc-src/pathod.html | 2 | ||||
-rw-r--r-- | libpathod/pathod.py | 14 | ||||
-rw-r--r-- | libpathod/utils.py | 21 | ||||
-rwxr-xr-x | pathod | 24 | ||||
-rw-r--r-- | test/test_pathod.py | 3 | ||||
-rw-r--r-- | test/test_utils.py | 6 |
6 files changed, 38 insertions, 32 deletions
diff --git a/doc-src/pathod.html b/doc-src/pathod.html index 6cb28734..31fd0bbe 100644 --- a/doc-src/pathod.html +++ b/doc-src/pathod.html @@ -17,7 +17,7 @@ case just a vanilla 200 OK response. See the docs below to get (much) fancier. You can also add anchors to the __pathod__ server that serve a fixed response whenever a matching URL is requested: - pathod --anchor "/foo=200" + pathod -a "/foo=200" Here, "/foo" a regex specifying the anchor path, and the part after the "=" is a response specifier. diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 1491072c..ba537768 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -2,6 +2,8 @@ import urllib, threading, re from netlib import tcp, http, odict, wsgi import version, app, rparse +class PathodError(Exception): pass + class PathodHandler(tcp.BaseHandler): def handle(self): @@ -73,7 +75,7 @@ class Pathod(tcp.TCPServer): addr: (address, port) tuple. If port is 0, a free port will be automatically chosen. ssloptions: a dictionary containing certfile and keyfile specifications. - prefix: string specifying the prefix at which to anchor response generation. + prefix: string specifying the prefix at which to anchor response generation. staticdir: path to a directory of static resources, or None. anchors: A list of (regex, spec) tuples, or None. """ @@ -88,8 +90,14 @@ class Pathod(tcp.TCPServer): self.anchors = [] if anchors: for i in anchors: - arex = re.compile(i[0]) - aresp = rparse.parse(self.request_settings, i[1]) + try: + arex = re.compile(i[0]) + except re.error: + raise PathodError("Invalid regex in anchor: %s"%i[0]) + try: + aresp = rparse.parse(self.request_settings, i[1]) + except rparse.ParseException, v: + raise PathodError("Invalid page spec in anchor: '%s', %s"%(i[1], str(v))) self.anchors.append((arex, aresp)) @property diff --git a/libpathod/utils.py b/libpathod/utils.py index f421b8a6..39732849 100644 --- a/libpathod/utils.py +++ b/libpathod/utils.py @@ -1,27 +1,14 @@ import os, re import rparse -class AnchorError(Exception): pass - -def parse_anchor_spec(s, settings): +def parse_anchor_spec(s): """ - For now, this is very simple, and you can't have an '=' in your regular - expression. + Return a tuple, or None on error. """ if not "=" in s: - raise AnchorError("Invalid anchor definition: %s"%s) - rex, spec = s.split("=", 1) - try: - re.compile(rex) - except re.error: - raise AnchorError("Invalid regex in anchor: %s"%s) - try: - rparse.parse(settings, spec) - except rparse.ParseException, v: - raise AnchorError("Invalid page spec in anchor: '%s', %s"%(s, str(v))) - - return rex, spec + return None + return tuple(s.split("=", 1)) class Data: @@ -7,7 +7,7 @@ if __name__ == "__main__": parser.add_argument("-p", dest='port', default=9999, type=int, help='Port. Specify 0 to pick an arbitrary empty port.') parser.add_argument("-l", dest='address', default="0.0.0.0", type=str, help='Listening address.') parser.add_argument( - "-a", dest='anchors', default=[], type=str, action="append", + "-a", dest='anchors', default=[], type=str, action="append", metavar="ANCHOR", help='Add an anchor. Specified as a string with the form pattern=pagespec' ) parser.add_argument( @@ -43,12 +43,22 @@ if __name__ == "__main__": else: ssl = None - pd = pathod.Pathod( - (args.address, args.port), - ssloptions = ssl, - staticdir = args.staticdir, - anchors = args.anchors - ) + alst = [] + for i in args.anchors: + parts = utils.parse_anchor_spec(i) + if not parts: + parser.error("Invalid anchor specification: %s"%i) + alst.append(parts) + + try: + pd = pathod.Pathod( + (args.address, args.port), + ssloptions = ssl, + staticdir = args.staticdir, + anchors = alst + ) + except pathod.PathodError, v: + parser.error(str(v)) try: print "%s listening on port %s"%(version.NAMEVERSION, pd.port) pd.serve_forever() diff --git a/test/test_pathod.py b/test/test_pathod.py index e00694cd..4073926f 100644 --- a/test/test_pathod.py +++ b/test/test_pathod.py @@ -1,4 +1,5 @@ from libpathod import pathod +import tutils class _TestApplication: def test_anchors(self): @@ -20,6 +21,8 @@ class TestPathod: anchors = [(".*", "200")] ) assert p.anchors + tutils.raises("invalid regex", pathod.Pathod, ("127.0.0.1", 0), anchors=[("*", "200")]) + tutils.raises("invalid page spec", pathod.Pathod, ("127.0.0.1", 0), anchors=[("foo", "bar")]) def test_logging(self): p = pathod.Pathod(("127.0.0.1", 0)) diff --git a/test/test_utils.py b/test/test_utils.py index 5cd0fd3d..72c892f0 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -3,10 +3,8 @@ import tutils def test_parse_anchor_spec(): - assert utils.parse_anchor_spec("foo=200", {}) == ("foo", "200") - tutils.raises(utils.AnchorError, utils.parse_anchor_spec, "foobar", {}) - tutils.raises(utils.AnchorError, utils.parse_anchor_spec, "*=200", {}) - tutils.raises(utils.AnchorError, utils.parse_anchor_spec, "foo=bar", {}) + assert utils.parse_anchor_spec("foo=200") == ("foo", "200") + assert utils.parse_anchor_spec("foo") == None def test_data_path(): |