Add get_query and set_query methods to Request.

This commit is contained in:
Aldo Cortesi 2012-02-09 16:40:31 +13:00
parent 46bd780862
commit 2709441d5b
4 changed files with 62 additions and 10 deletions

View File

@ -377,7 +377,7 @@ class ConnectionView(common.WWrap):
conn = self.flow.response
self.flow.backup()
if part == "b":
if part == "r":
c = self.master.spawn_editor(conn.content or "")
conn.content = c.rstrip("\n")
elif part == "h":
@ -445,7 +445,7 @@ class ConnectionView(common.WWrap):
"Edit request",
(
("header", "h"),
("body", "b"),
("raw body", "r"),
("url", "u"),
("method", "m"),
),
@ -458,7 +458,7 @@ class ConnectionView(common.WWrap):
("code", "c"),
("message", "m"),
("header", "h"),
("body", "b"),
("raw body", "r"),
),
self.edit
)

View File

@ -2,7 +2,7 @@
This module provides more sophisticated flow tracking. These match requests
with their responses, and provide filtering and interception facilities.
"""
import hashlib, Cookie, cookielib, copy, re
import hashlib, Cookie, cookielib, copy, re, urlparse
import time
import tnetstring, filt, script, utils, encoding, proxy
from email.utils import parsedate_tz, formatdate, mktime_tz
@ -312,6 +312,25 @@ class Request(HTTPMsg):
host = "%s:%s"%(self.host, self.port)
return host
def get_query(self):
"""
Gets the request query string. Returns a list of (key, value)
tuples.
"""
_, _, _, _, query, _ = urlparse.urlparse(self.get_url())
if not query:
return []
return utils.urldecode(query)
def set_query(self, q):
"""
Takes a list of (key, value) tuples, and sets the request query
string.
"""
scheme, netloc, path, params, _, fragment = urlparse.urlparse(self.get_url())
query = utils.urlencode(q)
self.set_url(urlparse.urlunparse([scheme, netloc, path, params, query, fragment]))
def get_url(self):
"""
Returns a URL string, constructed from the Request's URL compnents.

View File

@ -12,7 +12,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re, os, subprocess, datetime, urlparse, string
import re, os, subprocess, datetime, urlparse, string, urllib
import time, functools, cgi, textwrap, hashlib
import json
@ -123,9 +123,19 @@ def pretty_json(s):
def urldecode(s):
"""
Takes a urlencoded string and returns a list of (key, value) tuples.
"""
return cgi.parse_qsl(s)
def urlencode(s):
"""
Takes a list of (key, value) tuples and returns a urlencoded string.
"""
return urllib.urlencode(s, False)
def hexdump(s):
"""
Returns a set of typles:

View File

@ -577,6 +577,29 @@ class uRequest(libpry.AutoTree):
r2 = r.copy()
assert r == r2
def test_getset_query(self):
h = flow.Headers()
r = flow.Request(None, "host", 22, "https", "GET", "/foo?x=y&a=b", h, "content")
q = r.get_query()
assert q == [("x", "y"), ("a", "b")]
r = flow.Request(None, "host", 22, "https", "GET", "/", h, "content")
q = r.get_query()
assert not q
r = flow.Request(None, "host", 22, "https", "GET", "/?adsfa", h, "content")
q = r.get_query()
assert not q
r = flow.Request(None, "host", 22, "https", "GET", "/foo?x=y&a=b", h, "content")
assert r.get_query()
r.set_query([])
assert not r.get_query()
qv = [("a", "b"), ("c", "d")]
r.set_query(qv)
assert r.get_query() == qv
def test_anticache(self):
h = flow.Headers()
r = flow.Request(None, "host", 22, "https", "GET", "/", h, "content")