Exemplo n.º 1
0
def parse_response(f, conn, debug=0):
    """parse_response(file_obj)

    Read response from input file object, and parse it
    
    The parsed data is returned to the caller
    """
    p,u=xmlrpclib.getparser()
    if debug:
      s=StringIO.StringIO()
    i=0
    try:
      while 1:
        try:
#          print f.fp._line_consumed, f.fp._line_left
          response = f.read()
        except AssertionError:
          break
        if not response:
            break
        if debug:
          s.write(response)
        p.feed(response)
      if debug:
        err_msg(s.getvalue())
      p.close()
      return u.close()
    except:
      if debug:
        err_msg(s.getvalue())
      raise
Exemplo n.º 2
0
 def process_request(self, request):
     format_string = getattr(settings, 'FORMAT_STRING', 'format')
     format = request.GET.get(format_string, '')
     if format:
         request.format = format.lower() #could be "json", "xmlrpc", etc
     else:
         request.format = getattr(settings, 'DEFAULT_FORMAT', 'json')
     if request.format == 'xmlrpc':
         import xmlrpclib
         p, u = xmlrpclib.getparser()
         p.feed(request.raw_post_data)
         p.close()
         
         args = u.close()
         if len(args) > 0:
             args = args[0]
             if not isinstance(args, dict):
                 xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % 'Arguments should be a dict'), methodresponse=1)				
                 return HttpResponse(xml, mimetype='text/xml; charset=utf-8')
                 
             old = request.POST._mutable
             request.POST._mutable = True
             for k, v in args.items():
                 request.POST[k] = v
             request.POST._mutable = old
Exemplo n.º 3
0
Arquivo: rpc.py Projeto: 002jnm/Pylogs
def call(request):
	"""
	This is the view you need to map into your URL space to process RPC
	calls.
	"""
	if request.POST:		
		p, u = xmlrpclib.getparser()
		p.feed(request.raw_post_data)
		p.close()
		#print request.raw_post_data
		args = u.close()
		method = u.getmethodname()		
		func = getattr(__import__('blog'), 'metaweblogapi')
		func = getattr(func,str(method.split('.')[1]))		
		if DEBUG: print method, func, args
		if func is not None:
			try:
				result = func(*args)
				xml = xmlrpclib.dumps((result,), methodresponse=1)
				if DEBUG: print result
			except Exception, e:
				if DEBUG: print e
				xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % e), methodresponse=1)				
		else:
			xml = xmlrpclib.dumps(xmlrpclib.Fault(-32601, 'method unknown: %s' % method), methodresponse=1)
			
		return HttpResponse(xml, mimetype='text/xml; charset=utf-8')
	def __send(self, methodname, args):

        self.__ensure_channel()

        request=xmlrpclib.dumps(args, methodname, encoding=None) 

        self.__channel.sendall( ("%08d" % (len(request),))+request)

        resplen=self.__recvall(self.__channel, 8)

        resplen=int(resplen)

        response=self.__recvall(self.__channel, resplen)

        p, u = xmlrpclib.getparser()

        p.feed(response)

        p.close()

        try:

            response=u.close()

        except xmlrpclib.Fault,e:

            if e.faultCode!=17:

                raise e

            klass,str=e.faultString.split(':', 1)

            raise common.getfullname(klass)(str)
Exemplo n.º 5
0
def parse_response_xmlrpc(f, conn, debug=1):
    """parse_response(file_obj)

    Read response from input file object, and parse it
    
    The parsed data is returned to the caller
    """
    p,u=xmlrpclib.getparser()
    if debug:
      s=StringIO.StringIO()
    i=0
    try:
      while 1:
        try:
          response = f.read()
        except AssertionError:
          break
        if not response:
            break
        if debug:
          s.write(response)
        p.feed(response)
      if debug:
        err_msg(s.getvalue())
      p.close()
      return u.close()
    except:
      if debug:
        err_msg(s.getvalue())
      raise
