Wrong results using proxies and HTTPS

Hi!

I've found that requests behaves quite weirdly when using a proxy with HTTPS endpoints.
It doesn't change if the proxy is specified using environment variables or using the _proxies_ attribute.
### Without proxy

```
import requests
rs = requests.session()
res = rs.request("get", 
   "https://api.bitbucket.org/1.0/repositories/tutorials/tutorials.bitbucket.org")

print res.text
>> u'{\n    "scm": "hg",
...
"/1.0/repositories/tutorials/tutorials.bitbucket.org"\n}'
```
### Using a proxy

```
import requests
rs = requests.session()

proxies = { 'https': "http://localhost:3128" }
res = rs.request("get", 
   "https://api.bitbucket.org/1.0/repositories/tutorials/tutorials.bitbucket.org",
    proxies=proxies)

print res.text
>> u'<HTML></HTML>\r\n'
```

Note 1: that proxy uses a CNTML authentication
Note 2: it works with HTTP endpoints


Requests' current implementation of proxying is lacking when it comes to HTTPS, and right now there isn't a lot we can do about it. We're waiting on shazow/urllib3#170 (which was shazow/urllib3#139, which was intended to resolve shazow/urllib3#50, just in case you wanted to know how long this has been going on) to implement the HTTP CONNECT verb in urllib3.

At the moment, if you try to proxy an HTTPS connection through an HTTP proxy, everything is just...weird. I've seen a wide variety of behaviour. In the best case, the proxy will see that you requested an HTTPS connection and will create such a connection between itself and the endpoint, then strip the SSL and send the data to you over HTTP, but realistically that rarely happens.

Thanks for reporting this bug, and I'm sorry there isn't more we can do right now. You're one of a fairly long list of people who is waiting on this, and I promise that it is very, very high up our list of priorities.


@astratto Can you try: https://github.com/shazow/urllib3/pull/170#issuecomment-21234629


@schlamar I've just tried and it works! Great!

``` python
import sys
sys.path.insert(0, 'requests-new-urllib3-api/requests/packages/')
sys.path.insert(0, 'requests-new-urllib3-api')

import requests
rs = requests.session()

proxies = { 'https': "http://localhost:3128" }
res = rs.request("get",
...    "https://api.bitbucket.org/1.0/repositories/tutorials/tutorials.bitbucket.org",
...     proxies=proxies)

print res.text
{"scm": "hg", "has_wiki": true, "last_updated": "2013-07-19 08:30:48", "creator": null, "forks_count": 3036, "created_on": "2011-12-20 17:35:06", "owner": "tutorials", "logo": "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/tutorials.bitbucket.org-logo-1456883302-9_avatar.png", "email_mailinglist": "\ndocs@bitbucket.org", "is_mq": false, "size": 50212734, "read_only": false, "fork_of": null, "mq_of": null, "followers_count": 14, "state": "available", "utc_created_on": "2011-12-20 16:35:06+00:00", "website": "", "description": "Site for tutorial101 files", "has_issues": true, "is_fork": false, "slug": "tutorials.bitbucket.org", "is_private": false, "name": "tutorials.bitbucket.org", "language": "html/css", "utc_last_updated": "2013-07-19 06:30:48+00:00", "email_writers": true, "no_public_forks": false, "resource_uri": "/1.0/repositories/tutorials/tutorials.bitbucket.org"}
```


The above seems to get rid of the following error:
`requests.exceptions.HTTPError: 501 Server Error: Not Implemented`

however it seems as though auth still doesn't work when requesting a https page. I'm now getting:
`requests.packages.urllib3.exceptions.ProxyError: Cannot connect to proxy. Socket error: Tunnel connection failed: 407 Proxy Authentication Required.`

Any insight?


How are you authorising?


Ignore that, I've spotted the problem. Yeah, with that set of code you won't get the Proxy-Authorization header. I'll mention it over at our PR.