Exemplo n.º 1
0
 def test_post_multipart(self):
     alphabet = "abcdefghijklmnopqrstuvwxyz"
     # generate file contents for a large post
     contents = "".join([c * 65536 for c in alphabet])
     
     # encode as multipart form data
     files=[('file', 'file.txt', contents)]
     content_type, body = encode_multipart_formdata(files)
     
     # post file
     if self.scheme == 'https':
         c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
     else:
         c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
     c.putrequest('POST', '/post_multipart')
     c.putheader('Content-Type', content_type)
     c.putheader('Content-Length', str(len(body)))
     c.endheaders()
     c.send(body)
     
     response = c.getresponse()
     self.body = response.fp.read()
     self.status = str(response.status)
     self.assertStatus(200)
     self.assertBody(", ".join(["%s * 65536" % c for c in alphabet]))
Exemplo n.º 2
0
    def Get_Domain(exchangeserver):
        target_name = None
        try:

            HTTPREQ = HTTPSConnection(exchangeserver, context=ssl._create_unverified_context())
            HTTPREQ.putrequest("GET", "/autodiscover/autodiscover.xml")
            HTTPREQ.putheader("Content-length", "%d" % 0)
            HTTPREQ.putheader("Connection", "Keep-Alive")
            # #HTTPREQ.putheader("User-Agent", 'Python-urllib/2.6')
            # #if user == "":
            # #    user = raw_input("[+][devalias.net][NTLM Authentication] Enter username (DOMAIN\username): ")
            # #if password == "":
            # #    password = raw_input("[+][devalias.net][NTLM Authentication] Enter password: "******"xxx").decode('ascii')
            auth = u'%s %s' % ('NTLM', negotiate_message)
            HTTPREQ.putheader("Authorization", auth)
            HTTPREQ.endheaders()
            try:
                resp = HTTPREQ.getresponse()
                challenge = resp.msg.get('WWW-Authenticate').split(' ')[1]
                target_name = DOMAIN_utils.parse_NTLM_CHALLENGE_MESSAGE(challenge)
            except HTTPError as e:
                return targetname
            except URLError as e:
                return target_name
            except HTTPException as e:
                return target_name
            except Exception as e:
                #print('Reason4: ', e)
                return target_name
        except Exception as e:
            return target_name
        else:
            return target_name
Exemplo n.º 3
0
 def wrap(request, *args, **kwargs):
     if 'token' not in request.GET and 'token' not in request.session:
         # no token at all, request one-time-token
         # next: where to redirect
         # scope: what service you want to get access to
         base_url='https://www.google.com/accounts/AuthSubRequest'
         scope='https://docs.google.com/feeds/%20https://docs.googleusercontent.com/'
         
         urlbase = Site.objects.get_current().domain
         next_url='http://%s%s' %  (urlbase, request.get_full_path())
         session_val='1'
         target_url="%s?next=%s&scope=%s&session=%s" % (base_url, next_url, scope, session_val)
         return HttpResponseRedirect(target_url)
     elif 'token' not in request.session and 'token' in request.GET:
         # request session token using one-time-token
         conn = HTTPSConnection("www.google.com")
         conn.putrequest('GET', '/accounts/AuthSubSessionToken')
         conn.putheader('Authorization', 'AuthSub token="%s"' % request.GET['token'])
         conn.endheaders()
         conn.send(' ')
         r = conn.getresponse()
         if str(r.status) == '200':
             token = r.read()
             token = token.split('=')[1]
             token = token.replace('', '')
             request.session['token'] = token
     return f(request, *args, **kwargs)
Exemplo n.º 4
0
 def wrap(request, *args, **kwargs):
     if 'token' not in request.GET and 'token' not in request.session:
         # no token at all, request one-time-token
         # next: where to redirect
         # scope: what service you want to get access to
         base_url='https://www.google.com/accounts/AuthSubRequest'
         scope='https://docs.google.com/feeds/%20https://docs.googleusercontent.com/'
         next_url='http://ilsgateway.com%s' %  request.get_full_path()
         session_val='1'
         target_url="%s?next=%s&scope=%s&session=%s" % (base_url, next_url, scope, session_val)
         return HttpResponseRedirect(target_url)
     elif 'token' not in request.session and 'token' in request.GET:
         # request session token using one-time-token
         conn = HTTPSConnection("www.google.com")
         conn.putrequest('GET', '/accounts/AuthSubSessionToken')
         conn.putheader('Authorization', 'AuthSub token="%s"' % request.GET['token'])
         conn.endheaders()
         conn.send(' ')
         r = conn.getresponse()
         if str(r.status) == '200':
             token = r.read()
             token = token.split('=')[1]
             token = token.replace('', '')
             request.session['token'] = token
     return f(request, *args, **kwargs)
