Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setup(self, curl_opener):
"""Setup auth method for curl opener
"""
curl_opener.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST)
curl_opener.setopt(pycurl.USERPWD, "%s:%s" % (self._username,
self._password))
if request.auth_username is not None:
userpwd = "%s:%s" % (request.auth_username, request.auth_password or '')
if request.auth_mode is None or request.auth_mode == "basic":
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
elif request.auth_mode == "digest":
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST)
else:
raise ValueError("Unsupported auth_mode %s" % request.auth_mode)
curl.setopt(pycurl.USERPWD, native_str(userpwd))
gen_log.debug("%s %s (username: %r)", request.method, request.url,
request.auth_username)
else:
curl.unsetopt(pycurl.USERPWD)
gen_log.debug("%s %s", request.method, request.url)
if request.client_cert is not None:
curl.setopt(pycurl.SSLCERT, request.client_cert)
if request.client_key is not None:
curl.setopt(pycurl.SSLKEY, request.client_key)
if threading.activeCount() > 1:
# libcurl/pycurl is not thread-safe by default. When multiple threads
# are used, signals should be disabled. This has the side effect
# of disabling DNS timeouts in some environments (when libcurl is
# not linked against ares), so we don't do it when there is only one
# thread. Applications that use many short-lived threads may need
# to set NOSIGNAL manually in a prepare_curl_callback since
# there may not be any other threads running at the time we call
if debug:
## Callback function invoked when header data is ready
def header(buf):
print "header", buf
def _debug_handler(debug_type, debug_msg):
print "pycurl:debug(%d): %s" % (debug_type, debug_msg)
ch.setopt(pycurl.VERBOSE, 1)
ch.setopt(pycurl.DEBUGFUNCTION, debug_callback or _debug_handler)
ch.setopt(pycurl.HEADERFUNCTION, header)
if username:
ch.setopt(pycurl.USERPWD, ('%s:%s' %
(username, password)).encode('utf-8'))
ch.setopt(pycurl.HTTPAUTH, getattr(pycurl, "HTTPAUTH_%s" %
auth_type.upper()))
try:
ch.perform()
resp = data.getvalue()
logging.debug("Posting to %r which returned %r", url, resp)
return (url, resp)
except pycurl.error, e:
logging.debug("Posting to %r resulted in error: %r", url, e)
return (url, e)
def ioctl(cmd):
if cmd == curl.IOCMD_RESTARTREAD:
request_buffer.seek(0)
curl.setopt(pycurl.IOCTLFUNCTION, ioctl)
curl.setopt(pycurl.POSTFIELDSIZE, len(request.body))
else:
curl.setopt(pycurl.INFILESIZE, len(request.body))
if request.auth_username and request.auth_password:
userpwd = "%s:%s" % (request.auth_username, request.auth_password)
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
curl.setopt(pycurl.USERPWD, userpwd)
_log.info("%s %s (username: %r)", request.method, request.url,
request.auth_username)
else:
curl.unsetopt(pycurl.USERPWD)
_log.info("%s %s", request.method, request.url)
if request.prepare_curl_callback is not None:
request.prepare_curl_callback(curl)
def ioctl(cmd):
if cmd == curl.IOCMD_RESTARTREAD:
request_buffer.seek(0)
curl.setopt(pycurl.IOCTLFUNCTION, ioctl)
curl.setopt(pycurl.POSTFIELDSIZE, len(request.body))
else:
curl.setopt(pycurl.INFILESIZE, len(request.body))
if request.auth_username and request.auth_password:
userpwd = "%s:%s" % (request.auth_username, request.auth_password)
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
curl.setopt(pycurl.USERPWD, userpwd)
logging.info("%s %s (username: %r)", request.method, request.url,
request.auth_username)
else:
curl.unsetopt(pycurl.USERPWD)
logging.info("%s %s", request.method, request.url)
if threading.active_count() > 1:
# libcurl/pycurl is not thread-safe by default. When multiple threads
# are used, signals should be disabled. This has the side effect
# of disabling DNS timeouts in some environments (when libcurl is
# not linked against ares), so we don't do it when there is only one
# thread. Applications that use many short-lived threads may need
# to set NOSIGNAL manually in a prepare_curl_callback since
# there may not be any other threads running at the time we call
# threading.active_count.
curl.setopt(pycurl.NOSIGNAL, 1)
if request.prepare_curl_callback is not None:
request.prepare_curl_callback(curl)
curl.setopt(pycurl.INFILESIZE, len(request.body))
elif request.method == "GET":
if request.body is not None:
raise AssertionError('Body must be empty for GET request')
if request.auth_username is not None:
userpwd = "%s:%s" % (request.auth_username, request.auth_password or '')
if request.auth_mode is None or request.auth_mode == "basic":
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
elif request.auth_mode == "digest":
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST)
else:
raise ValueError("Unsupported auth_mode %s" % request.auth_mode)
curl.setopt(pycurl.USERPWD, native_str(userpwd))
gen_log.debug("%s %s (username: %r)", request.method, request.url,
request.auth_username)
else:
curl.unsetopt(pycurl.USERPWD)
gen_log.debug("%s %s", request.method, request.url)
if request.client_cert is not None:
curl.setopt(pycurl.SSLCERT, request.client_cert)
if request.client_key is not None:
curl.setopt(pycurl.SSLKEY, request.client_key)
if threading.activeCount() > 1:
# libcurl/pycurl is not thread-safe by default. When multiple threads
# are used, signals should be disabled. This has the side effect
# of disabling DNS timeouts in some environments (when libcurl is
#postfields = ".%2Fsling%3AresourceType=cq%2Freplication%2Fcomponents%2Fagent&.%2Fjcr%3AlastModified=&.%2Fjcr%3AlastModifiedBy=&_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=Default%20Agent&.%2Fjcr%3Adescription=Agent%20that%20replicates%20to%20the%20default%20publish%20instance.&.%2Fenabled=true&.%2Fenabled%40Delete=&.%2FserializationType=durbo&.%2FretryDelay=60000&.%2FuserId=&.%2FlogLevel=info&.%2FreverseReplication%40Delete=&.%2FtransportUri=http%3A%2F%2Fpublish%3A4503%2Fbin%2Freceive%3Fsling%3AauthRequestLogin%3D1&.%2FtransportUser=admin&.%2FtransportPassword=admin&.%2FtransportNTLMDomain=&.%2FtransportNTLMHost=&.%2Fssl=&.%2FprotocolHTTPExpired%40Delete=&.%2FproxyHost=&.%2FproxyPort=&.%2FproxyUser=&.%2FproxyPassword=&.%2FproxyNTLMDomain=&.%2FproxyNTLMHost=&.%2FprotocolInterface=&.%2FprotocolHTTPMethod=&.%2FprotocolHTTPHeaders%40Delete=&.%2FprotocolHTTPConnectionClose%40Delete=true&.%2FprotocolConnectTimeout=&.%2FprotocolSocketTimeout=&.%2FprotocolVersion=&.%2FtriggerSpecific%40Delete=&.%2FtriggerModified%40Delete=&.%2FtriggerDistribute%40Delete=&.%2FtriggerOnOffTime%40Delete=&.%2FtriggerReceive%40Delete=&.%2FnoStatusUpdate%40Delete=&.%2FnoVersioning%40Delete=&.%2FqueueBatchMode%40Delete=&.%2FqueueBatchWaitTime=&.%2FqueueBatchMaxSize="
post_data = {"./sling:resourceType":"cq/replication/components/agent","./jcr:lastModified":"","./jcr:lastModifiedBy":"","_charset_":"utf-8",":status":"browser","./jcr:title":"Default Agent","./jcr:description":"Agent that replicates to the default publish instance.","./enabled":"true","./enabled@Delete":"","./serializationType":"durbo","./retryDelay":"60000","./userId":"","./logLevel":"info","./reverseReplication@Delete":"","./transportUri":"http://publish:4503/bin/receive?sling:authRequestLogin=1","./transportUser":"admin","./transportPassword":"admin","./transportNTLMDomain":"","./transportNTLMHost":"","./ssl":"","./protocolHTTPExpired@Delete":"","./proxyHost":"","./proxyPort":"","./proxyUser":"","./proxyPassword":"","./proxyNTLMDomain":"","./proxyNTLMHost":"","./protocolInterface":"","./protocolHTTPMethod":"","./protocolHTTPHeaders@Delete":"","./protocolHTTPConnectionClose@Delete":"true","./protocolConnectTimeout":"","./protocolSocketTimeout":"","./protocolVersion":"","./triggerSpecific@Delete":"","./triggerModified@Delete":"","./triggerDistribute@Delete":"","./triggerOnOffTime@Delete":"","./triggerReceive@Delete":"","./noStatusUpdate@Delete":"","./noVersioning@Delete":"","./queueBatchMode@Delete":"","./queueBatchWaitTime":"","./queueBatchMaxSize":""}
# Form data must be provided already urlencoded.
postfields = urlencode(post_data)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded
# and data to send in request body.
c.setopt(c.POSTFIELDS, postfields)
c.perform()
print "Checking Agent"
#Print Publisher status
c = pycurl.Curl()
c.setopt(c.URL, baseUrl + "/etc/replication/agents.author/publish/jcr:content.json")
c.setopt(pycurl.USERPWD, password)
c.perform()
packageList = "packagelist.txt"
current_dir = os.getcwd()
print "Current directory " + current_dir
if os.path.isfile(packageList):
with open(packageList) as fp:
for package in fp:
print "Package is: @\"" + current_dir + "/packages/" + package + "\""
c1 = pycurl.Curl()
c1.setopt(c1.URL, baseUrl + "/crx/packmgr/service.jsp")
c1.setopt(c1.POST, 1)
c1.setopt(pycurl.USERPWD, password)
file_name = current_dir + "/packages/" + package
#print file_name
c1.setopt(c1.HTTPPOST, [('file', (c1.FORM_FILE, file_name)), ('force', 'true'), ('install', 'true')])
def ioctl(cmd):
if cmd == curl.IOCMD_RESTARTREAD:
request_buffer.seek(0)
curl.setopt(pycurl.IOCTLFUNCTION, ioctl)
curl.setopt(pycurl.POSTFIELDSIZE, len(request.body))
else:
curl.setopt(pycurl.INFILESIZE, len(request.body))
if request.auth_username is not None:
userpwd = "%s:%s" % (request.auth_username, request.auth_password or '')
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
curl.setopt(pycurl.USERPWD, utf8(userpwd))
logging.debug("%s %s (username: %r)", request.method, request.url,
request.auth_username)
else:
curl.unsetopt(pycurl.USERPWD)
logging.debug("%s %s", request.method, request.url)
if request.client_cert is not None:
curl.setopt(pycurl.SSLCERT, request.client_cert)
if request.client_key is not None:
curl.setopt(pycurl.SSLKEY, request.client_key)
if threading.activeCount() > 1:
# libcurl/pycurl is not thread-safe by default. When multiple threads
# are used, signals should be disabled. This has the side effect
# of disabling DNS timeouts in some environments (when libcurl is
# not linked against ares), so we don't do it when there is only one
# thread. Applications that use many short-lived threads may need
# to set NOSIGNAL manually in a prepare_curl_callback since
# there may not be any other threads running at the time we call
def ioctl(cmd):
if cmd == curl.IOCMD_RESTARTREAD:
request_buffer.seek(0)
curl.setopt(pycurl.IOCTLFUNCTION, ioctl)
curl.setopt(pycurl.POSTFIELDSIZE, len(request.body))
else:
curl.setopt(pycurl.INFILESIZE, len(request.body))
if request.auth_username is not None:
userpwd = "%s:%s" % (request.auth_username, request.auth_password or '')
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
curl.setopt(pycurl.USERPWD, utf8(userpwd))
logging.debug("%s %s (username: %r)", request.method, request.url,
request.auth_username)
else:
curl.unsetopt(pycurl.USERPWD)
logging.debug("%s %s", request.method, request.url)
if request.client_cert is not None:
curl.setopt(pycurl.SSLCERT, request.client_cert)
if request.client_key is not None:
curl.setopt(pycurl.SSLKEY, request.client_key)
if threading.activeCount() > 1:
# libcurl/pycurl is not thread-safe by default. When multiple threads
# are used, signals should be disabled. This has the side effect
# of disabling DNS timeouts in some environments (when libcurl is
# not linked against ares), so we don't do it when there is only one
# thread. Applications that use many short-lived threads may need
# to set NOSIGNAL manually in a prepare_curl_callback since
# there may not be any other threads running at the time we call
if header:
curl.setopt(pycurl.HTTPHEADER, [str(k) + ':' + str(v) for k, v in header.items()])
if post:
curl.setopt(pycurl.POST, 1)
if params:
if post:
curl.setopt(pycurl.POSTFIELDS, urllib.urlencode(params))
else:
url = "?".join((url, urllib.urlencode(params)))
curl.setopt(pycurl.URL, str(url))
if username and password:
curl.setopt(pycurl.USERPWD, "%s:%s" % (str(username), str(password)))
curl.setopt(pycurl.FOLLOWLOCATION, 1)
curl.setopt(pycurl.MAXREDIRS, 5)
curl.setopt(pycurl.TIMEOUT, 15)
curl.setopt(pycurl.CONNECTTIMEOUT, 8)
curl.setopt(pycurl.HTTP_VERSION, pycurl.CURL_HTTP_VERSION_1_0)
content = StringIO.StringIO()
hdr = StringIO.StringIO()
curl.setopt(pycurl.WRITEFUNCTION, content.write)
curl.setopt(pycurl.HEADERFUNCTION, hdr.write)
print curl, url, header
try:
curl.perform()
except pycurl.error, e: