Repository: mitmproxy/mitmproxy

Issue:
Editing cookies on an intercepted response crashes mitmproxy
##### Steps to reproduce the problem:
1. set an intercept filter
2. trigger a request that matches the filter
3. press 'a' to make the request pass
4. press <tab> to switch to the intercepted response
5. press 'e' to edit the response
6. press 'c' for editing cookies
##### What is the expected behavior?

It allows me to modify the cookies
##### What went wrong?

It crashes
##### Any other comments?

I installed through Homebrew, running Mac OS X 10.10.5
Mitmproxy version 0.16

---

```
Traceback (most recent call last):
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/__init__.py", line 498, in run
    self.loop.run()
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 278, in run
    self._run()
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 376, in _run
    self.event_loop.run()
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 682, in run
    self._loop()
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 719, in _loop
    self._watch_files[fd]()
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/raw_display.py", line 393, in <lambda>
    event_loop, callback, self.get_available_raw_input())
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/raw_display.py", line 493, in parse_input
    callback(processed, processed_codes)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 403, in _update
    self.process_input(keys)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 503, in process_input
    k = self._topmost_widget.keypress(self.screen_size, k)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/window.py", line 39, in keypress
    k = super(self.__class__, self).keypress(size, k)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/container.py", line 1116, in keypress
    return self.footer.keypress((maxcol,),key)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/statusbar.py", line 126, in keypress
    return self.ab.keypress(*args, **kwargs)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/statusbar.py", line 81, in keypress
    self.prompt_execute(k)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/statusbar.py", line 106, in prompt_execute
    msg = p(txt, *args)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/flowview.py", line 400, in edit
    message
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/__init__.py", line 557, in view_grideditor
    ge.make_help()
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/blinker/base.py", line 267, in send
    for receiver in self.receivers_for(sender)]
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/__init__.py", line 298, in sig_push_view_state
    self.loop.draw_screen()
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/main_loop.py", line 578, in draw_screen
    canvas = self._topmost_widget.render(self.screen_size, focus=True)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/container.py", line 1083, in render
    focus and self.focus_part == 'body')
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/decoration.py", line 225, in render
    canv = self._original_widget.render(size, focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 1751, in render
    canv = get_delegate(self).render(size, focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/container.py", line 1083, in render
    focus and self.focus_part == 'body')
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/listbox.py", line 457, in render
    (maxcol, maxrow), focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/listbox.py", line 339, in calculate_visible
    self._set_focus_complete( (maxcol, maxrow), focus )
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/listbox.py", line 704, in _set_focus_complete
    (maxcol,maxrow), focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/listbox.py", line 674, in _set_focus_first_selectable
    (maxcol, maxrow), focus=focus)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/vendor/lib/python2.7/site-packages/urwid/listbox.py", line 342, in calculate_visible
    focus_widget, focus_pos = self.body.get_focus()
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/grideditor.py", line 297, in get_focus
    self.lst[self.focus]
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/grideditor.py", line 144, in __init__
    w = self.editor.columns[i].text(v)
  File "/usr/local/Cellar/mitmproxy/0.16/libexec/lib/python2.7/site-packages/libmproxy/console/grideditor.py", line 72, in text
    p = http_cookies._format_pairs(obj, sep="\n")
NameError: global name 'http_cookies' is not defined

mitmproxy has crashed!
Please lodge a bug report at:
    https://github.com/mitmproxy/mitmproxy
Shutting down...
```



Discussion:
Thanks for the great report! :smiley:


Resolving patch:
diff --git a/mitmproxy/console/grideditor.py b/mitmproxy/console/grideditor.py
--- a/mitmproxy/console/grideditor.py
+++ b/mitmproxy/console/grideditor.py
@@ -6,7 +6,7 @@
 import urwid
 
 from netlib import odict
-from netlib.http import user_agents
+from netlib.http import user_agents, cookies
 
 from . import common, signals
 from .. import utils, filt, script
@@ -69,7 +69,7 @@ def __init__(self, heading, subeditor):
         self.subeditor = subeditor
 
     def text(self, obj):
-        p = http_cookies._format_pairs(obj, sep="\n")
+        p = cookies._format_pairs(obj, sep="\n")
         return urwid.Text(p)
 
     def blank(self):


Test patch: