Skip to content

Commit 5881050

Browse files
Improved documentation.
1 parent 1b4f903 commit 5881050

File tree

9 files changed

+67
-67
lines changed

9 files changed

+67
-67
lines changed

lib/protocol/rack/adapter.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ module Adapter
3232

3333
# Creates a new adapter instance for the given Rack application.
3434
#
35-
# @parameter app [Interface(:call)] A Rack application that responds to #call
36-
# @returns [Protocol::HTTP::Middleware] An adapter that can handle HTTP requests
35+
# @parameter app [Interface(:call)] A Rack application that responds to `call`.
36+
# @returns [Protocol::HTTP::Middleware] An adapter that can handle HTTP requests.
3737
def self.new(app)
3838
IMPLEMENTATION.wrap(app)
3939
end
4040

41-
# Converts a Rack response into a Protocol::HTTP response.
41+
# Converts a Rack response into a {Protocol::HTTP::Response}.
4242
#
43-
# @parameter env [Hash] The Rack environment
44-
# @parameter response [Array] The Rack response [status, headers, body]
45-
# @returns [Protocol::HTTP::Response] A Protocol::HTTP response
43+
# @parameter env [Hash] The Rack environment.
44+
# @parameter response [Array] The Rack response tuple `[status, headers, body]`.
45+
# @returns [Protocol::HTTP::Response] A Protocol::HTTP response.
4646
def self.make_response(env, response)
4747
IMPLEMENTATION.make_response(env, response)
4848
end
4949

50-
# Parses a file path from the Rack environment.
50+
# Parses a Rackup file and returns the application.
5151
#
52-
# @parameter env [Hash] The Rack environment
53-
# @returns [String | Nil] The parsed file path or nil if not found
52+
# @parameter path [String] The path to the Rackup file.
53+
# @returns [Interface(:call)] The parsed Rack application.
5454
def self.parse_file(...)
5555
IMPLEMENTATION.parse_file(...)
5656
end

lib/protocol/rack/adapter/generic.rb

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ def self.wrap(app)
3030
# @parameter path [String] The path to the Rackup file.
3131
# @returns [Interface(:call)] The Rack application.
3232
def self.parse_file(...)
33-
# This is the old interface, which was changed in Rack 3.
33+
# This is the old interface, which was changed in Rack 3:
3434
::Rack::Builder.parse_file(...).first
3535
end
3636

37-
# Initialize the rack adaptor middleware.
37+
# Initialize the Rack adapter middleware.
3838
#
39-
# @parameter app [Interface(:call)] The rack middleware.
39+
# @parameter app [Interface(:call)] The Rack middleware.
4040
# @raises [ArgumentError] If the app does not respond to `call`.
4141
def initialize(app)
4242
@app = app
@@ -51,20 +51,20 @@ def logger
5151
Console
5252
end
5353

54-
# Unwrap HTTP headers into the CGI-style expected by Rack middleware, and add them to the rack `env`.
54+
# Unwrap HTTP headers into the CGI-style expected by Rack middleware, and add them to the Rack `env`.
5555
#
56-
# e.g. `accept-encoding` becomes `HTTP_ACCEPT_ENCODING`.
56+
# For example, `accept-encoding` becomes `HTTP_ACCEPT_ENCODING`.
5757
#
58-
# Headers keys with underscores will generate the same CGI-style header key as headers with dashes.
58+
# Header keys with underscores will generate the same CGI-style header key as headers with dashes.
5959
#
60-
# e.g `accept_encoding` becomes `HTTP_ACCEPT_ENCODING` too.
60+
# For example, `accept_encoding` becomes `HTTP_ACCEPT_ENCODING` too.
6161
#
6262
# You should not implicitly trust the `HTTP_` headers for security purposes, as they are generated by the client.
6363
#
6464
# Multiple headers are combined with a comma, with one exception: `HTTP_COOKIE` headers are combined with a semicolon.
6565
#
6666
# @parameter headers [Protocol::HTTP::Headers] The raw HTTP request headers.
67-
# @parameter env [Hash] The rack request `env`.
67+
# @parameter env [Hash] The Rack request `env`.
6868
def unwrap_headers(headers, env)
6969
headers.each do |key, value|
7070
http_key = "HTTP_#{key.upcase.tr('-', '_')}"
@@ -81,17 +81,17 @@ def unwrap_headers(headers, env)
8181
end
8282
end
8383

