示例#1
0
文件: client.py 项目: edevil/soaplib
    def __call__(self, *args, **kwargs):
        '''
        This method executes the http request to the remote web service.  With
        the exception of 'headers', 'msgid', and 'mtom'; all keyword arguments
        to this method are put in the http header.  The 'headers' keyword is to
        denote a list of elements to be included in the soap header, 'msgid'
        is a convenience keyword used in async web services which creates a
        WS-Addressing messageid header to be included in the soap headers, and
        'mtom' enables the Message Transmission Optimization Mechanism.

        @param the arguments to the remote method
        @param the keyword arguments
        '''
        if len(args) != len(self.descriptor.inMessage.params):
            argstring = '\r\n'.join(['    ' + str(arg) for arg in args])
            paramstring = '\r\n'.join(['    ' + str(p[0])
                for p in self.descriptor.inMessage.params])
            err_msg = _err_format % (argstring, paramstring)
            raise Exception(err_msg)

        msg = self.descriptor.inMessage.to_xml(*args)

        # grab the soap headers passed into this call
        headers = kwargs.get('headers', [])
        mtom = kwargs.get('mtom', False)
        msgid = kwargs.get('msgid')
        if msgid:
            # special case for the msgid field as a convenience
            # when dealing with async callback methods
            headers.append(create_relates_to_header(msgid))

        tns = self.descriptor.inMessage.ns
        envelope = make_soap_envelope(msg, tns, header_elements=headers)

        body = ElementTree.tostring(envelope)
        methodName = '\"%s\"' % self.descriptor.soapAction
        httpHeaders = {'Content-Length': len(body),
                      'Content-type': 'text/xml; charset="UTF-8"',
                      'Accept': ('application/soap+xml, application/dime, '
                                'multipart/related, text/*'),
                      'User-Agent': 'Soaplib/1.0',
                      'SOAPAction': methodName,
                      }

        for k, v in kwargs.items():
            # add all the other keywords to the http headers
            if k not in ('headers', 'msgid', 'mtom'):
                httpHeaders[k] = v

        if mtom:
            httpHeaders, body = apply_mtom(httpHeaders, body,
                self.descriptor.inMessage.params, args)

        dump(self.host, self.path, httpHeaders, body)

        if self.scheme == "http":
            conn = httplib.HTTPConnection(self.host)
        elif self.scheme == "https":
            conn = httplib.HTTPSConnection(self.host)
        else:
            raise RuntimeError("Unsupported URI connection scheme: %s" %
                self.scheme)

        conn.request("POST", self.path, body=body, headers=httpHeaders)
        response = conn.getresponse()
        data = response.read()

        dump(self.host, self.path, dict(response.getheaders()), data)

        contenttype = response.getheader('Content-Type')
        data = collapse_swa(contenttype, data)

        conn.close()
        if str(response.status) not in['200', '202']:
            # consider everything NOT 200 or 202 as an error response

            if str(response.status) == '500':
                fault = None
                try:
                    payload, headers = from_soap(data)
                    fault = Fault.from_xml(payload)
                except:
                    trace = StringIO()
                    import traceback
                    traceback.print_exc(file=trace)

                    fault = Exception("Unable to read response \n"
                        "%s %s \n %s \n %s" %
                        (response.status, response.reason, trace.getvalue(),
                         data))
                raise fault
            else:
                raise Exception("%s %s" % (response.status, response.reason))

        if not self.descriptor.outMessage.params:
            return

        payload, headers = from_soap(data)
        results = self.descriptor.outMessage.from_xml(payload)
        return results[0]
示例#2
0
    def __call__(self, *args, **kwargs):
        '''
        This method executes the http request to the remote web service.  With
        the exception of 'headers', 'msgid', and 'mtom'; all keyword arguments
        to this method are put in the http header.  The 'headers' keyword is to
        denote a list of elements to be included in the soap header, 'msgid'
        is a convenience keyword used in async web services which creates a
        WS-Addressing messageid header to be included in the soap headers, and
        'mtom' enables the Message Transmission Optimization Mechanism.

        @param the arguments to the remote method
        @param the keyword arguments
        '''
        if len(args) != len(self.descriptor.inMessage.params):
            argstring = '\r\n'.join(['    ' + str(arg) for arg in args])
            paramstring = '\r\n'.join(
                ['    ' + str(p[0]) for p in self.descriptor.inMessage.params])
            err_msg = _err_format % (argstring, paramstring)
            raise Exception(err_msg)

        msg = self.descriptor.inMessage.to_xml(*args)

        # grab the soap headers passed into this call
        headers = kwargs.get('headers', [])
        mtom = kwargs.get('mtom', False)
        msgid = kwargs.get('msgid')
        if msgid:
            # special case for the msgid field as a convenience
            # when dealing with async callback methods
            headers.append(create_relates_to_header(msgid))

        tns = self.descriptor.inMessage.ns
        envelope = make_soap_envelope(msg, tns, header_elements=headers)

        body = ElementTree.tostring(envelope)
        methodName = '\"%s\"' % self.descriptor.soapAction
        httpHeaders = {
            'Content-Length':
            len(body),
            'Content-type':
            'text/xml; charset="UTF-8"',
            'Accept': ('application/soap+xml, application/dime, '
                       'multipart/related, text/*'),
            'User-Agent':
            'Soaplib/1.0',
            'SOAPAction':
            methodName,
        }

        for k, v in kwargs.items():
            # add all the other keywords to the http headers
            if k not in ('headers', 'msgid', 'mtom'):
                httpHeaders[k] = v

        if mtom:
            httpHeaders, body = apply_mtom(httpHeaders, body,
                                           self.descriptor.inMessage.params,
                                           args)

        dump(self.host, self.path, httpHeaders, body)

        if self.scheme == "http":
            conn = httplib.HTTPConnection(self.host)
        elif self.scheme == "https":
            conn = httplib.HTTPSConnection(self.host)
        else:
            raise RuntimeError("Unsupported URI connection scheme: %s" %
                               self.scheme)

        conn.request("POST", self.path, body=body, headers=httpHeaders)
        response = conn.getresponse()
        data = response.read()

        dump(self.host, self.path, dict(response.getheaders()), data)

        contenttype = response.getheader('Content-Type')
        data = collapse_swa(contenttype, data)

        conn.close()
        if str(response.status) not in ['200', '202']:
            # consider everything NOT 200 or 202 as an error response

            if str(response.status) == '500':
                fault = None
                try:
                    payload, headers = from_soap(data)
                    fault = Fault.from_xml(payload)
                except:
                    trace = StringIO()
                    import traceback
                    traceback.print_exc(file=trace)

                    fault = Exception("Unable to read response \n"
                                      "%s %s \n %s \n %s" %
                                      (response.status, response.reason,
                                       trace.getvalue(), data))
                raise fault
            else:
                raise Exception("%s %s" % (response.status, response.reason))

        if not self.descriptor.outMessage.params:
            return

        payload, headers = from_soap(data)
        results = self.descriptor.outMessage.from_xml(payload)
        #TODO: consider supporting multiple return types in a better manner
        if len(results) > 1:
            return results
        return results[0]
示例#3
0
class WSGISoapApp(object):
    '''
    This is the base object representing a soap web application, and conforms
    to the WSGI specification (PEP 333).  This object should be overridden
    and getHandler(environ) overridden to provide the object implementing
    the specified functionality.  Hooks have been added so that the subclass
    can react to various events that happen durring the execution of the
    request.
    '''
    def onCall(self, environ):
        '''
        This is the first method called when this WSGI app is invoked
        @param the wsgi environment
        '''
        pass

    def onWsdl(self, environ, wsdl):
        '''
        This is called when a wsdl is requested
        @param the wsgi environment
        @param the wsdl string
        '''
        pass

    def onWsdlException(self, environ, exc, resp):
        '''
        Called when an exception occurs durring wsdl generation
        @param the wsgi environment
        @param exc the exception
        @param the fault response string
        '''
        pass

    def onMethodExec(self, environ, body, py_params, soap_params):
        '''
        Called BEFORE the service implementing the functionality is called
        @param the wsgi environment
        @param the body element of the soap request
        @param the tuple of python params being passed to the method
        @param the soap elements for each params
        '''
        pass

    def onResults(self, environ, py_results, soap_results):
        '''
        Called AFTER the service implementing the functionality is called
        @param the wsgi environment
        @param the python results from the method
        @param the xml serialized results of the method
        '''
        pass

    def onException(self, environ, exc, resp):
        '''
        Called when an error occurs durring execution
        @param the wsgi environment
        @param the exception
        @param the response string
        '''
        pass

    def onReturn(self, environ, returnString):
        '''
        Called before the application returns
        @param the wsgi environment
        @param return string of the soap request
        '''
        pass

    def getHandler(self, environ):
        '''
        This method returns the object responsible for processing a given
        request, and needs to be overridden by a subclass to handle
        the application specific  mapping of the request to the appropriate
        handler.
        @param the wsgi environment
        @returns the object to be called for the soap operation
        '''
        raise Exception("Not implemented")

    def __call__(self, environ, start_response, address_url=None):
        '''
        This method conforms to the WSGI spec for callable wsgi applications
        (PEP 333). It looks in environ['wsgi.input'] for a fully formed soap
        request envelope, will deserialize the request parameters and call the
        method on the object returned by the getHandler() method.
        @param the http environment
        @param a callable that begins the response message
        @returns the string representation of the soap call
        '''
        methodname = ''
        try:
            reset_request()
            request.environ = environ

            # implementation hook
            self.onCall(environ)

            serviceName = environ['PATH_INFO'].split('/')[-1]
            service = self.getHandler(environ)
            if ((environ['QUERY_STRING'].endswith('wsdl')
                 or environ['PATH_INFO'].endswith('wsdl'))
                    and environ['REQUEST_METHOD'].lower() == 'get'):
                # get the wsdl for the service
                #
                # Assume path_info matches pattern
                # /stuff/stuff/stuff/serviceName.wsdl or ?WSDL
                #
                serviceName = serviceName.split('.')[0]
                if address_url:
                    url = address_url
                else:
                    url = reconstruct_url(environ).split('.wsdl')[0]

                start_response('200 OK', [('Content-type', 'text/xml')])
                try:
                    wsdl_content = service.wsdl(url)

                    # implementation hook
                    self.onWsdl(environ, wsdl_content)
                except Exception, e:

                    # implementation hook
                    buffer = cStringIO.StringIO()
                    traceback.print_exc(file=buffer)
                    buffer.seek(0)
                    stacktrace = str(buffer.read())
                    faultStr = ElementTree.tostring(make_soap_fault(
                        str(e), detail=stacktrace),
                                                    encoding=string_encoding)
                    exceptions(faultStr)
                    self.onWsdlException(environ, e, faultStr)
                    # initiate the response
                    start_response('500',
                                   [('Content-type', 'text/xml'),
                                    ('Content-length', str(len(faultStr)))])
                    return [faultStr]

                reset_request()
                return [wsdl_content]

            if environ['REQUEST_METHOD'].lower() != 'post':
                start_response('405 Method Not Allowed', [('Allow', 'POST')])
                return ''

            input = environ.get('wsgi.input')
            length = environ.get("CONTENT_LENGTH")
            body = input.read(int(length))
            debug(body)
            body = collapse_swa(environ.get("CONTENT_TYPE"), body)

            # deserialize the body of the message
            try:
                payload, header = from_soap(body)
            except SyntaxError, e:
                payload = None
                header = None

            if payload:
                methodname = payload.tag.split('}')[-1]
            else:
                # check HTTP_SOAPACTION
                methodname = environ.get("HTTP_SOAPACTION")
                if methodname.startswith('"') and methodname.endswith('"'):
                    methodname = methodname[1:-1]
                if methodname.find('/') > 0:
                    methodname = methodname.split('/')[1]

            request.header = header

            # call the method
            func = getattr(service, methodname)

            # retrieve the method descriptor
            descriptor = func(_soap_descriptor=True, klazz=service.__class__)
            if payload:
                params = descriptor.inMessage.from_xml(*[payload])
            else:
                params = ()
            # implementation hook
            self.onMethodExec(environ, body, params,
                              descriptor.inMessage.params)

            # call the method
            retval = func(*params)

            # transform the results into an element
            # only expect a single element
            results = None
            if not (descriptor.isAsync or descriptor.isCallback):
                results = descriptor.outMessage.to_xml(*[retval])

            # implementation hook
            self.onResults(environ, results, retval)

            # grab any headers that were included in the request
            response_headers = None
            if hasattr(request, 'response_headers'):
                response_headers = request.response_headers

            # construct the soap response, and serialize it
            envelope = make_soap_envelope(results,
                                          tns=service.__tns__,
                                          header_elements=response_headers)
            ElementTree.cleanup_namespaces(envelope)
            resp = ElementTree.tostring(envelope, encoding=string_encoding)
            headers = {'Content-Type': 'text/xml'}

            if descriptor.mtom:
                headers, resp = apply_mtom(headers, resp,
                                           descriptor.outMessage.params,
                                           (retval, ))

            if 'CONTENT_LENGTH' in environ:
                del environ['CONTENT_LENGTH']

            # initiate the response
            start_response('200 OK', headers.items())

            self.onReturn(environ, resp)

            debug(resp)

            # return the serialized results
            reset_request()
            return [resp]
