Class: SiteDiff::Webserver::ResultServer::CacheServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/sitediff/webserver/resultserver.rb

Overview

Display a page from the cache

Instance Method Summary collapse

Constructor Details

#initialize(_server, cache) ⇒ CacheServlet

Returns a new instance of CacheServlet.



12
13
14
# File 'lib/sitediff/webserver/resultserver.rb', line 12

def initialize(_server, cache)
  @cache = cache
end

Instance Method Details

#do_GET(req, res) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::InternalServerError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sitediff/webserver/resultserver.rb', line 16

def do_GET(req, res)
  path = req.path_info
  (md = %r{^/([^/]+)(/.*)$}.match(path)) ||
    raise(WEBrick::HTTPStatus::NotFound)
  tag, path = *md.captures
  (r = @cache.get(tag.to_sym, path)) ||
    raise(WEBrick::HTTPStatus::NotFound)

  raise WEBrick::HTTPStatus[r.error_code] if r.error_code
  raise WEBrick::HTTPStatus::InternalServerError, r.error if r.error

  res['content-type'] = 'text/html'
  res.body = r.content
end