Module: Arachni::RPC::Serializer

Extended by:
Serializer
Included in:
Serializer
Defined in:
lib/arachni/rpc/serializer.rb

Overview

Used for serialization of Arachni::RPC messages.

It’s simply a delegator for ‘MessagePack` with `Zlib` compression for messages that are larger than COMPRESS_LARGER_THAN.

Author:

Constant Summary collapse

COMPRESS_LARGER_THAN =

Compress object dumps larger than 1KB.

1_000

Instance Method Summary collapse

Instance Method Details

#compress(string) ⇒ String

Note:

Ignores strings smaller than #COMPRESS_LARGER_THAN.

Returns Compressed (or not) ‘string`.

Parameters:

  • string (String)

    String to compress.

Returns:

  • (String)

    Compressed (or not) ‘string`.



67
68
69
70
# File 'lib/arachni/rpc/serializer.rb', line 67

def compress( string )
    return string if string.size < COMPRESS_LARGER_THAN
    Zlib::Deflate.deflate string
end

#decompress(string) ⇒ String

Note:

Will return the ‘string` as is if it was not compressed.

Returns Decompressed string.

Parameters:

  • string (String)

    String to decompress.

Returns:

  • (String)

    Decompressed string.



79
80
81
82
83
84
85
86
87
88
# File 'lib/arachni/rpc/serializer.rb', line 79

def decompress( string )
    # Just an ID representing a serialized, empty data structure.
    return string if string.size == 1

    begin
        Zlib::Inflate.inflate string
    rescue Zlib::DataError
        string
    end
end

#deep_clone(object) ⇒ Object

Parameters:

  • object (#to_rpc_data, .from_rpc_data)

Returns:



56
57
58
# File 'lib/arachni/rpc/serializer.rb', line 56

def deep_clone( object )
    object.class.from_rpc_data rpc_data( object )
end

#dump(object) ⇒ String

Returns Compressed ‘object` dump.

Parameters:

  • object (#to_rpc_data)

Returns:



29
30
31
32
# File 'lib/arachni/rpc/serializer.rb', line 29

def dump( object )
    # ap object
    compress( serializer.dump( object.to_rpc_data_or_self ) )
end

#load(dump) ⇒ Object

Parameters:

Returns:



38
39
40
# File 'lib/arachni/rpc/serializer.rb', line 38

def load( dump )
    serializer.load( decompress( dump ) )
end

#rpc_data(object) ⇒ Object

Simulates an object’s over-the-wire transmission by dumping and then loading.

Parameters:

  • object (#to_rpc_data, .from_rpc_data)

Returns:

  • (Object)

    Data that the peer would receive.



49
50
51
# File 'lib/arachni/rpc/serializer.rb', line 49

def rpc_data( object )
    load( dump( object ) )
end

#serializerObject



90
91
92
# File 'lib/arachni/rpc/serializer.rb', line 90

def serializer
    MessagePack
end