Exemplo n.º 5
0
def getsid():
	"""Connetti al cpanel per ottenere l'id di sessione"""
	# "Basic" authentication encodes userid:password in base64. Note
	# that base64.encodestring adds some extra newlines/carriage-returns
	# to the end of the result. string.strip is a simple way to remove
	# these characters.
        print " - Fetching session id from CPanel for user %s" % userid
	global sid,psi
	auth = 'Basic ' + strip(encodestring(userid + ':' + passwd))
	conn = HTTPSConnection('cp.tophost.it')
	conn.putrequest('GET', '/dnsjump.php')
	conn.putheader('Authorization', auth )
	conn.putheader(ua[0],ua[1])
	conn.endheaders()
	r=conn.getresponse()
	if r.status!=200:
		print 'Connessione fallita: ',r.status,r.reason
		exit(1)
	psi=r.getheader('Set-Cookie').replace('PHPSESSID=','').replace('; path=/','')
	page=r.read()
	bound='<input type="hidden" name="sid" value="'
	s=page.find(bound); e=page.find('">',s)
	sid=page[s+len(bound):e]
	conn.close()
	return sid,psi
Exemplo n.º 6
0
Arquivo: situp.py Projeto: xpqz/situp
 def request(server, method, url, auth=False):
     conn = None
     if server.startswith('https://'):
         conn = HTTPSConnection(server.strip('https://'))
     else:
         conn = HTTPConnection(server.strip('http://'))
     conn.putrequest(method, url)
     if auth:
         conn.putheader("Authorization", "Basic %s" % auth)
     conn.putheader("User-Agent", "situp-%s" % __version__)
     conn.endheaders()
     response = conn.getresponse()
     conn.close()
     return response
Exemplo n.º 7
0
 def request(server, method, url, auth=False):
     conn = None
     if server.startswith('https://'):
         conn = HTTPSConnection(server.strip('https://'))
     else:
         conn = HTTPConnection(server.strip('http://'))
     conn.putrequest(method, url)
     if auth:
         conn.putheader("Authorization", "Basic %s" % auth)
     conn.putheader("User-Agent", "situp-%s" % __version__)
     conn.endheaders()
     response = conn.getresponse()
     conn.close()
     return response
Exemplo n.º 8
0
    def fetch_file(self, server, path):
        "Fetch file using httplib module."

        print("downloading https://%s%s" % (server, path))

        req = HTTPSConnection(server)
        req.putrequest('GET', path)
        req.putheader('Host', server)
        req.putheader('Accept', 'text/svg')
        req.endheaders()
        r1 = req.getresponse()
        data = r1.read().decode('utf-8')
        req.close()

        return data
Exemplo n.º 9
0
    def fetch_file(self, server, path):
        "Fetch file using httplib module."

        print("downloading https://%s%s" % (server, path))

        req = HTTPSConnection(server)
        req.putrequest('GET', path)
        req.putheader('Host', server)
        req.putheader('Accept', 'text/svg')
        req.endheaders()
        r1 = req.getresponse()
        data = r1.read().decode('utf-8')
        req.close()

        return data
Exemplo n.º 10
0
 def test_malformed_header(self):
     if self.scheme == 'https':
         c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
     else:
         c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
     c.putrequest('GET', '/')
     c.putheader('Content-Type', 'text/plain')
     # See http://www.cherrypy.org/ticket/941 
     c._output('Re, 1.2.3.4#015#012')
     c.endheaders()
     
     response = c.getresponse()
     self.status = str(response.status)
     self.assertStatus(400)
     self.body = response.fp.read()
     self.assertBody("Illegal header line.")
Exemplo n.º 11
0
 def request(index):
     if self.scheme == 'https':
         c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
     else:
         c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
     for i in range(request_count):
         c.putrequest('GET', '/')
         for k, v in cookies:
             c.putheader(k, v)
         c.endheaders()
         response = c.getresponse()
         body = response.read()
         if response.status != 200 or not body.isdigit():
             errors.append((response.status, body))
         else:
             data_dict[index] = max(data_dict[index], int(body))
Exemplo n.º 12
0
	def request_feed(self, user, passwd, label=''):
		DOMAIN = 'mail.google.com'
		ATOM_FEED = '/mail/feed/atom'

		if len(label) > 0 and label[0] != '/':
			label = '/' + label

		b64 = base64.encodestring(user + ':' + passwd)
		connection = HTTPSConnection(DOMAIN)
		connection.putrequest('GET', ATOM_FEED + label)
		connection.putheader('Accept', 'application/atom+xml')
		connection.putheader('Authorization', 'Basic %s' % b64)
		connection.endheaders()
		response = connection.getresponse()
		data = response.read()
		response.close()
		return self.parse_xml(user, data)
Exemplo n.º 13
0
def request(method, url, data, headers, callback=None):
    url = urlparse(url)

    # Connect
    if url.scheme == 'https':
        request = HTTPSConnection(url.netloc)
    else:
        request = HTTPConnection(url.netloc)

    request.connect()

    # Initiate request
    request.putrequest(method, url.path)

    encoded_data = multipart.encode(data)
    encoded_data_headers = encoded_data.get_headers()
    all_headers = chain(
        encoded_data_headers.iteritems(),
        headers.iteritems()
    )

    # Send headers
    for name, value in all_headers:
        request.putheader(name, value)

    request.endheaders()

    # Send body
    bytes_sent = 0
    bytes_total = int(encoded_data_headers['Content-Length'])
    for chunk in encoded_data:
        request.send(chunk)
        bytes_sent += len(chunk)

        if callable(callback):
            callback(bytes_sent, bytes_total)

    # TODO: Wrap the response in a container to allow chunked reading.
    response = request.getresponse()

    response_status = response.status
    response_data = response.read()

    request.close()

    return response_status, response_data
