File: gzip_servlet.rb

package info (click to toggle)
ruby-mechanize 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,312 kB
  • ctags: 1,480
  • sloc: ruby: 11,170; makefile: 5
file content (34 lines) | stat: -rw-r--r-- 890 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
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
require 'stringio'
require 'zlib'

class GzipServlet < WEBrick::HTTPServlet::AbstractServlet

  # Test directory changed to what matches the Debian build process
  # TEST_DIR = File.expand_path '../../../../test', __FILE__
  TEST_DIR = File.expand_path (Dir.pwd + '/test')

  def do_GET(req, res)
    if req['Accept-Encoding'] !~ /gzip/ then
      res.code = 400
      res.body = 'Content-Encoding: gzip is not supported by your user-agent'
      return
    end

    if name = req.query['file'] then
      open "#{TEST_DIR}/htdocs/#{name}" do |io|
        string = ""
        zipped = StringIO.new string, 'w'
        Zlib::GzipWriter.wrap zipped do |gz|
          gz.write io.read
        end
        res.body = string
      end
    else
      res.body = ''
    end

    res['Content-Encoding'] = req['X-ResponseContentEncoding'] || 'gzip'
    res['Content-Type'] = "text/html"
  end
end