Class: Falcon::Adapters::Rack
- Inherits:
-
Object
- Object
- Falcon::Adapters::Rack
- Defined in:
- lib/falcon/adapters/rack.rb
Constant Summary collapse
- HTTP_HOST =
CGI constants
'HTTP_HOST'.freeze
- PATH_INFO =
'PATH_INFO'.freeze
- REQUEST_METHOD =
'REQUEST_METHOD'.freeze
- REQUEST_PATH =
'REQUEST_PATH'.freeze
- REQUEST_URI =
'REQUEST_URI'.freeze
- SCRIPT_NAME =
'SCRIPT_NAME'.freeze
- QUERY_STRING =
'QUERY_STRING'.freeze
- SERVER_PROTOCOL =
'SERVER_PROTOCOL'.freeze
- SERVER_NAME =
'SERVER_NAME'.freeze
- SERVER_PORT =
'SERVER_PORT'.freeze
- REMOTE_ADDR =
'REMOTE_ADDR'.freeze
- CONTENT_TYPE =
'CONTENT_TYPE'.freeze
- CONTENT_LENGTH =
'CONTENT_LENGTH'.freeze
- RACK_VERSION =
Rack environment variables
'rack.version'.freeze
- RACK_ERRORS =
'rack.errors'.freeze
- RACK_LOGGER =
'rack.logger'.freeze
- RACK_INPUT =
'rack.input'.freeze
- RACK_MULTITHREAD =
'rack.multithread'.freeze
- RACK_MULTIPROCESS =
'rack.multiprocess'.freeze
- RACK_RUNONCE =
'rack.run_once'.freeze
- RACK_URL_SCHEME =
'rack.url_scheme'.freeze
- RACK_HIJACK =
'rack.hijack'.freeze
- RACK_IS_HIJACK =
'rack.hijack?'.freeze
- RACK_HIJACK_IO =
'rack.hijack_io'.freeze
- HTTP_X_FORWARDED_PROTO =
Header constants
'HTTP_X_FORWARDED_PROTO'.freeze
Instance Method Summary collapse
- #call(request) ⇒ Object
- #failure_response(exception) ⇒ Object
-
#initialize(app, logger = Async.logger) ⇒ Rack
constructor
A new instance of Rack.
- #make_response(request, status, headers, body) ⇒ Object
-
#unwrap_headers(headers, env) ⇒ Object
Rack separates multiple headers with the same key, into a single field with multiple “lines”.
-
#unwrap_request(request, env) ⇒ Object
Process the incoming request into a valid rack env.
Constructor Details
#initialize(app, logger = Async.logger) ⇒ Rack
Returns a new instance of Rack.
62 63 64 65 66 67 68 |
# File 'lib/falcon/adapters/rack.rb', line 62 def initialize(app, logger = Async.logger) @app = app raise ArgumentError, "App must be callable!" unless @app.respond_to?(:call) @logger = logger end |
Instance Method Details
#call(request) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/falcon/adapters/rack.rb', line 116 def call(request) request_path, query_string = request.path.split('?', 2) server_name, server_port = (request. || '').split(':', 2) env = { RACK_VERSION => [2, 0, 0], RACK_INPUT => Input.new(request.body), RACK_ERRORS => $stderr, RACK_LOGGER => Async.logger, RACK_MULTITHREAD => true, RACK_MULTIPROCESS => true, RACK_RUNONCE => 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, REQUEST_PATH => request_path, REQUEST_URI => 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, # The request scheme: RACK_URL_SCHEME => request.scheme, # I'm not sure what sane defaults should be here: SERVER_NAME => server_name || '', SERVER_PORT => server_port || '', } self.unwrap_request(request, env) if request.hijack? env[RACK_IS_HIJACK] = true env[RACK_HIJACK] = lambda do wrapper = request.hijack # We dup this as it might be taken out of the normal control flow, and the io will be closed shortly after returning from this method. io = wrapper.io.dup wrapper.close # This is implicitly returned: env[RACK_HIJACK_IO] = io end else env[RACK_IS_HIJACK] = false end status, headers, body = @app.call(env) # Partial hijack is not supported/tested. # if hijack = headers.delete('rack.hijack') # body = Async::HTTP::Body::Writable.new # # Task.current.async do # hijack.call(body) # end # return nil # end # if env['rack.hijack_io'] # return nil # end return make_response(request, status, headers, body) rescue => exception @logger.error "#{exception.class}: #{exception.message}\n\t#{$!.backtrace.join("\n\t")}" return failure_response(exception) end |
#failure_response(exception) ⇒ Object
198 199 200 |
# File 'lib/falcon/adapters/rack.rb', line 198 def failure_response(exception) Async::HTTP::Response.for_exception(exception) end |
#make_response(request, status, headers, body) ⇒ Object
110 111 112 113 114 |
# File 'lib/falcon/adapters/rack.rb', line 110 def make_response(request, status, headers, body) # @logger.debug(request) {"Rack response: #{status} #{headers.inspect} #{body.class}"} return Response.wrap(status, headers, body) end |
#unwrap_headers(headers, env) ⇒ Object
Rack separates multiple headers with the same key, into a single field with multiple “lines”.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/falcon/adapters/rack.rb', line 71 def unwrap_headers(headers, env) headers.each do |key, value| http_key = "HTTP_#{key.upcase.tr('-', '_')}" if current_value = env[http_key] env[http_key] = "#{current_value}\n#{value}" else env[http_key] = value end end end |
#unwrap_request(request, env) ⇒ Object
Process the incoming request into a valid rack env.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/falcon/adapters/rack.rb', line 84 def unwrap_request(request, env) if content_type = request.headers.delete('content-type') env[CONTENT_TYPE] = content_type end # In some situations we don't know the content length, e.g. when using chunked encoding, or when decompressing the body. if body = request.body and length = body.length env[CONTENT_LENGTH] = length.to_s end self.unwrap_headers(request.headers, env) # HTTP/2 prefers `:authority` over `host`, so we do this for backwards compatibility. env[HTTP_HOST] ||= request. # This is the HTTP/1 header for the scheme of the request and is used by Rack. # Technically it should use the Forwarded header but this is not common yet. # https://tools.ietf.org/html/rfc7239#section-5.4 # https://github.com/rack/rack/issues/1310 env[HTTP_X_FORWARDED_PROTO] ||= request.scheme if remote_address = request.remote_address env[REMOTE_ADDR] = remote_address.ip_address if remote_address.ip? end end |