Exemplo n.º 14
0
def test_page1(request):
    """
    Simple example - list google docs documents
    """
    if TOKEN_VAR in request.session:
        con = HTTPSConnection("www.google.com")
        con.putrequest('GET', '/m8/feeds/contacts/[email protected]/full')
        con.putheader('Authorization', 'AuthSub token="%s"' % request.session[TOKEN_VAR])
        con.endheaders()
        con.send('')
        r2 = con.getresponse()
        dane = r2.read()
        soup = BeautifulStoneSoup(dane)
        dane = soup.prettify()
        return render(request, 'pin/a.html', {'dane': dane})
    else:
        return render(request, 'pin/a.html', {'dane': 'bad bad'})
Exemplo n.º 15
0
def test_page1(request):
    """
    Simple example - list google docs documents
    """
    if TOKEN_VAR in request.session:
        con = HTTPSConnection("www.google.com")
        con.putrequest('GET', '/m8/feeds/contacts/[email protected]/full')
        con.putheader('Authorization',
                      'AuthSub token="%s"' % request.session[TOKEN_VAR])
        con.endheaders()
        con.send('')
        r2 = con.getresponse()
        dane = r2.read()
        soup = BeautifulStoneSoup(dane)
        dane = soup.prettify()
        return render(request, 'pin/a.html', {'dane': dane})
    else:
        return render(request, 'pin/a.html', {'dane': 'bad bad'})
Exemplo n.º 16
0
 def wrap(request, *args, **kwargs):
     if TOKEN_IN_GET not in request.GET and TOKEN_VAR not in request.session:
         # no token at all, request one-time-token
         # next: where to redirect
         # scope: what service you want to get access to
         return HttpResponseRedirect("https://www.google.com/accounts/AuthSubRequest?next=http://127.0.0.1:8000/pin/test_page&scope=https://www.google.com/m8/feeds&session=1")
     elif TOKEN_VAR not in request.session and TOKEN_IN_GET in request.GET:
         # request session token using one-time-token
         conn = HTTPSConnection("www.google.com")
         conn.putrequest('GET', '/accounts/AuthSubSessionToken')
         conn.putheader('Authorization', 'AuthSub token="%s"' % request.GET[TOKEN_IN_GET])
         conn.endheaders()
         conn.send(' ')
         r = conn.getresponse()
         if str(r.status) == '200':
             token = r.read()
             token = token.split('=')[1]
             token = token.replace('', '')
             request.session[TOKEN_VAR] = token
     return f(request, *args, **kwargs)
Exemplo n.º 17
0
    def create_token(params):
        """
        Contact the specified keystone server to return the token
        """
        if 'username' in params and 'password' in params and 'auth_url' in params and 'tenant' in params:
            try:
                keystone_uri = params['auth_url']
                uri = uriparse(keystone_uri)
                server = uri[1].split(":")[0]
                port = int(uri[1].split(":")[1])

                conn = HTTPSConnection(server, port)
                conn.putrequest('POST', "/v2.0/tokens")
                conn.putheader('Accept', 'application/json')
                conn.putheader('Content-Type', 'application/json')
                conn.putheader('Connection', 'close')

                body = ('{"auth":{"passwordCredentials":{"username": "******","password": "******"},"tenantName": "' +
                        params['tenant'] + '"}}')

                conn.putheader('Content-Length', len(body))
                conn.endheaders(body)

                resp = conn.getresponse()

                # format: -> "{\"access\": {\"token\": {\"issued_at\":
                # \"2014-12-29T17:10:49.609894\", \"expires\":
                # \"2014-12-30T17:10:49Z\", \"id\":
                # \"c861ab413e844d12a61d09b23dc4fb9c\"}, \"serviceCatalog\":
                # [], \"user\": {\"username\":
                # \"/DC=es/DC=irisgrid/O=upv/CN=miguel-caballer\",
                # \"roles_links\": [], \"id\":
                # \"475ce4978fb042e49ce0391de9bab49b\", \"roles\": [],
                # \"name\": \"/DC=es/DC=irisgrid/O=upv/CN=miguel-caballer\"},
                # \"metadata\": {\"is_admin\": 0, \"roles\": []}}}"
                output = json.loads(resp.read())
                token_id = output['access']['token']['id']

                if conn.cert_file and os.path.isfile(conn.cert_file):
                    os.unlink(conn.cert_file)

                return token_id
            except:
                return None
        else:
            raise Exception(
                "Incorrect auth data, auth_url, username, password and tenant must be specified"
            )
