This commit is contained in:
Maximilian Hils 2016-12-13 14:03:00 +01:00
parent b39380b00f
commit e5b3c8bed3
5 changed files with 47 additions and 36 deletions

View File

@ -16,21 +16,22 @@ class Replace:
rex: a regular expression, as bytes. rex: a regular expression, as bytes.
s: the replacement string, as bytes s: the replacement string, as bytes
""" """
lst = [] if "replacements" in updated:
for fpatt, rex, s in options.replacements: lst = []
flt = flowfilter.parse(fpatt) for fpatt, rex, s in options.replacements:
if not flt: flt = flowfilter.parse(fpatt)
raise exceptions.OptionsError( if not flt:
"Invalid filter pattern: %s" % fpatt raise exceptions.OptionsError(
) "Invalid filter pattern: %s" % fpatt
try: )
re.compile(rex) try:
except re.error as e: re.compile(rex)
raise exceptions.OptionsError( except re.error as e:
"Invalid regular expression: %s - %s" % (rex, str(e)) raise exceptions.OptionsError(
) "Invalid regular expression: %s - %s" % (rex, str(e))
lst.append((rex, s, flt)) )
self.lst = lst lst.append((rex, s, flt))
self.lst = lst
def execute(self, f): def execute(self, f):
for rex, s, flt in self.lst: for rex, s, flt in self.lst:

View File

@ -8,18 +8,22 @@ class StickyAuth:
self.hosts = {} self.hosts = {}
def configure(self, options, updated): def configure(self, options, updated):
if options.stickyauth: if "stickyauth" in updated:
flt = flowfilter.parse(options.stickyauth) if options.stickyauth:
if not flt: flt = flowfilter.parse(options.stickyauth)
raise exceptions.OptionsError( if not flt:
"stickyauth: invalid filter expression: %s" % options.stickyauth raise exceptions.OptionsError(
) "stickyauth: invalid filter expression: %s" % options.stickyauth
self.flt = flt )
self.flt = flt
else:
self.flt = None
def request(self, flow): def request(self, flow):
host = flow.request.host if self.flt:
if "authorization" in flow.request.headers: host = flow.request.host
self.hosts[host] = flow.request.headers["authorization"] if "authorization" in flow.request.headers:
elif flowfilter.match(self.flt, flow): self.hosts[host] = flow.request.headers["authorization"]
if host in self.hosts: elif flowfilter.match(self.flt, flow):
flow.request.headers["authorization"] = self.hosts[host] if host in self.hosts:
flow.request.headers["authorization"] = self.hosts[host]

View File

@ -34,13 +34,16 @@ class StickyCookie:
self.flt = None self.flt = None
def configure(self, options, updated): def configure(self, options, updated):
if options.stickycookie: if "stickycookie" in updated:
flt = flowfilter.parse(options.stickycookie) if options.stickycookie:
if not flt: flt = flowfilter.parse(options.stickycookie)
raise exceptions.OptionsError( if not flt:
"stickycookie: invalid filter expression: %s" % options.stickycookie raise exceptions.OptionsError(
) "stickycookie: invalid filter expression: %s" % options.stickycookie
self.flt = flt )
self.flt = flt
else:
self.flt = None
def response(self, flow): def response(self, flow):
if self.flt: if self.flt:

View File

@ -29,6 +29,8 @@ class StreamFile:
raise exceptions.OptionsError( raise exceptions.OptionsError(
"Invalid filter specification: %s" % options.filtstr "Invalid filter specification: %s" % options.filtstr
) )
else:
self.filt = None
if "streamfile" in updated: if "streamfile" in updated:
if self.stream: if self.stream:
self.done() self.done()

View File

@ -15,7 +15,8 @@ def test_configure():
def test_simple(): def test_simple():
r = stickyauth.StickyAuth() r = stickyauth.StickyAuth()
with taddons.context(): with taddons.context() as tctx:
tctx.configure(r, stickyauth=".*")
f = tflow.tflow(resp=True) f = tflow.tflow(resp=True)
f.request.headers["authorization"] = "foo" f.request.headers["authorization"] = "foo"
r.request(f) r.request(f)