Class: Protocol::Rack::Adapter::Rack3
- Defined in:
- lib/protocol/rack/adapter/rack3.rb
Overview
The Rack 3 adapter provides compatibility with Rack 3.x applications. It handles the conversion between HTTP and Rack 3 environments. Unlike Rack 2, this adapter supports streaming responses and has a simpler environment setup.
Direct Known Subclasses
Class Method Summary collapse
-
.make_response(env, response) ⇒ Object
Convert a HTTP::Response into a Rack 3 response tuple.
-
.parse_file ⇒ Object
Parses a Rackup file and returns the application.
-
.wrap(app) ⇒ Object
Creates a new adapter instance for the given Rack application.
Instance Method Summary collapse
-
#make_environment(request) ⇒ Object
Create a Rack 3 environment hash for the request.
-
#wrap_headers(fields) ⇒ Object
Process the rack response headers into a HTTP::Headers instance, along with any extra ‘rack.` metadata.
Methods inherited from Generic
#call, extract_protocol, #failure_response, #handle_error, #initialize, #logger, #unwrap_headers, #unwrap_request
Constructor Details
This class inherits a constructor from Protocol::Rack::Adapter::Generic
Class Method Details
.make_response(env, response) ⇒ Object
Convert a HTTP::Response into a Rack 3 response tuple. Handles protocol upgrades and streaming responses. Unlike Rack 2, this adapter forces streaming responses by converting the body to a callable.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/protocol/rack/adapter/rack3.rb', line 123 def self.make_response(env, response) # These interfaces should be largely compatible: headers = response.headers.to_h self.extract_protocol(env, response, headers) if body = response.body and body.stream? # Force streaming response: body = body.method(:call) end [response.status, headers, body] end |
.parse_file ⇒ Object
Parses a Rackup file and returns the application. Uses the Rack 3.x interface for parsing Rackup files.
32 33 34 |
# File 'lib/protocol/rack/adapter/rack3.rb', line 32 def self.parse_file(...) ::Rack::Builder.parse_file(...) end |
.wrap(app) ⇒ Object
Creates a new adapter instance for the given Rack application. Unlike Rack 2, this adapter doesn’t require a Rewindable wrapper.
23 24 25 |
# File 'lib/protocol/rack/adapter/rack3.rb', line 23 def self.wrap(app) self.new(app) end |
Instance Method Details
#make_environment(request) ⇒ Object
Create a Rack 3 environment hash for the request. Sets up all required Rack 3 environment variables and processes the request. Unlike Rack 2, this adapter doesn’t set Rack version or threading flags.
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 |
# File 'lib/protocol/rack/adapter/rack3.rb', line 42 def make_environment(request) request_path, query_string = request.path.split("?", 2) server_name, server_port = (request. || "").split(":", 2) env = { PROTOCOL_HTTP_REQUEST => request, RACK_INPUT => Input.new(request.body), RACK_ERRORS => $stderr, RACK_LOGGER => self.logger, # The response finished callbacks: RACK_RESPONSE_FINISHED => [], # The HTTP request method, such as "GET" or "POST". This cannot ever be an empty string, and so is always required. CGI::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. CGI::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. CGI::PATH_INFO => request_path, CGI::REQUEST_PATH => request_path, CGI::REQUEST_URI => request.path, # The portion of the request URL that follows the ?, if any. May be empty, but is always required! CGI::QUERY_STRING => query_string || "", # The server protocol (e.g. HTTP/1.1): CGI::SERVER_PROTOCOL => request.version, # The request scheme: RACK_URL_SCHEME => request.scheme, # I'm not sure what sane defaults should be here: CGI::SERVER_NAME => server_name, } # SERVER_PORT is optional but must not be set if it is not present. if server_port env[CGI::SERVER_PORT] = server_port end self.unwrap_request(request, env) return env end |
#wrap_headers(fields) ⇒ Object
Process the rack response headers into a HTTP::Headers instance, along with any extra ‘rack.` metadata. Unlike Rack 2, this adapter handles array values directly without splitting on newlines.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/protocol/rack/adapter/rack3.rb', line 95 def wrap_headers(fields) headers = ::Protocol::HTTP::Headers.new = {} fields.each do |key, value| key = key.downcase if key.start_with?("rack.") [key] = value elsif value.is_a?(Array) value.each do |value| headers.add(key, value) end else headers.add(key, value) end end return headers, end |