Class: Webmachine::Adapters::WEBrick::Server

Inherits:
WEBrick::HTTPServer
  • Object
show all
Defined in:
lib/webmachine/adapters/webrick.rb

Overview

WEBRick::HTTPServer that is run by the WEBrick adapter.

Instance Method Summary (collapse)

Constructor Details

- (Server) initialize(options)



33
34
35
36
# File 'lib/webmachine/adapters/webrick.rb', line 33

def initialize(options)
  @application = options[:application]
  super(options)
end

Instance Method Details

- (Object) service(wreq, wres)

Handles a request



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/webmachine/adapters/webrick.rb', line 39

def service(wreq, wres)
  header = Webmachine::Headers.new
  wreq.each {|k,v| header[k] = v }
  request = Webmachine::Request.new(wreq.request_method,
                                    wreq.request_uri,
                                    header,
                                    LazyRequestBody.new(wreq))

  response = Webmachine::Response.new
  @application.dispatcher.dispatch(request, response)
  wres.status = response.code.to_i

  headers = response.headers.flattened.reject { |k,v| k == 'Set-Cookie' }
  headers.each { |k,v| wres[k] = v }

  cookies = [response.headers['Set-Cookie'] || []].flatten
  cookies.each { |c| wres.cookies << c }

  wres['Server'] = [Webmachine::SERVER_STRING, wres.config[:ServerSoftware]].join(" ")
  case response.body
  when String
    wres.body << response.body
  when Enumerable
    wres.chunked = response.headers['Transfer-Encoding'] == 'chunked'
    response.body.each {|part| wres.body << part }
  else
    if response.body.respond_to?(:call)
      wres.chunked = true
      wres.body << response.body.call
    end
  end
end