From a77ccc406dc23a419b32cd37ca3a83542bd6681a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 19 Aug 2012 13:03:21 +1200 Subject: Getter and setter for path component on Requests. --- libmproxy/flow.py | 22 +++++++++++++++++++++- test/test_flow.py | 20 +++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/libmproxy/flow.py b/libmproxy/flow.py index 6076d202..868419e5 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -18,7 +18,7 @@ with their responses, and provide filtering and interception facilities. """ import hashlib, Cookie, cookielib, copy, re, urlparse, os -import time +import time, urllib import tnetstring, filt, script, utils, encoding, proxy from email.utils import parsedate_tz, formatdate, mktime_tz from netlib import odict, http, certutils @@ -397,6 +397,26 @@ class Request(HTTPMsg): self.headers["Content-Type"] = [HDR_FORM_URLENCODED] self.content = utils.urlencode(odict.lst) + def get_path_components(self): + """ + Returns the path components of the URL as a list of strings. + + Components are unquoted. + """ + _, _, path, _, _, _ = urlparse.urlparse(self.get_url()) + return [urllib.unquote(i) for i in path.split("/") if i] + + def set_path_components(self, lst): + """ + Takes a list of strings, and sets the path component of the URL. + + Components are quoted. + """ + lst = [urllib.quote(i, safe="") for i in lst] + path = "/" + "/".join(lst) + scheme, netloc, _, params, query, fragment = urlparse.urlparse(self.get_url()) + self.set_url(urlparse.urlunparse([scheme, netloc, path, params, query, fragment])) + def get_query(self): """ Gets the request query string. Returns an ODict object. diff --git a/test/test_flow.py b/test/test_flow.py index 47a09360..53d92f25 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -741,7 +741,25 @@ class TestRequest: r.content = flow.CONTENT_MISSING assert not r._assemble() - + def test_path_components(self): + h = flow.ODictCaseless() + c = flow.ClientConnect(("addr", 2222)) + r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content") + assert r.get_path_components() == [] + r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/foo/bar", h, "content") + assert r.get_path_components() == ["foo", "bar"] + q = flow.ODict() + q["test"] = ["123"] + r.set_query(q) + assert r.get_path_components() == ["foo", "bar"] + + r.set_path_components([]) + assert r.get_path_components() == [] + r.set_path_components(["foo"]) + assert r.get_path_components() == ["foo"] + r.set_path_components(["/oo"]) + assert r.get_path_components() == ["/oo"] + assert "%2F" in r.path def test_getset_form_urlencoded(self): h = flow.ODictCaseless() -- cgit v1.2.3