Skip to content

Commit 33e5375

Browse files
committed
Add support for Rack 3.1 optional input.
1 parent c2b1674 commit 33e5375

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

.github/workflows/test.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ jobs:
3030
gemfile:
3131
- gems/rack-v1.rb
3232
- gems/rack-v2.rb
33-
- gems/rack-v3.rb
33+
- gems/rack-v30.rb
34+
- gems/rack-v31.rb
3435

3536
experimental: [false]
3637

gems/rack-v3.rb renamed to gems/rack-v30.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
eval_gemfile("gems.rb")
77

8-
gem "rack", "~> 3.0"
8+
gem "rack", "~> 3.0.0"

gems/rack-v31.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2022-2023, by Samuel Williams.
5+
6+
eval_gemfile("gems.rb")
7+
8+
gem "rack", "~> 3.1.0"

lib/protocol/rack/adapter.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
require "rack"
77

8-
require_relative "adapter/rack2"
9-
require_relative "adapter/rack3"
10-
118
module Protocol
129
module Rack
1310
module Adapter
14-
if ::Rack.release >= "3"
11+
if ::Rack.release >= "3.1"
12+
require_relative "adapter/rack31"
13+
IMPLEMENTATION = Rack31
14+
elsif ::Rack.release >= "3"
15+
require_relative "adapter/rack3"
1516
IMPLEMENTATION = Rack3
1617
else
18+
require_relative "adapter/rack2"
1719
IMPLEMENTATION = Rack2
1820
end
1921

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2022-2024, by Samuel Williams.
5+
6+
require "console"
7+
8+
require_relative "rack3"
9+
10+
module Protocol
11+
module Rack
12+
module Adapter
13+
class Rack31 < Rack3
14+
def make_environment(request)
15+
request_path, query_string = request.path.split("?", 2)
16+
server_name, server_port = (request.authority || "").split(":", 2)
17+
18+
env = {
19+
PROTOCOL_HTTP_REQUEST => request,
20+
21+
RACK_ERRORS => $stderr,
22+
RACK_LOGGER => self.logger,
23+
24+
# The request protocol, either from the upgrade header or the HTTP/2 pseudo header of the same name.
25+
RACK_PROTOCOL => request.protocol,
26+
27+
# The response finished callbacks:
28+
RACK_RESPONSE_FINISHED => [],
29+
30+
# The HTTP request method, such as “GET” or “POST”. This cannot ever be an empty string, and so is always required.
31+
CGI::REQUEST_METHOD => request.method,
32+
33+
# 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.
34+
CGI::SCRIPT_NAME => "",
35+
36+
# 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.
37+
CGI::PATH_INFO => request_path,
38+
CGI::REQUEST_PATH => request_path,
39+
CGI::REQUEST_URI => request.path,
40+
41+
# The portion of the request URL that follows the ?, if any. May be empty, but is always required!
42+
CGI::QUERY_STRING => query_string || "",
43+
44+
# The server protocol (e.g. HTTP/1.1):
45+
CGI::SERVER_PROTOCOL => request.version,
46+
47+
# The request scheme:
48+
RACK_URL_SCHEME => request.scheme,
49+
50+
# I'm not sure what sane defaults should be here:
51+
CGI::SERVER_NAME => server_name,
52+
CGI::SERVER_PORT => server_port,
53+
}
54+
55+
if body = request.body
56+
if body.empty?
57+
body.close
58+
else
59+
env[RACK_INPUT] = Input.new(body)
60+
end
61+
end
62+
63+
self.unwrap_request(request, env)
64+
65+
return env
66+
end
67+
end
68+
end
69+
end
70+
end

0 commit comments

Comments
 (0)