From affb57ed08ca5167d38a21affeb84d8de2a77501 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Thu, 15 Jan 2015 12:37:26 -0300 Subject: [PATCH] added support for saving file if content is binary. still wip --- libmproxy/console/common.py | 44 +++++++++++++++++++++++++++++++---- libmproxy/console/flowlist.py | 3 ++- libmproxy/console/flowview.py | 3 ++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/libmproxy/console/common.py b/libmproxy/console/common.py index 3ba89f8e4..84dc8aeff 100644 --- a/libmproxy/console/common.py +++ b/libmproxy/console/common.py @@ -1,6 +1,7 @@ from __future__ import absolute_import import urwid import urwid.util +import os from .. import utils from ..protocol.http import CONTENT_MISSING try: @@ -164,20 +165,55 @@ def raw_format_flow(f, focus, extended, padding): pile.append(urwid.Columns(resp, dividechars=1)) return urwid.Pile(pile) -def server_copy_response( k, response, statusbar): +## common save body parts +def _save_body(path, master, state, content): + if not path: + return + state.last_saveload = path + path = os.path.expanduser(path) + try: + f = file(path, "wb") + f.write(str(content)) + f.close() + except IOError, v: + master.statusbar.message(v.strerror) + +def save_body(k, master, state, content): + if k == "y": + master.path_prompt( + "Save response body: ", + state.last_saveload, + _save_body, + master, + state, + content, + ) + +## common server_copy_response parts +def server_copy_response( k, master, state, response): if pyperclip: if k == "c": try: pyperclip.copy(response.get_decoded_content()) except TypeError: - statusbar.message("Content is binary or can be converted to text") + master.prompt_onekey( + "Content is binary do you want to save it to a file instead?", + ( + ("yes", "y"), + ("no", "n"), + ), + save_body, + master, + state, + response.get_decoded_content(), + ) elif k == "h": try: pyperclip.copy(str(response.headers)) except TypeError: - statusbar.message("Error converting headers to text") + master.statusbar.message("Error converting headers to text") else: - statusbar.message("No clipboard support on your system, sorry.") + master.statusbar.message("No clipboard support on your system, sorry.") class FlowCache: diff --git a/libmproxy/console/flowlist.py b/libmproxy/console/flowlist.py index 2dc20544c..214c10db4 100644 --- a/libmproxy/console/flowlist.py +++ b/libmproxy/console/flowlist.py @@ -211,8 +211,9 @@ class ConnectionItem(common.WWrap): ("headers", "h"), ), common.server_copy_response, + self.master, + self.state, self.flow.response, - self.master.statusbar ) else: return key diff --git a/libmproxy/console/flowview.py b/libmproxy/console/flowview.py index abca2ed63..bc0b5f83b 100644 --- a/libmproxy/console/flowview.py +++ b/libmproxy/console/flowview.py @@ -760,8 +760,9 @@ class FlowView(common.WWrap): ("headers", "h"), ), common.server_copy_response, + self.master, + self.state, self.flow.response, - self.master.statusbar ) elif key == "m": p = list(contentview.view_prompts)