Exemplo n.º 6
0
 def http_POST(self, request):
     parser, unmarshaller = xmlrpclib.getparser()
     deferred = stream.readStream(request.stream, parser.feed)
     deferred.addCallback(lambda x: self._cbDispatch(request, parser, unmarshaller))
     deferred.addErrback(self._ebRender)
     deferred.addCallback(self._cbRender, request)
     return deferred
    def request(self, host, handler, request_body, verbose=0):
        p, u = getparser()
        ssh = self.getssh()
        ssh.stdin.write("""POST /%s HTTP/1.1
User-Agent: Xen
Host: %s
Content-Type: text/xml
Content-Length: %d

%s""" % (handler, host, len(request_body), request_body))
        ssh.stdin.flush()

        content_length = 0
        line = ssh.stdout.readline()
        if line.split()[1] != '200':
            raise Fault(0, 'Server returned %s' % (' '.join(line[1:])))
        
        while line not in ['', '\r\n', '\n']:
            if line.lower().startswith('content-length:'):
                content_length = int(line[15:].strip())
            line = ssh.stdout.readline()
        content = ssh.stdout.read(content_length)
        p.feed(content)
        p.close()
        return u.close()
Exemplo n.º 8
0
    def request(self, host, handler, request_body, verbose=0):
        p, u = getparser()
        ssh = self.getssh()
        ssh.stdin.write("""POST /%s HTTP/1.1
User-Agent: Xen
Host: %s
Content-Type: text/xml
Content-Length: %d

%s""" % (handler, host, len(request_body), request_body))
        ssh.stdin.flush()

        content_length = 0
        line = ssh.stdout.readline()
        if line.split()[1] != '200':
            raise Fault(0, 'Server returned %s' % (' '.join(line[1:])))

        while line not in ['', '\r\n', '\n']:
            if line.lower().startswith('content-length:'):
                content_length = int(line[15:].strip())
            line = ssh.stdout.readline()
        content = ssh.stdout.read(content_length)
        p.feed(content)
        p.close()
        return u.close()
Exemplo n.º 9
0
    def process_request(self, request):
        format_string = getattr(settings, 'FORMAT_STRING', 'format')
        format = request.GET.get(format_string, '')
        if format:
            request.format = format.lower()  #could be "json", "xmlrpc", etc
        else:
            request.format = getattr(settings, 'DEFAULT_FORMAT', 'json')
        if request.format == 'xmlrpc':
            import xmlrpclib
            p, u = xmlrpclib.getparser()
            p.feed(request.raw_post_data)
            p.close()

            args = u.close()
            if len(args) > 0:
                args = args[0]
                if not isinstance(args, dict):
                    xml = xmlrpclib.dumps(xmlrpclib.Fault(
                        -32400,
                        'system error: %s' % 'Arguments should be a dict'),
                                          methodresponse=1)
                    return HttpResponse(xml,
                                        mimetype='text/xml; charset=utf-8')

                old = request.POST._mutable
                request.POST._mutable = True
                for k, v in args.items():
                    request.POST[k] = v
                request.POST._mutable = old
Exemplo n.º 10
0
 def http_POST(self, request):
     parser, unmarshaller = xmlrpclib.getparser()
     deferred = stream.readStream(request.stream, parser.feed)
     deferred.addCallback(
         lambda x: self._cbDispatch(request, parser, unmarshaller))
     deferred.addErrback(self._ebRender)
     deferred.addCallback(self._cbRender, request)
     return deferred
Exemplo n.º 11
0
 def request(self, host, handler, request, verbose):
     response = self._session.post(self._uri, data = request,
             headers={"Content-Type": "text/xml; charset=UTF-8"})
     p, u = xmlrpclib.getparser()
     p.feed(response.content)
     p.close()
     response.close()
     return u.close()
Exemplo n.º 12
0
 def _read_request(self, stream):
     parser, unmarshaller = getparser()
     while True:
         chunk = stream.read(8192)
         if not chunk:
             break
         parser.feed(chunk)
     parser.close()
     return unmarshaller.close(), unmarshaller.getmethodname()
