Class: SPDY::Zlib

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeZlib

Returns a new instance of Zlib.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spdy/compressor.rb', line 21

def initialize
  @inflate_zstream = FFI::Zlib::Z_stream.new
  result = FFI::Zlib.inflateInit(@inflate_zstream)
  raise "invalid stream" if result != FFI::Zlib::Z_OK

  @deflate_zstream = FFI::Zlib::Z_stream.new
  result = FFI::Zlib.deflateInit(@deflate_zstream, FFI::Zlib::Z_DEFAULT_COMPRESSION)
  raise "invalid stream" if result != FFI::Zlib::Z_OK

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

Instance Method Details

#deflate(data) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/spdy/compressor.rb', line 64

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

  @deflate_zstream[:avail_in]  = in_buf.size-1
  @deflate_zstream[:avail_out] = CHUNK
  @deflate_zstream[:next_in]   = in_buf
  @deflate_zstream[:next_out]  = out_buf

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

  out_buf.get_bytes(0, CHUNK - @deflate_zstream[:avail_out])
end

#inflate(data) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spdy/compressor.rb', line 42

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

  @inflate_zstream[:avail_in]  = in_buf.size-1
  @inflate_zstream[:avail_out] = CHUNK
  @inflate_zstream[:next_in]   = in_buf
  @inflate_zstream[:next_out]  = out_buf

  result = FFI::Zlib.inflate(@inflate_zstream, FFI::Zlib::Z_SYNC_FLUSH)
  if result == FFI::Zlib::Z_NEED_DICT
    result = FFI::Zlib.inflateSetDictionary(@inflate_zstream, DICT, DICT.size)
    raise "invalid dictionary" if result != FFI::Zlib::Z_OK
  
    result = FFI::Zlib.inflate(@inflate_zstream, FFI::Zlib::Z_SYNC_FLUSH)
  end

  raise "cannot inflate" if result != FFI::Zlib::Z_OK && result != FFI::Zlib::Z_STREAM_END

  out_buf.get_bytes(0, CHUNK - @inflate_zstream[:avail_out])
end

#resetObject



34
35
36
37
38
39
40
# File 'lib/spdy/compressor.rb', line 34

def reset
  result = FFI::Zlib.inflateReset(@inflate_zstream)
  raise "invalid stream" if result != FFI::Zlib::Z_OK

  result = FFI::Zlib.deflateReset(@deflate_zstream)
  raise "invalid stream" if result != FFI::Zlib::Z_OK
end