示例#4
0
文件: wsgi_soap.py 项目: tubav/teagle
    def __call__(self, environ, start_response):
        '''
        This method conforms to the WSGI spec for callable wsgi applications (PEP 333).
        This method looks in environ['wsgi.input'] for a fully formed soap request envelope,
        will deserialize the request parameters and call the method on the object returned
        by the getHandler() method.
        @param the http environment
        @param a callable that begins the response message
        @returns the string representation of the soap call
        '''
        methodname = ''
        try:
            reset_request()
            request.environ = environ
            
            # implementation hook
            self.onCall(environ)
            
            serviceName = environ['PATH_INFO'].split('/')[-1]
            service = self.getHandler(environ)
            if serviceName.lower().endswith('wsdl'):
                # get the wsdl for the service
                #
                # Assume path_info matches pattern
                # /stuff/stuff/stuff/serviceName.wsdl or ?WSDL
                #
                serviceName = serviceName.split('.')[0]
                url = reconstruct_url(environ).split('.wsdl')[0]
                
                start_response('200 OK',[('Content-type','text/xml')])
                try:
                    wsdl_content = service.wsdl(url)
                    
                    # implementation hook
                    self.onWsdl(environ,wsdl_content)
                except Exception, e:
                    
                    # implementation hook
                    buffer = cStringIO.StringIO()
                    traceback.print_exc(file=buffer)
                    buffer.seek(0)
                    stacktrace = str(buffer.read())
                    faultStr = ElementTree.tostring(make_soap_fault( str(e), detail=stacktrace), encoding=string_encoding)
                    
                    exceptions(faultStr)
                    
                    self.onWsdlException(environ,e,faultStr)
                    
                    # initiate the response
                    start_response('500',[('Content-type','text/xml'),('Content-length',str(len(faultStr)))])
                    return [faultStr]
                
                reset_request()
                return [wsdl_content]
            
            if environ['REQUEST_METHOD'].lower() != 'post':
                start_response('405 Method Not Allowed',[('Allow','POST')])
                return ''
                
            input = environ.get('wsgi.input')
            length = environ.get("CONTENT_LENGTH")
            body = input.read(int(length))
            debug(body)
            body = collapse_swa( environ.get("CONTENT_TYPE"), body)
            
            # deserialize the body of the message
            payload, header = from_soap(body)
            if payload:
                methodname = payload.tag.split('}')[-1]
            else:
                # check HTTP_SOAPACTION
                methodname = environ.get("HTTP_SOAPACTION")
                if methodname.startswith('"') and methodname.endswith('"'):
                    methodname = methodname[1:-1]
            request.header = header
            
            # call the method
            func = getattr(service, methodname)
            
            # retrieve the method descriptor
            descriptor = func(_soap_descriptor=True)
            if payload:
                params = descriptor.inMessage.from_xml(*[payload])
            else:
                params = ()
            # implementation hook
            self.onMethodExec(environ,body,params,descriptor.inMessage.params)
            
            # call the method
            retval = func(*params)
            
            # transform the results into an element
            # only expect a single element
            results = None
            if not (descriptor.isAsync or descriptor.isCallback):
                
                results = descriptor.outMessage.to_xml(*[retval])

            # implementation hook
            self.onResults(environ,results,retval)
            
            # grab any headers that were included in the request
            response_headers = None
            if hasattr(request,'response_headers'):
                response_headers = request.response_headers
            
            # construct the soap response, and serialize it
            envelope = make_soap_envelope(results,tns=service.__tns__,header_elements=response_headers) 
            resp = ElementTree.tostring(envelope, encoding=string_encoding)
            headers = {'Content-Type': 'text/xml'}

            if descriptor.mtom:
                headers, resp = apply_mtom( headers, resp,
                                            descriptor.outMessage.params,
                                            (retval,) )

            
            if environ.has_key('CONTENT_LENGTH'):
                del(environ['CONTENT_LENGTH'])
                
            # initiate the response
            start_response('200 OK',headers.items())
            
            self.onReturn(environ,resp)
            
            debug(resp)
            
            # return the serialized results
            reset_request()
            return [resp]
示例#5
0
    def __handle_soap_request(self, req_env, start_response, url):
        http_resp_headers = {
            'Content-Type': 'text/xml',
            'Content-Length': '0',
        }
        method_name = None

        try:
            # implementation hook
            self.on_call(req_env)

            if req_env['REQUEST_METHOD'].lower() != 'post':
                http_resp_headers['Allow'] = 'POST'
                start_response(HTTP_405, http_resp_headers.items())
                return ['']

            input = req_env.get('wsgi.input')
            length = req_env.get("CONTENT_LENGTH")
            body = input.read(int(length))

            try:
                service = None
                soap_req_header, soap_req_payload = self.__decode_soap_request(
                                                                req_env, body)
                self.validate_request(soap_req_payload)

                method_name = self.__get_method_name(req_env, soap_req_payload)
                if method_name is None:
                    resp = "Could not get method name!"
                    http_resp_headers['Content-Length'] = str(len(resp))
                    start_response(HTTP_500, http_resp_headers.items())
                    return [resp]

                service_class = self.get_service_class(method_name)
                service = self.get_service(service_class, req_env)

            finally:
                # for performance reasons, we don't want the following to run
                # in production even though we won't see the results.
                if logger.level == logging.DEBUG:
                    try:
                        logger.debug(etree.tostring(etree.fromstring(body),
                                                             pretty_print=True))
                    except:
                        logger.debug(body)
                        raise

            # retrieve the method descriptor
            descriptor = service.get_method(method_name)
            func = getattr(service, descriptor.name)

            serializers = service.get_serializers()

            # decode header object
            if soap_req_header is not None and len(soap_req_header) > 0:
                in_header = descriptor.in_header
                service.soap_in_header = in_header.from_xml(soap_req_header, serializers)

            # decode method arguments
            if soap_req_payload is not None and len(soap_req_payload) > 0:
                params = descriptor.in_message.from_xml(soap_req_payload, serializers)
            else:
                params = [None] * len(descriptor.in_message._type_info)

            # implementation hook
            service.on_method_call(req_env, method_name, params,
                                                               soap_req_payload)

            # call the method
            result_raw = service.call_wrapper(func, params)

            # construct the soap response, and serialize it
            envelope = etree.Element('{%s}Envelope' % soaplib.ns_soap_env,
                                                            nsmap=soaplib.nsmap)

            #
            # header
            #
            soap_header_elt = etree.SubElement(envelope,
                                             '{%s}Header' % soaplib.ns_soap_env)

            if service.soap_out_header != None:
                if descriptor.out_header is None:
                    logger.warning("Skipping soap response header as %r method "
                                   "is not published to have a soap response "
                                   "header" % method_name)
                else:
                    descriptor.out_header.to_xml(
                        service.soap_out_header,
                        self.get_tns(),
                        soap_header_elt,
                        descriptor.out_header.get_type_name()
                    )

            if len(soap_header_elt) > 0:
                envelope.append(soap_header_elt)

            #
            # body
            #
            soap_body = etree.SubElement(envelope,
                                               '{%s}Body' % soaplib.ns_soap_env)

            # instantiate the result message
            result_message = descriptor.out_message()

            # assign raw result to its wrapper, result_message
            out_type = descriptor.out_message._type_info

            if len(out_type) > 0:
                assert len(out_type) == 1

                attr_name = descriptor.out_message._type_info.keys()[0]
                setattr(result_message, attr_name, result_raw)

            # transform the results into an element
            if not (descriptor.is_async or descriptor.is_callback):
                descriptor.out_message.to_xml(result_message, self.get_tns(),
                                                                      soap_body)

            # implementation hook
            service.on_method_return(req_env, result_raw, soap_body,
                                                              http_resp_headers)

            #
            # misc
            #
            results_str = etree.tostring(envelope, xml_declaration=True,
                                                       encoding=string_encoding)

            if descriptor.mtom:
                http_resp_headers, results_str = apply_mtom(http_resp_headers,
                    results_str, descriptor.out_message._type_info,[result_raw])

            # implementation hook
            self.on_return(req_env, http_resp_headers, results_str)

            # initiate the response
            http_resp_headers['Content-Length'] = str(len(results_str))
            start_response(HTTP_200, http_resp_headers.items())

            if logger.level == logging.DEBUG:
                logger.debug('\033[91m'+ "Response" + '\033[0m')
                logger.debug(etree.tostring(envelope, xml_declaration=True,
                                                             pretty_print=True))

            # return the serialized results
            return [results_str]

        # The user issued a Fault, so handle it just like an exception!
        except Fault, e:
            return self.__handle_fault(req_env, start_response,
                                                http_resp_headers, service, e)
示例#6
0
    def _Application__handle_soap_request(self, req_env, start_response, url):
        """
        This function is too big.
        """
        import threading
        curThread = threading.currentThread()
        curThread.REMOTE_ADDR = req_env.get('REMOTE_ADDR')
        curThread.REMOTE_PORT = req_env.get('REMOTE_PORT')
        ip = req_env.get('REMOTE_ADDR')

        http_resp_headers = {
            'Content-Type': 'text/xml',
            'Content-Length': '0',
        }
        method_name = None

        self.create_path()
        try:
            # implementation hook
            self.on_call(req_env)

            if req_env['REQUEST_METHOD'].lower() != 'post':
                http_resp_headers['Allow'] = 'POST'
                start_response(HTTP_405, http_resp_headers.items())
                return ['']

            input = req_env.get('wsgi.input')
            length = req_env.get("CONTENT_LENGTH")
            body = input.read(int(length))

            try:
                service = None
                soap_req_header, soap_req_payload = \
                self._Application__decode_soap_request(req_env, body)
                if not (soap_req_payload is None):
                    self.validate_request(soap_req_payload)

                method_name = \
                    self._Application__get_method_name \
                                                (req_env, soap_req_payload)
                if method_name is None:
                    resp = "Could not extract method name from the request!"
                    http_resp_headers['Content-Length'] = str(len(resp))
                    start_response(HTTP_500, http_resp_headers.items())
                    return [resp]

                service_class = self.get_service_class(method_name)
                service = self.get_service(service_class, req_env)

            finally:
                # for performance reasons, we don't want the following to run
                # in production even though we won't see the results.
                if logger.level == logging.DEBUG:
                    try:
                        logger.debug(etree.tostring(etree.fromstring(body),
                                                           pretty_print=True))
                    except etree.XMLSyntaxError,e:
                        logger.debug(body)
                        raise Fault('Client.XMLSyntax',\
                                    'Error at line: %d, col: %d' % e.position)

            # retrieve the method descriptor
            descriptor = service.get_method(method_name)
            func = getattr(service, descriptor.name)

            # decode header object
            if soap_req_header is not None and len(soap_req_header) > 0:
                in_header = descriptor.in_header
                service.soap_in_header = in_header.from_xml(soap_req_header)

            # decode method arguments
            if soap_req_payload is not None and len(soap_req_payload) > 0:
                params = descriptor.in_message.from_xml(soap_req_payload)
            else:
                params = [None] * len(descriptor.in_message._type_info)

        #### check_rights
            import threading
            curThread = threading.currentThread()
            if hasattr (params, 'sid'):
                curThread.lang = service.get_lang(params.sid)
            # check exists client certificate
            if not hasattr (curThread, 'client_cert'):
                curThread.client_cert = None
            # check rights client certificate for the method
            check = self.check_rights(method_name, req_env, params)
            if not check:
                if curThread.client_cert:
                    certobj = OpenSSL.crypto.load_certificate \
                            (OpenSSL.SSL.FILETYPE_PEM, curThread.client_cert)
                    finger = certobj.digest('SHA1')
                    self.log.debug('%s %s %s forbidden %s' \
                            %(datetime.datetime.now().__str__(), finger, ip, \
                            method_name[5:]))
                resp = "Permission denied: " + method_name
                http_resp_headers['Content-Length'] = str(len(resp))
                start_response(HTTP_403, http_resp_headers.items())
                return [resp]

        #### logging
            if curThread.client_cert:
                certobj = OpenSSL.crypto.load_certificate \
                        (OpenSSL.SSL.FILETYPE_PEM, curThread.client_cert)
                finger = certobj.digest('SHA1')
                if not method_name[5:] in not_log_list and \
                                        not method_name[5:].endswith('_view'):
                    self.log.debug('%s %s %s allowed %s' \
                            %(datetime.datetime.now().__str__(), finger, ip, \
                            method_name[5:]))

            # implementation hook
            service.on_method_call(req_env, method_name, params,
                                                             soap_req_payload)

            # call the method
            result_raw = service.call_wrapper(func, params)

            # construct the soap response, and serialize it
            envelope = etree.Element('{%s}Envelope' % soaplib.ns_soap_env,
                                                          nsmap=soaplib.nsmap)

            #
            # header
            #
            soap_header_elt = etree.SubElement(envelope,
                                           '{%s}Header' % soaplib.ns_soap_env)

            if service.soap_out_header != None:
                if descriptor.out_header is None:
                    logger.warning("Skipping soap response header as %r "
                                   "method is not published to have a soap "
                                   "response header" % method_name)
                else:
                    descriptor.out_header.to_xml(
                        service.soap_out_header,
                        self.get_tns(),
                        soap_header_elt,
                        descriptor.out_header.get_type_name()
                    )
            if len(soap_header_elt) > 0:
                envelope.append(soap_header_elt)

            #
            # body
            #
            soap_body = etree.SubElement(envelope,
                                            '{%s}Body' % soaplib.ns_soap_env)

            # instantiate the result message
            result_message = descriptor.out_message()

            # assign raw result to its wrapper, result_message
            out_type = descriptor.out_message._type_info

            if len(out_type) > 0:
                if len(out_type) == 1:
                    attr_name = descriptor.out_message._type_info.keys()[0]
                    setattr(result_message, attr_name, result_raw)
                else:
                    for i in range(len(out_type)):
                        attr_name = descriptor.out_message._type_info.keys()[i]
                        setattr(result_message, attr_name, result_raw[i])

            # transform the results into an element
            descriptor.out_message.to_xml(result_message, self.get_tns(),
                                                                    soap_body)
            # implementation hook
            service.on_method_return(req_env, result_raw, soap_body,
                                                            http_resp_headers)

            #
            # misc
            #
            results_str = etree.tostring(envelope, xml_declaration=True,
                                                     encoding=string_encoding)

            if descriptor.mtom:
                http_resp_headers, results_str = apply_mtom(http_resp_headers,
                               results_str, descriptor.out_message._type_info,
                               [result_raw])

            # implementation hook
            self.on_return(req_env, http_resp_headers, results_str)

            # initiate the response
            http_resp_headers['Content-Length'] = str(len(results_str))
            start_response(HTTP_200, http_resp_headers.items())

            if logger.level == logging.DEBUG:
                logger.debug('\033[31m'+ "Response" + '\033[0m')
                logger.debug(etree.tostring(envelope, xml_declaration=True,
                                                            pretty_print=True))
            # return the serialized results
            return [results_str]