84-
# Process the incoming request into a valid rack `env`.
84+
# Process the incoming request into a valid Rack `env`.
8585
#
8686
# - Set the `env['CONTENT_TYPE']` and `env['CONTENT_LENGTH']` based on the incoming request body.
8787
# - Set the `env['HTTP_HOST']` header to the request authority.
8888
# - Set the `env['HTTP_X_FORWARDED_PROTO']` header to the request scheme.
89-
# - Set `env['REMOTE_ADDR']` to the request remote adress.
89+
# - Set `env['REMOTE_ADDR']` to the request remote address.
9090
#
9191
# @parameter request [Protocol::HTTP::Request] The incoming request.
9292
# @parameter env [Hash] The rack `env`.
9393
def unwrap_request(request, env)
94-
# The request protocol, either from the upgrade header or the HTTP/2 pseudo header of the same name.
94+
# The request protocol, either from the upgrade header or the HTTP/2 pseudo header of the same name:
9595
if protocol = request.protocol
9696
env[RACK_PROTOCOL] = protocol
9797
end
@@ -100,15 +100,15 @@ def unwrap_request(request, env)
100100
env[CGI::CONTENT_TYPE] = content_type
101101
end
102102

103-
# In some situations we don't know the content length, e.g. when using chunked encoding, or when decompressing the body.
103+
# In some situations we don't know the content length, e.g. when using chunked encoding, or when decompressing the body:
104104
if body = request.body and length = body.length
105105
env[CGI::CONTENT_LENGTH] = length.to_s
106106
end
107107

108108
# We ignore trailers for the purpose of constructing the rack environment:
109109
self.unwrap_headers(request.headers.header, env)
110110

111-
# For the sake of compatibility, we set the `HTTP_UPGRADE` header to the requested protocol.
111+
# For the sake of compatibility, we set the `HTTP_UPGRADE` header to the requested protocol:
112112
if protocol = request.protocol and request.version.start_with?("HTTP/1")
113113
env[CGI::HTTP_UPGRADE] = Array(protocol).join(",")
114114
end
@@ -118,7 +118,7 @@ def unwrap_request(request, env)
118118
env[RACK_HIJACK] = proc{request.hijack!.io}
119119
end
120120

121-
# HTTP/2 prefers `:authority` over `host`, so we do this for backwards compatibility.
121+
# HTTP/2 prefers `:authority` over `host`, so we do this for backwards compatibility:
122122
env[CGI::HTTP_HOST] ||= request.authority
123123

124124
if peer = request.peer
@@ -149,26 +149,23 @@ def make_environment(request)
149149
def handle_error(env, status, headers, body, error)
150150
Console.error(self, "Error occurred during request processing:", error)
151151

152-
# Close the response body if it exists and supports closing.
152+
# Close the response body if it exists and supports closing:
153153
body&.close if body.respond_to?(:close)
154154

155-
# Invoke `rack.response_finished` callbacks in reverse order of registration.
156-
# This ensures that callbacks registered later are invoked first, matching the Rack specification.
155+
# Invoke `rack.response_finished` callbacks in reverse order of registration. This ensures that callbacks registered later are invoked first, matching the Rack specification.
157156
env&.[](RACK_RESPONSE_FINISHED)&.reverse_each do |callback|
158157
begin
159158
callback.call(env, status, headers, error)
160159
rescue => callback_error
161-
# If a callback raises an exception, log it but continue invoking other callbacks.
162-
# The Rack specification states that callbacks should not raise exceptions, but we handle
163-
# this gracefully to prevent one misbehaving callback from breaking others.
160+
# If a callback raises an exception, log it but continue invoking other callbacks. The Rack specification states that callbacks should not raise exceptions, but we handle this gracefully to prevent one misbehaving callback from breaking others.
164161
Console.error(self, "Error occurred during response finished callback:", callback_error)
165162
end
166163
end
167164

168165
return failure_response(error)
169166
end
170167

