aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/addons/streambodies.py
blob: 88cb74f33c980090d71e7947066938e7436f622a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from mitmproxy.net.http import http1
from mitmproxy import exceptions
from mitmproxy import ctx


class StreamBodies:
    def __init__(self):
        self.max_size = None

    def configure(self, options, updated):
        self.max_size = options.stream_large_bodies

    def run(self, f, is_request):
        if self.max_size:
            r = f.request if is_request else f.response
            try:
                expected_size = http1.expected_http_body_size(
                    f.request, f.response if not is_request else None
                )
            except exceptions.HTTPException:
                f.reply.kill()
                return
            if expected_size and not r.raw_content and not (0 <= expected_size <= self.max_size):
                # r.stream may already be a callable, which we want to preserve.
                r.stream = r.stream or True
                # FIXME: make message generic when we add rquest streaming
                ctx.log.info("Streaming response from %s" % f.request.host)

    # FIXME! Request streaming doesn't work at the moment.
    def requestheaders(self, f):
        self.run(f, True)

    def responseheaders(self, f):
        self.run(f, False)