Class: Protocol::Rack::Adapter::Generic
- Inherits:
-
Object
- Object
- Protocol::Rack::Adapter::Generic
- Defined in:
- lib/protocol/rack/adapter/generic.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#call(request) ⇒ Object
Build a rack
envfrom the incoming request and apply it to the rack middleware. -
#failure_response(exception) ⇒ Object
Generate a suitable response for the given exception.
-
#initialize(app) ⇒ Generic
constructor
Initialize the rack adaptor middleware.
- #logger ⇒ Object
-
#unwrap_headers(headers, env) ⇒ Object
Unwrap raw HTTP headers into the CGI-style expected by Rack middleware.
-
#unwrap_request(request, env) ⇒ Object
Process the incoming request into a valid rack
env.
Constructor Details
#initialize(app) ⇒ Generic
Initialize the rack adaptor middleware.
22 23 24 25 26 |
# File 'lib/protocol/rack/adapter/generic.rb', line 22 def initialize(app) @app = app raise ArgumentError, "App must be callable!" unless @app.respond_to?(:call) end |
Class Method Details
.wrap(app) ⇒ Object
16 17 18 |
# File 'lib/protocol/rack/adapter/generic.rb', line 16 def self.wrap(app) self.new(app) end |
Instance Method Details
#call(request) ⇒ Object
Build a rack env from the incoming request and apply it to the rack middleware.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/protocol/rack/adapter/generic.rb', line 84 def call(request) env = self.make_environment(request) status, headers, body = @app.call(env) headers, = self.wrap_headers(headers) return Response.wrap(env, status, headers, , body, request) rescue => exception Console.logger.error(self) {exception} body&.close if body.respond_to?(:close) env&.[](RACK_RESPONSE_FINISHED)&.each do |callback| callback.call(env, status, headers, exception) end return failure_response(exception) end |
#failure_response(exception) ⇒ Object
Generate a suitable response for the given exception.
107 108 109 |
# File 'lib/protocol/rack/adapter/generic.rb', line 107 def failure_response(exception) Protocol::HTTP::Response.for_exception(exception) end |
#logger ⇒ Object
28 29 30 |
# File 'lib/protocol/rack/adapter/generic.rb', line 28 def logger Console.logger end |
#unwrap_headers(headers, env) ⇒ Object
Unwrap raw HTTP headers into the CGI-style expected by Rack middleware.
Rack separates multiple headers with the same key, into a single field with multiple lines.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/protocol/rack/adapter/generic.rb', line 38 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};#{value}" else env[http_key] = value end end end |
#unwrap_request(request, env) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/protocol/rack/adapter/generic.rb', line 59 def unwrap_request(request, env) if content_type = request.headers.delete('content-type') env[CGI::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[CGI::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[CGI::HTTP_HOST] ||= request. if request.respond_to?(:remote_address) if remote_address = request.remote_address env[CGI::REMOTE_ADDR] = remote_address.ip_address if remote_address.ip? end end end |