Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

uncompress header data via custom dictionary

  • Loading branch information...
commit 83ada25cf964daf43bd040f53a4f4bb70267f754 1 parent 0fa67a9
Ilya Grigorik authored
3  Gemfile
... ... @@ -1,4 +1,3 @@
1 1 source "http://rubygems.org"
2 2
3   -# Specify your gem's dependencies in spdy.gemspec
4   -gemspec
  3 +gemspec
4 lib/spdy.rb
... ... @@ -1,3 +1 @@
1   -module Spdy
2   - # Your code goes here...
3   -end
  1 +require 'spdy/compressor'
48 lib/spdy/compressor.rb
... ... @@ -0,0 +1,48 @@
  1 +require 'ffi/zlib'
  2 +
  3 +module SPDY
  4 + module Zlib
  5 +
  6 + DICT = \
  7 + "optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-" \
  8 + "languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi" \
  9 + "f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser" \
  10 + "-agent10010120020120220320420520630030130230330430530630740040140240340440" \
  11 + "5406407408409410411412413414415416417500501502503504505accept-rangesageeta" \
  12 + "glocationproxy-authenticatepublicretry-afterservervarywarningwww-authentic" \
  13 + "ateallowcontent-basecontent-encodingcache-controlconnectiondatetrailertran" \
  14 + "sfer-encodingupgradeviawarningcontent-languagecontent-lengthcontent-locati" \
  15 + "oncontent-md5content-rangecontent-typeetagexpireslast-modifiedset-cookieMo" \
  16 + "ndayTuesdayWednesdayThursdayFridaySaturdaySundayJanFebMarAprMayJunJulAugSe" \
  17 + "pOctNovDecchunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplic" \
  18 + "ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1" \
  19 + ".1statusversionurl\0"
  20 +
  21 + CHUNK = 10*1024 # this is silly, but it'll do for now
  22 +
  23 + def self.inflate(data)
  24 + in_buf = FFI::MemoryPointer.from_string(data)
  25 + out_buf = FFI::MemoryPointer.new(CHUNK)
  26 +
  27 + zstream = FFI::Zlib::Z_stream.new
  28 + zstream[:avail_in] = in_buf.size
  29 + zstream[:avail_out] = CHUNK
  30 + zstream[:next_in] = in_buf
  31 + zstream[:next_out] = out_buf
  32 +
  33 + result = FFI::Zlib.inflateInit(zstream)
  34 + raise "invalid stream" if result != FFI::Zlib::Z_OK
  35 +
  36 + result = FFI::Zlib.inflate(zstream, FFI::Zlib::Z_SYNC_FLUSH)
  37 + raise "invalid stream" if result != FFI::Zlib::Z_NEED_DICT
  38 +
  39 + result = FFI::Zlib.inflateSetDictionary(zstream, DICT, DICT.size)
  40 + raise "invalid dictionary" if result != FFI::Zlib::Z_OK
  41 +
  42 + result = FFI::Zlib.inflate(zstream, FFI::Zlib::Z_SYNC_FLUSH)
  43 + raise "cannot inflate" if result != FFI::Zlib::Z_OK
  44 +
  45 + out_buf.get_bytes(0, zstream[:total_out])
  46 + end
  47 + end
  48 +end
2  spdy.gemspec
@@ -14,6 +14,8 @@ Gem::Specification.new do |s|
14 14
15 15 s.rubyforge_project = "spdy"
16 16
  17 + s.add_dependency "bindata"
  18 + s.add_dependency "ffi-zlib"
17 19 s.add_development_dependency "rspec"
18 20
19 21 s.files = `git ls-files`.split("\n")
9 spec/compressor_spec.rb
... ... @@ -0,0 +1,9 @@
  1 +require 'helper'
  2 +
  3 +describe SPDY::Zlib do
  4 + it "should inflate header with custom dictionary" do
  5 + data = "8\xEA\xDF\xA2Q\xB2b\xE0f`\x83\xA4\x17\x86(4\xDBu0\xAC\xD7\x06\x89\xC2\xFDa]hk\xA0g\xA9\x83p\x13X\xC0B\a\xEE?\x1D-}-\xB0\x98)\x03\x1Fjff\x90\xF3\f\xF6\x87:U\a\xECV\xB0:s\x1D\x88zc\x06~\xB4\xEC\xCE \b\xF2\x8C\x0E\xD47:\xC5)\xC9\x19p5\xB0\x14\xC2\xC0\x97\x9A\xA7\e\x1A\xAC\x93\nu\b\x03\e$V\x19Z\x8A\x8D\x8C\xD3\xD2u\x8D\v\x8C\xCC\x8D\x92\v,\xCB\r\xE2\x8Bl\xCD\xAD\x15\f\xB3\xCD\v\xCDu3\rR\xCC\xD3\x8B\v\r-\xCC\x81\xA2\x06\xD6\n\xF1 '\x96$\xA5&\x96\x18\x01d[\x9Cj\x9CUQ\x92dT\x99e\x9C\x9A\x93\x93j\f\x94\x8D//)\x8F/\xCB,\x8E\afy[k\x85\xB2\xC4\xBC\xCC\x92\xCA\xF8\xCC\x14\xDB4c#\x8B\xE4$3\x13c\x93d`\xFEM12N154OI32O\x03\x16\x04\xA6\x96I\f,\xA0\xC2\x88\x81\x0F\x94bs@L+K\x03\x03\x03\x06\xB6\\`!\x98\x9F\xC2\xC0\xEC\xEE\x1A\xC2\xC0V\f\xCC7\xB9\xA9\f\xAC\x19%%\x05\xC5\f\xCC\xA0\bb\xD4g\xE0B\x94*\fe\xBE\xF9U\x9999\x89\xFA\xA6z\x06\x00)h\xF8&&g\xE6\x95\xE4\x17gX+x\x02\x13z\x8E\x02P@\xC1?X!B\xC1\xD0 \xDE,\xDE\\S\xC1\x11\x18\x87\xA9\xE1\xA9I\xDE\x99%\xFA\xA6\xC6&zF&\n\x1A\xDE\x1E!\xBE>:\n9\x99\xD9\xA9\n\xEE\xA9\xC9\xD9\xF9\x9A\n\xCE\x19\xC0\xD22U\xDF\xD0P\xCF@\xCF\xCC\xD2L\xCF\xC8B!81-\xB1(\x13\xAA\x89\x81\x1D\x9Af\x188`I\t\x00\x00\x00\xFF\xFF"
  6 +
  7 + SPDY::Zlib.inflate(data).should match('HTTP/1.1')
  8 + end
  9 +end

0 comments on commit 83ada25

Please sign in to comment.
Something went wrong with that request. Please try again.