Module: SPDY::Zlib

Defined in:
lib/spdy/compressor.rb

Constant Summary collapse

DICT =
"optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-" \
"languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi" \
"f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser" \
"-agent10010120020120220320420520630030130230330430530630740040140240340440" \
"5406407408409410411412413414415416417500501502503504505accept-rangesageeta" \
"glocationproxy-authenticatepublicretry-afterservervarywarningwww-authentic" \
"ateallowcontent-basecontent-encodingcache-controlconnectiondatetrailertran" \
"sfer-encodingupgradeviawarningcontent-languagecontent-lengthcontent-locati" \
"oncontent-md5content-rangecontent-typeetagexpireslast-modifiedset-cookieMo" \
"ndayTuesdayWednesdayThursdayFridaySaturdaySundayJanFebMarAprMayJunJulAugSe" \
"pOctNovDecchunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplic" \
"ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1" \
".1statusversionurl\0"
CHUNK =

this is silly, but it’ll do for now

10*1024

Class Method Summary collapse

Class Method Details

.deflate(data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spdy/compressor.rb', line 46

def self.deflate(data)
  in_buf  = FFI::MemoryPointer.from_string(data)
  out_buf = FFI::MemoryPointer.new(CHUNK)

  zstream = FFI::Zlib::Z_stream.new
  zstream[:avail_in]  = in_buf.size-1
  zstream[:avail_out] = CHUNK
  zstream[:next_in]   = in_buf
  zstream[:next_out]  = out_buf

  result = FFI::Zlib.deflateInit(zstream, -1)
  raise "invalid stream" if result != FFI::Zlib::Z_OK

  result = FFI::Zlib.deflateSetDictionary(zstream, DICT, DICT.size)
  raise "invalid dictionary" if result != FFI::Zlib::Z_OK

  result = FFI::Zlib.deflate(zstream, FFI::Zlib::Z_SYNC_FLUSH)
  raise "cannot deflate" if result != FFI::Zlib::Z_OK

  out_buf.get_bytes(0, zstream[:total_out])
end

.inflate(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spdy/compressor.rb', line 21

def self.inflate(data)
  in_buf  = FFI::MemoryPointer.from_string(data)
  out_buf = FFI::MemoryPointer.new(CHUNK)

  zstream = FFI::Zlib::Z_stream.new
  zstream[:avail_in]  = in_buf.size
  zstream[:avail_out] = CHUNK
  zstream[:next_in]   = in_buf
  zstream[:next_out]  = out_buf

  result = FFI::Zlib.inflateInit(zstream)
  raise "invalid stream" if result != FFI::Zlib::Z_OK

  result = FFI::Zlib.inflate(zstream, FFI::Zlib::Z_SYNC_FLUSH)
  raise "invalid stream" if result != FFI::Zlib::Z_NEED_DICT

  result = FFI::Zlib.inflateSetDictionary(zstream, DICT, DICT.size)
  raise "invalid dictionary" if result != FFI::Zlib::Z_OK

  result = FFI::Zlib.inflate(zstream, FFI::Zlib::Z_SYNC_FLUSH)
  raise "cannot inflate" if result != FFI::Zlib::Z_OK

  out_buf.get_bytes(0, zstream[:total_out])
end