Skip to content

Commit

Permalink
Merge branch 'main' into json-body-parser-error-class
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 7, 2023
2 parents 830c578 + a9cd089 commit 4d7dc55
Show file tree
Hide file tree
Showing 49 changed files with 262 additions and 200 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: CI

on: [push, pull_request]

permissions:
contents: read

jobs:
test:
name: Ruby ${{ matrix.ruby }} Rack ${{ matrix.rack }}
strategy:
fail-fast: false
matrix:
os: [ ubuntu-20.04 ]
rack: [ '~> 2.0', '~> 3.0' ]
ruby: [ 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1, 3.2 ]
gemfile: [ Gemfile ]
exclude:
# Rack 3 needs >= Ruby 2.4
- { ruby: 2.2, rack: '~> 3.0' }
- { ruby: 2.3, rack: '~> 3.0' }
runs-on: ${{ matrix.os }}
env:
RACK_VERSION: ${{ matrix.rack }}
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}
steps:
- uses: actions/checkout@v4

- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- run: bundle exec rake
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

19 changes: 19 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@

source 'https://rubygems.org'

gem 'git-version-bump', '~> 0.15'
gem 'github-release', '~> 0.1'
gem 'i18n', '~> 0.6', '>= 0.6.8'
gem 'json', '~> 2.0'
gem 'mime-types', '~> 3.0'
gem 'minitest', '~> 5.6'
gem 'minitest-hooks', '~> 1.0'
gem 'mail', '~> 2.3', '>= 2.6.4'
gem 'nbio-csshttprequest', '~> 1.0'
gem 'rack', ENV['RACK_VERSION']
gem 'rake'
gem 'rdoc', '~> 5.0'
gem 'ruby-prof'
gem 'timecop', '~> 0.9'

# See https://github.com/ruby/cgi/pull/29
# Needed to have passing tests on Ruby 2.7, Ruby 3.0
gem 'cgi', '>= 0.3.6' if RUBY_VERSION >= '2.7.0' && RUBY_VERSION <= '3.1.0'

gemspec
4 changes: 2 additions & 2 deletions lib/rack/contrib/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def remap(mapping)
raise ArgumentError, "paths need to start with /"
end
location = location.chomp('/')
match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", nil, 'n')
match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", Regexp::NOENCODING)

ipmasks.collect! do |ipmask|
ipmask.is_a?(IPAddr) ? ipmask : IPAddr.new(ipmask)
Expand Down Expand Up @@ -72,7 +72,7 @@ def ipmasks_for_path(env)
end

def forbidden!
[403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
[403, { 'content-type' => 'text/html', 'content-length' => '0' }, []]
end

def ip_authorized?(request, ipmasks)
Expand Down
4 changes: 2 additions & 2 deletions lib/rack/contrib/backstage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def initialize(app, path)
end

def call(env)
if File.exists?(@file)
if File.exist?(@file)
content = File.read(@file)
length = content.bytesize.to_s
[503, {'Content-Type' => 'text/html', 'Content-Length' => length}, [content]]
[503, {'content-type' => 'text/html', 'content-length' => length}, [content]]
else
@app.call(env)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/contrib/bounce_favicon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(app)

def call(env)
if env["PATH_INFO"] == "/favicon.ico"
[404, {"Content-Type" => "text/html", "Content-Length" => "0"}, []]
[404, {"content-type" => "text/html", "content-length" => "0"}, []]
else
@app.call(env)
end
Expand Down
5 changes: 4 additions & 1 deletion lib/rack/contrib/common_cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ class CommonCookies
LOCALHOST_OR_IP_REGEXP = /^([\d.]+|localhost)$/
PORT = /:\d+$/

HEADERS_KLASS = Rack.release < "3" ? Utils::HeaderHash : Headers
private_constant :HEADERS_KLASS

def initialize(app)
@app = app
end

def call(env)
status, headers, body = @app.call(env)
headers = Utils::HeaderHash.new(headers)
headers = HEADERS_KLASS.new.merge(headers)

host = env['HTTP_HOST'].sub PORT, ''
share_cookie(headers, host)
Expand Down
4 changes: 3 additions & 1 deletion lib/rack/contrib/csshttprequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Rack

# A Rack middleware for providing CSSHTTPRequest responses.
class CSSHTTPRequest
HEADERS_KLASS = Rack.release < "3" ? Utils::HeaderHash : Headers
private_constant :HEADERS_KLASS

def initialize(app)
@app = app
Expand All @@ -15,7 +17,7 @@ def initialize(app)
# the CSSHTTPRequest encoder
def call(env)
status, headers, response = @app.call(env)
headers = Utils::HeaderHash.new(headers)
headers = HEADERS_KLASS.new.merge(headers)

if chr_request?(env)
encoded_response = encode(response)
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/contrib/deflect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def call env
end

def deflect!
[403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
[403, { 'content-type' => 'text/html', 'content-length' => '0' }, []]
end

def deflect? env
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/contrib/enforce_valid_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def call env
Rack::Utils.unescape(full_path).valid_encoding?
@app.call env
else
[400, {'Content-Type'=>'text/plain'}, ['Bad Request']]
[400, {'content-type'=>'text/plain'}, ['Bad Request']]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rack/contrib/expectation_cascade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class ExpectationCascade
Expect = "HTTP_EXPECT".freeze
ContinueExpectation = "100-continue".freeze

ExpectationFailed = [417, {"Content-Type" => "text/html"}, []].freeze
NotFound = [404, {"Content-Type" => "text/html"}, []].freeze
ExpectationFailed = [417, {"content-type" => "text/html"}, []]
NotFound = [404, {"content-type" => "text/html"}, []]

attr_reader :apps

Expand Down
2 changes: 1 addition & 1 deletion lib/rack/contrib/host_meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(app, &block)

def call(env)
if env['PATH_INFO'] == '/host-meta'
[200, {'Content-Type' => 'application/host-meta'}, [@response]]
[200, {'content-type' => 'application/host-meta'}, [@response]]
else
@app.call(env)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/rack/contrib/json_body_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module Rack
# === Parse POST and GET requests only
# use Rack::JSONBodyParser, verbs: ['POST', 'GET']
#
# === Parse POST|PATCH|PUT requests whose Content-Type matches 'json'
# === Parse POST|PATCH|PUT requests whose content-type matches 'json'
# use Rack::JSONBodyParser, media: /json/
#
# === Parse POST requests whose Content-Type is 'application/json' or 'application/vnd+json'
# === Parse POST requests whose content-type is 'application/json' or 'application/vnd+json'
# use Rack::JSONBodyParser, verbs: ['POST'], media: ['application/json', 'application/vnd.api+json']
#
class JSONBodyParser
Expand Down Expand Up @@ -63,7 +63,7 @@ def call(env)
end
rescue ParserError
body = { error: 'Failed to parse body as JSON' }.to_json
header = { 'Content-Type' => 'application/json' }
header = { 'content-type' => 'application/json' }
return Rack::Response.new(body, 400, header).finish
end
@app.call(env)
Expand All @@ -77,7 +77,7 @@ def update_form_hash_with_json_body(env)
body = env[Rack::RACK_INPUT]
return unless (body_content = body.read) && !body_content.empty?

body.rewind # somebody might try to read this stream
body.rewind if body.respond_to?(:rewind) # somebody might try to read this stream

begin
parsed_body = @parser.call(body_content)
Expand Down
7 changes: 5 additions & 2 deletions lib/rack/contrib/jsonp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class JSONP
# "\342\200\251" # => "\u2029"
U2028, U2029 = ("\u2028" == 'u2028') ? ["\342\200\250", "\342\200\251"] : ["\u2028", "\u2029"]

HEADERS_KLASS = Rack.release < "3" ? Utils::HeaderHash : Headers
private_constant :HEADERS_KLASS

def initialize(app)
@app = app
end
Expand All @@ -42,7 +45,7 @@ def call(env)
return status, headers, response
end

headers = HeaderHash.new(headers)
headers = HEADERS_KLASS.new.merge(headers)

if is_json?(headers) && has_callback?(request)
callback = request.params['callback']
Expand Down Expand Up @@ -108,7 +111,7 @@ def pad(callback, response)
end

def bad_request(body = "Bad Request")
[ 400, { 'Content-Type' => 'text/plain', 'Content-Length' => body.bytesize.to_s }, [body] ]
[ 400, { 'content-type' => 'text/plain', 'content-length' => body.bytesize.to_s }, [body] ]
end

end
Expand Down
10 changes: 5 additions & 5 deletions lib/rack/contrib/lazy_conditional_get.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ def initialize app, cache={}

def call env
if reading? env and fresh? env
return [304, {'Last-Modified' => env['HTTP_IF_MODIFIED_SINCE']}, []]
return [304, {'last-modified' => env['HTTP_IF_MODIFIED_SINCE']}, []]
end

status, headers, body = @app.call env
headers = Utils::HeaderHash.new(headers)
headers = Rack.release < "3" ? Utils::HeaderHash.new(headers) : headers

update_cache unless (reading?(env) or skipping?(headers))
headers['Last-Modified'] = cached_value if stampable? headers
headers['last-modified'] = cached_value if stampable? headers
[status, headers, body]
end

Expand All @@ -96,11 +96,11 @@ def reading? env
end

def skipping? headers
headers['Rack-Lazy-Conditional-Get'] == 'skip'
headers['rack-lazy-conditional-get'] == 'skip'
end

def stampable? headers
!headers.has_key?('Last-Modified') and headers['Rack-Lazy-Conditional-Get'] == 'yes'
!headers.has_key?('last-modified') and headers['rack-lazy-conditional-get'] == 'yes'
end

def update_cache
Expand Down
5 changes: 4 additions & 1 deletion lib/rack/contrib/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

module Rack
class Locale
HEADERS_KLASS = Rack.release < "3" ? Utils::HeaderHash : Headers
private_constant :HEADERS_KLASS

def initialize(app)
@app = app
end
Expand All @@ -16,7 +19,7 @@ def call(env)

env['rack.locale'] = I18n.locale = locale.to_s
status, headers, body = @app.call(env)
headers = Utils::HeaderHash.new(headers)
headers = HEADERS_KLASS.new.merge(headers)

unless headers['Content-Language']
headers['Content-Language'] = locale.to_s
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/contrib/mailexceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def send_notification(exception, env)

def extract_body(env)
if io = env['rack.input']
io.rewind
io.rewind if io.respond_to?(:rewind)
io.read
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/contrib/not_found.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(path = nil, content_type = 'text/html')
end

def call(env)
[404, {'Content-Type' => @content_type, 'Content-Length' => @length}, [@content]]
[404, {'content-type' => @content_type, 'content-length' => @length}, [@content]]
end
end
end
4 changes: 2 additions & 2 deletions lib/rack/contrib/post_body_content_type_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def initialize(app, &block)

def call(env)
if Rack::Request.new(env).media_type == APPLICATION_JSON && (body = env[POST_BODY].read).length != 0
env[POST_BODY].rewind # somebody might try to read this stream
env[POST_BODY].rewind if env[POST_BODY].respond_to?(:rewind) # somebody might try to read this stream
env.update(FORM_HASH => @block.call(body), FORM_INPUT => env[POST_BODY])
end
@app.call(env)
Expand All @@ -84,7 +84,7 @@ def call(env)
end

def bad_request(body = 'Bad Request')
[ 400, { 'Content-Type' => 'text/plain', 'Content-Length' => body.bytesize.to_s }, [body] ]
[ 400, { 'content-type' => 'text/plain', 'content-length' => body.bytesize.to_s }, [body] ]
end
end
end
2 changes: 1 addition & 1 deletion lib/rack/contrib/printout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(app)
end

def call(env)
# See http://rack.rubyforge.org/doc/SPEC.html for details
# See https://github.com/rack/rack/blob/main/SPEC.rdoc for details
puts "**********\n Environment\n **************"
puts env.inspect

Expand Down
Loading

0 comments on commit 4d7dc55

Please sign in to comment.