Better content-type detection and unicode fix
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ib Lundgren · committed by Ib Lundgren · parent 763393f · Browse files · 1 file·+42 added−5 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+42 added−5 removed
- other:+42 added−5 removed
Provenance
- Commit
c24665b- Tree
ca014cd- Author
Ib Lundgren <ib.lundgren@gmail.com>- Committer
Ib Lundgren <ib.lundgren@gmail.com>- Parent
763393f
1 file changed+42 added−5 removed
modified:requests/auth.py+42 added-5 removed
58 unmodified lines59 resource_owner_secret, callback_uri, signature_method,60 signature_type, rsa_key, verifier)61 62 def __call__(self, r):63+ """Add OAuth parameters to the request.64+65+ Parameters may be included from the body if the content-type is66+ urlencoded, if no content type is set an educated guess is made.67+ """68 contenttype = r.headers.get('Content-Type', None)69+ # extract_params will not give params unless the body is a properly70+ # formatted string, a dictionary or a list of 2-tuples.64- decoded_body = extract_params(r.data)71+ decoded_body = extract_params(r.data) 72 if contenttype == None and decoded_body != None:73+ # extract_params can only check the present r.data and does not know74+ # of r.files, thus an extra check is performed. We know that 75+ # if files are present the request will not have 76+ # Content-type: x-www-form-urlencoded. We guess it will have77+ # a mimetype of multipart/form-encoded and if this is not the case78+ # we assume the correct header will be set later.79+ if r.files:80+ # Omit body data in the signing and since it will always81+ # be empty (cant add paras to body if multipart) and we wish82+ # to preserve body. 83+ r.headers['Content-Type'] = 'multipart/form-encoded'84+ r.url, r.headers, _ = self.client.sign(85+ unicode(r.url), unicode(r.method), None, r.headers)86+ else:87+ # Normal signing66- r.headers['Content-Type'] = 'application/x-www-form-urlencoded'88+ r.headers['Content-Type'] = 'application/x-www-form-urlencoded'89+ r.url, r.headers, r.data = self.client.sign(90+ unicode(r.url), unicode(r.method), r.data, r.headers)91 68- r.url, r.headers, r.data = self.client.sign(69- unicode(r.url), unicode(r.method), r.data, r.headers)70- return r92+ # Having the authorization header, key or value, in unicode will93+ # result in UnicodeDecodeErrors when the request is concatenated94+ # by httplib. This can easily be seen when attaching files.95+ # Note that simply encoding the value is not enough since Python96+ # saves the type of first key set. Thus we remove and re-add.97+ # >>> d = {u'a':u'foo'}98+ # >>> d['a'] = 'foo'99+ # >>> d100+ # { u'a' : 'foo' }101+ if u'Authorization' in r.headers:102+ auth_header = r.headers[u'Authorization'].encode('utf-8')103+ del r.headers[u'Authorization']104+ r.headers['Authorization'] = auth_header105+ print r.headers106 107+ return r108 109+110 class HTTPBasicAuth(AuthBase):111 """Attaches HTTP Basic Authentication to the given Request object."""112 def __init__(self, username, password):113 self.username = username115 unmodified lines