Exemplo n.º 18
0
def getsid():
    """Connetti al cpanel per ottenere l'id di sessione"""
    # "Basic" authentication encodes userid:password in base64. Note
    # that base64.encodestring adds some extra newlines/carriage-returns
    # to the end of the result. string.strip is a simple way to remove
    # these characters.
    global sid, psi
    auth = 'Basic ' + strip(encodestring(userid + ':' + passwd))
    conn = HTTPSConnection('cp.tophost.it')
    conn.putrequest('GET', '/dnsjump.php')
    conn.putheader('Authorization', auth)
    conn.putheader(ua[0], ua[1])
    conn.endheaders()
    r = conn.getresponse()
    psi = r.getheader('Set-Cookie').replace('PHPSESSID=',
                                            '').replace('; path=/', '')
    page = r.read()
    bound = '<input type="hidden" name="sid" value="'
    s = page.find(bound)
    e = page.find('">', s)
    sid = page[s + len(bound):e]
    conn.close()
    return sid, psi
Exemplo n.º 19
0
 def wrap(request, *args, **kwargs):
     if TOKEN_IN_GET not in request.GET and TOKEN_VAR not in request.session:
         # no token at all, request one-time-token
         # next: where to redirect
         # scope: what service you want to get access to
         return HttpResponseRedirect(
             "https://www.google.com/accounts/AuthSubRequest?next=http://127.0.0.1:8000/pin/test_page&scope=https://www.google.com/m8/feeds&session=1"
         )
     elif TOKEN_VAR not in request.session and TOKEN_IN_GET in request.GET:
         # request session token using one-time-token
         conn = HTTPSConnection("www.google.com")
         conn.putrequest('GET', '/accounts/AuthSubSessionToken')
         conn.putheader('Authorization',
                        'AuthSub token="%s"' % request.GET[TOKEN_IN_GET])
         conn.endheaders()
         conn.send(' ')
         r = conn.getresponse()
         if str(r.status) == '200':
             token = r.read()
             token = token.split('=')[1]
             token = token.replace('', '')
             request.session[TOKEN_VAR] = token
     return f(request, *args, **kwargs)
Exemplo n.º 20
0
def post_multipart(host, selector, fields, files):
    content_type, body = encode_multipart_formdata(fields, files)
    h = HTTPSConnection(host)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body.encode('utf-8'))))
    h.putheader('User-Agent', 'sms-python')
    h.endheaders()
    h.send(body.encode('utf-8'))
    resp = h.getresponse()
    return resp.status, resp. reason, resp.read().decode()
Exemplo n.º 21
0
def post_multipart(host, selector, fields, files):
    content_type, body = encode_multipart_formdata(fields, files)
    h = HTTPSConnection(host)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body.encode('utf-8'))))
    h.putheader('User-Agent', 'sms-python')
    h.endheaders()
    h.send(body.encode('utf-8'))
    resp = h.getresponse()
    return resp.status, resp.reason, resp.read().decode()
Exemplo n.º 22
0
    def run(self):
        self.started = time.time()
        timeout = self.hostControlBackend._hostRpcTimeout
        if timeout < 0:
            timeout = 0

        try:
            query = toJson({
                'id': 1,
                'method': self.method,
                'params': self.params
            }).encode('utf-8')

            connection = HTTPSConnection(host=self.address,
                                         port=self.hostPort,
                                         timeout=timeout)
            with closingConnection(connection) as connection:
                non_blocking_connect_https(connection, timeout)
                connection.putrequest('POST', '/opsiclientd')
                connection.putheader('User-Agent', self._USER_AGENT)
                connection.putheader('content-type', 'application/json')
                connection.putheader('content-length', str(len(query)))
                auth = u'{0}:{1}'.format(self.username, self.password)
                connection.putheader(
                    'Authorization',
                    'Basic ' + base64.b64encode(auth.encode('latin-1')))
                connection.endheaders()
                connection.send(query)

                response = connection.getresponse()
                response = response.read()
                response = fromJson(unicode(response, 'utf-8'))

                if response and isinstance(response, dict):
                    self.error = response.get('error')
                    self.result = response.get('result')
                else:
                    self.error = u"Bad response from client: %s" % forceUnicode(
                        response)
        except Exception as e:
            self.error = forceUnicode(e)
        finally:
            self.ended = time.time()
Exemplo n.º 23
0
    def zopeRequest(self, method, headers={}, body=''):
        """Send a request back to Zope"""
        try:
            if self.ssl:
                h = HTTPSConnection(self.host)
            else:
                h = HTTPConnection(self.host)

            h.putrequest(method, self.path)
            h.putheader('User-Agent', 'Zope External Editor/%s' % __version__)
            h.putheader('Connection', 'close')

            for header, value in headers.items():
                h.putheader(header, value)

            h.putheader("Content-Length", str(len(body)))

            if self.metadata.get('auth','').startswith('Basic'):
                h.putheader("Authorization", self.metadata['auth'])

            if self.metadata.get('cookie'):
                h.putheader("Cookie", self.metadata['cookie'])

            h.endheaders()
            h.send(body)
            return h.getresponse()
        except:
            # On error return a null response with error info
            class NullResponse:                
                def getheader(self, n, d=None):
                    return d
                    
                def read(self): 
                    return '(No Response From Server)'
            
            response = NullResponse()
            response.reason = sys.exc_info()[1]
            
            try:
                response.status, response.reason = response.reason
            except ValueError:
                response.status = 0
            
            if response.reason == 'EOF occurred in violation of protocol':
                # Ignore this protocol error as a workaround for
                # broken ssl server implementations
                response.status = 200
                
            return response
