def do_POST(self): '''The POST command. This is called by HTTPServer, not twisted. action -- SOAPAction(HTTP header) or wsa:Action(SOAP:Header) ''' global _contexts soapAction = self.headers.getheader('SOAPAction') post = self.path if not post: raise PostNotSpecified, 'HTTP POST not specified in request' if soapAction: soapAction = soapAction.strip('\'"') post = post.strip('\'"') try: ct = self.headers['content-type'] if ct.startswith('multipart/'): cid = resolvers.MIMEResolver(ct, self.rfile) xml = cid.GetSOAPPart() ps = ParsedSoap(xml, resolver=cid.Resolve, readerclass=DomletteReader) else: length = int(self.headers['content-length']) ps = ParsedSoap(self.rfile.read(length), readerclass=DomletteReader) except ParseException, e: self.send_fault(FaultFromZSIException(e))
def test_local_attribute1(self): """ """ self.types_module xml = """<?xml version="1.0" encoding="UTF-8"?> <holder xmlns='urn:subGroup:types'> <baseElt><base>from base</base></baseElt> <childElt><base>from base</base><child>from child</child></childElt></holder>""" ps = ParsedSoap(xml, envelope=False) p1 = ps.Parse(GED("urn:subGroup:types", "holder")) b1 = p1.BaseElt[0] c1 = p1.BaseElt[1] sw = SoapWriter(envelope=False) sw.serialize(p1) ps = ParsedSoap(str(sw), envelope=False) p2 = ps.Parse(GED("urn:subGroup:types", "holder")) b2 = p2.BaseElt[0] c2 = p2.BaseElt[1] self.assertEqual(b1.Base, b2.Base) self.assertEqual(c1.Base, c2.Base) self.assertEqual(c1.Child, c2.Child)
def do_POST(self): '''The POST command. This is called by HTTPServer, not twisted. action -- SOAPAction(HTTP header) or wsa:Action(SOAP:Header) ''' global _contexts soapAction = self.headers.getheader('SOAPAction') post = self.path if not post: raise PostNotSpecified('HTTP POST not specified in request') if soapAction: soapAction = soapAction.strip('\'"') post = post.strip('\'"') try: ct = self.headers['content-type'] if ct.startswith('multipart/'): cid = resolvers.MIMEResolver(ct, self.rfile) xml = cid.GetSOAPPart() ps = ParsedSoap(xml, resolver=cid.Resolve, readerclass=DomletteReader) else: length = int(self.headers['content-length']) ps = ParsedSoap(self.rfile.read(length), readerclass=DomletteReader) except ParseException as e: self.send_fault(FaultFromZSIException(e)) except Exception as e: # Faulted while processing; assume it's in the header. self.send_fault(FaultFromException(e, 1, sys.exc_info()[2])) else: # Keep track of calls thread_id = _thread.get_ident() _contexts[thread_id] = SOAPContext(self.server, xml, ps, self.connection, self.headers, soapAction) try: _Dispatch(ps, self.server, self.send_xml, self.send_fault, post=post, action=soapAction) except Exception as e: self.send_fault(FaultFromException(e, 0, sys.exc_info()[2])) # Clean up after the call if thread_id in _contexts: del _contexts[thread_id]
def check_list_defs(self): gl = globals() for klass in map( lambda h: gl[h], filter( lambda g: (g.startswith('TestList') and issubclass( gl[g], ZSI.TC.List)), gl)): typecode = klass('whatever', nillable=True) data = None for i in range(10): sw = SoapWriter() sw.serialize(data, typecode) s = str(sw) print s ps = ParsedSoap(s) pyobj = ps.Parse(typecode) assert pyobj == data, 'Data corruption expected "%s", got "%s"' % ( str(data), str(pyobj)) if data is None: data = [] continue # # cut last 3 fields off: weekday (0-6, Monday is 0), Julian day (day in the year, 1-366), # DST (Daylight Savings Time) flag (-1, 0 or 1) # utc = list(time.gmtime(i)[:-3]) + [999, 0, 0] data.append(tuple(utc))
def test_local_type_substitution1(self): """test extension. Parse known instance, serialize an equivalent, Parse it back. """ attr1 = 'myclass' attr2 = 'whatever' self.types_module pyobj = GED('urn:test', 'test').pyclass() # [ 1489129 ] Unexpected subsitution error message # try to parse before type ever initialized ps = ParsedSoap(MSG1) pyobj0 = ps.Parse(pyobj.typecode) sub0 = pyobj0.Actor self.failUnless(sub0.get_attribute_attr1() == attr1, 'bad attribute1') self.failUnless(sub0.get_attribute_attr2() == attr2, 'bad attribute2') # [ 1489090 ] Derived type attributes don't populate the attr dictionary # pyobj.Actor = sub1 = GTD('urn:test', 'MiddleActor')(None).pyclass() sub1.Element1 = 'foo' sub1.Element2 = 'bar' sub1.set_attribute_attr1(attr1) sub1.set_attribute_attr2(attr2) sw = SoapWriter() sw.serialize(pyobj) xml = str(sw) ps = ParsedSoap(xml) pyobj2 = ps.Parse(pyobj.typecode) sub2 = pyobj2.Actor self.failUnless(sub2.get_attribute_attr1() == attr1, 'bad attribute class') self.failUnless(sub2.get_attribute_attr2() == attr2, 'bad attribute name') # check parsed out correct type self.failUnless( isinstance(sub2.typecode, sub1.typecode.__class__), 'local element actor "%s" must be an instance of "%s"' % (sub2.typecode, sub1.typecode.__class__)) # check local element is derived from base base = GTD('urn:test', 'BaseActor') self.failUnless( isinstance(sub2.typecode, base), 'local element actor must be a derived type of "%s"' % base)
def _Deserialize(self, xml): # Doc/lit ps = ParsedSoap(xml, readerClass=None, keepdom=False) return ZSI.TC.AnyElement(aname="any", minOccurs=0, maxOccurs=1, nillable=False, processContents="lax").parse( ps.body_root, ps)
def test_local_parse_missing_type_substitution(self): """attempt to substitute an unregistered/unknown type """ attr1 = 'myclass' attr2 = 'whatever' self.types_module pyobj = GED('urn:test', 'test').pyclass() ps = ParsedSoap(NO_SUB_MSG) self.failUnlessRaises(EvaluateException, ps.Parse, pyobj.typecode)
def test_local_type_substitution2(self): """test extension of extension""" attr1 = 'aone' attr2 = 'atwo' attr3 = 'athree' self.types_module pyobj = GED('urn:test', 'test').pyclass() # [ 1489129 ] Unexpected subsitution error message # try to parse before type ever initialized """ ps = ParsedSoap(MSG1) pyobj0 = ps.Parse(pyobj.typecode) sub0 = pyobj0.Actor self.failUnless(sub0.get_attribute_attr1() == attr1, 'bad attribute1') self.failUnless(sub0.get_attribute_attr2() == attr2, 'bad attribute2') """ # [ 1489090 ] Derived type attributes don't populate the attr dictionary # [ 1489677 ] Derivation from derived type missing derived element # pyobj.Actor = sub1 = GTD('urn:test', 'TopActor')(None).pyclass() sub1.Element1 = 'one' sub1.Element2 = 'two' sub1.Element3 = 'three' sub1.set_attribute_attr1(attr1) sub1.set_attribute_attr2(attr2) sub1.set_attribute_attr3(attr3) sw = SoapWriter() sw.serialize(pyobj) xml = str(sw) ps = ParsedSoap(xml) pyobj2 = ps.Parse(pyobj.typecode) sub2 = pyobj2.Actor self.failUnless(sub2.get_attribute_attr1() == attr1, 'bad attribute 1') self.failUnless(sub2.get_attribute_attr2() == attr2, 'bad attribute 2') self.failUnless(sub2.get_attribute_attr3() == attr3, 'bad attribute 3') self.failUnless(sub2.Element1 == sub1.Element1, 'bad element 1') self.failUnless(sub2.Element2 == sub1.Element2, 'bad element 2') self.failUnless(sub2.Element3 == sub1.Element3, 'bad element 3') # check parsed out correct type self.failUnless( isinstance(sub2.typecode, sub1.typecode.__class__), 'local element actor "%s" must be an instance of "%s"' % (sub2.typecode, sub1.typecode.__class__)) # check local element is derived from base base = GTD('urn:test', 'BaseActor') self.failUnless( isinstance(sub2.typecode, base), 'local element actor must be a derived type of "%s"' % base)
def do_POST(self): '''The POST command. action -- SOAPAction(HTTP header) or wsa:Action(SOAP:Header) ''' self._fix_chunked_encoding() logger.debug("Request Host: {}".format(self.client_address)) logger.debug("Request URI: {}".format(self.requestline)) for key, value in self.headers.items(): logger.debug("Request Header: {}: {}".format(key, value)) content_type = self.headers.get("content-type", '') action_matchobj = re.search("action=\"(urn:\w+)\"", content_type) if action_matchobj is not None: # SOAP 1.2 soapAction = action_matchobj.group(1) else: # SOAP 1.1 soapAction = self.headers.getheader('SOAPAction') if soapAction: soapAction = soapAction.strip('\'"') self._soapAction = soapAction post = self.path if not post: raise PostNotSpecified, 'HTTP POST not specified in request' post = post.strip('\'"') try: ct = self.headers['content-type'] if ct.startswith('multipart/'): cid = resolvers.MIMEResolver(ct, self.rfile) xml = cid.GetSOAPPart() ps = ParsedSoap(xml, resolver=cid.Resolve) else: length = int(self.headers['content-length']) xml = self.rfile.read(length) logger.debug("Request Body: {}".format(xml)) ps = ParsedSoap(xml) except ParseException, e: self.send_fault(FaultFromZSIException(e))
def check_union_long(self): import time typecode = GED("http://www.pasoa.org/schemas/version024/PStruct.xsd", "localPAssertionId") for value in (1234455, "whatever", "urn:whatever"): sw = ZSI.SoapWriter() sw.serialize(value, typecode) xml = str(sw) ps = ParsedSoap(xml) pyobj = ps.Parse(typecode) # Union Limitation: # currently it tries to parse it sequentially via memberTypes, # so string is going to parse the URI when we want anyURI self.failUnless(value == pyobj, 'Expected equivalent')
def test_local_empty_attribute(self): # [ 1452752 ] attribute with empty value doesn't appear in parsed object myString = "" pyobj = GED("urn:example", "Test1").pyclass() pyobj.set_attribute_myString(myString) sw = SoapWriter() sw.serialize(pyobj) soap = str(sw) print soap ps = ParsedSoap(soap) pyobj2 = ps.Parse(pyobj.typecode) test = pyobj2.get_attribute_myString() self.failUnlessEqual(myString, str(test))
def check_c14n_exc3(self): """http://www.w3.org/TR/xml-exc-c14n/ tests if a namespace defined in a parent node to the top node to be canonicalized is added when discovered that this namespace is used. """ self.ps = ParsedSoap(XML_INST2) self.el = self.ps.body s = StringIO() Canonicalize(self.el, s, unsuppressedPrefixes=[]) cxml = s.getvalue() print(cxml) d1 = base64.encodestring(sha.sha(C14N_EXCL3).digest()).strip() d2 = base64.encodestring(sha.sha(cxml).digest()).strip() self.assertEqual(d1, C14N_EXCL3_DIGEST) self.assertEqual(d1, d2)
def test_local_type_substitution_test2(self): """test extension of extension""" attr1 = 'aone' attr2 = 'atwo' attr3 = 'athree' self.types_module pyobj = GED('urn:test', 'test2').pyclass() # Test maxOccurs>1 for substitution # pyobj.Actor = [GTD('urn:test', 'TopActor')(None).pyclass()] sub1 = pyobj.Actor[0] sub1.Element1 = 'one' sub1.Element2 = 'two' sub1.Element3 = 'three' sub1.set_attribute_attr1(attr1) sub1.set_attribute_attr2(attr2) sub1.set_attribute_attr3(attr3) sw = SoapWriter() sw.serialize(pyobj) xml = str(sw) ps = ParsedSoap(xml) pyobj2 = ps.Parse(pyobj.typecode) sub2 = pyobj2.Actor[0] self.failUnless(sub2.get_attribute_attr1() == attr1, 'bad attribute 1') self.failUnless(sub2.get_attribute_attr2() == attr2, 'bad attribute 2') self.failUnless(sub2.get_attribute_attr3() == attr3, 'bad attribute 3') self.failUnless(sub2.Element1 == sub1.Element1, 'bad element 1') self.failUnless(sub2.Element2 == sub1.Element2, 'bad element 2') self.failUnless(sub2.Element3 == sub1.Element3, 'bad element 3') # check parsed out correct type self.failUnless( isinstance(sub2.typecode, sub1.typecode.__class__), 'local element actor "%s" must be an instance of "%s"' % (sub2.typecode, sub1.typecode.__class__)) # check local element is derived from base base = GTD('urn:test', 'BaseActor') self.failUnless( isinstance(sub2.typecode, base), 'local element actor must be a derived type of "%s"' % base)
def processResponse(cls, soapdata, **kw): """called by deferred, returns pyobj representing reply. Parameters and Key Words: soapdata -- SOAP Data replytype -- reply type of response """ if len(soapdata) == 0: raise TypeError('Received empty response') # log.msg("_" * 33, time.ctime(time.time()), # "RESPONSE: \n%s" %soapdata, debug=True) ps = ParsedSoap(soapdata, readerclass=cls.readerClass) if ps.IsAFault() is True: log.msg('Received SOAP:Fault', debug=True) raise FaultFromFaultMessage(ps) return ps
def xcheck_c14n_exc4(self): RCVDIGEST = "jhTbi7gWlY9oLqsRr+EZ0bokRFA=" CALDIGEST = "IkMyI4zCDlK41qE7sZxvkFHJioU=" d1 = base64.encodestring(sha.sha(WRONG).digest()).strip() d2 = base64.encodestring(sha.sha(CORRECT).digest()).strip() ps = ParsedSoap(XML_INST4) el = filter(lambda el: _get_element_nsuri_name(el) == (WSA200403.ADDRESS, "MessageID"), ps.header_elements)[0] s = StringIO() Canonicalize(el, s, unsuppressedPrefixes=[]) cxml = s.getvalue() print "-- "*20 print cxml print "-- "*20 d3 = base64.encodestring(sha.sha(cxml).digest()).strip() self.assertEqual(d1, RCVDIGEST) self.assertEqual(d2, CALDIGEST) self.assertEqual(d3, CALDIGEST)
def test_local_attribute1(self): """ """ myDouble = 4.5 myInt = 9 myFloat = 3.0001 myDecimal = 8.999 myGDateTime = time.gmtime() myAnyURI = "urn:whatever" myQName = ("urn:test", "qbert") myString = "whatever" myHexBinary = hex(888) pyobj = GED("urn:example", "Test1").pyclass() # Test serialize/parse pyobj.set_attribute_myDecimal(myDecimal) pyobj.set_attribute_myDouble(myDouble) pyobj.set_attribute_myFloat(myFloat) pyobj.set_attribute_myInt(myInt) pyobj.set_attribute_myDateTime(myGDateTime) pyobj.set_attribute_myGDay(myGDateTime) pyobj.set_attribute_myGMonth(myGDateTime) pyobj.set_attribute_myGYear(myGDateTime) pyobj.set_attribute_myGYearMonth(myGDateTime) pyobj.set_attribute_myDate(myGDateTime) pyobj.set_attribute_myTime(myGDateTime) pyobj.set_attribute_myAnyURI(myAnyURI) pyobj.set_attribute_myString(myString) pyobj.set_attribute_myHexBinary(myHexBinary) pyobj.set_attribute_myDuration(myGDateTime) # Problems parsings pyobj.set_attribute_myQName(myQName) pyobj.set_attribute_myGMonthDay(myGDateTime) #TODO: #pyobj.set_attribute_myBase64Binary("") #pyobj.set_attribute_myNOTATION("NOT") sw = SoapWriter() sw.serialize(pyobj) soap = str(sw) ps = ParsedSoap(soap) pyobj2 = ps.Parse(pyobj.typecode) test = pyobj2.get_attribute_myInt() self.failUnlessEqual(myInt, test) test = pyobj2.get_attribute_myDouble() self.failUnlessEqual(myDouble, test) test = pyobj2.get_attribute_myFloat() self.failUnlessEqual(myFloat, test) test = pyobj2.get_attribute_myDecimal() self.failUnlessEqual(myDecimal, test) test = pyobj2.get_attribute_myAnyURI() self.failUnlessEqual(myAnyURI, test) test = pyobj2.get_attribute_myQName() self.failUnlessEqual(myQName, test) test = pyobj2.get_attribute_myString() self.failUnlessEqual(myString, test) test = pyobj2.get_attribute_myHexBinary() self.failUnlessEqual(myHexBinary, test) # DateTime stuff test = pyobj2.get_attribute_myDateTime() self.failUnlessEqual(myGDateTime[:-3], test[:-3]) test = pyobj2.get_attribute_myDate() self.failUnlessEqual(myGDateTime[:3], test[:3]) test = pyobj2.get_attribute_myTime() self.failUnlessEqual(myGDateTime[4:5], test[4:5]) test = pyobj.get_attribute_myDuration() self.failUnlessEqual(myGDateTime, test) # Bug [ 1453421 ] Incorrect format for type gDay test = pyobj2.get_attribute_myGDay() self.failUnlessEqual(myGDateTime[2], test[2]) test = pyobj2.get_attribute_myGMonth() self.failUnlessEqual(myGDateTime[1], test[1]) test = pyobj2.get_attribute_myGYear() self.failUnlessEqual(myGDateTime[0], test[0]) test = pyobj2.get_attribute_myGYearMonth() self.failUnlessEqual(myGDateTime[:2], test[:2])
def setUp(self): self.ps = ParsedSoap(XML_INST1) self.el = [ el for el in self.ps.header_elements if _get_element_nsuri_name(el) == (WSA200403.ADDRESS, "From") ][0]
def setUp(self): self.ps = ParsedSoap(XML_INST1) self.el = filter( lambda el: _get_element_nsuri_name(el) == (WSA200403.ADDRESS, "From"), self.ps.header_elements)[0]
def processRequest(cls, input, **kw): return ParsedSoap(input, readerclass=cls.readerClass)