Exemplo n.º 13
0
 def __parse_action_data(self, action):
     """ Parse action data and returns (method, params) """
     data = action['action']
     parser, decoder = xmlrpclib.getparser()
     parser.feed(data.encode("utf-8"))
     parser.close()
     params = decoder.close()
     method = decoder.getmethodname()
     return (method, params)
Exemplo n.º 14
0
 def __parse_action_data(self, action):
     """ Parse action data and returns (method, params) """
     data = action['action']
     parser, decoder = xmlrpclib.getparser()
     parser.feed(bstr(data))
     parser.close()
     params = decoder.close()
     method = decoder.getmethodname()
     return (method, params)
Exemplo n.º 15
0
 def __parse(self, response):
     p, u = xmlrpclib.getparser()
     p.feed(response.body)
     p.close()
     tmp = u.close()
     
     if len(tmp) == 1:
         tmp = tmp[0]
         
     return tmp
Exemplo n.º 16
0
 def request(self, host, handler, request, verbose):
     response = self._session.post(
         self._uri,
         data=request,
         headers={"Content-Type": "text/xml; charset=UTF-8"})
     p, u = xmlrpclib.getparser()
     p.feed(response.content)
     p.close()
     response.close()
     return u.close()
Exemplo n.º 17
0
	def request(self, host, uripath, body, verbose=0):
		self._http_request(uripath, body, host)
		response=self.conn.getresponse()
		if response.status != 200:
			raise xmlrpclib.ProtocolError(host+uripath, response.status,
							response.reason, response.msg)
		data=response.read()
		parser, unmarshaller=xmlrpclib.getparser()
		parser.feed(data)
		parser.close()
		return unmarshaller.close()
Exemplo n.º 18
0
 def request(self, host, uripath, body, verbose=0):
     self._http_request(uripath, body, host)
     response = self.conn.getresponse()
     if response.status != 200:
         raise xmlrpclib.ProtocolError(host + uripath, response.status,
                                       response.reason, response.msg)
     data = response.read()
     parser, unmarshaller = xmlrpclib.getparser()
     parser.feed(data)
     parser.close()
     return unmarshaller.close()
Exemplo n.º 19
0
 def _read_request(self, stream):
     parser, unmarshaller = getparser()
     rlen = 0
     maxlen = opts.get('MaxRequestLength', None)
     while True:
         chunk = stream.read(8192)
         if not chunk:
             break
         rlen += len(chunk)
         if maxlen and rlen > maxlen:
             raise koji.GenericError, 'Request too long'
         parser.feed(chunk)
     parser.close()
     return unmarshaller.close(), unmarshaller.getmethodname()
Exemplo n.º 20
0
 def _read_request(self, stream):
     parser, unmarshaller = getparser()
     rlen = 0
     maxlen = opts.get('MaxRequestLength', None)
     while True:
         chunk = stream.read(8192)
         if not chunk:
             break
         rlen += len(chunk)
         if maxlen and rlen > maxlen:
             raise koji.GenericError, 'Request too long'
         parser.feed(chunk)
     parser.close()
     return unmarshaller.close(), unmarshaller.getmethodname()
Exemplo n.º 21
0
Arquivo: xmlrpc.py Projeto: angvp/gord
  def _parse_response(self, response_body):
    """Parse XML-RPC response without socket connection.

    Args:
      response_body: string, XML-RPC response body.

    Returns:
      A tuple containing the unmarshalled XML-RPC response.
    """

    # pylint: disable-msg=E6412
    # Disable error for unexpected keyword argument.
    parser, unmarshaller = xmlrpclib.getparser(use_datetime=0)
    # pylint: enable-msg=E6412
    parser.feed(response_body)
    return unmarshaller.close()
Exemplo n.º 22
0
    def request(self, host, handler, request_body, verbose = False):
        parser, unmarshaller = getparser()

        response = self.client.post(handler, request_body, 'text/xml')

        if response.status_code != 200:
            raise ProtocolError(
              '%s%s' % (host, handler),
              response.status_code,
              responses.get(response.status_code, ''),
              dict(response.items()),
            )

        parser.feed(response.content)

        return unmarshaller.close()