171-
# Build a rack `env` from the incoming request and apply it to the rack middleware.
168+
# Build a Rack `env` from the incoming request and apply it to the Rack middleware.
172169
#
173170
# @parameter request [Protocol::HTTP::Request] The incoming request.
174171
# @returns [Protocol::HTTP::Response] The HTTP response.
@@ -178,12 +175,12 @@ def call(request)
178175

179176
status, headers, body = @app.call(env)
180177

181-
# The status must always be an integer.
178+
# The status must always be an integer:
182179
unless status.is_a?(Integer)
183180
raise ArgumentError, "Status must be an integer!"
184181
end
185182

186-
# Headers must always be a hash or equivalent.
183+
# Headers must always be a hash or equivalent:
187184
unless headers
188185
raise ArgumentError, "Headers must not be nil!"
189186
end
@@ -205,7 +202,7 @@ def failure_response(exception)
205202

206203
# Extract protocol information from the environment and response.
207204
#
208-
# @parameter env [Hash] The rack environment.
205+
# @parameter env [Hash] The Rack environment.
209206
# @parameter response [Protocol::HTTP::Response] The HTTP response.
210207
# @parameter headers [Hash] The response headers to modify.
211208
def self.extract_protocol(env, response, headers)

lib/protocol/rack/adapter/rack2.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def make_environment(request)
7979
return env
8080
end
8181

82-
# Build a rack `env` from the incoming request and apply it to the rack middleware.
82+
# Build a Rack `env` from the incoming request and apply it to the Rack middleware.
8383
#
8484
# @parameter request [Protocol::HTTP::Request] The incoming request.
8585
# @returns [Protocol::HTTP::Response] The HTTP response.
@@ -105,7 +105,7 @@ def call(request)
105105

106106
headers, meta = self.wrap_headers(headers)
107107

108-
# Rack 2 spec does not allow only partial hijacking.
108+
# Rack 2 spec does not allow only partial hijacking:
109109
# if hijack_body = meta[RACK_HIJACK]
110110
# body = hijack_body
111111
# end
@@ -115,7 +115,7 @@ def call(request)
115115
return self.handle_error(env, status, headers, body, error)
116116
end
117117

118-
# Process the rack response headers into a {Protocol::HTTP::Headers} instance, along with any extra `rack.` metadata.
118+
# Process the Rack response headers into a {Protocol::HTTP::Headers} instance, along with any extra `rack.` metadata.
119119
# Headers with newline-separated values are split into multiple headers.
120120
#
121121
# @parameter fields [Hash] The raw response headers.
@@ -144,9 +144,9 @@ def wrap_headers(fields)
144144
# Convert a {Protocol::HTTP::Response} into a Rack 2 response tuple.
145145
# Handles protocol upgrades and streaming responses.
146146
#
147-
# @parameter env [Hash] The rack environment.
147+
# @parameter env [Hash] The Rack environment.
148148
# @parameter response [Protocol::HTTP::Response] The HTTP response.
149-
# @returns [Tuple(Integer, Hash, Object)] The Rack 2 response tuple [status, headers, body].
149+
# @returns [Tuple(Integer, Hash, Object)] The Rack 2 response tuple `[status, headers, body]`.
150150
def self.make_response(env, response)
151151
# These interfaces should be largely compatible:
152152
headers = response.headers.to_h

lib/protocol/rack/adapter/rack3.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ def wrap_headers(fields)
117117
# Handles protocol upgrades and streaming responses.
118118
# Unlike Rack 2, this adapter forces streaming responses by converting the body to a callable.
119119
#
120-
# @parameter env [Hash] The rack environment.
120+
# @parameter env [Hash] The Rack environment.
121121
# @parameter response [Protocol::HTTP::Response] The HTTP response.
122-
# @returns [Tuple(Integer, Hash, Object)] The Rack 3 response tuple [status, headers, body].
122+
# @returns [Tuple(Integer, Hash, Object)] The Rack 3 response tuple `[status, headers, body]`.
123123
def self.make_response(env, response)
124124
# These interfaces should be largely compatible:
125125
headers = response.headers.to_h

