Class: Falcon::Server

Inherits:
Async::HTTP::Server
  • Object
show all
Defined in:
lib/falcon/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, *args) ⇒ Server

Returns a new instance of Server.



27
28
29
30
31
# File 'lib/falcon/server.rb', line 27

def initialize(app, *args)
  super(*args)
  
  @app = app
end

Instance Method Details

#failure_response(exception) ⇒ Object



106
107
108
# File 'lib/falcon/server.rb', line 106

def failure_response(exception)
  [500, {'Content-Type' => 'text/plain'}, ["Request failed due to: #{exception.class}: #{exception.message}"]]
end

#handle_request(request, peer, address) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/falcon/server.rb', line 37

def handle_request(request, peer, address)
  request_path, query_string = request.path.split('?', 2)
  server_name, server_port = (request.authority || '').split(':', 2)
  
  env = {
    'rack.version' => [2, 0, 0],
    
    'rack.input' => Input.new(request.body),
    'rack.errors' => $stderr,
    
    'rack.multithread' => true,
    'rack.multiprocess' => true,
    'rack.run_once' => false,
    
    # The HTTP request method, such as “GET” or “POST”. This cannot ever be an empty string, and so is always required.
    'REQUEST_METHOD' => request.method,
    
    # The initial portion of the request URL's “path” that corresponds to the application object, so that the application knows its virtual “location”. This may be an empty string, if the application corresponds to the “root” of the server.
    'SCRIPT_NAME' => '',
    
    # The remainder of the request URL's “path”, designating the virtual “location” of the request's target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash. This value may be percent-encoded when originating from a URL.
    'PATH_INFO' => request_path,
    
    # The portion of the request URL that follows the ?, if any. May be empty, but is always required!
    'QUERY_STRING' => query_string || '',
    
    # The server protocol, e.g. HTTP/1.1
    'SERVER_PROTOCOL' => request.version,
    'rack.url_scheme' => 'http',
    
    # I'm not sure what sane defaults should be here:
    'SERVER_NAME' => server_name || '',
    'SERVER_PORT' => server_port || '',
  }
  
  if content_type = request.headers['content-type']
    env['CONTENT_TYPE'] = content_type
  end
  
  request.headers.each do |key, value|
    env["HTTP_#{key.upcase.tr('-', '_')}"] = value
  end
  
  env['rack.hijack?'] = true
  env['rack.hijack'] = lambda do
    env['rack.hijack_io'] = peer
  end
  
  if content_type = request.headers['HTTP_CONTENT_TYPE']
    env['CONTENT_TYPE'] = content_type
  end
  
  if remote_address = peer.remote_address
    env['REMOTE_ADDR'] = remote_address.ip_address if remote_address.ip?
  end
  
  response = @app.call(env)
  
  if env['rack.hijack_io']
    throw :hijack
  else
    return response
  end
rescue => exception
  logger.error "#{exception.class}: #{exception.message}\n\t#{$!.backtrace.join("\n\t")}"
  
  return failure_response(exception)
end

#loggerObject



33
34
35
# File 'lib/falcon/server.rb', line 33

def logger
  Async.logger
end