<s> """<STR_LIT>""" <EOL> ident = '<STR_LIT>' <EOL> from version import __version__ <EOL> import re <EOL> import socket <EOL> import sys <EOL> import SocketServer <EOL> from types import * <EOL> import BaseHTTPServer <EOL> import thread <EOL> from Parser import parseSOAPRPC <EOL> from Config import Config <EOL> from Types import faultType , voidType , simplify <EOL> from NS import NS <EOL> from SOAPBuilder import buildSOAP <EOL> from Utilities import debugHeader , debugFooter <EOL> try : from M2Crypto import SSL <EOL> except : pass <EOL> ident = '<STR_LIT>' <EOL> from version import __version__ <EOL> _contexts = dict ( ) <EOL> def GetSOAPContext ( ) : <EOL> global _contexts <EOL> return _contexts [ thread . get_ident ( ) ] <EOL> class MethodSig : <EOL> def __init__ ( self , func , keywords = <NUM_LIT:0> , context = <NUM_LIT:0> ) : <EOL> self . func = func <EOL> self . keywords = keywords <EOL> self . context = context <EOL> self . __name__ = func . __name__ <EOL> def __call__ ( self , * args , ** kw ) : <EOL> return apply ( self . func , args , kw ) <EOL> class SOAPContext : <EOL> def __init__ ( self , header , body , attrs , xmldata , connection , httpheaders , <EOL> soapaction ) : <EOL> self . header = header <EOL> self . body = body <EOL> self . attrs = attrs <EOL> self . xmldata = xmldata <EOL> self . connection = connection <EOL> self . httpheaders = httpheaders <EOL> self . soapaction = soapaction <EOL> class HeaderHandler : <EOL> def __init__ ( self , header , attrs ) : <EOL> for i in header . __dict__ . keys ( ) : <EOL> if i [ <NUM_LIT:0> ] == "<STR_LIT:_>" : <EOL> continue <EOL> d = getattr ( header , i ) <EOL> try : <EOL> fault = int ( attrs [ id ( d ) ] [ ( NS . ENV , '<STR_LIT>' ) ] ) <EOL> except : <EOL> fault = <NUM_LIT:0> <EOL> if fault : <EOL> raise faultType , ( "<STR_LIT>" % NS . ENV_T , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:%s>" % i ) <EOL> class SOAPServerBase : <EOL> def get_request ( self ) : <EOL> sock , addr = SocketServer . TCPServer . get_request ( self ) <EOL> if self . ssl_context : <EOL> sock = SSL . Connection ( self . ssl_context , sock ) <EOL> sock . _setup_ssl ( addr ) <EOL> if sock . accept_ssl ( ) != <NUM_LIT:1> : <EOL> raise socket . error , "<STR_LIT>" <EOL> return sock , addr <EOL> def registerObject ( self , object , namespace = '<STR_LIT>' , path = '<STR_LIT>' ) : <EOL> if namespace == '<STR_LIT>' and path == '<STR_LIT>' : namespace = self . namespace <EOL> if namespace == '<STR_LIT>' and path != '<STR_LIT>' : <EOL> namespace = path . replace ( "<STR_LIT:/>" , "<STR_LIT::>" ) <EOL> if namespace [ <NUM_LIT:0> ] == "<STR_LIT::>" : namespace = namespace [ <NUM_LIT:1> : ] <EOL> self . objmap [ namespace ] = object <EOL> def registerFunction ( self , function , namespace = '<STR_LIT>' , funcName = None , <EOL> path = '<STR_LIT>' ) : <EOL> if not funcName : funcName = function . __name__ <EOL> if namespace == '<STR_LIT>' and path == '<STR_LIT>' : namespace = self . namespace <EOL> if namespace == '<STR_LIT>' and path != '<STR_LIT>' : <EOL> namespace = path . replace ( "<STR_LIT:/>" , "<STR_LIT::>" ) <EOL> if namespace [ <NUM_LIT:0> ] == "<STR_LIT::>" : namespace = namespace [ <NUM_LIT:1> : ] <EOL> if self . funcmap . has_key ( namespace ) : <EOL> self . funcmap [ namespace ] [ funcName ] = function <EOL> else : <EOL> self . funcmap [ namespace ] = { funcName : function } <EOL> def registerKWObject ( self , object , namespace = '<STR_LIT>' , path = '<STR_LIT>' ) : <EOL> if namespace == '<STR_LIT>' and path == '<STR_LIT>' : namespace = self . namespace <EOL> if namespace == '<STR_LIT>' and path != '<STR_LIT>' : <EOL> namespace = path . replace ( "<STR_LIT:/>" , "<STR_LIT::>" ) <EOL> if namespace [ <NUM_LIT:0> ] == "<STR_LIT::>" : namespace = namespace [ <NUM_LIT:1> : ] <EOL> for i in dir ( object . __class__ ) : <EOL> if i [ <NUM_LIT:0> ] != "<STR_LIT:_>" and callable ( getattr ( object , i ) ) : <EOL> self . registerKWFunction ( getattr ( object , i ) , namespace ) <EOL> def registerKWFunction ( self , function , namespace = '<STR_LIT>' , funcName = None , <EOL> path = '<STR_LIT>' ) : <EOL> if namespace == '<STR_LIT>' and path == '<STR_LIT>' : namespace = self . namespace <EOL> if namespace == '<STR_LIT>' and path != '<STR_LIT>' : <EOL> namespace = path . replace ( "<STR_LIT:/>" , "<STR_LIT::>" ) <EOL> if namespace [ <NUM_LIT:0> ] == "<STR_LIT::>" : namespace = namespace [ <NUM_LIT:1> : ] <EOL> self . registerFunction ( MethodSig ( function , keywords = <NUM_LIT:1> ) , namespace , <EOL> funcName ) <EOL> def unregisterObject ( self , object , namespace = '<STR_LIT>' , path = '<STR_LIT>' ) : <EOL> if namespace == '<STR_LIT>' and path == '<STR_LIT>' : namespace = self . namespace <EOL> if namespace == '<STR_LIT>' and path != '<STR_LIT>' : <EOL> namespace = path . replace ( "<STR_LIT:/>" , "<STR_LIT::>" ) <EOL> if namespace [ <NUM_LIT:0> ] == "<STR_LIT::>" : namespace = namespace [ <NUM_LIT:1> : ] <EOL> del self . objmap [ namespace ] <EOL> class SOAPRequestHandler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> def version_string ( self ) : <EOL> return '<STR_LIT>' + '<STR_LIT>' + __version__ + '<STR_LIT>' + sys . version . split ( ) [ <NUM_LIT:0> ] + '<STR_LIT:)>' <EOL> def date_time_string ( self ) : <EOL> self . __last_date_time_string = BaseHTTPServer . BaseHTTPRequestHandler . date_time_string ( self ) <EOL> return self . __last_date_time_string <EOL> def do_POST ( self ) : <EOL> global _contexts <EOL> status = <NUM_LIT> <EOL> try : <EOL> if self . server . config . dumpHeadersIn : <EOL> s = '<STR_LIT>' <EOL> debugHeader ( s ) <EOL> print self . raw_requestline . strip ( ) <EOL> print "<STR_LIT:\n>" . join ( map ( lambda x : x . strip ( ) , <EOL> self . headers . headers ) ) <EOL> debugFooter ( s ) <EOL> data = self . rfile . read ( int ( self . headers [ "<STR_LIT>" ] ) ) <EOL> if self . server . config . dumpSOAPIn : <EOL> s = '<STR_LIT>' <EOL> debugHeader ( s ) <EOL> print data , <EOL> if data [ - <NUM_LIT:1> ] != '<STR_LIT:\n>' : <EOL> print <EOL> debugFooter ( s ) <EOL> ( r , header , body , attrs ) = parseSOAPRPC ( data , header = <NUM_LIT:1> , body = <NUM_LIT:1> , attrs = <NUM_LIT:1> ) <EOL> method = r . _name <EOL> args = r . _aslist ( ) <EOL> kw = r . _asdict ( ) <EOL> if Config . simplify_objects : <EOL> args = simplify ( args ) <EOL> kw = simplify ( kw ) <EOL> if Config . specialArgs : <EOL> ordered_args = { } <EOL> named_args = { } <EOL> for ( k , v ) in kw . items ( ) : <EOL> if k [ <NUM_LIT:0> ] == "<STR_LIT:v>" : <EOL> try : <EOL> i = int ( k [ <NUM_LIT:1> : ] ) <EOL> ordered_args [ i ] = v <EOL> except ValueError : <EOL> named_args [ str ( k ) ] = v <EOL> else : <EOL> named_args [ str ( k ) ] = v <EOL> ns = r . _ns <EOL> if len ( self . path ) > <NUM_LIT:1> and not ns : <EOL> ns = self . path . replace ( "<STR_LIT:/>" , "<STR_LIT::>" ) <EOL> if ns [ <NUM_LIT:0> ] == "<STR_LIT::>" : ns = ns [ <NUM_LIT:1> : ] <EOL> a = None <EOL> keylist = ordered_args . keys ( ) <EOL> keylist . sort ( ) <EOL> tmp = map ( lambda x : ordered_args [ x ] , keylist ) <EOL> ordered_args = tmp <EOL> resp = "<STR_LIT>" <EOL> if ns : <EOL> nsmethod = "<STR_LIT>" % ( ns , method ) <EOL> else : <EOL> nsmethod = method <EOL> try : <EOL> if self . server . funcmap . has_key ( ns ) and self . server . funcmap [ ns ] . has_key ( method ) : <EOL> f = self . server . funcmap [ ns ] [ method ] <EOL> if self . server . config . authMethod != None : <EOL> authmethod = self . server . config . authMethod <EOL> if self . server . funcmap . has_key ( ns ) and self . server . funcmap [ ns ] . has_key ( authmethod ) : <EOL> a = self . server . funcmap [ ns ] [ authmethod ] <EOL> else : <EOL> f = self . server . objmap [ ns ] <EOL> if self . server . config . authMethod != None : <EOL> authmethod = self . server . config . authMethod <EOL> if hasattr ( f , authmethod ) : <EOL> a = getattr ( f , authmethod ) <EOL> l = method . split ( "<STR_LIT:.>" ) <EOL> for i in l : <EOL> f = getattr ( f , i ) <EOL> except : <EOL> info = sys . exc_info ( ) <EOL> try : <EOL> resp = buildSOAP ( faultType ( "<STR_LIT>" % NS . ENV_T , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % ( nsmethod , <EOL> info [ <NUM_LIT:0> ] , <EOL> info [ <NUM_LIT:1> ] , <EOL> info [ <NUM_LIT:2> ] ) ) , <EOL> encoding = self . server . encoding , <EOL> config = self . server . config ) <EOL> finally : <EOL> del info <EOL> status = <NUM_LIT> <EOL> else : <EOL> try : <EOL> if header : <EOL> x = HeaderHandler ( header , attrs ) <EOL> fr = <NUM_LIT:1> <EOL> if "<STR_LIT>" . lower ( ) not in self . headers . keys ( ) or self . headers [ "<STR_LIT>" ] == "<STR_LIT>" : <EOL> self . headers [ "<STR_LIT>" ] = method <EOL> thread_id = thread . get_ident ( ) <EOL> _contexts [ thread_id ] = SOAPContext ( header , body , <EOL> attrs , data , <EOL> self . connection , <EOL> self . headers , <EOL> self . headers [ "<STR_LIT>" ] ) <EOL> if a != None : <EOL> if not apply ( a , ( ) , { "<STR_LIT>" : <EOL> _contexts [ thread_id ] } ) : <EOL> raise faultType ( "<STR_LIT>" % NS . ENV_T , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:%s>" % nsmethod ) <EOL> if isinstance ( f , MethodSig ) : <EOL> c = None <EOL> if f . context : <EOL> c = _contexts [ thread_id ] <EOL> if Config . specialArgs : <EOL> if c : <EOL> named_args [ "<STR_LIT>" ] = c <EOL> fr = apply ( f , ordered_args , named_args ) <EOL> elif f . keywords : <EOL> strkw = { } <EOL> for ( k , v ) in kw . items ( ) : <EOL> strkw [ str ( k ) ] = v <EOL> if c : <EOL> strkw [ "<STR_LIT>" ] = c <EOL> fr = apply ( f , ( ) , strkw ) <EOL> elif c : <EOL> fr = apply ( f , args , { '<STR_LIT>' : c } ) <EOL> else : <EOL> fr = apply ( f , args , { } ) <EOL> else : <EOL> if Config . specialArgs : <EOL> fr = apply ( f , ordered_args , named_args ) <EOL> else : <EOL> fr = apply ( f , args , { } ) <EOL> if type ( fr ) == type ( self ) and isinstance ( fr , voidType ) : <EOL> resp = buildSOAP ( kw = { '<STR_LIT>' % method : fr } , <EOL> encoding = self . server . encoding , <EOL> config = self . server . config ) <EOL> else : <EOL> resp = buildSOAP ( kw = <EOL> { '<STR_LIT>' % method : { '<STR_LIT>' : fr } } , <EOL> encoding = self . server . encoding , <EOL> config = self . server . config ) <EOL> if _contexts . has_key ( thread_id ) : <EOL> del _contexts [ thread_id ] <EOL> except Exception , e : <EOL> import traceback <EOL> info = sys . exc_info ( ) <EOL> try : <EOL> if self . server . config . dumpFaultInfo : <EOL> s = '<STR_LIT>' % nsmethod <EOL> debugHeader ( s ) <EOL> traceback . print_exception ( info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] , <EOL> info [ <NUM_LIT:2> ] ) <EOL> debugFooter ( s ) <EOL> if isinstance ( e , faultType ) : <EOL> f = e <EOL> else : <EOL> f = faultType ( "<STR_LIT>" % NS . ENV_T , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:%s>" % nsmethod ) <EOL> if self . server . config . returnFaultInfo : <EOL> f . _setDetail ( "<STR_LIT>" . join ( traceback . format_exception ( <EOL> info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] , info [ <NUM_LIT:2> ] ) ) ) <EOL> elif not hasattr ( f , '<STR_LIT>' ) : <EOL> f . _setDetail ( "<STR_LIT>" % ( info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] ) ) <EOL> finally : <EOL> del info <EOL> resp = buildSOAP ( f , encoding = self . server . encoding , <EOL> config = self . server . config ) <EOL> status = <NUM_LIT> <EOL> else : <EOL> status = <NUM_LIT:200> <EOL> except faultType , e : <EOL> import traceback <EOL> info = sys . exc_info ( ) <EOL> try : <EOL> if self . server . config . dumpFaultInfo : <EOL> s = '<STR_LIT>' <EOL> debugHeader ( s ) <EOL> traceback . print_exception ( info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] , <EOL> info [ <NUM_LIT:2> ] ) <EOL> debugFooter ( s ) <EOL> if self . server . config . returnFaultInfo : <EOL> e . _setDetail ( "<STR_LIT>" . join ( traceback . format_exception ( <EOL> info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] , info [ <NUM_LIT:2> ] ) ) ) <EOL> elif not hasattr ( e , '<STR_LIT>' ) : <EOL> e . _setDetail ( "<STR_LIT>" % ( info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] ) ) <EOL> finally : <EOL> del info <EOL> resp = buildSOAP ( e , encoding = self . server . encoding , <EOL> config = self . server . config ) <EOL> status = <NUM_LIT> <EOL> except Exception , e : <EOL> if self . server . config . dumpFaultInfo : <EOL> s = '<STR_LIT>' % e <EOL> import traceback <EOL> debugHeader ( s ) <EOL> info = sys . exc_info ( ) <EOL> try : <EOL> traceback . print_exception ( info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] , info [ <NUM_LIT:2> ] ) <EOL> finally : <EOL> del info <EOL> debugFooter ( s ) <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . end_headers ( ) <EOL> if self . server . config . dumpHeadersOut and self . request_version != '<STR_LIT>' : <EOL> s = '<STR_LIT>' <EOL> debugHeader ( s ) <EOL> if self . responses . has_key ( status ) : <EOL> s = '<STR_LIT:U+0020>' + self . responses [ status ] [ <NUM_LIT:0> ] <EOL> else : <EOL> s = '<STR_LIT>' <EOL> print "<STR_LIT>" % ( self . protocol_version , <NUM_LIT> , s ) <EOL> print "<STR_LIT>" , self . version_string ( ) <EOL> print "<STR_LIT>" , self . __last_date_time_string <EOL> debugFooter ( s ) <EOL> else : <EOL> self . send_response ( status ) <EOL> t = '<STR_LIT>' ; <EOL> if self . server . encoding != None : <EOL> t += '<STR_LIT>' % self . server . encoding <EOL> self . send_header ( "<STR_LIT>" , t ) <EOL> self . send_header ( "<STR_LIT>" , str ( len ( resp ) ) ) <EOL> self . end_headers ( ) <EOL> if self . server . config . dumpHeadersOut and self . request_version != '<STR_LIT>' : <EOL> s = '<STR_LIT>' <EOL> debugHeader ( s ) <EOL> if self . responses . has_key ( status ) : <EOL> s = '<STR_LIT:U+0020>' + self . responses [ status ] [ <NUM_LIT:0> ] <EOL> else : <EOL> s = '<STR_LIT>' <EOL> print "<STR_LIT>" % ( self . protocol_version , status , s ) <EOL> print "<STR_LIT>" , self . version_string ( ) <EOL> print "<STR_LIT>" , self . __last_date_time_string <EOL> print "<STR_LIT>" , t <EOL> print "<STR_LIT>" , len ( resp ) <EOL> debugFooter ( s ) <EOL> if self . server . config . dumpSOAPOut : <EOL> s = '<STR_LIT>' <EOL> debugHeader ( s ) <EOL> print resp , <EOL> if resp [ - <NUM_LIT:1> ] != '<STR_LIT:\n>' : <EOL> print <EOL> debugFooter ( s ) <EOL> self . wfile . write ( resp ) <EOL> self . wfile . flush ( ) <EOL> if self . server . config . SSLserver and isinstance ( self . connection , SSL . Connection ) : <EOL> self . connection . set_shutdown ( SSL . SSL_SENT_SHUTDOWN | <EOL> SSL . SSL_RECEIVED_SHUTDOWN ) <EOL> else : <EOL> self . connection . shutdown ( <NUM_LIT:1> ) <EOL> def do_GET ( self ) : <EOL> path = self . path . lower ( ) <EOL> if path . endswith ( '<STR_LIT>' ) : <EOL> method = '<STR_LIT>' <EOL> function = namespace = None <EOL> if self . server . funcmap . has_key ( namespace ) and self . server . funcmap [ namespace ] . has_key ( method ) : <EOL> function = self . server . funcmap [ namespace ] [ method ] <EOL> else : <EOL> if namespace in self . server . objmap . keys ( ) : <EOL> function = self . server . objmap [ namespace ] <EOL> l = method . split ( "<STR_LIT:.>" ) <EOL> for i in l : <EOL> function = getattr ( function , i ) <EOL> if function : <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> self . end_headers ( ) <EOL> response = apply ( function , ( ) ) <EOL> self . wfile . write ( str ( response ) ) <EOL> return <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( '''<STR_LIT>''' ) <EOL> def log_message ( self , format , * args ) : <EOL> if self . server . log : <EOL> BaseHTTPServer . BaseHTTPRequestHandler . log_message ( self , format , * args ) <EOL> class SOAPServer ( SOAPServerBase , SocketServer . TCPServer ) : <EOL> def __init__ ( self , addr = ( '<STR_LIT:localhost>' , <NUM_LIT> ) , <EOL> RequestHandler = SOAPRequestHandler , log = <NUM_LIT:0> , encoding = '<STR_LIT>' , <EOL> config = Config , namespace = None , ssl_context = None ) : <EOL> if encoding != None : <EOL> '<STR_LIT>' . encode ( encoding ) <EOL> if ssl_context != None and not config . SSLserver : <EOL> raise AttributeError , "<STR_LIT>" <EOL> self . namespace = namespace <EOL> self . objmap = { } <EOL> self . funcmap = { } <EOL> self . ssl_context = ssl_context <EOL> self . encoding = encoding <EOL> self . config = config <EOL> self . log = log <EOL> self . allow_reuse_address = <NUM_LIT:1> <EOL> SocketServer . TCPServer . __init__ ( self , addr , RequestHandler ) <EOL> class ThreadingSOAPServer ( SOAPServerBase , SocketServer . ThreadingTCPServer ) : <EOL> def __init__ ( self , addr = ( '<STR_LIT:localhost>' , <NUM_LIT> ) , <EOL> RequestHandler = SOAPRequestHandler , log = <NUM_LIT:0> , encoding = '<STR_LIT>' , <EOL> config = Config , namespace = None , ssl_context = None ) : <EOL> if encoding != None : <EOL> '<STR_LIT>' . encode ( encoding ) <EOL> if ssl_context != None and not config . SSLserver : <EOL> raise AttributeError , "<STR_LIT>" <EOL> self . namespace = namespace <EOL> self . objmap = { } <EOL> self . funcmap = { } <EOL> self . ssl_context = ssl_context <EOL> self . encoding = encoding <EOL> self . config = config <EOL> self . log = log <EOL> self . allow_reuse_address = <NUM_LIT:1> <EOL> SocketServer . ThreadingTCPServer . __init__ ( self , addr , RequestHandler ) <EOL> if hasattr ( socket , "<STR_LIT>" ) : <EOL> class SOAPUnixSocketServer ( SOAPServerBase , SocketServer . UnixStreamServer ) : <EOL> def __init__ ( self , addr = <NUM_LIT> , <EOL> RequestHandler = SOAPRequestHandler , log = <NUM_LIT:0> , encoding = '<STR_LIT>' , <EOL> config = Config , namespace = None , ssl_context = None ) : <EOL> if encoding != None : <EOL> '<STR_LIT>' . encode ( encoding ) <EOL> if ssl_context != None and not config . SSLserver : <EOL> raise AttributeError , "<STR_LIT>" <EOL> self . namespace = namespace <EOL> self . objmap = { } <EOL> self . funcmap = { } <EOL> self . ssl_context = ssl_context <EOL> self . encoding = encoding <EOL> self . config = config <EOL> self . log = log <EOL> self . allow_reuse_address = <NUM_LIT:1> <EOL> SocketServer . UnixStreamServer . __init__ ( self , str ( addr ) , RequestHandler ) </s>