示例#7
0
文件: wsgi.py 项目: yangkf1985/torweb
    def __handle_soap_request(self, req_env, start_response, url):
        """
        This function is too big.
        """

        http_resp_headers = {
            'Content-Type': 'text/xml',
            'Content-Length': '0',
        }
        method_name = None

        try:
            # implementation hook
            self.on_call(req_env)

            if req_env['REQUEST_METHOD'].lower() != 'post':
                http_resp_headers['Allow'] = 'POST'
                start_response(HTTP_405, http_resp_headers.items())
                return ['']

            input = req_env.get('wsgi.input')
            length = req_env.get("CONTENT_LENGTH")
            body = input.read(int(length))

            try:
                service = None
                soap_req_header, soap_req_payload = self.__decode_soap_request(
                                                                req_env, body)
                if not (soap_req_payload is None):
                    self.validate_request(soap_req_payload)

                method_name = self.__get_method_name(req_env, soap_req_payload)
                if method_name is None:
                    resp = "Could not extract method name from the request!"
                    http_resp_headers['Content-Length'] = str(len(resp))
                    start_response(HTTP_500, http_resp_headers.items())
                    return [resp]

                service_class = self.get_service_class(method_name)
                service = self.get_service(service_class, req_env)

            finally:
                # for performance reasons, we don't want the following to run
                # in production even though we won't see the results.
                if logger.level == logging.DEBUG:
                    try:
                        logger.debug(etree.tostring(etree.fromstring(body),
                                                             pretty_print=True))
                    except etree.XMLSyntaxError,e:
                        logger.debug(body)
                        raise Fault('Client.XMLSyntax', 'Error at line: %d, col: %d'
                                                                    % e.position)

            # retrieve the method descriptor
            descriptor = service.get_method(method_name)
            func = getattr(service, descriptor.name)

            # decode header object
            if soap_req_header is not None and len(soap_req_header) > 0:
                in_header = descriptor.in_header
                service.soap_in_header = in_header.from_xml(soap_req_header)

            # decode method arguments
            if soap_req_payload is not None and len(soap_req_payload) > 0:
                params = descriptor.in_message.from_xml(soap_req_payload)
            else:
                params = [None] * len(descriptor.in_message._type_info)

            # implementation hook
            service.on_method_call(req_env, method_name, params,
                                                               soap_req_payload)

            # call the method
            result_raw = service.call_wrapper(func, params)

            # construct the soap response, and serialize it
            envelope = etree.Element('{%s}Envelope' % soaplib.ns_soap_env,
                                                            nsmap=soaplib.nsmap)

            #
            # header
            #
            soap_header_elt = etree.SubElement(envelope,
                                             '{%s}Header' % soaplib.ns_soap_env)

            if service.soap_out_header != None:
                if descriptor.out_header is None:
                    logger.warning("Skipping soap response header as %r method "
                                   "is not published to have a soap response "
                                   "header" % method_name)
                else:
                    descriptor.out_header.to_xml(
                        service.soap_out_header,
                        self.get_tns(),
                        soap_header_elt,
                        descriptor.out_header.get_type_name()
                    )

            if len(soap_header_elt) > 0:
                envelope.append(soap_header_elt)

            #
            # body
            #
            soap_body = etree.SubElement(envelope,
                                               '{%s}Body' % soaplib.ns_soap_env)

            # instantiate the result message
            result_message = descriptor.out_message()

            # assign raw result to its wrapper, result_message
            out_type = descriptor.out_message._type_info

            if len(out_type) > 0:
                if len(out_type) == 1:
                    attr_name = descriptor.out_message._type_info.keys()[0]
                    setattr(result_message, attr_name, result_raw)
                else:
                    for i in range(len(out_type)):
                        attr_name = descriptor.out_message._type_info.keys()[i]
                        setattr(result_message, attr_name, result_raw[i])

            # transform the results into an element
            descriptor.out_message.to_xml(result_message, self.get_tns(),
                                                                      soap_body)

            # implementation hook
            service.on_method_return(req_env, result_raw, soap_body,
                                                              http_resp_headers)

            #
            # misc
            #
            results_str = etree.tostring(envelope, xml_declaration=True,
                                                       encoding=string_encoding)

            if descriptor.mtom:
                http_resp_headers, results_str = apply_mtom(http_resp_headers,
                    results_str, descriptor.out_message._type_info,[result_raw])

            # implementation hook
            self.on_return(req_env, http_resp_headers, results_str)

            # initiate the response
            http_resp_headers['Content-Length'] = str(len(results_str))
            start_response(HTTP_200, http_resp_headers.items())

            if logger.level == logging.DEBUG:
                logger.debug('\033[91m'+ "Response" + '\033[0m')
                logger.debug(etree.tostring(envelope, xml_declaration=True,
                                                             pretty_print=True))

            # return the serialized results
            return [results_str]
示例#8
0
    def __call__(self, environ, start_response):
        '''
        This method conforms to the WSGI spec for callable wsgi applications (PEP 333).
        This method looks in environ['wsgi.input'] for a fully formed soap request envelope,
        will deserialize the request parameters and call the method on the object returned
        by the getHandler() method.
        @param the http environment
        @param a callable that begins the response message
        @returns the string representation of the soap call
        '''
        methodname = ''
        try:
            reset_request()
            request.environ = environ
            
            # implementation hook
            self.onCall(environ)
            
            serviceName = environ['PATH_INFO'].split('/')[-1]
            service = self.getHandler(environ)
            if serviceName.lower().endswith('wsdl'):
                # get the wsdl for the service
                #
                # Assume path_info matches pattern
                # /stuff/stuff/stuff/serviceName.wsdl or ?WSDL
                #
                serviceName = serviceName.split('.')[0]
                url = reconstruct_url(environ).split('.wsdl')[0]
                
                start_response('200 OK',[('Content-type','text/xml')])
                try:
                    wsdl_content = service.wsdl(url)
                    
                    # implementation hook
                    self.onWsdl(environ,wsdl_content)
                except Exception, e:
                    
                    # implementation hook
                    buffer = cStringIO.StringIO()
                    traceback.print_exc(file=buffer)
                    buffer.seek(0)
                    stacktrace = str(buffer.read())
                    faultStr = ElementTree.tostring(make_soap_fault( str(e), detail=stacktrace), encoding=string_encoding)
                    
                    exceptions(faultStr)
                    
                    self.onWsdlException(environ,e,faultStr)
                    
                    # initiate the response
                    start_response('500',[('Content-type','text/xml'),('Content-length',str(len(faultStr)))])
                    return [faultStr]
                
                reset_request()
                return [wsdl_content]
            
            if environ['REQUEST_METHOD'].lower() != 'post':
                start_response('405 Method Not Allowed',[('Allow','POST')])
                return ''
                
            input = environ.get('wsgi.input')
            length = environ.get("CONTENT_LENGTH")
            body = input.read(int(length))
            debug(body)
            body = collapse_swa( environ.get("CONTENT_TYPE"), body)
            
            # deserialize the body of the message
            payload, header = from_soap(body)
            methodname = payload.tag.split('}')[-1]
            request.header = header
            
            # call the method
            func = getattr(service, methodname)
            
            # retrieve the method descriptor
            descriptor = func(_soap_descriptor=True)
            params = descriptor.inMessage.from_xml(*[payload])
            # implementation hook
            self.onMethodExec(environ,body,params,descriptor.inMessage.params)
            
            # call the method
            retval = func(*params)
            
            # transform the results into an element
            # only expect a single element
            results = None
            if not (descriptor.isAsync or descriptor.isCallback):
                
                results = descriptor.outMessage.to_xml(*[retval])

            # implementation hook
            self.onResults(environ,results,retval)
            
            # grab any headers that were included in the request
            response_headers = None
            if hasattr(request,'response_headers'):
                response_headers = request.response_headers
            
            # construct the soap response, and serialize it
            envelope = make_soap_envelope(results,tns=service.__tns__,header_elements=response_headers) 
            resp = ElementTree.tostring(envelope, encoding=string_encoding)
            headers = {'Content-Type': 'text/xml'}

            if descriptor.mtom:
                headers, resp = apply_mtom( headers, resp,
                                            descriptor.outMessage.params,
                                            (retval,) )

            
            if environ.has_key('CONTENT_LENGTH'):
                del(environ['CONTENT_LENGTH'])
                
            # initiate the response
            start_response('200 OK',headers.items())
            
            self.onReturn(environ,resp)
            
            debug(resp)
            
            # return the serialized results
            reset_request()
            return [resp]
