diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2019-06-27 19:22:27 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-27 19:22:27 -0400 |
commit | 06f7c997706c612d7f8332d66f01daa303bd7c6c (patch) | |
tree | bcf4686bc1da55acb25d2dee54c21c78ceae0847 | |
parent | 2d41895fdcfb3c93c23afe702691ad8a9becf62c (diff) | |
parent | 61408e452f5adc71cd44b105093042e8010c37b3 (diff) | |
download | rack-06f7c997706c612d7f8332d66f01daa303bd7c6c.tar.gz |
Merge pull request #1355 from krzysiek1507/feature/use-regexp-match
Use Regexp match? instead of =~
-rw-r--r-- | lib/rack/builder.rb | 6 | ||||
-rw-r--r-- | lib/rack/core_ext/regexp.rb | 16 | ||||
-rw-r--r-- | lib/rack/deflater.rb | 6 | ||||
-rw-r--r-- | lib/rack/multipart/parser.rb | 5 | ||||
-rw-r--r-- | lib/rack/query_parser.rb | 6 | ||||
-rw-r--r-- | lib/rack/reloader.rb | 6 | ||||
-rw-r--r-- | lib/rack/server.rb | 6 | ||||
-rw-r--r-- | lib/rack/session/memcache.rb | 5 | ||||
-rw-r--r-- | lib/rack/static.rb | 13 | ||||
-rw-r--r-- | lib/rack/utils.rb | 22 |
10 files changed, 69 insertions, 22 deletions
diff --git a/lib/rack/builder.rb b/lib/rack/builder.rb index 6ebed3e2..f343fffc 100644 --- a/lib/rack/builder.rb +++ b/lib/rack/builder.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative 'core_ext/regexp' + module Rack # Rack::Builder implements a small DSL to iteratively construct Rack # applications. @@ -31,11 +33,13 @@ module Rack # You can use +map+ to construct a Rack::URLMap in a convenient way. class Builder + using ::Rack::RegexpExtensions + # https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom UTF_8_BOM = '\xef\xbb\xbf' def self.parse_file(config, opts = Server::Options.new) - if config =~ /\.ru$/ + if /\.ru$/.match?(config) return self.load_file(config, opts) else require config diff --git a/lib/rack/core_ext/regexp.rb b/lib/rack/core_ext/regexp.rb new file mode 100644 index 00000000..02975345 --- /dev/null +++ b/lib/rack/core_ext/regexp.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# Regexp has `match?` since Ruby 2.4 +# so to support Ruby < 2.4 we need to define this method + +module Rack + module RegexpExtensions + if Gem::Version.new(RUBY_VERSION) < Gem::Version.new(2.4) + refine Regexp do + def match?(string, pos = 0) + !!match(string, pos) + end unless //.respond_to?(:match?) + end + end + end +end diff --git a/lib/rack/deflater.rb b/lib/rack/deflater.rb index 67598ef2..ce248c66 100644 --- a/lib/rack/deflater.rb +++ b/lib/rack/deflater.rb @@ -4,6 +4,8 @@ require "zlib" require "time" # for Time.httpdate require 'rack/utils' +require_relative 'core_ext/regexp' + module Rack # This middleware enables compression of http responses. # @@ -17,6 +19,8 @@ module Rack # directive of 'no-transform' is present, or when the response status # code is one that doesn't allow an entity body. class Deflater + using ::Rack::RegexpExtensions + ## # Creates Rack::Deflater middleware. # @@ -108,7 +112,7 @@ module Rack # Skip compressing empty entity body responses and responses with # no-transform set. if Utils::STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) || - headers['Cache-Control'].to_s =~ /\bno-transform\b/ || + /\bno-transform\b/.match?(headers['Cache-Control'].to_s) || (headers['Content-Encoding'] && headers['Content-Encoding'] !~ /\bidentity\b/) return false end diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb index 1bce2809..d683de53 100644 --- a/lib/rack/multipart/parser.rb +++ b/lib/rack/multipart/parser.rb @@ -2,12 +2,15 @@ require 'rack/utils' require 'strscan' +require 'rack/core_ext/regexp' module Rack module Multipart class MultipartPartLimitError < Errno::EMFILE; end class Parser + using ::Rack::RegexpExtensions + BUFSIZE = 1_048_576 TEXT_PLAIN = "text/plain" TEMPFILE_FACTORY = lambda { |filename, content_type| @@ -311,7 +314,7 @@ module Rack return unless filename - if filename.scan(/%.?.?/).all? { |s| s =~ /%[0-9a-fA-F]{2}/ } + if filename.scan(/%.?.?/).all? { |s| /%[0-9a-fA-F]{2}/.match?(s) } filename = Utils.unescape_path(filename) end diff --git a/lib/rack/query_parser.rb b/lib/rack/query_parser.rb index 6f69e0ed..fce1ce91 100644 --- a/lib/rack/query_parser.rb +++ b/lib/rack/query_parser.rb @@ -1,7 +1,11 @@ # frozen_string_literal: true +require_relative 'core_ext/regexp' + module Rack class QueryParser + using ::Rack::RegexpExtensions + DEFAULT_SEP = /[&;] */n COMMON_SEP = { ";" => /[;] */n, ";," => /[;,] */n, "&" => /[&] */n } @@ -137,7 +141,7 @@ module Rack end def params_hash_has_key?(hash, key) - return false if key =~ /\[\]/ + return false if /\[\]/.match?(key) key.split(/[\[\]]+/).inject(hash) do |h, part| next h if part == '' diff --git a/lib/rack/reloader.rb b/lib/rack/reloader.rb index c97d8635..e23ed1fb 100644 --- a/lib/rack/reloader.rb +++ b/lib/rack/reloader.rb @@ -6,6 +6,8 @@ require 'pathname' +require_relative 'core_ext/regexp' + module Rack # High performant source reloader @@ -22,6 +24,8 @@ module Rack # It is performing a check/reload cycle at the start of every request, but # also respects a cool down time, during which nothing will be done. class Reloader + using ::Rack::RegexpExtensions + def initialize(app, cooldown = 10, backend = Stat) @app = app @cooldown = cooldown @@ -71,7 +75,7 @@ module Rack paths = ['./', *$LOAD_PATH].uniq files.map{|file| - next if file =~ /\.(so|bundle)$/ # cannot reload compiled files + next if /\.(so|bundle)$/.match?(file) # cannot reload compiled files found, stat = figure_path(file, paths) next unless found && stat && mtime = stat.mtime diff --git a/lib/rack/server.rb b/lib/rack/server.rb index 2a3095b3..f0bc1500 100644 --- a/lib/rack/server.rb +++ b/lib/rack/server.rb @@ -3,10 +3,12 @@ require 'optparse' require 'fileutils' +require_relative 'core_ext/regexp' module Rack class Server + using ::Rack::RegexpExtensions class Options def parse!(args) @@ -134,7 +136,7 @@ module Rack has_options = false server.valid_options.each do |name, description| - next if name.to_s.match(/^(Host|Port)[^a-zA-Z]/) # ignore handler's host and port options, we do our own. + next if /^(Host|Port)[^a-zA-Z]/.match?(name.to_s) # ignore handler's host and port options, we do our own. info << " -O %-21s %s" % [name, description] has_options = true end @@ -252,7 +254,7 @@ module Rack class << self def logging_middleware lambda { |server| - server.server.name =~ /CGI/ || server.options[:quiet] ? nil : [Rack::CommonLogger, $stderr] + /CGI/.match?(server.server.name) || server.options[:quiet] ? nil : [Rack::CommonLogger, $stderr] } end diff --git a/lib/rack/session/memcache.rb b/lib/rack/session/memcache.rb index a05ea0fb..dd587633 100644 --- a/lib/rack/session/memcache.rb +++ b/lib/rack/session/memcache.rb @@ -4,6 +4,7 @@ require 'rack/session/abstract/id' require 'memcache' +require 'rack/core_ext/regexp' module Rack module Session @@ -22,6 +23,8 @@ module Rack # a full description of behaviour, please see memcache's documentation. class Memcache < Abstract::ID + using ::Rack::RegexpExtensions + attr_reader :mutex, :pool DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge \ @@ -52,7 +55,7 @@ module Rack with_lock(env) do unless sid and session = @pool.get(sid) sid, session = generate_sid, {} - unless /^STORED/ =~ @pool.add(sid, session) + unless /^STORED/.match?(@pool.add(sid, session)) raise "Session collision on '#{sid.inspect}'" end end diff --git a/lib/rack/static.rb b/lib/rack/static.rb index 766ba0bf..3c76a847 100644 --- a/lib/rack/static.rb +++ b/lib/rack/static.rb @@ -3,6 +3,8 @@ require "rack/file" require "rack/utils" +require_relative 'core_ext/regexp' + module Rack # The Rack::Static middleware intercepts requests for static files @@ -84,6 +86,7 @@ module Rack # ] # class Static + using ::Rack::RegexpExtensions def initialize(app, options = {}) @app = app @@ -101,7 +104,7 @@ module Rack end def add_index_root?(path) - @index && route_file(path) && path =~ /\/$/ + @index && route_file(path) && /\/$/.match?(path) end def overwrite_file_path(path) @@ -122,7 +125,7 @@ module Rack if can_serve(path) if overwrite_file_path(path) env[PATH_INFO] = (add_index_root?(path) ? path + @index : @urls[path]) - elsif @gzip && env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/ + elsif @gzip && /\bgzip\b/.match?(env['HTTP_ACCEPT_ENCODING']) path = env[PATH_INFO] env[PATH_INFO] += '.gz' response = @file_server.call(env) @@ -159,14 +162,14 @@ module Rack when :all true when :fonts - path =~ /\.(?:ttf|otf|eot|woff2|woff|svg)\z/ + /\.(?:ttf|otf|eot|woff2|woff|svg)\z/.match?(path) when String path = ::Rack::Utils.unescape(path) path.start_with?(rule) || path.start_with?('/' + rule) when Array - path =~ /\.(#{rule.join('|')})\z/ + /\.(#{rule.join('|')})\z/.match?(path) when Regexp - path =~ rule + rule.match?(path) else false end diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index d4ef386f..2abe7572 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -8,11 +8,15 @@ require 'tempfile' require 'rack/query_parser' require 'time' +require_relative 'core_ext/regexp' + module Rack # Rack::Utils contains a grab-bag of useful methods for writing web # applications adopted from all kinds of Ruby libraries. module Utils + using ::Rack::RegexpExtensions + ParameterTypeError = QueryParser::ParameterTypeError InvalidParameterError = QueryParser::InvalidParameterError DEFAULT_SEP = QueryParser::DEFAULT_SEP @@ -275,15 +279,15 @@ module Rack cookies = header end - cookies.reject! { |cookie| - if value[:domain] - cookie =~ /\A#{escape(key)}=.*domain=#{value[:domain]}/ - elsif value[:path] - cookie =~ /\A#{escape(key)}=.*path=#{value[:path]}/ - else - cookie =~ /\A#{escape(key)}=/ - end - } + regexp = if value[:domain] + /\A#{escape(key)}=.*domain=#{value[:domain]}/ + elsif value[:path] + /\A#{escape(key)}=.*path=#{value[:path]}/ + else + /\A#{escape(key)}=/ + end + + cookies.reject! { |cookie| regexp.match? cookie } cookies.join("\n") end |