Skip to content

Commit

Permalink
Merge pull request #6718 from emaszs/1.0.14_crab
Browse files Browse the repository at this point in the history
Allow pycurl_manager.py to accept encoded string as data. Fixes #6467
  • Loading branch information
ericvaandering committed Mar 18, 2016
2 parents d1f5127 + e1e654a commit a301919
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/python/WMCore/Services/pycurl_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,43 @@ def __init__(self, config=None, logger=None):
self.maxredirs = config.get('maxredirs', 5)
self.logger = logger if logger else logging.getLogger()

def encode_params(self, params, verb, doseq):
""" Encode request parameters for usage with the 4 verbs.
Assume params is alrady encoded if it is a string and
uses a different encoding depending on the HTTP verb
(either json.dumps or urllib.urlencode)
"""
#data is already encoded, just return it
if isinstance(params, basestring):
return params

#data is not encoded, we need to do that
if verb in ['GET', 'HEAD']:
if params:
encoded_data = urllib.urlencode(params, doseq=doseq)
else:
return ''
else:
if params:
encoded_data = json.dumps(params)
else:
return {}

return encoded_data

def set_opts(self, curl, url, params, headers,
ckey=None, cert=None, capath=None, verbose=None, verb='GET', doseq=True, cainfo=None):
"""Set options for given curl object, params should be a dictionary"""
if params == None:
params = {}
if not isinstance(params, dict):
raise TypeError("pycurl parameters should be passed as dictionary")
if not (isinstance(params, dict) or isinstance(params, basestring) or params==None):
raise TypeError("pycurl parameters should be passed as dictionary or an (encoded) string")
curl.setopt(pycurl.NOSIGNAL, self.nosignal)
curl.setopt(pycurl.TIMEOUT, self.timeout)
curl.setopt(pycurl.CONNECTTIMEOUT, self.connecttimeout)
curl.setopt(pycurl.FOLLOWLOCATION, self.followlocation)
curl.setopt(pycurl.MAXREDIRS, self.maxredirs)

if params:
encoded_data = urllib.urlencode(params, doseq=doseq)
else:
encoded_data = ''
encoded_data = self.encode_params(params, verb, doseq)

if verb == 'GET':
if encoded_data:
url = url + '?' + encoded_data
Expand All @@ -95,13 +115,13 @@ def set_opts(self, curl, url, params, headers,
curl.setopt(pycurl.NOBODY, True)
elif verb == 'POST':
curl.setopt(pycurl.POST, 1)
if params:
curl.setopt(pycurl.POSTFIELDS, json.dumps(params))
if encoded_data:
curl.setopt(pycurl.POSTFIELDS, encoded_data)
elif verb == 'DELETE' or verb == 'PUT':
curl.setopt(pycurl.CUSTOMREQUEST, verb)
curl.setopt(pycurl.HTTPHEADER, ['Transfer-Encoding: chunked'])
if params:
curl.setopt(pycurl.POSTFIELDS, json.dumps(params))
if encoded_data:
curl.setopt(pycurl.POSTFIELDS, encoded_data)
else:
raise Exception('Unsupported HTTP method "%s"' % verb)

Expand Down

0 comments on commit a301919

Please sign in to comment.