lib/protocol/rack/body.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ def self.no_content?(status)
4545
def self.wrap(env, status, headers, body, input = nil, head = false)
4646
# In no circumstance do we want this header propagating out:
4747
if length = headers.delete(CONTENT_LENGTH)
48-
# We don't really trust the user to provide the right length to the transport.
48+
# We don't really trust the user to provide the right length to the transport:
4949
length = Integer(length)
5050
end
5151

52-
# If we have an Async::HTTP body, we return it directly:
52+
# If we have a Protocol::HTTP body, we return it directly:
5353
if body.is_a?(::Protocol::HTTP::Body::Readable)
5454
# Ignore.
5555
elsif status == 200 and body.respond_to?(:to_path)
5656
begin
57-
# Don't mangle partial responses (206)
57+
# Don't mangle partial responses (206):
5858
body = ::Protocol::HTTP::Body::File.open(body.to_path).tap do
5959
body.close if body.respond_to?(:close) # Close the original body.
6060
end
6161
rescue Errno::ENOENT
62-
# If the file is not available, ignore.
62+
# If the file is not available, ignore:
6363
end
6464
elsif body.respond_to?(:each)
6565
body = Body::Enumerable.wrap(body, length)
@@ -116,14 +116,12 @@ def self.wrap(env, status, headers, body, input = nil, head = false)
116116
# @returns [Proc] A callback that calls all response finished handlers.
117117
def self.completion_callback(response_finished, env, status, headers)
118118
proc do |error|
119-
# Invoke callbacks in reverse order of registration, as specified by the Rack specification.
119+
# Callbacks are invoked in reverse order of registration, as required by the Rack specification:
120120
response_finished.reverse_each do |callback|
121121
begin
122122
callback.call(env, status, headers, error)
123123
rescue => callback_error
124-
# If a callback raises an exception, log it but continue invoking other callbacks.
125-
# The Rack specification states that callbacks should not raise exceptions, but we handle
126-
# this gracefully to prevent one misbehaving callback from breaking others.
124+
# If a callback raises an exception, log it but continue invoking other callbacks. The Rack specification states that callbacks should not raise exceptions, but we handle this gracefully to prevent one misbehaving callback from breaking others:
127125
Console.error(self, "Error occurred during response finished callback:", callback_error)
128126
end
129127
end

lib/protocol/rack/body/streaming.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module Rack
1010
module Body
1111
# Wraps a Rack streaming response body.
1212
# The body must be callable and accept a stream argument.
13-
# This is typically used for Rack hijack responses or bodies wrapped in `Rack::BodyProxy`.
1413
# When closed, this class ensures the wrapped body's `close` method is called if it exists.
1514
class Streaming < ::Protocol::HTTP::Body::Streamable::ResponseBody
1615
# Initialize the streaming body wrapper.

lib/protocol/rack/constants.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
module Protocol
77
module Rack
8-
# Used for injecting the raw request in the the rack environment.
8+
# Used for injecting the raw request into the Rack environment.
99
PROTOCOL_HTTP_REQUEST = "protocol.http.request"
1010

11-
# CGI keys <https://tools.ietf.org/html/rfc3875#section-4.1>:
11+
# CGI environment variable keys as defined in [RFC 3875](https://tools.ietf.org/html/rfc3875#section-4.1).
1212
module CGI
1313
HTTP_HOST = "HTTP_HOST"
1414
HTTP_UPGRADE = "HTTP_UPGRADE"
@@ -27,19 +27,19 @@ module CGI
2727

2828
HTTP_COOKIE = "HTTP_COOKIE"
2929

30-
# Header constants:
30+
# Additional HTTP header constants.
3131
HTTP_X_FORWARDED_PROTO = "HTTP_X_FORWARDED_PROTO"
3232
end
3333

34-
# Rack environment variables:
34+
# Rack environment variable keys.
3535
RACK_ERRORS = "rack.errors"
3636
RACK_LOGGER = "rack.logger"
3737
RACK_INPUT = "rack.input"
3838
RACK_URL_SCHEME = "rack.url_scheme"
3939
RACK_PROTOCOL = "rack.protocol"
4040
RACK_RESPONSE_FINISHED = "rack.response_finished"
4141

