<s> """<STR_LIT>""" <EOL> import urlparse <EOL> import xmlrpclib <EOL> from base64 import b64encode <EOL> from zope . interface import classProvides <EOL> from twisted . internet import defer <EOL> from twisted . internet import error as neterror <EOL> from twisted . web import error as weberror <EOL> from twisted . web . client import HTTPClientFactory <EOL> from twisted . python . util import InsensitiveDict <EOL> try : <EOL> import uuid <EOL> except ImportError : <EOL> uuid = None <EOL> from nagcat import errors , query <EOL> import coil <EOL> class HTTPQuery ( query . Query ) : <EOL> """<STR_LIT>""" <EOL> classProvides ( query . IQuery ) <EOL> scheme = "<STR_LIT:http>" <EOL> name = "<STR_LIT:http>" <EOL> port = <NUM_LIT> <EOL> def __init__ ( self , nagcat , conf ) : <EOL> super ( HTTPQuery , self ) . __init__ ( nagcat , conf ) <EOL> self . agent = "<STR_LIT>" <EOL> self . conf [ '<STR_LIT>' ] = self . addr <EOL> self . conf [ '<STR_LIT:port>' ] = int ( conf . get ( '<STR_LIT:port>' , self . port ) ) <EOL> self . conf [ '<STR_LIT:path>' ] = conf . get ( '<STR_LIT:path>' , '<STR_LIT:/>' ) <EOL> self . conf [ '<STR_LIT:data>' ] = conf . get ( '<STR_LIT:data>' , None ) <EOL> headers = conf . get ( '<STR_LIT>' , { } ) <EOL> if headers : <EOL> headers . expand ( ) <EOL> if not self . conf [ '<STR_LIT:path>' ] . startswith ( '<STR_LIT:/>' ) : <EOL> self . conf [ '<STR_LIT:path>' ] = '<STR_LIT>' % self . conf [ '<STR_LIT:path>' ] <EOL> if self . conf [ '<STR_LIT:port>' ] == self . port : <EOL> self . headers_host = self . host <EOL> else : <EOL> self . headers_host = "<STR_LIT>" % ( self . host , self . conf [ '<STR_LIT:port>' ] ) <EOL> self . headers = InsensitiveDict ( ) <EOL> for ( key , val ) in headers . iteritems ( ) : <EOL> if key . lower ( ) == '<STR_LIT:host>' : <EOL> self . headers_host = val <EOL> else : <EOL> self . headers [ key ] = val <EOL> user = conf . get ( '<STR_LIT:username>' , None ) <EOL> if user is not None : <EOL> auth = b64encode ( "<STR_LIT>" % ( user , conf [ '<STR_LIT:password>' ] ) ) <EOL> self . headers [ '<STR_LIT>' ] = "<STR_LIT>" % auth <EOL> self . conf [ '<STR_LIT>' ] = InsensitiveDict ( preserve = <NUM_LIT:0> ) <EOL> self . conf [ '<STR_LIT>' ] [ '<STR_LIT:host>' ] = self . headers_host <EOL> self . conf [ '<STR_LIT>' ] . update ( self . headers ) <EOL> if self . conf [ '<STR_LIT:data>' ] is not None : <EOL> method = "<STR_LIT:POST>" <EOL> else : <EOL> method = "<STR_LIT:GET>" <EOL> self . conf [ '<STR_LIT>' ] = conf . get ( '<STR_LIT>' , method ) <EOL> self . request_url = urlparse . urlunsplit ( ( self . scheme , <EOL> self . headers_host , self . conf [ '<STR_LIT:path>' ] , None , None ) ) <EOL> def _start ( self ) : <EOL> self . saved [ '<STR_LIT>' ] = self . request_url <EOL> if uuid : <EOL> request_id = str ( uuid . uuid1 ( ) ) <EOL> self . saved [ '<STR_LIT>' ] = request_id <EOL> self . headers [ '<STR_LIT>' ] = request_id <EOL> factory = HTTPClientFactory ( url = self . conf [ '<STR_LIT:path>' ] , <EOL> method = self . conf [ '<STR_LIT>' ] , postdata = self . conf [ '<STR_LIT:data>' ] , <EOL> headers = self . headers , agent = self . agent , <EOL> timeout = self . conf [ '<STR_LIT>' ] , followRedirect = <NUM_LIT:0> ) <EOL> factory . host = self . headers_host <EOL> factory . noisy = False <EOL> factory . deferred . addErrback ( self . _failure_tcp ) <EOL> factory . deferred . addErrback ( self . _failure_http ) <EOL> self . _connect ( factory ) <EOL> return factory . deferred <EOL> @ errors . callback <EOL> def _failure_http ( self , result ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( result . value , defer . TimeoutError ) : <EOL> raise errors . TestCritical ( "<STR_LIT>" ) <EOL> elif isinstance ( result . value , neterror . ConnectionDone ) : <EOL> raise errors . TestCritical ( "<STR_LIT>" ) <EOL> elif isinstance ( result . value , weberror . PageRedirect ) : <EOL> result = "<STR_LIT>" % ( result . value , result . value . location ) <EOL> elif isinstance ( result . value , weberror . Error ) : <EOL> raise errors . TestCritical ( "<STR_LIT>" % result . value ) <EOL> return result <EOL> class HTTPSQuery ( query . SSLMixin , HTTPQuery ) : <EOL> """<STR_LIT>""" <EOL> classProvides ( query . IQuery ) <EOL> scheme = "<STR_LIT>" <EOL> name = "<STR_LIT>" <EOL> port = <NUM_LIT> <EOL> class XMLRPCQuery ( HTTPQuery ) : <EOL> """<STR_LIT>""" <EOL> classProvides ( query . IQuery ) <EOL> scheme = "<STR_LIT:http>" <EOL> name = "<STR_LIT>" <EOL> def __init__ ( self , nagcat , conf ) : <EOL> conf . setdefault ( '<STR_LIT:path>' , '<STR_LIT>' ) <EOL> params = conf . get ( '<STR_LIT>' , conf . get ( '<STR_LIT>' , [ ] ) ) <EOL> if isinstance ( params , list ) : <EOL> params = tuple ( params ) <EOL> elif isinstance ( params , coil . struct . Struct ) : <EOL> params = ( params . dict ( ) , ) <EOL> else : <EOL> params = ( params , ) <EOL> conf [ '<STR_LIT:data>' ] = xmlrpclib . dumps ( params , conf [ '<STR_LIT>' ] ) <EOL> conf [ '<STR_LIT>' ] = '<STR_LIT:POST>' <EOL> super ( XMLRPCQuery , self ) . __init__ ( nagcat , conf ) <EOL> self . conf [ '<STR_LIT:result>' ] = conf . get ( '<STR_LIT:result>' , '<STR_LIT:value>' ) <EOL> if self . conf [ '<STR_LIT:result>' ] not in ( '<STR_LIT:value>' , '<STR_LIT>' ) : <EOL> raise errors . InitError ( "<STR_LIT>" ) <EOL> @ errors . callback <EOL> def _xmlrpc_failure ( self , result ) : <EOL> try : <EOL> value = xmlrpclib . loads ( result ) <EOL> except xmlrpclib . Fault , ex : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ( <EOL> ex . faultCode , ex . faultString ) ) <EOL> except xmlrpclib . Error , ex : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ( ex , ) ) <EOL> if self . conf [ '<STR_LIT:result>' ] == '<STR_LIT:value>' : <EOL> return str ( value [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return result <EOL> def _start ( self ) : <EOL> d = super ( XMLRPCQuery , self ) . _start ( ) <EOL> d . addCallback ( self . _xmlrpc_failure ) <EOL> return d <EOL> class XMLRPCSQuery ( query . SSLMixin , XMLRPCQuery ) : <EOL> """<STR_LIT>""" <EOL> classProvides ( query . IQuery ) <EOL> scheme = "<STR_LIT>" <EOL> name = "<STR_LIT>" <EOL> port = <NUM_LIT> </s>
