This commit is contained in:
Maximilian Hils 2016-09-23 22:52:58 -07:00
parent 9142da1a7d
commit 276b467b0e
5 changed files with 32 additions and 19 deletions

View File

@ -3,6 +3,7 @@ from __future__ import absolute_import, print_function, division
import urwid import urwid
import netlib.http.url import netlib.http.url
from mitmproxy import exceptions
from mitmproxy.console import common from mitmproxy.console import common
from mitmproxy.console import signals from mitmproxy.console import signals
from mitmproxy.flow import export from mitmproxy.flow import export
@ -180,7 +181,10 @@ class ConnectionItem(urwid.WidgetWrap):
self.state.enable_marked_filter() self.state.enable_marked_filter()
signals.flowlist_change.send(self) signals.flowlist_change.send(self)
elif key == "r": elif key == "r":
try:
self.master.replay_request(self.flow) self.master.replay_request(self.flow)
except exceptions.ReplayException as e:
signals.add_log("Replay error: %s" % e, "warn")
signals.flowlist_change.send(self) signals.flowlist_change.send(self)
elif key == "S": elif key == "S":
def stop_server_playback(response): def stop_server_playback(response):

View File

@ -5,6 +5,7 @@ import os
import sys import sys
import urwid import urwid
from mitmproxy import exceptions
from typing import Optional, Union # noqa from typing import Optional, Union # noqa
from mitmproxy import contentviews from mitmproxy import contentviews
@ -544,7 +545,10 @@ class FlowView(tabs.Tabs):
elif key == "p": elif key == "p":
self.view_prev_flow(self.flow) self.view_prev_flow(self.flow)
elif key == "r": elif key == "r":
try:
self.master.replay_request(self.flow) self.master.replay_request(self.flow)
except exceptions.ReplayException as e:
signals.add_log("Replay error: %s" % e, "warn")
signals.flow_change.send(self, flow = self.flow) signals.flow_change.send(self, flow = self.flow)
elif key == "V": elif key == "V":
if self.flow.modified(): if self.flow.modified():

View File

@ -97,7 +97,3 @@ class OptionsError(Exception):
class AddonError(Exception): class AddonError(Exception):
pass pass
class ReplayError(Exception):
pass

View File

@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
import os import os
import sys import sys
from typing import List, Optional, Set # noqa from typing import Optional # noqa
import netlib.exceptions import netlib.exceptions
from mitmproxy import controller from mitmproxy import controller
@ -138,37 +138,44 @@ class FlowMaster(controller.Master):
def replay_request(self, f, block=False): def replay_request(self, f, block=False):
""" """
Returns an http_replay.RequestReplayThred object. Replay a HTTP request to receive a new response from the server.
May raise exceptions.ReplayError.
Args:
f: The flow to replay.
block: If True, this function will wait for the replay to finish.
This causes a deadlock if activated in the main thread.
Returns:
The thread object doing the replay.
Raises:
exceptions.ReplayException, if the flow is in a state
where it is ineligible for replay.
""" """
if f.live: if f.live:
raise exceptions.ReplayError( raise exceptions.ReplayException(
"Can't replay live flow." "Can't replay live flow."
) )
if f.intercepted: if f.intercepted:
raise exceptions.ReplayError( raise exceptions.ReplayException(
"Can't replay intercepted flow." "Can't replay intercepted flow."
) )
if f.request.raw_content is None: if f.request.raw_content is None:
raise exceptions.ReplayError( raise exceptions.ReplayException(
"Can't replay flow with missing content." "Can't replay flow with missing content."
) )
if not f.request: if not f.request:
raise exceptions.ReplayError( raise exceptions.ReplayException(
"Can't replay flow with missing request." "Can't replay flow with missing request."
) )
f.backup() f.backup()
f.request.is_replay = True f.request.is_replay = True
# TODO: We should be able to remove this.
if "Content-Length" in f.request.headers:
f.request.headers["Content-Length"] = str(len(f.request.raw_content))
f.response = None f.response = None
f.error = None f.error = None
# FIXME: process through all addons?
# self.process_new_request(f)
rt = http_replay.RequestReplayThread( rt = http_replay.RequestReplayThread(
self.server.config, self.server.config,
f, f,

View File

@ -22,6 +22,7 @@ class RequestReplayThread(basethread.BaseThread):
processed. processed.
""" """
self.config, self.flow = config, flow self.config, self.flow = config, flow
flow.live = True
if event_queue: if event_queue:
self.channel = controller.Channel(event_queue, should_exit) self.channel = controller.Channel(event_queue, should_exit)
else: else:
@ -104,5 +105,6 @@ class RequestReplayThread(basethread.BaseThread):
self.channel.tell("log", Log(traceback.format_exc(), "error")) self.channel.tell("log", Log(traceback.format_exc(), "error"))
finally: finally:
r.first_line_format = first_line_format_backup r.first_line_format = first_line_format_backup
self.flow.live = False
if server: if server:
server.finish() server.finish()