Exemplo n.º 24
0
class THttpPersist (TTransport.TTransportBase):
	"""Http (keep-alive) implementation of TTransport base."""

	def __init__ (self, uri_or_host, port=None, path=None):
		"""THttpPersist supports two different types constructor parameters.
		THttpPersist(host, port, path) - deprecated
		THttpPersist(uri)
		Only the second supports https.
		"""
		if port is not None:
			warn (
				"Please use the THttpPersist('http://host:port/path') syntax",
				DeprecationWarning,
				stacklevel=2)
			self.host = uri_or_host
			self.port = port
			assert path
			self.path = path
			self.scheme = 'http'
		else:
			parsed = urlparse(uri_or_host)
			self.scheme = parsed.scheme
			assert self.scheme in ('http', 'https')
			if self.scheme == 'http':
				self.port = parsed.port or HTTP_PORT
			elif self.scheme == 'https':
				self.port = parsed.port or HTTPS_PORT
			self.host = parsed.hostname
			self.path = parsed.path
			if parsed.query:
				self.path += '?%s' % parsed.query
		self._bufr = StringIO()
		self._bufw = StringIO()
		self._conn = None
		self._timeout = None
		self._headers = None
	def open (self):
		if self._conn is not None:
			self.close()
		if self.scheme == 'http':
			self._conn = HTTPConnection (
			  self.host, self.port, timeout=self._timeout)
		else:
			self._conn = HTTPSConnection (
			  self.host, self.port, timeout=self._timeout)

	def close (self):
		if self._conn is None:
			return
		self._conn.close ()
		self._conn = None

	def isOpen (self):
		return self._conn is not None

	def setTimeout (self, ms):
		if ms is None:
			self._timeout = None
		else:
			self._timeout = ms / 1000.0
		# TODO: close connection to apply timeout

	def setCustomHeaders (self, headers):
		self._headers = headers

	def read (self, sz):
		return self._bufr.read (sz)

	def write (self, buf):
		self._bufw.write (buf)

	def flush (self):
		if not self.isOpen ():
			self.open ()

		# Pull data out of buffer
		data = self._bufw.getvalue ()
		self._bufw = StringIO ()

		# HTTP request
		self._conn.putrequest ('POST', self.path)

		# Write headers
		self._conn.putheader ('Host', self.host)
		self._conn.putheader ('Content-Type', 'application/x-thrift')
		self._conn.putheader ('Content-Length', str (len (data)))

		if not self._headers or 'User-Agent' not in self._headers:
			user_agent = 'Python/THttpClient'
			script = basename (argv[0])
			if script:
				user_agent = '%s (%s)' % (user_agent, quote(script))
			self._conn.putheader ('User-Agent', user_agent)

		if self._headers:
			for key, val in self._headers.iteritems ():
				self._conn.putheader (key, val)

		self._conn.endheaders ()

		# Write payload
		self._conn.send (data)

		# Get reply to flush the request
		response = self._conn.getresponse ()
		
		self.code = response.status
		self.message = response.reason
		self.headers = response.msg
		self._bufr = StringIO (response.read())
Exemplo n.º 25
0
    disk_file = ova_file.extractfile(disk_entry)
    size = disk_entry.size

    # Send the request head. Note the following:
    #
    # - We must send the Authorzation header with the signed ticket received
    #   from the transfer service.
    #
    # - the server requires Content-Range header even when sending the
    #   entire file.
    #
    # - the server requires also Content-Length.
    #

    proxy_connection.putrequest("PUT", proxy_url.path)
    proxy_connection.putheader('Authorization', transfer.signed_ticket)
    proxy_connection.putheader('Content-Range',
                               "bytes %d-%d/%d" % (0, size - 1, size))
    proxy_connection.putheader('Content-Length', "%d" % (size,))
    proxy_connection.endheaders()

    # Send the request body. Note the following:
    #
    # - we must send the number of bytes we promised in the Content-Range
    #   header.
    #
    # - we must extend the session, otherwise it will expire and the upload
    #   will fail.

    last_extend = time.time()