42-
# Rack hijack support:
42+
# Rack hijack support keys.
4343
RACK_IS_HIJACK = "rack.hijack?"
4444
RACK_HIJACK = "rack.hijack"
4545
end

lib/protocol/rack/request.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
module Protocol
1313
module Rack
1414
# A Rack-compatible HTTP request wrapper.
15-
# This class provides a bridge between Rack's environment hash and Protocol::HTTP::Request.
16-
# It handles conversion of Rack environment variables to HTTP request properties.
15+
#
16+
# This class provides a bridge between Rack's environment hash and {Protocol::HTTP::Request}. It handles conversion of Rack environment variables to HTTP request properties.
1717
class Request < ::Protocol::HTTP::Request
1818
# Get or create a Request instance for the given Rack environment.
1919
# The request is cached in the environment to avoid creating multiple instances.
@@ -43,7 +43,8 @@ def initialize(env)
4343
end
4444

4545
# Extract the protocol list from the Rack environment.
46-
# Checks both `rack.protocol` and `HTTP_UPGRADE` headers.
46+
#
47+
# Checks both `rack.protocol` and {CGI::HTTP_UPGRADE} headers.
4748
#
4849
# @parameter env [Hash] The Rack environment hash.
4950
# @returns [Array(String) | Nil] The list of protocols or `nil` if none specified.

lib/protocol/rack/response.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212

1313
module Protocol
1414
module Rack
15-
# A wrapper for a `Rack` response.
15+
# A Rack-compatible HTTP response wrapper
1616
#
17-
# A Rack response consisting of `[status, headers, body]` includes various rack-specific elements, including:
17+
# A Rack response consisting of `[status, headers, body]` includes various Rack-specific elements, including:
1818
#
1919
# - A `headers['rack.hijack']` callback which bypasses normal response handling.
2020
# - Potentially invalid content length.
2121
# - Potentially invalid body when processing a `HEAD` request.
2222
# - Newline-separated header values.
2323
# - Other `rack.` specific header key/value pairs.
2424
#
25-
# This wrapper takes those issues into account and adapts the rack response tuple into a {Protocol::HTTP::Response}.
25+
# This wrapper takes those issues into account and adapts the Rack response tuple into a {Protocol::HTTP::Response}.
2626
class Response < ::Protocol::HTTP::Response
2727
# HTTP hop headers which *should* not be passed through the proxy.
2828
HOP_HEADERS = [
@@ -34,11 +34,15 @@ class Response < ::Protocol::HTTP::Response
3434
"upgrade",
3535
]
3636

37-
# Wrap a rack response.
38-
# @parameter status [Integer] The rack response status.
39-
# @parameter headers [Duck(:each)] The rack response headers.
40-
# @parameter body [Duck(:each, :close) | Nil] The rack response body.
41-
# @parameter request [Protocol::HTTP::Request] The original request.
37+
# Wrap a Rack response into a {Response} instance.
38+
#
39+
# @parameter env [Hash] The Rack environment hash.
40+
# @parameter status [Integer] The Rack response status code.
41+
# @parameter headers [Protocol::HTTP::Headers] The response headers.
42+
# @parameter meta [Hash] The Rack-specific metadata (e.g., `rack.hijack`).
43+
# @parameter body [Object] The Rack response body.
44+
# @parameter request [Protocol::HTTP::Request | Nil] The original request.
45+
# @returns [Response] A new response instance.
4246
def self.wrap(env, status, headers, meta, body, request = nil)
4347
ignored = headers.extract(HOP_HEADERS)
4448

@@ -64,10 +68,11 @@ def self.wrap(env, status, headers, meta, body, request = nil)
6468
end
6569

6670
# Initialize the response wrapper.
67-
# @parameter status [Integer] The response status.
71+
#
72+
# @parameter status [Integer] The response status code.
6873
# @parameter headers [Protocol::HTTP::Headers] The response headers.
6974
# @parameter body [Protocol::HTTP::Body] The response body.
70-
# @parameter protocol [String] The response protocol for upgraded requests.
75+
# @parameter protocol [String | Nil] The response protocol for upgraded requests.
7176
def initialize(status, headers, body, protocol = nil)
7277
super(nil, status, headers, body, protocol)
7378
end

0 commit comments

Comments
 (0)