def test_default_proxy_flag(self): amf3.use_proxies_default = True decoder = amf3.Decoder(self.buf, context=self.context) self.assertTrue(decoder.use_proxies) amf3.use_proxies_default = False decoder = amf3.Decoder(self.buf, context=self.context) self.assertFalse(decoder.use_proxies)
def test_decode_amf3(self): stream = util.BufferedByteStream( '\n\x07Cflex.messaging.io.ArrayCollection\t\x03\x01\x06\teggs') decoder = amf3.Decoder(stream) x = decoder.readElement() self.assertEqual(x.__class__, flex.ArrayCollection) self.assertEqual(x, ['eggs'])
def test_complex_encode_decode_dict(self): complex = {'element': 'ignore', 'objects': self.build_complex()} self.encoder.writeElement(complex) encoded = self.encoder.stream.getvalue() context = amf3.Context() decoded = amf3.Decoder(encoded, context).readElement() self.complex_encode_decode_test(decoded['objects'])
def test_decode(self): stream = util.BufferedByteStream( '\x0a\x07;flex.messaging.io.ObjectProxy\x09\x01\x03a\x06\x09spam' '\x03b\x04\x05\x01') decoder = amf3.Decoder(stream) x = decoder.readElement() self.assertEqual(x.__class__, flex.ObjectProxy) self.assertEqual(x._amf_object, {'a': 'spam', 'b': 5})
def test_decode_proxy(self): stream = util.BufferedByteStream( '\x0a\x07;flex.messaging.io.ObjectProxy\x09\x01\x03a\x06\x09spam' '\x03b\x04\x05\x01') decoder = amf3.Decoder(stream) decoder.use_proxies = True x = decoder.readElement() self.assertEqual(x.__class__, pyamf.MixedArray) self.assertEqual(x, {'a': 'spam', 'b': 5})
def test_decode(self): stream = util.BufferedByteStream( '\n\x07Cflex.messaging.io.ArrayCollection' '\t\x01\x09spam\x06\x09eggs\x01') decoder = amf3.Decoder(stream) x = decoder.readElement() self.assertEquals(x.__class__, flex.ArrayCollection) self.assertEquals(x.keys(), ['spam']) self.assertEquals(x.items(), [('spam', u'eggs')])
def setUp(self): self.buf = util.BufferedByteStream() self.decoder = amf3.Decoder(self.buf)
def parse_stacktrace(filename, c, e, v): event_id = e value_id = v with open(filename, "rb") as f: buf = f.read() pos = 0 while True: context = amf3.Context() decoder = amf3.Decoder(buf[pos:], context) try: data = decoder.readElement() except pyamf.EOStream: #print "EOStream" break except IOError: # premature end of stream #print "IOError" break except Exception, e: print "Warning:", e, pos pos = pos + 1 continue if not data: pos = pos + 1 continue pos = pos + decoder.stream.tell() if type(data) != list: print "Warning: top level data not valid: ", type(data) continue l = len(data) name, session, method, opcode = data[0].split(":", 4) c.execute( "INSERT INTO stack_events (event_id,session_method,opcode) VALUES (?,?,?);", (event_id, "%s:%s" % (session, method), opcode)) #if dbtool.verbose: # print "\n", event_id, data[0] for i in range(0, (l - 1) / 2): t = data[(i * 2) + 1] a = data[(i * 2) + 2] #print event_id, t, data[0], repr(a)[:32] if type(a) != pyamf.amf3.ByteArray: print "Warning: top level data not valid: ", type(a) d = None else: try: d = amf3.Decoder(a).readElement() except: print "Warning: decoder failed @", data[0], t d = "" value_id = insert_stackval(c, value_id, event_id, t, d, -1, 0) event_id = event_id + 1 return (event_id, value_id)
def next(self): """ Read one RTMP message from the stream and return it. """ if self.stream.at_eof(): raise StopIteration # Read the message into body_stream. The message may span a number of # chunks (each one with its own header). message_body = [] msg_body_len = 0 _header = header.decode(self.stream) log.debug('header %s' % _header) # FIXME: this should really be implemented inside header_decode if _header.data_type == rtmp_type.DT_NONE: _header = self.prv_header self.prv_header = _header while True: # NOTE: this whole loop section needs to be looked at. read_bytes = min(_header.body_length - msg_body_len, self.chunk_size) message_body.append(self.stream.read(read_bytes)) msg_body_len += read_bytes if msg_body_len >= _header.body_length: break next_header = header.decode(self.stream) # WORKAROUND: even though the RTMP specification states that the # extended timestamp field DOES NOT follow type 3 chunks, it seems # that Flash player 10.1.85.3 and Flash Media Server 3.0.2.217 send # and expect this field here. if _header.timestamp >= 0x00ffffff: self.stream.read_ulong() assert next_header.stream_id == -1, (_header, next_header) assert next_header.data_type == -1, (_header, next_header) assert next_header.timestamp == -1, (_header, next_header) assert next_header.body_length == -1, (_header, next_header) assert _header.body_length == msg_body_len, (_header, msg_body_len) body_stream = pyamf.util.BufferedByteStream(''.join(message_body)) # Decode the message based on the datatype present in the header ret = {'msg': _header.data_type} if ret['msg'] == rtmp_type.DT_NONE: log.warning('WARNING: message with datatype None received: %s' % _header) return self.next() elif ret['msg'] == rtmp_type.DT_USER_CONTROL: ret['event_type'] = body_stream.read_ushort() ret['event_data'] = body_stream.read() elif ret['msg'] == rtmp_type.DT_WINDOW_ACK_SIZE: ret['window_ack_size'] = body_stream.read_ulong() elif ret['msg'] == rtmp_type.DT_SET_PEER_BANDWIDTH: ret['window_ack_size'] = body_stream.read_ulong() ret['limit_type'] = body_stream.read_uchar() elif ret['msg'] == rtmp_type.DT_SHARED_OBJECT: decoder = amf0.Decoder(body_stream) obj_name = decoder.readString() curr_version = body_stream.read_ulong() flags = body_stream.read(8) # A shared object message may contain a number of events. events = [] while not body_stream.at_eof(): event = self.read_shared_object_event(body_stream, decoder) events.append(event) ret['obj_name'] = obj_name ret['curr_version'] = curr_version ret['flags'] = flags ret['events'] = events elif ret['msg'] == rtmp_type.DT_AMF3_SHARED_OBJECT: decoder = amf3.Decoder(body_stream) obj_name = decoder.readString() curr_version = body_stream.read_ulong() flags = body_stream.read(8) # A shared object message may contain a number of events. events = [] while not body_stream.at_eof(): event = self.read_shared_object_event(body_stream, decoder) events.append(event) ret['obj_name'] = obj_name ret['curr_version'] = curr_version ret['flags'] = flags ret['events'] = events elif ret['msg'] == rtmp_type.DT_COMMAND: decoder = amf0.Decoder(body_stream) commands = [] while not body_stream.at_eof(): commands.append(decoder.readElement()) ret['command'] = commands elif ret['msg'] == rtmp_type.DT_AMF3_COMMAND: decoder = amf3.Decoder(body_stream) commands = [] while not body_stream.at_eof(): commands.append(decoder.readElement()) ret['command'] = commands elif ret['msg'] == rtmp_type.DT_SET_CHUNK_SIZE: ret['chunk_size'] = body_stream.read_ulong() else: assert False, _header log.debug('recv %r', ret) return ret