Exemplo n.º 23
0
    def request(self, host, handler, request_body, verbose=False):
        parser, unmarshaller = getparser()

        response = self.client.post(handler, request_body, 'text/xml')

        if response.status_code != 200:
            raise ProtocolError(
                '%s%s' % (host, handler),
                response.status_code,
                responses.get(response.status_code, ''),
                dict(response.items()),
            )

        parser.feed(response.content)

        return unmarshaller.close()
Exemplo n.º 24
0
    def _parse_response(self, response_body):
        """Parse XML-RPC response without socket connection.

    Args:
      response_body: string, XML-RPC response body.

    Returns:
      A tuple containing the unmarshalled XML-RPC response.
    """

        # pylint: disable-msg=E6412
        # Disable error for unexpected keyword argument.
        parser, unmarshaller = xmlrpclib.getparser(use_datetime=0)
        # pylint: enable-msg=E6412
        parser.feed(response_body)
        return unmarshaller.close()
Exemplo n.º 25
0
 def __send(self, methodname, args):
     self.__ensure_channel()
     request=xmlrpclib.dumps(args, methodname, encoding=None) #  allow_none=False (allow_none is py2.3+)
     self.__channel.sendall( ("%08d" % (len(request),))+request)
     resplen=self.__recvall(self.__channel, 8)
     resplen=int(resplen)
     response=self.__recvall(self.__channel, resplen)
     p, u = xmlrpclib.getparser()
     p.feed(response)
     p.close()
     # if the response was a Fault, then it is raised by u.close()
     try:
         response=u.close()
     except xmlrpclib.Fault,e:
         if e.faultCode!=17:
             raise e
         klass,str=e.faultString.split(':', 1)
         raise common.getfullname(klass)(str)
Exemplo n.º 26
0
    def _parse(self, f, chan):
        n = 0
        line = f.readline()
        if line.split()[1] != '200':
            raise ControlConnectError, 'HTTP request failed' + line

        while n == 0:
            if line.lower().startswith('content-length:'):
                n = int(line[15:].strip())
            line = f.readline()
        content = f.read(n)
        f.close()
        chan.close()

        p, u = xmlrpclib.getparser()
        p.feed(content)
        p.close()
        return u.close()
Exemplo n.º 27
0
	def continue_request( self, data, request ):
		if self.set.conf.has_key('defaultencoding'):
			encoding = self.set.conf['defaultencoding']
			new = self.set.PatchEncodingHeader( data )
			OriginalUnmarshaller = xmlrpclib.Unmarshaller
			xmlrpclib.Unmarshaller = Unmarshaller
			p, u = xmlrpclib.getparser()
			xmlrpclib.Unmarshaller = OriginalUnmarshaller
			u.set_resultencoding( encoding )
			p.feed( new )
			p.close()
			params = u.close()
			method = u.getmethodname()
			#parms = []
			#for p in params:
			#	if type( p ) == type( u'' ):
			#		parms.append( p.encode( encoding ) )
			#	else:
			#		parms.append( p )
			#params = tuple(parms)
			try:
				# generate response
				try:
					response = self.call( method, params )
					if type( response ) != type( () ):
						response = ( response, )
				except:
					# report exception back to server
					response = xmlrpclib.dumps( 
						xmlrpclib.Fault( 1, "%s:%s" % (sys.exc_type, sys.exc_value))
						)
				else:
					response = xmlrpclib.dumps( response, methodresponse=1, encoding=encoding )
			except:
				# report internal error
				request.error( 500 )
			else:
				# got a valid response
				request['Content-Type'] = 'text/xml'
				request.push( response )
				request.done()
		else:
			xmlrpc_handler.xmlrpc_handler.continue_request( self, data, request )
