Merge branch 'develop' of https://github.com/catwell/requests into develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents d4a77cf, f3ad56f · Browse files · 3 files·+55 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+55 added−23 removed
- tests:+14 added−0 removed
- other:+41 added−23 removed
Provenance
- Commit
e3ba495- Tree
8475b23- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
d4a77cf- Parent 2
f3ad56f
3 files changed+55 added−23 removed
modified:requests/auth.py+2 added
54 unmodified lines55 56 def handle_401(self, r):57 """Takes the given response and tries digest-auth, if needed."""58 59+ r.request.deregister_hook('response', self.handle_401)60+61 s_auth = r.headers.get('www-authenticate', '')62 63 if 'digest' in s_auth.lower():64 86 unmodified linesmodified:requests/models.py+39 added-23 removed
78 unmodified lines79 #: Dictionary of HTTP Headers to attach to the :class:`Request <Request>`.80 self.headers = dict(headers or [])81 82 #: Dictionary of files to multipart upload (``{filename: content}``).83- self.files = files83+ self.files = None84 85 #: HTTP Method to use.86 self.method = method87 25 unmodified lines113 self.proxies['https'] = os.environ['HTTPS_PROXY']114 115 self.data, self._enc_data = self._encode_params(data)116 self.params, self._enc_params = self._encode_params(params)117+ self.files, self._enc_files = self._encode_files(files)118 119 #: :class:`Response <Response>` instance, containing120 #: content and metadata of HTTP Response, once :attr:`sent <send>`.121 self.response = Response()207 unmodified lines329 return result, urlencode(result, doseq=True)330 else:331 return data, data332 333+ def _encode_files(self,files):334+335+ if (not files) or isinstance(self.data, str):336+ return None, None337+338+ try:339+ fields = self.data.copy()340+ except AttributeError:341+ fields = dict(self.data)342+343+ for (k, v) in list(files.items()):344+ # support for explicit filename345+ if isinstance(v, (tuple, list)):346+ fn, fp = v347+ else:348+ fn = guess_filename(v) or k349+ fp = v350+ fields.update({k: (fn, fp.read())})351+352+ (body, content_type) = encode_multipart_formdata(fields)353+354+ return files, (body, content_type)355+356 @property357 def full_url(self):358 """Build the actual URL to use."""359 71 unmodified lines431 432 def register_hook(self, event, hook):433 """Properly register a hook."""434 411- return self.hooks[event].append(hook)435+ self.hooks[event].append(hook)436 437+ def deregister_hook(self,event,hook):438+ """Deregister a previously registered hook.439+ Returns True if the hook existed, False if not.440+ """441+442+ try:443+ self.hooks[event].remove(hook)444+ return True445+ except ValueError:446+ return False447+448 def send(self, anyway=False, prefetch=False):449 """Sends the request. Returns True of successful, False if not.450 If there was an HTTPError during transmission,451 self.response.status_code will contain the HTTPError code.18 unmodified lines470 content_type = None471 472 # Multi-part file uploads.473 if self.files:439- if not isinstance(self.data, str):440-441- try:442- fields = self.data.copy()443- except AttributeError:444- fields = dict(self.data)445-446- for (k, v) in list(self.files.items()):447- # support for explicit filename448- if isinstance(v, (tuple, list)):449- fn, fp = v450- else:451- fn = guess_filename(v) or k452- fp = v453- fields.update({k: (fn, fp.read())})454-455- (body, content_type) = encode_multipart_formdata(fields)456- else:457- pass458- # TODO: Conflict?474+ (body, content_type) = self._enc_files475 else:476 if self.data:477 478 body = self._enc_data291 unmodified lines770 771 except AttributeError:772 self._content = None773 758- self._content_consumed = True 774+ self._content_consumed = True775 return self._content776 777 def _detected_encoding(self):778 try:59 unmodified linesmodified:tests/test_requests.py+14 added
270 unmodified lines271 s = requests.session(auth=auth)272 r = get(url, session=s)273 self.assertEqual(r.status_code, 200)274 275+ def test_DIGESTAUTH_WRONG_HTTP_401_GET(self):276+277+ for service in SERVICES:278+279+ auth = HTTPDigestAuth('user', 'wrongpass')280+ url = service('digest-auth', 'auth', 'user', 'pass')281+282+ r = get(url, auth=auth)283+ self.assertEqual(r.status_code, 401)284+285+ s = requests.session(auth=auth)286+ r = get(url, session=s)287+ self.assertEqual(r.status_code, 401)288+289 def test_POSTBIN_GET_POST_FILES(self):290 291 for service in SERVICES:292 545 unmodified lines