Skip to content
Myr

Start work on sending cookies back

Before adoption: this commit came in with the history the project adopted, so no landing records it.

On 401's the cookies received aren't sent back to the server. See: #1336

authored by Ian Ross and Ian Cordasco · committed by Ian Cordasco · parent 77bd9c4 · Browse files · 2 files+25 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+25 added−2 removed

  • other:+25 added−2 removed

2 files+25 added−2 removed

modified:requests/auth.py+9 added-2 removed
16 unmodified lines
1718from .compat import urlparse, str19from .utils import parse_dict_header202121log = logging.getLogger(__name__)2223CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded'24CONTENT_TYPE_MULTI_PART = 'multipart/form-data'
119 unmodified lines
144145 def handle_401(self, r, **kwargs):146 """Takes the given response and tries digest-auth, if needed."""147148 from .models import PreparedRequest149 num_401_calls = getattr(self, 'num_401_calls', 1)150 s_auth = r.headers.get('www-authenticate', '')151152 if 'digest' in s_auth.lower() and num_401_calls < 2:
5 unmodified lines
158 # Consume content and release the original connection159 # to allow our new request to reuse the same one.160 r.content161 r.raw.release_conn()162 prepared_request = PreparedRequest()163 prepared_request.url = r.request.url164 prepared_request.body = r.request.body165 prepared_request.headers = r.request.headers.copy()166 prepared_request.hooks = r.request.hooks167 prepared_request.prepare_cookies(r.cookies)168169 r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url)164 _r = r.connection.send(r.request, **kwargs)170 _r = r.connection.send(prepared_request, **kwargs)171 _r.history.append(r)172 _r.request = prepared_request173174 return _r175176 setattr(self, 'num_401_calls', 1)
8 unmodified lines
modified:test_requests.py+16 added
287 unmodified lines
288 s.auth = auth289 r = s.get(url)290 self.assertEqual(r.status_code, 200)291292 def test_DIGEST_AUTH_RETURNS_COOKIE(self):293 url = httpbin('digest-auth', 'auth', 'user', 'pass')294 auth = HTTPDigestAuth('user', 'pass')295 r = requests.get(url)296 assert r.cookies['fake'] == 'fake_value'297298 r = requests.get(url, auth=auth)299 assert r.status_code == 200300301 def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self):302 url = httpbin('digest-auth', 'auth', 'user', 'pass')303 auth = HTTPDigestAuth('user', 'pass')304 s = requests.Session()305 s.get(url, auth=auth)306 assert s.cookies['fake'] == 'fake_value'307308 def test_DIGEST_STREAM(self):309310 auth = HTTPDigestAuth('user', 'pass')311 url = httpbin('digest-auth', 'auth', 'user', 'pass')
494 unmodified lines