Exemplo n.º 28
0
    def __init__(self, client_version, req):
        self.client = client_version
        self.req = req
        # grab an Input object
        self.input = transports.Input(req.headers_in)
        # make sure we have a parser and a decoder available
        self.parser, self.decoder = xmlrpclib.getparser()
        # Make sure the decoder doesn't assume UTF-8 data, that would break if
        # non-UTF-8 chars are sent (bug 139370)
        self.decoder._encoding = None

        # extract the server we're talking to and the root directory
        # from the request configuration options
        req_config = req.get_options()
        # XXX: attempt to catch these KeyErrors sometime when there is
        # time to play nicely
        self.server = req_config["SERVER"]
        # Load the server classes
        # XXX: some day we're going to trust the timestamp stuff...
        self.servers = None
        self._setup_servers()
Exemplo n.º 29
0
    def __init__(self, client_version, req):
        self.client = client_version
        self.req = req
        # grab an Input object
        self.input = transports.Input(req.headers_in)
        # make sure we have a parser and a decoder available
        self.parser, self.decoder = xmlrpclib.getparser()
        # Make sure the decoder doesn't assume UTF-8 data, that would break if
        # non-UTF-8 chars are sent (bug 139370)
        self.decoder._encoding = None

        # extract the server we're talking to and the root directory
        # from the request configuration options
        req_config = req.get_options()
        # XXX: attempt to catch these KeyErrors sometime when there is
        # time to play nicely
        self.server = req_config["SERVER"]
        # Load the server classes
        # XXX: some day we're going to trust the timestamp stuff...
        self.servers = None
        self._setup_servers()
Exemplo n.º 30
0
    def warranted(cls):
        # We cache it so that we don't actually retest.
        if cls._warranted is not None:
            return cls._warranted

        good = True
        parser, unmarshaller = xmlrpclib.getparser()
        try:
            # This will fail if the unmarshaller is unable to handle ex:nil.
            # We do not test for every value that we patch for. We assume if
            # ex:nil won't work, no other extended value will.
            parser.feed("<params><param><value><ex:nil/></value></param></params>")
        except xmlrpclib.ResponseError:
            good = False

        # Trying to close if there was an error earlier won't work.
        if good:
            parser.close()
            unmarshaller.close()

        cls._warranted = not good
        return cls._warranted
Exemplo n.º 31
0
def patchRPCParser(error_handler):
    """
    Patch xmlrpcmlib's parser class, so it logs data content in case of errors,
    to ease debugging.
    Warning: this installs a monkey patch on a generic class, so it's last
    comes wins. Must *not* be enabled by default.

    error_handler (callable)
      Receives the erroneous data as first parameter, and the exception
      instance as second parameter.
      If it returns a false value (ie, handler did not recover from the error),
      exception is re-raised.
    """
    parser, _ = xmlrpclib.getparser()
    parser_klass = parser.__class__
    original_feed = parser_klass.feed
    def verbose_feed(self, data):
        try:
            return original_feed(self, data)
        except Exception, exc:
            if not error_handler(data, exc):
                raise
Exemplo n.º 32
0
def patchRPCParser(error_handler):
    """
    Patch xmlrpcmlib's parser class, so it logs data content in case of errors,
    to ease debugging.
    Warning: this installs a monkey patch on a generic class, so it's last
    comes wins. Must *not* be enabled by default.

    error_handler (callable)
      Receives the erroneous data as first parameter, and the exception
      instance as second parameter.
      If it returns a false value (ie, handler did not recover from the error),
      exception is re-raised.
    """
    parser, _ = xmlrpclib.getparser()
    parser_klass = parser.__class__
    original_feed = parser_klass.feed

    def verbose_feed(self, data):
        try:
            return original_feed(self, data)
        except Exception, exc:
            if not error_handler(data, exc):
                raise
Exemplo n.º 33
0
def handle_action(action):
    global server

    log.log_debug("handle_action", action)

    version = action['version']
    action_id = action['id']
    data = action['action']

    log.log_debug("handle_action actionid = %s, version = %s" %
                  (action_id, version))

    # Decipher the data
    parser, decoder = xmlrpclib.getparser()
    parser.feed(data)
    parser.close()
    params = decoder.close()
    method = decoder.getmethodname()
    data = {}

    (status, message, data) = run_action(method, params)

    log.log_debug("Sending back response", (status, message, data))
    return submit_response(action_id, status, message, data)
