Repository: mitmproxy/mitmproxy

Issue:
brotli encode/decode crash
##### Steps to reproduce the problem:

1. load google.com in browser
2. press `enter` on `GET https://www.google.com/ HTTP/2.0`
3. press `z` to select encoding in either `Request` or `Response`
4. press `b` to select brotli


##### Any other comments? What have you tried so far?

```
Traceback (most recent call last):                                                                                                            
  File "/home/whackashoe/code/mitmproxy/mitmproxy/tools/console/master.py", line 281, in run 
    self.loop.run()
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 278, in run 
    self._run()
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 376, in _run
    self.event_loop.run()
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 682, in run 
    self._loop()
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 719, in _loop
    self._watch_files[fd]()
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/raw_display.py", line 393, in <lambda>
    event_loop, callback, self.get_available_raw_input())
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/raw_display.py", line 493, in parse_input
    callback(processed, processed_codes)
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 403, in _update
    self.process_input(keys)
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 503, in process_input
    k = self._topmost_widget.keypress(self.screen_size, k)
  File "/home/whackashoe/code/mitmproxy/mitmproxy/tools/console/window.py", line 84, in keypress
    k = super().keypress(size, k)
  File "/home/whackashoe/code/mitmproxy/venv/lib/python3.5/site-packages/urwid/container.py", line 1116, in keypress
    return self.footer.keypress((maxcol,),key)
  File "/home/whackashoe/code/mitmproxy/mitmproxy/tools/console/statusbar.py", line 155, in keypress
    return self.master.ab.keypress(*args, **kwargs)
  File "/home/whackashoe/code/mitmproxy/mitmproxy/tools/console/statusbar.py", line 108, in keypress
    self.prompt_execute(k)
  File "/home/whackashoe/code/mitmproxy/mitmproxy/tools/console/statusbar.py", line 133, in prompt_execute
    msg = p(txt)
  File "/home/whackashoe/code/mitmproxy/mitmproxy/tools/console/statusbar.py", line 31, in __call__
    return self.callback(txt, *self.args)
  File "/home/whackashoe/code/mitmproxy/mitmproxy/tools/console/flowview.py", line 686, in encode_callback
    conn.encode(encoding_map[key])
  File "/home/whackashoe/code/mitmproxy/mitmproxy/net/http/message.py", line 245, in encode
    raise ValueError("Invalid content encoding {}".format(repr(e)))
ValueError: Invalid content encoding 'brotli'
```

Here is a patch which suppresses the error:

```
diff --git a/mitmproxy/tools/console/flowview.py b/mitmproxy/tools/console/flowview.py                                          
index a97a9b3..650ef42 100644
--- a/mitmproxy/tools/console/flowview.py
+++ b/mitmproxy/tools/console/flowview.py
@@ -683,5 +683,9 @@ class FlowView(tabs.Tabs):
             "d": "deflate",
             "b": "brotli",
         }
-        conn.encode(encoding_map[key])
+        try:
+            conn.encode(encoding_map[key])
+        except ValueError:
+            pass
+
         signals.flow_change.send(self, flow = self.flow)
```


##### System information

```
$ mitmproxy --version
Mitmproxy version: 3.0.0 (2.0.0dev0020-0x2aecffd) 
Python version: 3.5.0
Platform: Linux-3.13.0-107-generic-x86_64-with-Ubuntu-14.04-trusty
SSL version: OpenSSL 1.0.2k  26 Jan 2017
Linux distro: Ubuntu 14.04 trusty
```





Discussion:
Thanks. I think the actual bug here is that we should invoke the encoding with "br", not "brotli".

Resolving patch:
diff --git a/mitmproxy/tools/console/flowview.py b/mitmproxy/tools/console/flowview.py
--- a/mitmproxy/tools/console/flowview.py
+++ b/mitmproxy/tools/console/flowview.py
@@ -681,7 +681,7 @@ def encode_callback(self, key, conn):
         encoding_map = {
             "z": "gzip",
             "d": "deflate",
-            "b": "brotli",
+            "b": "br",
         }
         conn.encode(encoding_map[key])
         signals.flow_change.send(self, flow = self.flow)


Test patch: