Skip to content

Commit

Permalink
Modernize gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 24, 2024
1 parent dc67087 commit 83ac7ff
Show file tree
Hide file tree
Showing 29 changed files with 247 additions and 245 deletions.
4 changes: 2 additions & 2 deletions config/sus.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.
# Copyright, 2022-2024, by Samuel Williams.

require 'covered/sus'
require "covered/sus"
include Covered::Sus
20 changes: 11 additions & 9 deletions examples/puma/config.ru
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
require 'protocol/http/middleware'
require_relative '../../lib/protocol/rack'
# frozen_string_literal: true

require "protocol/http/middleware"
require_relative "../../lib/protocol/rack"

# Your native application:
middleware = Protocol::HTTP::Middleware::HelloWorld

run proc{|env|
# Convert the rack request to a compatible rich request object:
request = Protocol::Rack::Request[env]
# Call your application
response = middleware.call(request)
Protocol::Rack::Adapter.make_response(env, response)
# Convert the rack request to a compatible rich request object:
request = Protocol::Rack::Request[env]
# Call your application
response = middleware.call(request)
Protocol::Rack::Adapter.make_response(env, response)
}
30 changes: 15 additions & 15 deletions examples/server/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.
# Copyright, 2022-2024, by Samuel Williams.

require 'async'
require 'async/http/server'
require 'async/http/client'
require 'async/http/endpoint'
require_relative '../../lib/protocol/rack/adapter'
require "async"
require "async/http/server"
require "async/http/client"
require "async/http/endpoint"
require_relative "../../lib/protocol/rack/adapter"

app = proc{|env| [200, {}, ["Hello World"]]}
middleware = Protocol::Rack::Adapter.new(app)

Async do
endpoint = Async::HTTP::Endpoint.parse("http://localhost:9292")
server_task = Async(transient: true) do
server = Async::HTTP::Server.new(middleware, endpoint)
server.run
end
client = Async::HTTP::Client.new(endpoint)
pp client.get("/").read
endpoint = Async::HTTP::Endpoint.parse("http://localhost:9292")
server_task = Async(transient: true) do
server = Async::HTTP::Server.new(middleware, endpoint)
server.run
end
client = Async::HTTP::Client.new(endpoint)
pp client.get("/").read
end
6 changes: 3 additions & 3 deletions fixtures/server_context.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.
# Copyright, 2022-2024, by Samuel Williams.

require 'sus/fixtures/async/http/server_context'
require "sus/fixtures/async/http/server_context"

module ServerContext
include Sus::Fixtures::Async::HTTP::ServerContext

def app
->(env){[200, {}, ['Hello World!']]}
->(env){[200, {}, ["Hello World!"]]}
end

def middleware
Expand Down
6 changes: 3 additions & 3 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
gemspec

group :maintenance, optional: true do
gem 'bake-modernize'
gem 'bake-gem'
gem "bake-modernize"
gem "bake-gem"

gem 'utopia-project'
gem "utopia-project"
end

group :test do
Expand Down
10 changes: 5 additions & 5 deletions lib/protocol/rack.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.
# Copyright, 2022-2024, by Samuel Williams.

require_relative 'rack/version'
require_relative 'rack/adapter'
require_relative 'rack/request'
require_relative 'rack/response'
require_relative "rack/version"
require_relative "rack/adapter"
require_relative "rack/request"
require_relative "rack/response"
6 changes: 3 additions & 3 deletions lib/protocol/rack/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Released under the MIT License.
# Copyright, 2022-2024, by Samuel Williams.

require 'rack'
require "rack"

require_relative 'adapter/rack2'
require_relative 'adapter/rack3'
require_relative "adapter/rack2"
require_relative "adapter/rack3"

module Protocol
module Rack
Expand Down
20 changes: 10 additions & 10 deletions lib/protocol/rack/adapter/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# Released under the MIT License.
# Copyright, 2022-2024, by Samuel Williams.

require 'console'
require "console"

require_relative '../constants'
require_relative '../input'
require_relative '../response'
require_relative "../constants"
require_relative "../input"
require_relative "../response"

module Protocol
module Rack
Expand Down Expand Up @@ -66,7 +66,7 @@ def unwrap_headers(headers, env)
# @parameter request [Protocol::HTTP::Request] The incoming request.
# @parameter env [Hash] The rack `env`.
def unwrap_request(request, env)
if content_type = request.headers.delete('content-type')
if content_type = request.headers.delete("content-type")
env[CGI::CONTENT_TYPE] = content_type
end

Expand All @@ -78,7 +78,7 @@ def unwrap_request(request, env)
self.unwrap_headers(request.headers, env)

# For the sake of compatibility, we set the `HTTP_UPGRADE` header to the requested protocol.
if protocol = request.protocol and request.version.start_with?('HTTP/1')
if protocol = request.protocol and request.version.start_with?("HTTP/1")
env[CGI::HTTP_UPGRADE] = Array(protocol).join(",")
end

Expand Down Expand Up @@ -146,13 +146,13 @@ def failure_response(exception)
def self.extract_protocol(env, response, headers)
if protocol = response.protocol
# This is the newer mechanism for protocol upgrade:
if env['rack.protocol']
headers['rack.protocol'] = protocol
if env["rack.protocol"]
headers["rack.protocol"] = protocol

# Older mechanism for protocol upgrade:
elsif env[CGI::HTTP_UPGRADE]
headers['upgrade'] = protocol
headers['connection'] = 'upgrade'
headers["upgrade"] = protocol
headers["connection"] = "upgrade"
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions lib/protocol/rack/adapter/rack2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
# Released under the MIT License.
# Copyright, 2022-2024, by Samuel Williams.

require 'console'
require "console"

require_relative 'generic'
require_relative '../rewindable'
require_relative "generic"
require_relative "../rewindable"

module Protocol
module Rack
module Adapter
class Rack2 < Generic
RACK_VERSION = 'rack.version'
RACK_MULTITHREAD = 'rack.multithread'
RACK_MULTIPROCESS = 'rack.multiprocess'
RACK_RUN_ONCE = 'rack.run_once'
RACK_VERSION = "rack.version"
RACK_MULTITHREAD = "rack.multithread"
RACK_MULTIPROCESS = "rack.multiprocess"
RACK_RUN_ONCE = "rack.run_once"

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

def make_environment(request)
request_path, query_string = request.path.split('?', 2)
server_name, server_port = (request.authority || '').split(':', 2)
request_path, query_string = request.path.split("?", 2)
server_name, server_port = (request.authority || "").split(":", 2)

env = {
RACK_VERSION => [2, 0],
Expand All @@ -44,15 +44,15 @@ def make_environment(request)
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 => '',
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 || '',
CGI::QUERY_STRING => query_string || "",

# The server protocol (e.g. HTTP/1.1):
CGI::SERVER_PROTOCOL => request.version,
Expand Down Expand Up @@ -103,7 +103,7 @@ def wrap_headers(fields)
fields.each do |key, value|
key = key.downcase

if key.start_with?('rack.')
if key.start_with?("rack.")
meta[key] = value
elsif value.is_a?(String)
value.split("\n").each do |value|
Expand Down
14 changes: 7 additions & 7 deletions lib/protocol/rack/adapter/rack3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Released under the MIT License.
# Copyright, 2022-2024, by Samuel Williams.

require 'console'
require "console"

require_relative 'generic'
require_relative "generic"

module Protocol
module Rack
Expand All @@ -20,8 +20,8 @@ def self.parse_file(...)
end

def make_environment(request)
request_path, query_string = request.path.split('?', 2)
server_name, server_port = (request.authority || '').split(':', 2)
request_path, query_string = request.path.split("?", 2)
server_name, server_port = (request.authority || "").split(":", 2)

env = {
PROTOCOL_HTTP_REQUEST => request,
Expand All @@ -40,15 +40,15 @@ def make_environment(request)
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 => '',
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 || '',
CGI::QUERY_STRING => query_string || "",

# The server protocol (e.g. HTTP/1.1):
CGI::SERVER_PROTOCOL => request.version,
Expand All @@ -75,7 +75,7 @@ def wrap_headers(fields)
fields.each do |key, value|
key = key.downcase

if key.start_with?('rack.')
if key.start_with?("rack.")
meta[key] = value
elsif value.is_a?(Array)
value.each do |value|
Expand Down
10 changes: 5 additions & 5 deletions lib/protocol/rack/body.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.
# Copyright, 2022-2024, by Samuel Williams.

require_relative 'body/streaming'
require_relative 'body/enumerable'
require 'protocol/http/body/completable'
require_relative "body/streaming"
require_relative "body/enumerable"
require "protocol/http/body/completable"

module Protocol
module Rack
module Body
CONTENT_LENGTH = 'content-length'
CONTENT_LENGTH = "content-length"

def self.wrap(env, status, headers, body, input = nil)
# In no circumstance do we want this header propagating out:
Expand Down
8 changes: 4 additions & 4 deletions lib/protocol/rack/body/enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.
# Copyright, 2022-2024, by Samuel Williams.

require 'protocol/http/body/readable'
require 'protocol/http/body/file'
require "protocol/http/body/readable"
require "protocol/http/body/file"

module Protocol
module Rack
Expand All @@ -13,7 +13,7 @@ module Body
#
# The `rack` body must respond to `each` and must only yield `String` values. If the body responds to `close`, it will be called after iteration.
class Enumerable < ::Protocol::HTTP::Body::Readable
CONTENT_LENGTH = 'content-length'.freeze
CONTENT_LENGTH = "content-length".freeze

# Wraps an array into a buffered body.
# @parameter body [Object] The `rack` response body.
Expand Down
6 changes: 3 additions & 3 deletions lib/protocol/rack/body/input_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.
# Copyright, 2022-2024, by Samuel Williams.

require 'protocol/http/body/readable'
require 'protocol/http/body/stream'
require "protocol/http/body/readable"
require "protocol/http/body/stream"

module Protocol
module Rack
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/rack/body/streaming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Released under the MIT License.
# Copyright, 2022-2024, by Samuel Williams.

require 'protocol/http/body/streamable'
require "protocol/http/body/streamable"

module Protocol
module Rack
Expand Down
Loading

0 comments on commit 83ac7ff

Please sign in to comment.