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

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

Constant Summary collapse

RACK_VERSION =
'rack.version'
RACK_MULTITHREAD =
'rack.multithread'
RACK_MULTIPROCESS =
'rack.multiprocess'
RACK_RUN_ONCE =
'rack.run_once'
RACK_IS_HIJACK =
'rack.hijack?'
RACK_HIJACK =
'rack.hijack'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#call, #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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/protocol/rack/adapter/rack2.rb', line 114

def self.make_response(env, response)
	# These interfaces should be largely compatible:
	headers = response.headers.to_h
	
	if protocol = response.protocol
		headers['rack.protocol'] = protocol
		# headers['upgrade'] = protocol
	end
	
	if body = response.body and body.stream?
		if env[RACK_IS_HIJACK]
			headers[RACK_HIJACK] = body
			body = []
		end
	end
	
	headers.transform_values! do |value|
		value.is_a?(Array) ? value.join("\n") : value
	end
	
	[response.status, headers, body]
end

.wrap(app) ⇒ Object



40
41
42
# File 'lib/protocol/rack/adapter/rack2.rb', line 40

def self.wrap(app)
	Rewindable.new(self.new(app))
end

Instance Method Details

#make_environment(request) ⇒ Object



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

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 request protocol, either from the upgrade header or the HTTP/2 pseudo header of the same name.
		RACK_PROTOCOL => request.protocol,
		
		# 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,
		CGI::SERVER_PORT => server_port,
	}
	
	self.unwrap_request(request, env)
	
	return env
end

#wrap_headers(fields) ⇒ Object

Process the rack response headers into into a HTTP::Headers instance, along with any extra `rack.` metadata.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/protocol/rack/adapter/rack2.rb', line 95

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
		else
			value.split("\n").each do |value|
				headers[key] = value
			end
		end
	end
	
	return headers, meta
end