Class: GzipServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/mechanize/test_case/gzip_servlet.rb

Constant Summary collapse

TEST_DIR =
File.expand_path '../../../../test', __FILE__

Instance Method Summary collapse

Instance Method Details

#do_GET(req, res) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mechanize/test_case/gzip_servlet.rb', line 9

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
    ::File.open("#{TEST_DIR}/htdocs/#{name}") do |io|
      string = String.new
      zipped = StringIO.new string, 'w'
      Zlib::GzipWriter.wrap zipped do |gz|
        gz.write io.read
      end
      res.body = string
    end
  else
    res.body = String.new
  end

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