Merge pull request #819 from ayanamist/develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Correct OAuth1 with query or data.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents b5b2e51, babac73 · Browse files · 1 file·+25 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+25 added−24 removed
- other:+25 added−24 removed
Provenance
- Commit
0ad4456- Tree
337a69d- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
b5b2e51- Parent 2
babac73
1 file changed+25 added−24 removed
modified:requests/auth.py+25 added-24 removed
31 unmodified lines32 33 log = logging.getLogger(__name__)34 35 CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded'36+CONTENT_TYPE_MULTI_PART = 'multipart/form-data'37 38 39 def _basic_auth_str(username, password):40 """Returns a Basic Auth string."""33 unmodified lines74 75 Parameters may be included from the body if the content-type is76 urlencoded, if no content type is set an educated guess is made.77 """78+ # split(";") because Content-Type may be "multipart/form-data; boundary=xxxxx"77- contenttype = r.headers.get('Content-Type', None)79+ contenttype = r.headers.get('Content-Type', '').split(";")[0].lower()80 # extract_params will not give params unless the body is a properly81 # formatted string, a dictionary or a list of 2-tuples.82 decoded_body = extract_params(r.data)83 82- _ct = (contenttype is None)83- _ct = _ct or contenttype.lower() == CONTENT_TYPE_FORM_URLENCODED84-85- if _ct and decoded_body != None:86- # extract_params can only check the present r.data and does not know87- # of r.files, thus an extra check is performed. We know that88- # if files are present the request will not have89- # Content-type: x-www-form-urlencoded. We guess it will have90- # a mimetype of multipart/form-encoded and if this is not the case91- # we assume the correct header will be set later.92- if r.files:93- # Omit body data in the signing and since it will always94- # be empty (cant add paras to body if multipart) and we wish95- # to preserve body.96- r.headers['Content-Type'] = 'multipart/form-encoded'97- r.url, r.headers, _ = self.client.sign(98- unicode(r.full_url), unicode(r.method), None, r.headers)99- else:100- # Normal signing101- r.headers['Content-Type'] = 'application/x-www-form-urlencoded'102- r.url, r.headers, r.data = self.client.sign(103- unicode(r.full_url), unicode(r.method), r.data, r.headers)104-84+ # extract_params can only check the present r.data and does not know85+ # of r.files, thus an extra check is performed. We know that86+ # if files are present the request will not have87+ # Content-type: x-www-form-urlencoded. We guess it will have88+ # a mimetype of multipart/form-data and if this is not the case89+ # we assume the correct header will be set later.90+ _oauth_signed = True91+ if r.files and contenttype == CONTENT_TYPE_MULTI_PART:92+ # Omit body data in the signing and since it will always93+ # be empty (cant add paras to body if multipart) and we wish94+ # to preserve body.95+ r.url, r.headers, _ = self.client.sign(96+ unicode(r.full_url), unicode(r.method), None, r.headers)97+ elif decoded_body != None and contenttype in (CONTENT_TYPE_FORM_URLENCODED, ''):98+ # Normal signing99+ if not contenttype:100+ r.headers['Content-Type'] = CONTENT_TYPE_FORM_URLENCODED101+ r.url, r.headers, r.data = self.client.sign(102+ unicode(r.full_url), unicode(r.method), r.data, r.headers)103+ else:104+ _oauth_signed = False105+ if _oauth_signed:106 # Both flows add params to the URL by using r.full_url,107 # so this prevents adding it again later108 r.params = {}109 273 unmodified lines