Exemplo n.º 34
0
def handle_action(action):
    global server

    log.log_debug("handle_action", action)

    version = action['version']
    action_id = action['id']
    data = action['action']

    log.log_debug("handle_action actionid = %s, version = %s" % (
        action_id, version))

    # Decipher the data
    parser, decoder = xmlrpclib.getparser()
    parser.feed(data)
    parser.close()
    params = decoder.close()
    method = decoder.getmethodname()
    data = {}

    (status, message, data) = run_action(method, params)

    log.log_debug("Sending back response", (status, message, data))
    return submit_response(action_id, status, message, data)
Exemplo n.º 35
0
 def getparser(self):
     return xmlrpclib.getparser()
Exemplo n.º 36
0
      test_line.stop()
"""
import httplib
import logging
import select
import socket
import threading
import time
import xmlrpclib

__all__ = ['TaskDistributionTool', 'TestResultProxy', 'TestResultLineProxy', 'patchRPCParser']

# Depending on used xmlrpc backend, different exceptions can be thrown.
SAFE_RPC_EXCEPTION_LIST = [socket.error, xmlrpclib.ProtocolError,
    xmlrpclib.Fault, httplib.BadStatusLine, httplib.ResponseNotReady]
parser, _ = xmlrpclib.getparser()
if xmlrpclib.ExpatParser and isinstance(parser, xmlrpclib.ExpatParser):
    SAFE_RPC_EXCEPTION_LIST.append(xmlrpclib.expat.ExpatError)
else:
    import sys
    print >> sys.stderr, 'Warning: unhandled xmlrpclib parser %r, some ' \
        'exceptions might get through safeRpcCall' % (parser, )
    del sys
SAFE_RPC_EXCEPTION_LIST = tuple(SAFE_RPC_EXCEPTION_LIST)
del parser, _

def null_callable(*args, **kw):
    pass

class NullLogger(object):
    def __getattr__(self, name):
Exemplo n.º 37
0
 def __parse_response(self, response_body):
     p, u = xmlrpclib.getparser(use_datetime=self._use_datetime)
     p.feed(response_body)
     return u.close()
Exemplo n.º 38
0
 def getparser(self):
     return xmlrpclib.getparser()
Exemplo n.º 39
0
 def getparser(self):
     # get parser and unmarshaller
     return getparser()
Exemplo n.º 40
0
 def getparser(self):
     # get parser and unmarshaller
     return xmlrpclib.getparser()
Exemplo n.º 41
0
 def getparser(self):
     return getparser(use_datetime=self._use_datetime)
Exemplo n.º 42
0
import socket
import threading
import time
import xmlrpclib

__all__ = [
    'TaskDistributor', 'TestResultProxy', 'TestResultLineProxy',
    'patchRPCParser'
]

# Depending on used xmlrpc backend, different exceptions can be thrown.
SAFE_RPC_EXCEPTION_LIST = [
    socket.error, xmlrpclib.ProtocolError, xmlrpclib.Fault,
    httplib.BadStatusLine, httplib.ResponseNotReady
]
parser, _ = xmlrpclib.getparser()
if xmlrpclib.ExpatParser and isinstance(parser, xmlrpclib.ExpatParser):
    SAFE_RPC_EXCEPTION_LIST.append(xmlrpclib.expat.ExpatError)
else:
    import sys
    print('Warning: unhandled xmlrpclib parser %r, some exceptions might get'
          ' through safeRpcCall' % parser,
          file=sys.stderr)
    del sys
SAFE_RPC_EXCEPTION_LIST = tuple(SAFE_RPC_EXCEPTION_LIST)
del parser, _


def null_callable(*args, **kw):
    pass
Exemplo n.º 43
0
 def getparser(self):
     return getparser(use_datetime=self._use_datetime)
Exemplo n.º 44
0
 def __parse_response(self, response_body):
     p, u = xmlrpclib.getparser(use_datetime=False)
     p.feed(response_body)
     return u.close()
Exemplo n.º 45
0
 def getparser(self):
     return getparser()