Repository: mitmproxy/mitmproxy

Issue:
"Cannot copy binary data to clipboard." when attempting to copy JSON response to clipboard
To replicate, make the following request:

```
GET /rest/v3/showtimelist?count=1&date=2015-06-03&format=json&mediafmt=mp4&page=1&partner=20078C967592&profile=large&theaters=W7514&sed=20150603&sig=uxFJEEPP7SFVgS1pV5NkGbUgsTQ= HTTP/1.1
Host: api.allocine.fr
Accept: */*
Cookie: geolevel1=73072; geolevel2=83090; geolevel3=83159; xtan427117=-; xtant427117=1; xtidc=150301131534959430; xtvrn=$427117$
User-Agent: AlloCine/2.9 CFNetwork/609.1.4 Darwin/13.0.0
Accept-Language: en;q=1
Accept-Encoding: gzip, deflate
Content-Length: 0
```

Saving the latter response to a file works fine:

```
HTTP/1.1 200 OK
Server: nginx
Content-Type: application/json; charset=utf-8
Last-Modified: Wed, 03 Jun 2015 04:14:58 GMT
p3p: CP="CAO PSA OUR"
X-ServerName: WEBF2-01
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-UA-Compatible: IE=8
Access-Control-Allow-Origin: *
X-KompressorName: kompressor5
Content-Length: 1079
Vary: Accept-Encoding
Cache-Control: public, max-age=3564
Date: Wed, 03 Jun 2015 04:14:58 GMT
Connection: keep-alive

{"feed":{"updated":"2015-06-03T06:14:58Z","page":1,"count":1,"totalResults":1,"theaterShowtimes":[{"place":{"theater":{"code":"W7514","name":"Cinema Paradiso Grand Palais","address":"Avenue Winston Churchill","postalCode":"75008","city":"Paris 8e arrondissement","picture":{"path":"\/pictures\/212\/062\/21206278_173021223.jpg","href":"http:\/\/fr.web.img2.acsta.net\/pictures\/212\/062\/21206278_173021223.jpg"},"hasPRMAccess":1,"geoloc":{"lat":48.8658950,"long":2.3138045},"link":[{"rel":"aco:web","href":"http:\/\/www.allocine.fr\/seance\/salle_gen_csalle=W7514.html"}]}}}],"poi":[{"code":4900,"operation":{"code":1,"$":"McDonald's"},"name":"PARIS CHAMPS ELYSEES GALERIE","address":"84, avenue des Champs Elysées","postalCode":"75008","city":"PARIS","geoloc":{"lat":48.87107,"long":2.30351},"thumbnail":{"href":"http:\/\/images.allocine.fr\/commons\/ad\/device\/pin_mcdonald.png"},"link":[{"rel":"mobile","href":"https:\/\/m.restaurants.mcdonalds.fr\/PARIS-CHAMPS-ELYSEES-GALERIE"},{"rel":"deep","href":"mcdonalds:\/\/open\/restaurant?code=PARIS-CHAMPS-ELYSEES-GALERIE"}]}]}}
```



Discussion:
Hi @gajus 
I can't reproduce the error on my machine, have  tried this with current master? 
which OS? because pyperclip (the library used for copying to clipboard) uses different commands based on which OS are you running. In Linux even has 3 methods for copying data (xsel, xclip & gtk clipboard) so perhaps is failing in only one scenario.
thanks

The latest OSX.

> On Jun 4, 2015, at 23:12, Marcelo Glezer notifications@github.com wrote:
> 
> Hi @gajus 
> I can't reproduce the error on my machine, have tried this with current master? 
> which OS? because pyperclip (the library used for copying to clipboard) uses different commands based on which OS are you running. In Linux even has 3 methods for copying data (xsel, xclip & gtk clipboard) so perhaps is failing in only one scenario.
> thanks
> 
> —
> Reply to this email directly or view it on GitHub.

Just in case, it works with most sites. It does not work with this particular request.

> On Jun 4, 2015, at 23:12, Marcelo Glezer notifications@github.com wrote:
> 
> Hi @gajus 
> I can't reproduce the error on my machine, have tried this with current master? 
> which OS? because pyperclip (the library used for copying to clipboard) uses different commands based on which OS are you running. In Linux even has 3 methods for copying data (xsel, xclip & gtk clipboard) so perhaps is failing in only one scenario.
> thanks
> 
> —
> Reply to this email directly or view it on GitHub.

Ok, i'm on ubuntu now, will try again on OSX later at home.

Never mind my last message. It happens with most request. I cannot see a reason why it works with some and doesn't with others.

I am running OSX 10.10.4 (14E17e).

I've made some test at home with osx at home and i could reproduce the error.
pyperclip throws an UnicodeDecodeError when reaches the é of  "Champs Elysées" but I still don't know why as unicode seems ok.
will try to see why this is an issue in OSX and not in linux. could you please generate a new GET as the current one seems to have the session expired?
Thanks.

Just make a request to http://en.wikipedia.org/wiki/%C3%89. That will result in the same behaviour.


Resolving patch:
diff --git a/libmproxy/console/common.py b/libmproxy/console/common.py
--- a/libmproxy/console/common.py
+++ b/libmproxy/console/common.py
@@ -284,8 +284,16 @@ def copy_flow(part, scope, flow, master, state):
             signals.status_message.send(message="No contents to copy.")
         return
 
+    # pyperclip calls encode('utf-8') on data to be copied without checking.
+    # if data are already encoded that way UnicodeDecodeError is thrown.
+    toclip = ""
     try:
-        pyperclip.copy(data)
+        toclip = data.decode('utf-8')
+    except (UnicodeDecodeError):        
+        toclip = data
+
+    try:
+        pyperclip.copy(toclip)
     except (RuntimeError, UnicodeDecodeError, AttributeError):
         def save(k):
             if k == "y":


Test patch: