9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/httpi/dime.rb', line 9
def initialize(body)
bytes = body.unpack('C*')
while bytes.length > 0
record = DimeRecord.new
byte = bytes.shift
record.version = (byte >> 3) & 31
record.first = (byte >> 2) & 1
record.last = (byte >> 1) & 1
record.chunked = byte & 1
record.type_format = (bytes.shift >> 4) & 15
lengths = []
lengths << [:options, (bytes.shift << 8) | bytes.shift]
lengths << [:id, (bytes.shift << 8) | bytes.shift]
lengths << [:type, (bytes.shift << 8) | bytes.shift]
lengths << [:data, (bytes.shift << 24) | (bytes.shift << 16) | (bytes.shift << 8) | bytes.shift]
lengths.each do |attribute_set|
attribute, length = attribute_set
content = bytes.slice!(0, length).pack('C*')
if attribute == :data && record.type_format == BINARY
content = StringIO.new(content)
end
record.send "#{attribute.to_s}=", content
bytes.slice!(0, 4 - (length & 3)) if (length & 3) != 0
end
self << record
end
end
|