Exemplo n.º 26
0
class HessianProxy(object):

    def __init__(self, service_uri, credentials=None, key_file=None, cert_file=None, timeout=10, buffer_size=65535, error_factory=lambda x: x, overload=False):
        self._headers = list()
        self._headers.append(('User-Agent', 'mustaine/' + __version__,))
        self._headers.append(('Content-Type', 'application/x-hessian',))

        if sys.version_info < (2,6):
            warn('HessianProxy timeout not enforceable before Python 2.6', RuntimeWarning, stacklevel=2)
            timeout = {}
        else:
            timeout = {'timeout': timeout}

        self._uri = urlparse(service_uri)
        if self._uri.scheme == 'http':
            self._client = HTTPConnection(self._uri.hostname,
                                          self._uri.port or 80,
                                          strict=True,
                                          **timeout)
        elif self._uri.scheme == 'https':
            self._client = HTTPSConnection(self._uri.hostname,
                                           self._uri.port or 443,
                                           key_file=key_file,
                                           cert_file=cert_file,
                                           strict=True,
                                           **timeout)
        else:
            raise NotImplementedError("HessianProxy only supports http:// and https:// URIs")

        # autofill credentials if they were passed via url instead of kwargs
        if (self._uri.username and self._uri.password) and not credentials:
            credentials = (self._uri.username, self._uri.password)

        if credentials:
            auth = 'Basic ' + base64.b64encode(':'.join(credentials))
            self._headers.append(('Authorization', auth))

        self._buffer_size = buffer_size
        self._error_factory = error_factory
        self._overload = overload
        self._parser = Parser()

    class __RemoteMethod(object):
        # dark magic for autoloading methods
        def __init__(self, caller, method):
            self.__caller = caller
            self.__method = method
        def __call__(self, *args):
            return self.__caller(self.__method, args)

    def __getattr__(self, method):
        return self.__RemoteMethod(self, method)

    def __repr__(self):
        return "<mustaine.client.HessianProxy(\"%s\")>" % (self._uri.geturl(),)

    def __str__(self):
        return self.__repr__()

    def __call__(self, method, args):
        try:
            self._client.putrequest('POST', self._uri.path)
            for header in self._headers:
                self._client.putheader(*header)

            request = encode_object(Call(method, args, overload=self._overload))
            self._client.putheader("Content-Length", str(len(request)))
            self._client.endheaders()
            self._client.send(str(request))

            response = self._client.getresponse()
            if response.status != 200:
                raise ProtocolError(self._uri.geturl(), response.status, response.reason)

            length = response.getheader('Content-Length', -1)
            if length == '0':
                raise ProtocolError(self._uri.geturl(), 'FATAL:', 'Server sent zero-length response')

            reply = self._parser.parse_stream(BufferedReader(response, buffer_size=self._buffer_size))
            self._client.close()

            if isinstance(reply.value, Fault):
                raise self._error_factory(reply.value)
            else:
                return reply.value
        except:
            self._client.close()
            raise
Exemplo n.º 27
0
    disk_file = ova_file.extractfile(disk_entry)
    image_size = disk_entry.size

    # Send the request head. Note the following:
    #
    # - We must send the Authorzation header with the signed ticket received
    #   from the transfer service.
    #
    # - the server requires Content-Range header even when sending the
    #   entire file.
    #
    # - the server requires also Content-Length.
    #

    proxy_connection.putrequest("PUT", proxy_url.path)
    proxy_connection.putheader('Authorization', transfer.signed_ticket)
    content_range = "bytes %d-%d/%d" % (0, image_size - 1, image_size)
    proxy_connection.putheader('Content-Range', content_range)
    proxy_connection.putheader('Content-Length', "%d" % (image_size, ))
    proxy_connection.endheaders()

    # Send the request body. Note the following:
    #
    # - we must send the number of bytes we promised in the Content-Range
    #   header.
    #
    # - we must extend the session, otherwise it will expire and the upload
    #   will fail.

    start = last_progress = time.time()
Exemplo n.º 28
0
    def zope_request(self, method, headers={}, body=''):
        """Send a request back to Zope"""
        try:
            if self.ssl:
                h = HTTPSConnection(self.host)
            else:
                h = HTTPConnection(self.host)

            h.putrequest(method, self.path)
            #h.putheader("Host", self.host)  # required by HTTP/1.1
            h.putheader('User-Agent', 'Zope External Editor/%s' % __version__)
            h.putheader('Connection', 'close')

            for header, value in headers.items():
                h.putheader(header, value)

            h.putheader("Content-Length", str(len(body)))

            if self.metadata.get('auth', '').startswith('Basic'):
                h.putheader("Authorization", self.metadata['auth'])

            if self.metadata.get('cookie'):
                h.putheader("Cookie", self.metadata['cookie'])

            h.endheaders()
            h.send(body)
            return h.getresponse()
        except:
            # On error return a null response with error info
            class NullResponse:
                def getheader(n, d):
                    return d

                def read():
                    return ''

            response = NullResponse()
            response.reason = sys.exc_info()[1]
            try:
                response.status, response.reason = response.reason
            except:
                response.status = 0
            return response
