GAE - not retrieving cookies from the response

I've found an issue when using requests with toolbelt through Google App Engine, because of the urllib3 patching with urlfetch. The problem is that the cookie jar is not populated due to a missing attribute (original_response) into the urllib3 response (when switching from sockets to urlfetch and using the HTTPAdapter through the toolbelt monkeypatch).

The bug is self explained within this patch that I've made and works flawlessly into the GAE production and development environments:

```python
"""Runtime monkey-patching for various problematic libraries."""


import re

import requests
from requests.cookies import MockRequest


class MockResponse(object):
    """Wraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`.

    ...what? Basically, expose the parsed HTTP headers from the server response
    the way `cookielib` expects to see them.
    """

    SEPARATOR = re.compile(r",\s*(?=[\w-]+=)")

    def __init__(self, response):
        """Make a MockResponse for `cookielib` to read.

        :param response: a HTTPResponse or analogous carrying the headers
        """
        self._response = response

    def info(self):
        return self

    def getheaders(self, name):
        headers = self._response.getheader(name)
        return self.SEPARATOR.split(headers) if headers else []


def extract_cookies_to_jar(jar, request, response):
    """Extract the cookies from the response into a CookieJar.

    :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
    :param request: our own requests.Request object
    :param response: urllib3.HTTPResponse object
    """
    # the _original_response field is the wrapped httplib.HTTPResponse object,
    req = MockRequest(request)
    # pull out the HTTPMessage with the headers and put it in the mock:
    res = MockResponse(response)
    jar.extract_cookies(res, req)


def _monkeypatch_requests():
    """Monkey patch that problematic cookie extraction function."""
    modules = [
        # Patch the entire namespace within Python.
        requests.adapters,
        requests.auth,
        requests.cookies,
        requests.sessions,
    ]
    for module in modules:
        setattr(module, "extract_cookies_to_jar", extract_cookies_to_jar)


def monkeypatch():
    _monkeypatch_requests()
```

https://pastebin.com/FgDZfT8y

You should integrate this somehow because is affecting everyone (that's the reason they switch to urlfetch, standard urllib or other primitives). Thanks!

Sorry, but it's not entirely clear to me where this bug is coming from. What code is causing this attribute to be missing? Because I don't think that problem is in the Requests code, is it?

The issue: http://stackoverflow.com/questions/18080556/python-gae-requests-not-returning-cookies-after-get-request/44079378#44079378

When using requests library in GAE, the cookies are not stored within the session, therefore you can't do authentications or request some pages that requires enabling cookies.

The problem is in the `MockResponse` class and into the `extract_cookies_to_jar`, because is using the old original response within the urllib3 response object and this is absent when ran through GAE which leads to the incapability of retrieving and storing the response cookies which make requests fail badly in most situations.

You should replace the code here: https://github.com/kennethreitz/requests/blob/master/requests/cookies.py#L98-L133 with the code I gave you above.. or something similar (selecting the old attribute first and if it's not existing, then getting the headers and splitting them from the new one). Line **126** is the real issue/bug. Also, there is a method called `getheaders` which is not returning. The bug didn't replicate yet because the cookielib from Python is not using that, but how could you let such things into production? https://github.com/kennethreitz/requests/blob/master/requests/cookies.py#L116

Ok, so that seems fair.

Please feel free to open a pull request with this change.

Here's the PR: https://github.com/kennethreitz/requests/pull/4044. Feel free to refactor it as you want.

Tested with GAE on development and production environments and also on normal runs (outside GAE). Thanks!

Also, through my patch, I've just discovered that there are some unstable/wrong tests whose issues weren't replicating due to unrelated circumstances: https://travis-ci.org/kennethreitz/requests/jobs/234560896

Should be fixed now that we've had a release of urllib3 and requests.