示例#9
0
文件: wsgi.py 项目: ridha/soaplib
    def __handle_soap_request(self, req_env, start_response, url):
        http_resp_headers = {
            'Content-Type': 'text/xml',
            'Content-Length': '0',
        }
        method_name = None

        try:
            # implementation hook
            self.on_call(req_env)

            if req_env['REQUEST_METHOD'].lower() != 'post':
                http_resp_headers['Allow'] = 'POST'
                start_response(HTTP_405, http_resp_headers.items())
                return ['']

            input = req_env.get('wsgi.input')
            length = req_env.get("CONTENT_LENGTH")
            body = input.read(int(length))

            try:
                service = None
                soap_req_header, soap_req_payload = self.__decode_soap_request(
                                                                req_env, body)
                self.validate_request(soap_req_payload)

                method_name = self.__get_method_name(req_env, soap_req_payload)
                if method_name is None:
                    resp = "Could not get method name!"
                    http_resp_headers['Content-Length'] = str(len(resp))
                    start_response(HTTP_500, http_resp_headers.items())
                    return [resp]

                service_class = self.get_service_class(method_name)
                service = self.get_service(service_class, req_env)

            finally:
                # for performance reasons, we don't want the following to run
                # in production even if we don't see the results.
                if logger.level == logging.DEBUG:
                    try:
                        logger.debug(etree.tostring(etree.fromstring(body),
                                                             pretty_print=True))
                    except:
                        logger.debug(body)
                        raise

            # retrieve the method descriptor
            descriptor = service.get_method(method_name)
            func = getattr(service, descriptor.name)

            # decode header object
            if soap_req_header is not None and len(soap_req_header) > 0:
                service.soap_in_header = descriptor.in_header.from_xml(soap_req_header)

            # decode method arguments
            if soap_req_payload is not None and len(soap_req_payload) > 0:
                params = descriptor.in_message.from_xml(soap_req_payload)
            else:
                params = [None] * len(descriptor.in_message._type_info)

            # implementation hook
            service.on_method_call(req_env, method_name, params, soap_req_payload)

            # call the method
            result_raw = service.call_wrapper(func, params)

            # create result message
            result_message = descriptor.out_message()

            # assign raw result to its wrapper, result_message
            out_type = descriptor.out_message._type_info

            if len(out_type) > 0:
                assert len(out_type) == 1
                
                attr_name = descriptor.out_message._type_info.keys()[0]
                setattr(result_message, attr_name, result_raw)

            # transform the results into an element
            # only expect a single element
            soap_resp_body = None
            if not (descriptor.is_async or descriptor.is_callback):
                soap_resp_body = descriptor.out_message.to_xml(result_message,
                                                              self.get_tns())
            soap_resp_header = None
            if not (descriptor.out_header is None or service.soap_out_header is None):
                soap_resp_header = descriptor.out_header.to_xml(
                                        service.soap_out_header, self.get_tns(),
                                        descriptor.out_header.get_type_name())

            # implementation hook
            service.on_method_return(req_env, result_raw, soap_resp_body,
                                                            http_resp_headers)

            # construct the soap response, and serialize it
            envelope = make_soap_envelope(soap_resp_header, soap_resp_body)
            results_str = etree.tostring(envelope, xml_declaration=True,
                                                       encoding=string_encoding)

            if descriptor.mtom:
                http_resp_headers, results_str = apply_mtom(http_resp_headers,
                    results_str,descriptor.out_message._type_info,[result_raw])

            # implementation hook
            self.on_return(req_env, http_resp_headers, results_str)

            # initiate the response
            http_resp_headers['Content-Length'] = str(len(results_str))
            start_response(HTTP_200, http_resp_headers.items())

            if logger.level == logging.DEBUG:
                logger.debug('\033[91m'+ "Response" + '\033[0m')
                logger.debug(etree.tostring(envelope, xml_declaration=True,
                                                             pretty_print=True))

            # return the serialized results
            return [results_str]

        # The user issued a Fault, so handle it just like an exception!
        except Fault, e:
            stacktrace=traceback.format_exc()
            logger.error(stacktrace)
            
            # FIXME: There's no way to alter soap response headers for the user.

            soap_resp_header = None
            soap_resp_body = Fault.to_xml(e, self.get_tns())

            fault_xml = make_soap_envelope(soap_resp_header, soap_resp_body)
            fault_str = etree.tostring(fault_xml, xml_declaration=True,
                                                    encoding=string_encoding)
            if logger.level == logging.DEBUG:
                logger.debug(etree.tostring(etree.fromstring(fault_str),
                                                            pretty_print=True))

            # implementation hook
            if not (service is None):
                service.on_method_exception(req_env, e, fault_xml, fault_str)
            self.on_exception(req_env,e,fault_str)

            # initiate the response
            http_resp_headers['Content-Length'] = str(len(fault_str))
            start_response(HTTP_500, http_resp_headers.items())

            return [fault_str]
