Class: RackJetty::ServletHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_jetty/servlet_handler.rb

Constant Summary collapse

DefaultRackEnv =
{
  'rack.version' => ::Rack::VERSION,
  'rack.multithread' => true,
  'rack.multiprocess' => false,
  'rack.run_once' => false,
  'rack.errors' => $stderr,
  'jruby.version' => JRUBY_VERSION,
  'SCRIPT_NAME' => '',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



8
9
10
# File 'lib/rack_jetty/servlet_handler.rb', line 8

def handler
  @handler
end

Instance Method Details

#handle(target, request, response, dispatch) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack_jetty/servlet_handler.rb', line 20

def handle(target, request, response, dispatch)
  begin
    env = DefaultRackEnv.merge({
      'rack.input' => Rack::RewindableInput.new(JavaInput.new(request.get_input_stream)),
      'rack.url_scheme' => request.get_scheme,
      'CONTENT_TYPE' => request.get_content_type,
      'CONTENT_LENGTH' => request.get_content_length, # some post-processing done below
      'REQUEST_METHOD' => request.get_method || "GET",
      'REQUEST_URI' => request.getRequestURI,
      'PATH_INFO' => request.get_path_info,
      'QUERY_STRING' => request.get_query_string || "",
      'SERVER_NAME' => request.get_server_name || "",
      'REMOTE_HOST' => request.get_remote_host || "",
      'REMOTE_ADDR' => request.get_remote_addr || "",
      'REMOTE_USER' => request.get_remote_user || "",
      'SERVER_PORT' => request.get_server_port.to_s
    })
    env['CONTENT_LENGTH'] = env['CONTENT_LENGTH'] >= 0? env['CONTENT_LENGTH'].to_s : "0"
    request.get_header_names.each do |h|
      next if h =~ /^Content-(Type|Length)$/i
      k = "HTTP_#{h.upcase.gsub(/-/, '_')}"
      env[k] = request.getHeader(h) unless env.has_key?(k)
    end

    # request.get_content_type returns nil if the Content-Type is not included
    # and Rack doesn't expect Content-Type => '' - certain methods will issue 
    # a backtrace if it's passed in. 
    #
    # The correct behaviour is not to include Content-Type in the env hash
    # if it is blank. 
    #
    # https://github.com/rack/rack/issues#issue/40 covers the problem from
    # Rack's end. 
    env.delete('CONTENT_TYPE') if [nil, ''].include?(env['CONTENT_TYPE'])

    status, headers, output = handler.app.call(env)
    
    if (match = %r{^([0-9]{3,3}) +([[:print:]]+)$}.match(status.to_s))
      response.set_status(match[1].to_i, match[2].to_s)
    else
      response.set_status(status.to_i)
    end
    
    headers.each do |k, v|
      case k
      when 'Content-Type'
        response.set_content_type(v)
      when 'Content-Length'
        response.set_content_length(v.to_i)
      else
        response.set_header(k, v)
      end
    end
    
    buffer = response.get_output_stream
    output.each do |s|
      buffer.write(s.to_java_bytes)
    end
  ensure
    request.set_handled(true)
  end
end