aboutsummaryrefslogtreecommitdiffstats
path: root/docs/scripting
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2016-10-16 11:12:58 +1300
committerAldo Cortesi <aldo@nullcube.com>2016-10-16 20:26:06 +1300
commitfb69c9c3453142ae6beb4040295accb41fdc6878 (patch)
tree91e4b36a9bdeeb801e1bfca6707f86bb4257f7a5 /docs/scripting
parent61040a7bcd46c057e34fe4671ef20b9111649e74 (diff)
downloadmitmproxy-fb69c9c3453142ae6beb4040295accb41fdc6878.tar.gz
mitmproxy-fb69c9c3453142ae6beb4040295accb41fdc6878.tar.bz2
mitmproxy-fb69c9c3453142ae6beb4040295accb41fdc6878.zip
docs: overview, classes, arguments
Diffstat (limited to 'docs/scripting')
-rw-r--r--docs/scripting/api.rst8
-rw-r--r--docs/scripting/overview.rst72
2 files changed, 60 insertions, 20 deletions
diff --git a/docs/scripting/api.rst b/docs/scripting/api.rst
index a1769329..a864b442 100644
--- a/docs/scripting/api.rst
+++ b/docs/scripting/api.rst
@@ -8,6 +8,8 @@ API
- `mitmproxy.models.http.HTTPRequest <#mitmproxy.models.http.HTTPRequest>`_
- `mitmproxy.models.http.HTTPResponse <#mitmproxy.models.http.HTTPResponse>`_
- `mitmproxy.models.http.HTTPFlow <#mitmproxy.models.http.HTTPFlow>`_
+- Errors
+ - `mitmproxy.models.flow.Error <#mitmproxy.models.flow.Error>`_
HTTP
----
@@ -20,3 +22,9 @@ HTTP
.. autoclass:: mitmproxy.models.http.HTTPFlow
:inherited-members:
+
+Errors
+------
+
+.. autoclass:: mitmproxy.models.flow.Error
+ :inherited-members:
diff --git a/docs/scripting/overview.rst b/docs/scripting/overview.rst
index a0dfe111..f8dd9f2e 100644
--- a/docs/scripting/overview.rst
+++ b/docs/scripting/overview.rst
@@ -1,13 +1,17 @@
.. _overview:
-Overview
-=========
+Introduction
+============
Mitmproxy has a powerful scripting API that allows you to control almost any
aspect of traffic being proxied. In fact, much of mitmproxy's own core
functionality is implemented using the exact same API exposed to scripters (see
:src:`mitmproxy/builtins`).
+
+A simple example
+----------------
+
Scripting is event driven, with named handlers on the script object called at
appropriate points of mitmproxy's operation. Here's a complete mitmproxy script
that adds a new header to every HTTP response before it is returned to the
@@ -17,18 +21,57 @@ client:
:caption: :src:`examples/add_header.py`
:language: python
-All events that deal with an HTTP request get an instance of
-:py:class:`~mitmproxy.models.HTTPFlow`, which we can use to manipulate the
-response itself. We can now run this script using mitmdump or mitmproxy as
-follows:
+All events that deal with an HTTP request get an instance of `HTTPFlow
+<api.html#mitmproxy.models.http.HTTPFlow>`_, which we can use to manipulate the
+response itself. We can now run this script using mitmdump, and the new header
+will be added to all responses passing through the proxy:
>>> mitmdump -s add_header.py
-The new header will be added to all responses passing through the proxy.
+Using classes
+-------------
+
+In the example above, the script object is the ``add_header`` module itself.
+That is, the handlers are declared at the global level of the script. This is
+great for quick hacks, but soon becomes limiting as scripts become more
+sophisticated.
+
+When a script first starts up, the `start <events.html#start>`_, event is
+called before anything else happens. You can replace the current script object
+by returning it from this handler. Here's how this looks when applied to the
+example above:
+
+.. literalinclude:: ../../examples/classes.py
+ :caption: :src:`examples/classes.py`
+ :language: python
+
+So here, we're using a module-level script to "boot up" into a class instance.
+From this point on, the module-level script is removed from the handler chain,
+and is replaced by the class instance.
+
+
+Handling arguments
+------------------
-mitmproxy comes with a variety of example inline scripts, which demonstrate
-many basic tasks.
+Scripts can handle their own command-line arguments, just like any other Python
+program. Let's build on the example above to do something slightly more
+sophisticated - replace one value with another in all responses. Mitmproxy's
+`HTTPRequest <api.html#mitmproxy.models.http.HTTPRequest>`_ and `HTTPResponse
+<api.html#mitmproxy.models.http.HTTPResponse>`_ objects have a handy `replace
+<api.html#mitmproxy.models.http.HTTPResponse.replace>`_ method that takes care
+of all the details for us.
+
+.. literalinclude:: ../../examples/arguments.py
+ :caption: :src:`examples/arguments.py`
+ :language: python
+
+We can now call this script on the command-line like this:
+
+>>> mitmdump -dd -s "./arguments.py html faketml"
+
+Whenever a handler is called, mitpmroxy rewrites the script environment so that
+it sees its own arguments as if it was invoked from the command-line.
Running scripts in parallel
@@ -43,17 +86,6 @@ While that's usually a very desirable behaviour, blocking scripts can be run thr
:caption: examples/nonblocking.py
:language: python
-Make scripts configurable with arguments
-----------------------------------------
-
-Sometimes, you want to pass runtime arguments to the inline script. This can be simply done by
-surrounding the script call with quotes, e.g. ```mitmdump -s 'script.py --foo 42'``.
-The arguments are then exposed in the start event:
-
-.. literalinclude:: ../../examples/modify_response_body.py
- :caption: examples/modify_response_body.py
- :language: python
-
Running scripts on saved flows
------------------------------