示例#10
0
class SOAPService(BaseHandler, SoapServiceBase):
    '''
    This is the base object representing a soap web application, and conforms
    to the WSGI specification (PEP 333).  This object should be overridden
    and getHandler(environ) overridden to provide the object implementing
    the specified functionality.  Hooks have been added so that the subclass
    can react to various events that happen durring the execution of the
    request.
    '''
    def __init__(self, application, request, **kwargs):
        setattr(self, "request", request)
        setattr(self, "application", application)
        BaseHandler.__init__(self, application, request, **kwargs)
        SoapServiceBase.__init__(self)

    def onCall(self, environ):
        '''
        This is the first method called when this WSGI app is invoked
        @param the wsgi environment
        '''
        pass

    def onWsdl(self, environ, wsdl):
        '''
        This is called when a wsdl is requested
        @param the wsgi environment
        @param the wsdl string 
        '''
        pass

    def onWsdlException(self, environ, exc, resp):
        '''
        Called when an exception occurs durring wsdl generation
        @param the wsgi environment
        @param exc the exception
        @param the fault response string
        '''
        pass

    def onMethodExec(self, environ, body, py_params, soap_params):
        '''
        Called BEFORE the service implementing the functionality is called
        @param the wsgi environment
        @param the body element of the soap request
        @param the tuple of python params being passed to the method
        @param the soap elements for each params
        '''
        pass

    def onResults(self, environ, py_results, soap_results):
        '''
        Called AFTER the service implementing the functionality is called
        @param the wsgi environment
        @param the python results from the method
        @param the xml serialized results of the method
        '''
        pass

    def onException(self, environ, exc, resp):
        '''
        Called when an error occurs durring execution
        @param the wsgi environment
        @param the exception
        @param the response string
        '''
        pass

    def onReturn(self, environ, returnString):
        '''
        Called before the application returns
        @param the wsgi environment
        @param return string of the soap request
        '''
        pass

    def getHandler(self, environ):
        '''
        This method returns the object responsible for processing a given request, and
        needs to be overridden by a subclass to handle the application specific
        mapping of the request to the appropriate handler.
        @param the wsgi environment
        @returns the object to be called for the soap operation
        '''
        return self

    def post(self):
        '''
        This method conforms to the WSGI spec for callable wsgi applications (PEP 333).
        This method looks in environ['wsgi.input'] for a fully formed soap request envelope,
        will deserialize the request parameters and call the method on the object returned
        by the getHandler() method.
        @param the http environment
        @param a callable that begins the response message
        @returns the string representation of the soap call
        '''
        methodname = ''
        container = tornado.wsgi.WSGIContainer(self.application)
        environ = container.environ(self.request)
        try:
            # implementation hook
            self.onCall(environ)

            serviceName = environ['PATH_INFO'].split('/')[-1]
            service = self.getHandler(environ)
            if (environ['QUERY_STRING'].endswith('wsdl')
                    or environ['PATH_INFO'].endswith('wsdl')
                ) and environ['REQUEST_METHOD'].lower() == 'get':
                # get the wsdl for the service
                #
                # Assume path_info matches pattern
                # /stuff/stuff/stuff/serviceName.wsdl or ?WSDL
                #
                serviceName = serviceName.split('.')[0]
                url = reconstruct_url(environ).split('.wsdl')[0]

                try:
                    wsdl_content = service.wsdl(url)

                    # implementation hook
                    self.onWsdl(environ, wsdl_content)
                except Exception, e:

                    # implementation hook
                    buffer = cStringIO.StringIO()
                    traceback.print_exc(file=buffer)
                    buffer.seek(0)
                    stacktrace = str(buffer.read())
                    faultStr = ElementTree.tostring(make_soap_fault(
                        str(e), detail=stacktrace),
                                                    encoding=string_encoding)

                    exceptions(faultStr)

                    self.onWsdlException(environ, e, faultStr)

                    # initiate the response
                    #return Response(faultStr, '500 Internal Server Error', [('Content-type','text/xml;charset=utf-8')])
                    self.set_header('Content-Type', 'text/xml;charset=utf-8')
                    return self.write(faultStr)

                #return Response(wsdl_content, '200 OK', [('Content-type','text/xml;charset=utf-8')])
                self.set_header('Content-Type', 'text/xml;charset=utf-8')
                return self.write(wsdl_content)
            if environ['REQUEST_METHOD'].lower() != 'post':
                #return Response('', '405 Method Not Allowed',[('Allow','POST')])
                self.set_header('Content-Type', 'text/html;charset=utf-8')
                return self.write_error(status_code=405)

            input = environ.get('wsgi.input')
            length = environ.get("CONTENT_LENGTH")
            body = input.read(int(length))
            debug(body)
            # body, _unmentioned_attachs = collapse_swa( environ.get("CONTENT_TYPE"), body)
            # collapse_swa has some problem
            try:
                body, _unmentioned_attachs = collapse_swa(
                    environ.get("CONTENT_TYPE"), body)
            except:
                body = collapse_swa(environ.get("CONTENT_TYPE"), body)
                _unmentioned_attachs = []
                pass
            # deserialize the body of the message
            try:
                payload, header = from_soap(body)
            except SyntaxError, e:
                payload = None
                header = None

            if payload is not None:
                methodname = payload.tag.split('}')[-1]
            else:
                # check HTTP_SOAPACTION
                methodname = environ.get("HTTP_SOAPACTION")
                if methodname.startswith('"') and methodname.endswith('"'):
                    methodname = methodname[1:-1]
                if methodname.find('/') > 0:
                    methodname = methodname.split('/')[1]

            # call the method
            func = getattr(service, methodname)

            # retrieve the method descriptor
            descriptor = func(_soap_descriptor=True, klazz=service.__class__)
            if payload is not None:
                params = descriptor.inMessage.from_xml(*[payload])
            else:
                params = ()
            # implementation hook
            self.onMethodExec(environ, body, params,
                              descriptor.inMessage.params)

            # call the method
            if len(_unmentioned_attachs) > 0:
                retval = func(*params,
                              unmentioned_attachs=_unmentioned_attachs)
            else:
                retval = func(*params)

            # transform the results into an element
            # only expect a single element
            results = None
            if not (descriptor.isAsync or descriptor.isCallback):
                results = descriptor.outMessage.to_xml(*[retval])

            # implementation hook
            self.onResults(environ, results, retval)

            # construct the soap response, and serialize it
            envelope = make_soap_envelope(results, tns=service.__tns__)
            #ElementTree.cleanup_namespaces(envelope)
            resp = ElementTree.tostring(envelope, encoding=string_encoding)
            headers = {'Content-Type': 'text/xml;charset=utf-8'}

            if descriptor.mtom:
                headers, resp = apply_mtom(headers, resp,
                                           descriptor.outMessage.params,
                                           (retval, ))

            resp = '<?xml version="1.0" encoding="utf-8"?>' + resp
            if environ.has_key('CONTENT_LENGTH'):
                del (environ['CONTENT_LENGTH'])

            self.onReturn(environ, resp)
            debug(resp)

            # return the serialized results
            #return Response(resp, '200 OK', headers.items())
            self.set_header('Content-Type', 'text/xml;charset=utf-8')
            return self.write(resp)