Exemplo n.º 29
0
    def handle_one_request(self):

        try:
            start_time = time.time()

            try:
                # noinspection PyAttributeOutsideInit
                self.command, self.path, self.request_version = \
                    self.rfile.readline().decode().strip().split(' ', 2)
            except ValueError:
                return

            target_netloc = self.server.proxy_target.netloc

            if '@' in target_netloc:
                # split authentication information from target description
                target_auth, target_netloc = target_netloc.split('@', 1)
            else:
                target_auth, target_netloc = None, target_netloc

            if ':' in target_netloc:
                # split host / port information from target description
                target_host, target_port = target_netloc.rsplit(':', 1)
            else:
                target_host, target_port = target_netloc, None  # port fallback to 80 / 443

            if self.server.proxy_target.scheme == 'https':
                client = HTTPSConnection(target_host, target_port)
            else:
                client = HTTPConnection(target_host, target_port)

            client.connect()
            client.putrequest(self.command, self.server.proxy_target.path + self.path, skip_host=True)

            request_host = ''
            request_content_length = 0
            request_line = self.rfile.readline()
            while request_line and b':' in request_line:

                # walk through the request header lines and pass them to the server connection
                key, value = request_line.split(b':', 1)
                unified_key = key.decode().lower()

                if unified_key == 'authorization':
                    # if there is an authorization in the request: ignore the given information
                    target_auth = None

                elif unified_key == 'content-length':
                    request_content_length = int(value.strip())

                elif unified_key == 'host':
                    request_host = value.strip()
                    # replace the requested host header with the wanted one
                    value = self.server.host_header if self.server.host_header else target_host

                client.putheader(key, value.strip())

                request_line = self.rfile.readline()

            if target_auth:
                client.putheader('Authorization', 'Basic %s' % b64encode(target_auth))

            client.endheaders()

            # pass the request body to the server connection
            for _ in range(request_content_length // 1024):
                client.send(self.rfile.read(1024))
            client.send(self.rfile.read(request_content_length % 1024))

            response = client.getresponse()

            self.send_response(response.status, response.reason)
            for key, value in response.getheaders():

                # walk through the response header lines and pass them to the client connection
                unified_key = key.lower()

                if unified_key == 'location':

                    # try to modify the location header to keep the browser requesting the proxy
                    redirect = list(urlparse(value))
                    if redirect[1]:
                        redirect[0], redirect[1] = 'http', request_host
                        logging.warning("REWRITE %s: %s -> %s", key, value, urlunparse(redirect))

                    self.send_header(key, urlunparse(redirect))

                elif unified_key not in ('keep-alive', 'connection'):
                    # its hard to support persistent connections properly because we open a
                    # new connection for every request, so disable it completely
                    self.send_header(key, value)

            self.end_headers()

            try:
                # pass the response body to the client connection
                chunk = True
                response_size = 0
                while chunk:
                    chunk = response.read(1024)
                    response_size += len(chunk)
                    self.wfile.write(chunk)

                self.wfile.flush()

            except socket.error:
                logging.debug("%s %s [connection reset, %.2fs]",
                              self.command, self.path,
                              time.time() - start_time)

            else:
                logging.info("%s %s [%s, %s, %.2fs]",
                             self.command, self.path,
                             response.status, human_size(response_size),
                             time.time() - start_time)

            finally:
                client.close()

        except KeyboardInterrupt:
            pass
Exemplo n.º 30
0
# Send the request head. Note the following:
#
# - For ovirt-engine < 4.2, we must send the 'Authorization' header with
#   the signed ticket received from the transfer service.
#   I.e. proxy_connection.putheader('Authorization', transfer.signed_ticket)
#
# - For ovirt-engine < 4.2, the server requires 'Content-Range' header
#   even when sending the entire file.
#   I.e. proxy_connection.putheader('Content-Range',
#            "bytes %d-%d/%d" % (0, image_size - 1, image_size))
#
# - the server requires also Content-Length.
#

proxy_connection.putrequest("PUT", destination_url.path)
proxy_connection.putheader('Content-Length', "%d" % (image_size, ))
proxy_connection.endheaders()

# Send the request body.

# Note that we must send the number of bytes we promised in the
# Content-Range header.

start = last_progress = time.time()

with open(image_path, "rb") as disk:
    pos = 0
    while pos < image_size:
        # Send the next chunk to the proxy.
        to_read = min(image_size - pos, BUF_SIZE)
        chunk = disk.read(to_read)
Exemplo n.º 31
0
    def zope_request(self, method, headers={}, body=''):
        """Send a request back to Zope"""
        try:
            if self.ssl:
                h = HTTPSConnection(self.host)
            else:
                h = HTTPConnection(self.host)

            h.putrequest(method, self.path)
            #h.putheader("Host", self.host)  # required by HTTP/1.1
            h.putheader('User-Agent', 'Zope External Editor/%s' % __version__)
            h.putheader('Connection', 'close')

            for header, value in headers.items():
                h.putheader(header, value)

            h.putheader("Content-Length", str(len(body)))

            if self.metadata.get('auth','').startswith('Basic'):
                h.putheader("Authorization", self.metadata['auth'])

            if self.metadata.get('cookie'):
                h.putheader("Cookie", self.metadata['cookie'])

            h.endheaders()
            h.send(body)
            return h.getresponse()
        except:
            # On error return a null response with error info
            class NullResponse:
                def getheader(n,d): return d
                def read(): return ''
            
            response = NullResponse()
            response.reason = sys.exc_info()[1]
            try:
                response.status, response.reason = response.reason
            except:
                response.status = 0
            return response
Exemplo n.º 32
0
def getter(host, url, ip=None, ssl=True, body='', force_post=False, headers={}):
	'''Make HTTP request that handles cookies in an acceptable manner
	Supports SSL and spoofed hostnames'''
	global prev_url, cookies, useragent
	ip = hostnames[host] if not ip and host in hostnames else ip
	print >> sys.stderr, "[getter] ", host, url, 'ip=%s' % ip, 'ssl=%s' % ssl
	origin = 'https://'+host if ssl else 'http://'+host


	# Assemble the request
	conn = HTTPSConnection(ip or host) if ssl else HTTPConnection(host)

	conn.putrequest('POST' if len(body)>0 or force_post else 'GET', url, skip_host=True)
	conn.putheader('Accept', '*/*')
	conn.putheader('Origin', origin)
	if prev_url != '': conn.putheader('Referer', prev_url)
	conn.putheader('User-Agent', useragent)
	if len(headers) > 0:
		for i in headers:
			conn.putheader(i, headers[i])
	if len(cookies) > 0: conn.putheader('Cookie', ''.join([i+'='+cookies[i]+'; ' for i in cookies]))
	if len(body) > 0 or force_post: conn.putheader('Content-Length', len(body))
	conn.putheader('Host', host)
	conn.endheaders(message_body=body if len(body) > 0 or force_post else None)

	prev_url = origin+url

	# Parse the response, determine new cookies and redirects
	resp = conn.getresponse()
	redirect = None
	for name,value in resp.getheaders():
		if name == 'set-cookie':
			# Parse cookies
			t_cookies = value.split(',')
			for i in t_cookies:
				try:
					temp = i.partition(';')[0].partition('=')
					if temp[2] == 'null' and temp[0].strip() in cookies:
						del cookies[temp[0].strip()]
					else:
						cookies[temp[0].strip()] = temp[2]
				except Exception:
					print "Broke on cookie-string: ", i
		if name == "location":
			# Set up redirect for when all headers are ready
			temp = value.partition('://')[2].partition('/')
			temp_host = temp[0]
			temp_url = '/'+temp[2]
			temp_ssl = "https://" in value
			redirect = True

	body = resp.read()
	if '<META name="GENERATOR" content="IBM WebSphere Studio">' in body[0:1000]:
		start = body.find('document.location.href="')+24
		end = body.find('"', start)
		temp_host = host
		temp_url = body[start:end]
		temp_ssl = True
		redirect = True



	if redirect: return getter(temp_host, temp_url, ssl=temp_ssl)
	return body
Exemplo n.º 33
0
class HessianProxy(object):

    def __init__(self, service_uri, credentials=None, key_file=None, cert_file=None, timeout=10, buffer_size=65535, error_factory=lambda x: x, overload=False):
        self._headers = list()
        self._headers.append(('User-Agent', 'mustaine/' + __version__,))
        self._headers.append(('Content-Type', 'application/x-hessian',))

        if sys.version_info < (2,6):
            warn('HessianProxy timeout not enforceable before Python 2.6', RuntimeWarning, stacklevel=2)
            timeout = {}
        else:
            timeout = {'timeout': timeout}

        self._uri = urlparse(service_uri)
        if self._uri.scheme == 'http':
            self._client = HTTPConnection(self._uri.hostname,
                                          self._uri.port or 80,
                                          strict=True,
                                          **timeout)
        elif self._uri.scheme == 'https':
            self._client = HTTPSConnection(self._uri.hostname,
                                           self._uri.port or 443,
                                           key_file=key_file,
                                           cert_file=cert_file,
                                           strict=True,
                                           **timeout)
        else:
            raise NotImplementedError("HessianProxy only supports http:// and https:// URIs")

        # autofill credentials if they were passed via url instead of kwargs
        if (self._uri.username and self._uri.password) and not credentials:
            credentials = (self._uri.username, self._uri.password)

        if credentials:
            auth = 'Basic ' + base64.b64encode(':'.join(credentials))
            self._headers.append(('Authorization', auth))

        self._buffer_size = buffer_size
        self._error_factory = error_factory
        self._overload = overload
        self._parser = Parser()

    class __RemoteMethod(object):
        # dark magic for autoloading methods
        def __init__(self, caller, method):
            self.__caller = caller
            self.__method = method
        def __call__(self, *args):
            return self.__caller(self.__method, args)

    def __getattr__(self, method):
        return self.__RemoteMethod(self, method)

    def __repr__(self):
        return "<mustaine.client.HessianProxy(\"%s\")>" % (self._uri.geturl(),)

    def __str__(self):
        return self.__repr__()

    def __call__(self, method, args):
        try:
            path = self._uri.path
            if self._uri.query:
                path += "?" + self._uri.query
                
            self._client.putrequest('POST', path)
            for header in self._headers:
                self._client.putheader(*header)

            request = encode_object(Call(method, args, overload=self._overload))
            self._client.putheader("Content-Length", str(len(request)))
            self._client.endheaders()
            self._client.send(str(request))

            response = self._client.getresponse()
            if response.status != 200:
                raise ProtocolError(self._uri.geturl(), response.status, response.reason)

            length = response.getheader('Content-Length', -1)
            if length == '0':
                raise ProtocolError(self._uri.geturl(), 'FATAL:', 'Server sent zero-length response')

            reply = self._parser.parse_stream(BufferedReader(response, buffer_size=self._buffer_size))
            self._client.close()

            if isinstance(reply.value, Fault):
                raise self._error_factory(reply.value)
            else:
                return reply.value
        except:
            self._client.close()
            raise