Options unification: streamfile

We now have one option to control this. If the path is prefixed with a "+" we
append, otherwise we overwrite.
This commit is contained in:
Aldo Cortesi 2017-03-06 13:42:11 +13:00
parent 71a830c836
commit 9b1f40da37
4 changed files with 11 additions and 30 deletions

View File

@ -35,11 +35,13 @@ class StreamFile:
if self.stream: if self.stream:
self.done() self.done()
if options.streamfile: if options.streamfile:
if options.streamfile_append: if options.streamfile.startswith("+"):
path = options.streamfile[1:]
mode = "ab" mode = "ab"
else: else:
path = options.streamfile
mode = "wb" mode = "wb"
self.start_stream_to_path(options.streamfile, mode, self.filt) self.start_stream_to_path(path, mode, self.filt)
def tcp_start(self, flow): def tcp_start(self, flow):
if self.stream: if self.stream:

View File

@ -142,8 +142,10 @@ class Options(optmanager.OptManager):
"Log verbosity." "Log verbosity."
) )
self.add_option("default_contentview", "auto", str) self.add_option("default_contentview", "auto", str)
self.add_option("streamfile", None, Optional[str]) self.add_option(
self.add_option("streamfile_append", False, bool) "streamfile", None, Optional[str],
help="Write flows to file. Prefix path with + to append."
)
self.add_option( self.add_option(
"server_replay_ignore_content", False, bool, "server_replay_ignore_content", False, bool,
"Ignore request's content while searching for a saved flow to replay." "Ignore request's content while searching for a saved flow to replay."

View File

@ -15,18 +15,6 @@ class ParseException(Exception):
def get_common_options(args): def get_common_options(args):
if args.streamfile and args.streamfile[0] == args.rfile:
if args.streamfile[1] == "wb":
raise exceptions.OptionsError(
"Cannot use '{}' for both reading and writing flows. "
"Are you looking for --afile?".format(args.rfile)
)
else:
raise exceptions.OptionsError(
"Cannot use '{}' for both reading and appending flows. "
"That would trigger an infinite loop."
)
# Proxy config # Proxy config
certs = [] certs = []
for i in args.certs or []: for i in args.certs or []:
@ -96,8 +84,7 @@ def get_common_options(args):
stickyauth=args.stickyauth, stickyauth=args.stickyauth,
stream_large_bodies=args.stream_large_bodies, stream_large_bodies=args.stream_large_bodies,
showhost=args.showhost, showhost=args.showhost,
streamfile=args.streamfile[0] if args.streamfile else None, streamfile=args.streamfile,
streamfile_append=True if args.streamfile and args.streamfile[1] == "a" else False,
verbosity=args.verbose, verbosity=args.verbose,
server_replay_nopop=args.server_replay_nopop, server_replay_nopop=args.server_replay_nopop,
server_replay_ignore_content=args.server_replay_ignore_content, server_replay_ignore_content=args.server_replay_ignore_content,
@ -168,17 +155,7 @@ def basic_options(parser, opts):
action="store_const", dest="verbose", const=3, action="store_const", dest="verbose", const=3,
help="Increase log verbosity." help="Increase log verbosity."
) )
streamfile = parser.add_mutually_exclusive_group() opts.make_parser(parser, "streamfile")
streamfile.add_argument(
"-w", "--wfile",
action="store", dest="streamfile", type=lambda f: (f, "w"),
help="Write flows to file."
)
streamfile.add_argument(
"-a", "--afile",
action="store", dest="streamfile", type=lambda f: (f, "a"),
help="Append flows to file."
)
opts.make_parser(parser, "anticomp") opts.make_parser(parser, "anticomp")
opts.make_parser(parser, "body_size_limit", metavar="SIZE") opts.make_parser(parser, "body_size_limit", metavar="SIZE")
opts.make_parser(parser, "stream_large_bodies") opts.make_parser(parser, "stream_large_bodies")

View File

@ -59,7 +59,7 @@ def test_simple():
tctx.configure(sa, streamfile=None) tctx.configure(sa, streamfile=None)
assert rd(p)[0].response assert rd(p)[0].response
tctx.configure(sa, streamfile=p, streamfile_append=True) tctx.configure(sa, streamfile="+" + p)
f = tflow.tflow() f = tflow.tflow()
sa.request(f) sa.request(f)
tctx.configure(sa, streamfile=None) tctx.configure(sa, streamfile=None)