Skip to content

Some fixes and additional options. #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
36 changes: 28 additions & 8 deletions browsermobproxy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@


class Client(object):
def __init__(self, url, params=None, options=None):
def __init__(self, url, data=None, params=None, options=None):
"""
Initialises a new Client object


:param url: This is where the BrowserMob Proxy lives
:param data: Data to be POST'ed (for example bindAddress, useEcc and trustAllServers)
:param params: URL query (for example httpProxy and httpsProxy vars)
:param options: Dictionary that can contain the port of an existing
proxy to use (for example 'existing_proxy_port_to_use')
"""
data = data if data is not None else {}
params = params if params is not None else {}
options = options if options is not None else {}
self.host = "http://" + url
Expand All @@ -28,7 +30,7 @@ def __init__(self, url, params=None, options=None):
if 'existing_proxy_port_to_use' in options:
self.port = options['existing_proxy_port_to_use']
else:
resp = requests.post('%s/proxy' % self.host + urlparams)
resp = requests.post('%s/proxy' % self.host + urlparams, data=data)
jcontent = json.loads(resp.content.decode('utf-8'))
self.port = jcontent['port']
url_parts = self.host.split(":")
Expand Down Expand Up @@ -196,22 +198,40 @@ def response_interceptor(self, js):
"""
Executes the javascript against each response

:param str js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/interceptor/response' % (self.host, self.port),
data=js)
return r.status_code

def response_filter(self, js):
"""
Executes the javascript against each response

:param str js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/filter/response' % (self.host, self.port),
data=js,
headers={'content-type': 'application/json'})
data=js)
return r.status_code

def request_interceptor(self, js):
"""
Executes the javascript against each request

:param str js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/interceptor/request' % (self.host, self.port),
data=js)
return r.status_code

def request_filter(self, js):
Copy link

@scythargon scythargon Feb 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Could this method help me to prevent interception of all websocket connections according to this issue? lightbody/browsermob-proxy#408
If so - what should I write in js function?
Thanks in advance!

"""
Executes the javascript against each request

:param str js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/filter/request' % (self.host, self.port),
data=js,
headers={'content-type': 'application/json'})
data=js)
return r.status_code

LIMITS = {
Expand All @@ -225,8 +245,8 @@ def limits(self, options):
Limit the bandwidth through the proxy.

:param dict options: A dictionary with all the details you want to set.
downstreamKbps - Sets the downstream kbps
upstreamKbps - Sets the upstream kbps
downstream_kbps - Sets the downstream kbps
upstream_kbps - Sets the upstream kbps
latency - Add the given latency to each HTTP request
"""
params = {}
Expand Down
24 changes: 12 additions & 12 deletions browsermobproxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ def url(self):
"""
return "http://%s:%d" % (self.host, self.port)

def create_proxy(self, params=None):
def create_proxy(self, data=None, params=None):
"""
Gets a client class that allow to set all the proxy details that you
may need to.

:param dict params: Dictionary where you can specify params
like httpProxy and httpsProxy
"""
data = data if data is not None else {}
params = params if params is not None else {}
client = Client(self.url[7:], params)
client = Client(self.url[7:], data, params)
return client

def _is_listening(self):
Expand Down Expand Up @@ -80,7 +81,7 @@ def __init__(self, path='browsermob-proxy', options=None):
" provided: %s" % path)

self.path = path
self.host = 'localhost'
self.host = options.get('host', 'localhost')
self.port = options.get('port', 8080)
self.process = None

Expand All @@ -99,13 +100,12 @@ def start(self, options=None):
of the log file with resp. keys of `log_path` and `log_file`
"""
if options is None:
options = {
'log_path': os.getcwd(),
'log_file': 'server.log',
}
log_path = options.get('log_path')
log_file = options.get('log_file')
log_path_name = os.path.join(log_path, os.path.sep, log_file)
options = {}
log_path = options.get('log_path', os.getcwd())
log_file = options.get('log_file', 'server.log')
retry_sleep = options.get('retry_sleep', 0.5)
retry_count = options.get('retry_count', 60)
log_path_name = os.path.join(log_path, log_file)
self.log_file = open(log_path_name, 'w')

self.process = subprocess.Popen(self.command,
Expand All @@ -120,9 +120,9 @@ def start(self, options=None):
"for a helpful error message.".format(self.log_file))

raise Exception(message)
time.sleep(0.5)
time.sleep(retry_sleep)
count += 1
if count == 60:
if count == retry_count:
self.stop()
raise Exception("Can't connect to Browsermob-Proxy")

Expand Down
16 changes: 8 additions & 8 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,34 +166,34 @@ def test_close(self):

def test_response_interceptor_with_parsing_js(self):
"""
/proxy/:port/interceptor/response
/proxy/:port/filter/response
"""
js = 'alert("foo")'
status_code = self.client.response_interceptor(js)
status_code = self.client.response_filter(js)
assert(status_code == 200)

def test_response_interceptor_with_invalid_js(self):
"""
/proxy/:port/interceptor/response
/proxy/:port/filter/response
"""
js = 'alert("foo"'
status_code = self.client.response_interceptor(js)
status_code = self.client.response_filter(js)
assert(status_code == 500)

def test_request_interceptor_with_parsing_js(self):
"""
/proxy/:port/interceptor/request
/proxy/:port/filter/request
"""
js = 'alert("foo")'
status_code = self.client.request_interceptor(js)
status_code = self.client.request_filter(js)
assert(status_code == 200)

def test_request_interceptor_with_invalid_js(self):
"""
/proxy/:port/interceptor/request
/proxy/:port/filter/request
"""
js = 'alert("foo"'
status_code = self.client.request_interceptor(js)
status_code = self.client.request_filter(js)
assert(status_code == 500)

def test_timeouts_invalid_timeouts(self):
Expand Down