Merge pull request #1851 from mhils/1850

fix #1850
This commit is contained in:
Maximilian Hils 2016-12-13 14:41:04 +01:00 committed by GitHub
commit e2c6d7ed0f
5 changed files with 47 additions and 36 deletions

View File

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

View File

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

View File

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

View File

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

View File

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