diff options
-rw-r--r-- | libmproxy/proxy.py | 27 | ||||
-rw-r--r-- | test/test_proxy.py | 20 |
2 files changed, 45 insertions, 2 deletions
diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index 48884c09..3a3db2e7 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -5,7 +5,8 @@ Development started from Neil Schemenauer's munchy.py """ -import sys, os, string, socket, urlparse, re, select, copy, base64 +import sys, os, string, socket, urlparse, re, select, copy, base64, time +from email.utils import parsedate_tz, formatdate, mktime_tz import shutil, tempfile import optparse, SocketServer, ssl import utils, controller @@ -280,6 +281,30 @@ class Response(controller.Msg): controller.Msg.__init__(self) self.replay = False + def refresh(self, now=None): + """ + This fairly complex and heuristic function refreshes a server + response for replay. + + - It adjusts date, expires and last-modified headers. + - It adjusts cookie expiration. + """ + if not now: + now = time.time() + delta = now - self.timestamp + refresh_headers = [ + "date", + "expires", + "last-modified", + ] + for i in refresh_headers: + if i in self.headers: + d = parsedate_tz(self.headers[i][0]) + new = mktime_tz(d) + delta + self.headers[i] = [formatdate(new)] + for i in self.headers.get("set-cookie", []): + pass + def set_replay(self): self.replay = True diff --git a/test/test_proxy.py b/test/test_proxy.py index 3bc5bcdb..a449071c 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -1,6 +1,7 @@ -import cStringIO +import cStringIO, time import libpry from libmproxy import proxy, controller, utils, dump, script +import email.utils import tutils @@ -114,6 +115,23 @@ class uResponse(libpry.AutoTree): resp = proxy.Response(req, 200, "msg", h.copy(), "content") assert resp.assemble() + def test_refresh(self): + r = tutils.tresp() + n = time.time() + r.headers["date"] = [email.utils.formatdate(n)] + pre = r.headers["date"] + r.refresh(n) + assert pre == r.headers["date"] + r.refresh(n+60) + + d = email.utils.parsedate_tz(r.headers["date"][0]) + d = email.utils.mktime_tz(d) + # Weird that this is not exact... + assert abs(60-(d-n)) <= 1 + + r.headers["set-cookie"] = ["MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"] + r.refresh() + def test_getset_state(self): h = utils.Headers() h["test"] = ["test"] |