Class: Protocol::Rack::Adapter::Rack2

Inherits:
Generic
  • Object
show all
Defined in:
lib/protocol/rack/adapter/rack2.rb

Overview

The Rack 2 adapter provides compatibility with Rack 2.x applications. It handles the conversion between HTTP and Rack 2 environments.

Constant Summary collapse

RACK_VERSION =

The Rack version constant.

"rack.version"
RACK_MULTITHREAD =

Whether the application is multithreaded.

"rack.multithread"
RACK_MULTIPROCESS =

Whether the application is multiprocess.

"rack.multiprocess"
RACK_RUN_ONCE =

Whether the application should run only once.

"rack.run_once"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

extract_protocol, #failure_response, #handle_error, #initialize, #logger, parse_file, #unwrap_headers, #unwrap_request, wrap

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 2 response tuple. Handles protocol upgrades and streaming responses.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/protocol/rack/adapter/rack2.rb', line 150

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?
    if env[RACK_IS_HIJACK]
      headers[RACK_HIJACK] = body
      body = []
    end
  end
  
  headers.transform_values! do |value|
    if value.is_a?(Array)
      value.join("\n")
    else
      value
    end
  end
  
  [response.status, headers, body]
end

Instance Method Details

#call(request) ⇒ Object

Build a rack ‘env` from the incoming request and apply it to the rack middleware.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/protocol/rack/adapter/rack2.rb', line 87

def call(request)
  env = self.make_environment(request)
  
  status, headers, body = @app.call(env)
  
  if status
    status = status.to_i
  else
    raise ArgumentError, "Status must be an integer!"
  end
  
  unless headers
    raise ArgumentError, "Headers must not be nil!"
  end
  
  # unless body.respond_to?(:each)
  #  raise ArgumentError, "Body must respond to #each!"
  # end
  
  headers, meta = self.wrap_headers(headers)
  
  # Rack 2 spec does not allow only partial hijacking.
  # if hijack_body = meta[RACK_HIJACK]
  #  body = hijack_body
  # end
  
  return Response.wrap(env, status, headers, meta, body, request)
rescue => error
  return self.handle_error(env, status, headers, body, error)
end

#make_environment(request) ⇒ Object

Create a Rack 2 environment hash for the request. Sets up all required Rack 2 environment variables and processes the request.



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
# File 'lib/protocol/rack/adapter/rack2.rb', line 32

def make_environment(request)
  request_path, query_string = request.path.split("?", 2)
  server_name, server_port = (request.authority || "").split(":", 2)
  
  env = {
    RACK_VERSION => [2, 0],
    RACK_MULTITHREAD => false,
    RACK_MULTIPROCESS => true,
    RACK_RUN_ONCE => false,
    
    PROTOCOL_HTTP_REQUEST => request,
    
    RACK_INPUT => Input.new(request.body),
    RACK_ERRORS => $stderr,
    RACK_LOGGER => self.logger,
    
    # 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. Headers with newline-separated values are split into multiple headers.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/protocol/rack/adapter/rack2.rb', line 123

def wrap_headers(fields)
  headers = ::Protocol::HTTP::Headers.new
  meta = {}
  
  fields.each do |key, value|
    key = key.downcase
    
    if key.start_with?("rack.")
      meta[key] = value
    elsif value.is_a?(String)
      value.split("\n").each do |value|
        headers.add(key, value)
      end
    else
      headers.add(key, value)
    end
  end
  
  return headers, meta
end