diff options
author | Jeremy Evans <code@jeremyevans.net> | 2020-01-27 14:30:11 -0800 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2020-01-27 19:30:54 -0800 |
commit | ab41dccfe287b7d2589778308cb297eb039e88c6 (patch) | |
tree | d3a65fce77c68542ead4b606c0a2dc02e294eeaf | |
parent | 68dde7b79b30dd1a721eaf503e20ace8e162b980 (diff) | |
download | rack-ab41dccfe287b7d2589778308cb297eb039e88c6.tar.gz |
Use require_relative and rely on autoload instead of using require
This does a better job of testing the autoloads work, and ensures
files are loaded from the current rack directory.
Some files required rack.rb, and in those cases I didn't remove
the require, just replaced it with require_relative, since the
intent is to make sure all of rack is loaded if a file under
rack is loaded.
This has the potential to break code that does:
require 'rack/content_type'
Rack::ContentType.new(app)
If the rack library itself is never required. This is because
the autoloads that rack.rb sets up would not be loaded. I'm not
sure if we want to support that, but if so we would want to keep
the requires and convert them to require_relative.
For core_ext/regexp, move the requires into the if < 2.4 block,
so that the file isn't loaded unless it is necessary.
44 files changed, 26 insertions, 114 deletions
diff --git a/lib/rack/auth/abstract/request.rb b/lib/rack/auth/abstract/request.rb index 23da4bf2..34042c40 100644 --- a/lib/rack/auth/abstract/request.rb +++ b/lib/rack/auth/abstract/request.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rack/request' - module Rack module Auth class AbstractRequest diff --git a/lib/rack/auth/basic.rb b/lib/rack/auth/basic.rb index b61bfffe..d5b4ea16 100644 --- a/lib/rack/auth/basic.rb +++ b/lib/rack/auth/basic.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'rack/auth/abstract/handler' -require 'rack/auth/abstract/request' +require_relative 'abstract/handler' +require_relative 'abstract/request' require 'base64' module Rack diff --git a/lib/rack/auth/digest/md5.rb b/lib/rack/auth/digest/md5.rb index 62bff984..04b103e2 100644 --- a/lib/rack/auth/digest/md5.rb +++ b/lib/rack/auth/digest/md5.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require 'rack/auth/abstract/handler' -require 'rack/auth/digest/request' -require 'rack/auth/digest/params' -require 'rack/auth/digest/nonce' +require_relative '../abstract/handler' +require_relative 'request' +require_relative 'params' +require_relative 'nonce' require 'digest/md5' module Rack diff --git a/lib/rack/auth/digest/request.rb b/lib/rack/auth/digest/request.rb index a3ab4743..7b89b760 100644 --- a/lib/rack/auth/digest/request.rb +++ b/lib/rack/auth/digest/request.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require 'rack/auth/abstract/request' -require 'rack/auth/digest/params' -require 'rack/auth/digest/nonce' +require_relative '../abstract/request' +require_relative 'params' +require_relative 'nonce' module Rack module Auth diff --git a/lib/rack/chunked.rb b/lib/rack/chunked.rb index e7e7d8d1..e11a9e5b 100644 --- a/lib/rack/chunked.rb +++ b/lib/rack/chunked.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rack/utils' - module Rack # Middleware that applies chunked transfer encoding to response bodies diff --git a/lib/rack/common_logger.rb b/lib/rack/common_logger.rb index de604f5b..9d8eaefb 100644 --- a/lib/rack/common_logger.rb +++ b/lib/rack/common_logger.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rack/body_proxy' - module Rack # Rack::CommonLogger forwards every request to the given +app+, and # logs a line in the diff --git a/lib/rack/conditional_get.rb b/lib/rack/conditional_get.rb index bda8daf6..24200053 100644 --- a/lib/rack/conditional_get.rb +++ b/lib/rack/conditional_get.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rack/utils' - module Rack # Middleware that enables conditional GET using If-None-Match and diff --git a/lib/rack/content_length.rb b/lib/rack/content_length.rb index f8012937..1acd79a7 100644 --- a/lib/rack/content_length.rb +++ b/lib/rack/content_length.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -require 'rack/utils' -require 'rack/body_proxy' - module Rack # Sets the Content-Length header on responses with fixed-length bodies. diff --git a/lib/rack/content_type.rb b/lib/rack/content_type.rb index 010cc37b..4814d200 100644 --- a/lib/rack/content_type.rb +++ b/lib/rack/content_type.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rack/utils' - module Rack # Sets the Content-Type header on responses which don't have one. diff --git a/lib/rack/deflater.rb b/lib/rack/deflater.rb index 93b2d01e..acfe9b70 100644 --- a/lib/rack/deflater.rb +++ b/lib/rack/deflater.rb @@ -2,9 +2,6 @@ require "zlib" require "time" # for Time.httpdate -require 'rack/utils' - -require_relative 'core_ext/regexp' module Rack # This middleware enables compression of http responses. @@ -19,7 +16,7 @@ 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 if RUBY_VERSION < '2.4' + (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4' ## # Creates Rack::Deflater middleware. diff --git a/lib/rack/directory.rb b/lib/rack/directory.rb index c00b28a1..5eee4d9f 100644 --- a/lib/rack/directory.rb +++ b/lib/rack/directory.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true require 'time' -require 'rack/utils' -require 'rack/mime' -require 'rack/files' module Rack # Rack::Directory serves entries below the +root+ given, according to the diff --git a/lib/rack/etag.rb b/lib/rack/etag.rb index 86efc5c7..aceb449d 100644 --- a/lib/rack/etag.rb +++ b/lib/rack/etag.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'rack' +require_relative '../rack' require 'digest/sha2' module Rack diff --git a/lib/rack/events.rb b/lib/rack/events.rb index 77b71675..8c63a2d0 100644 --- a/lib/rack/events.rb +++ b/lib/rack/events.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -require 'rack/response' -require 'rack/body_proxy' - module Rack ### This middleware provides hooks to certain places in the request / # response lifecycle. This is so that middleware that don't need to filter diff --git a/lib/rack/file.rb b/lib/rack/file.rb index 52b48e8b..fdcf9b3e 100644 --- a/lib/rack/file.rb +++ b/lib/rack/file.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'rack/files' +require_relative 'files' module Rack File = Files diff --git a/lib/rack/files.rb b/lib/rack/files.rb index b5a14d64..c50f2937 100644 --- a/lib/rack/files.rb +++ b/lib/rack/files.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true require 'time' -require 'rack/utils' -require 'rack/mime' -require 'rack/request' -require 'rack/head' module Rack # Rack::Files serves files below the +root+ directory given, according to the diff --git a/lib/rack/handler/cgi.rb b/lib/rack/handler/cgi.rb index fd7764c7..1c11ab36 100644 --- a/lib/rack/handler/cgi.rb +++ b/lib/rack/handler/cgi.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -require 'rack/content_length' -require 'rack/rewindable_input' - module Rack module Handler class CGI diff --git a/lib/rack/handler/fastcgi.rb b/lib/rack/handler/fastcgi.rb index 90ea6278..1df123e0 100644 --- a/lib/rack/handler/fastcgi.rb +++ b/lib/rack/handler/fastcgi.rb @@ -2,8 +2,6 @@ require 'fcgi' require 'socket' -require 'rack/content_length' -require 'rack/rewindable_input' if defined? FCGI::Stream class FCGI::Stream diff --git a/lib/rack/handler/lsws.rb b/lib/rack/handler/lsws.rb index 94a2337a..f12090bd 100644 --- a/lib/rack/handler/lsws.rb +++ b/lib/rack/handler/lsws.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true require 'lsapi' -require 'rack/content_length' -require 'rack/rewindable_input' module Rack module Handler diff --git a/lib/rack/handler/scgi.rb b/lib/rack/handler/scgi.rb index f0aa184e..e3b8d3c6 100644 --- a/lib/rack/handler/scgi.rb +++ b/lib/rack/handler/scgi.rb @@ -2,8 +2,6 @@ require 'scgi' require 'stringio' -require 'rack/content_length' -require 'rack/chunked' module Rack module Handler diff --git a/lib/rack/handler/thin.rb b/lib/rack/handler/thin.rb index 7378e325..d1629835 100644 --- a/lib/rack/handler/thin.rb +++ b/lib/rack/handler/thin.rb @@ -4,8 +4,6 @@ require "thin" require "thin/server" require "thin/logging" require "thin/backends/tcp_server" -require "rack/content_length" -require "rack/chunked" module Rack module Handler diff --git a/lib/rack/handler/webrick.rb b/lib/rack/handler/webrick.rb index fa792283..6161a5a7 100644 --- a/lib/rack/handler/webrick.rb +++ b/lib/rack/handler/webrick.rb @@ -2,7 +2,6 @@ require 'webrick' require 'stringio' -require 'rack/content_length' # This monkey patch allows for applications to perform their own chunking # through WEBrick::HTTPResponse if rack is set to true. diff --git a/lib/rack/head.rb b/lib/rack/head.rb index c257ae4d..8025a27d 100644 --- a/lib/rack/head.rb +++ b/lib/rack/head.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rack/body_proxy' - module Rack # Rack::Head returns an empty body for all HEAD requests. It leaves # all other requests unchanged. diff --git a/lib/rack/lint.rb b/lib/rack/lint.rb index e668417e..7a79b77f 100644 --- a/lib/rack/lint.rb +++ b/lib/rack/lint.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rack/utils' require 'forwardable' module Rack diff --git a/lib/rack/lobster.rb b/lib/rack/lobster.rb index 77b607c3..67345cec 100644 --- a/lib/rack/lobster.rb +++ b/lib/rack/lobster.rb @@ -2,9 +2,6 @@ require 'zlib' -require 'rack/request' -require 'rack/response' - module Rack # Paste has a Pony, Rack has a Lobster! class Lobster @@ -64,8 +61,7 @@ module Rack end if $0 == __FILE__ - require 'rack' - require 'rack/show_exceptions' + require_relative '../rack' Rack::Server.start( app: Rack::ShowExceptions.new(Rack::Lint.new(Rack::Lobster.new)), Port: 9292 ) diff --git a/lib/rack/lock.rb b/lib/rack/lock.rb index 96366cd3..4bae3a90 100644 --- a/lib/rack/lock.rb +++ b/lib/rack/lock.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'thread' -require 'rack/body_proxy' module Rack # Rack::Lock locks every request inside a mutex, so that every request diff --git a/lib/rack/mock.rb b/lib/rack/mock.rb index 6fc796af..5b2512ca 100644 --- a/lib/rack/mock.rb +++ b/lib/rack/mock.rb @@ -2,10 +2,7 @@ require 'uri' require 'stringio' -require 'rack' -require 'rack/lint' -require 'rack/utils' -require 'rack/response' +require_relative '../rack' require 'cgi/cookie' module Rack diff --git a/lib/rack/multipart.rb b/lib/rack/multipart.rb index bd91f43f..45f43bb6 100644 --- a/lib/rack/multipart.rb +++ b/lib/rack/multipart.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'rack/multipart/parser' +require_relative 'multipart/parser' module Rack # A multipart form data parser, adapted from IOWA. diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb index e9b3c804..28f50989 100644 --- a/lib/rack/multipart/parser.rb +++ b/lib/rack/multipart/parser.rb @@ -1,15 +1,13 @@ # frozen_string_literal: true -require 'rack/utils' require 'strscan' -require 'rack/core_ext/regexp' module Rack module Multipart class MultipartPartLimitError < Errno::EMFILE; end class Parser - using ::Rack::RegexpExtensions if RUBY_VERSION < '2.4' + (require_relative '../core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4' BUFSIZE = 1_048_576 TEXT_PLAIN = "text/plain" diff --git a/lib/rack/query_parser.rb b/lib/rack/query_parser.rb index cff8f5b0..67faa1b8 100644 --- a/lib/rack/query_parser.rb +++ b/lib/rack/query_parser.rb @@ -1,10 +1,8 @@ # frozen_string_literal: true -require_relative 'core_ext/regexp' - module Rack class QueryParser - using ::Rack::RegexpExtensions if RUBY_VERSION < '2.4' + (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4' DEFAULT_SEP = /[&;] */n COMMON_SEP = { ";" => /[;] */n, ";," => /[;,] */n, "&" => /[&] */n } diff --git a/lib/rack/reloader.rb b/lib/rack/reloader.rb index 647b4fc1..2f17f50b 100644 --- a/lib/rack/reloader.rb +++ b/lib/rack/reloader.rb @@ -6,8 +6,6 @@ require 'pathname' -require_relative 'core_ext/regexp' - module Rack # High performant source reloader @@ -24,7 +22,7 @@ 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 if RUBY_VERSION < '2.4' + (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4' def initialize(app, cooldown = 10, backend = Stat) @app = app diff --git a/lib/rack/request.rb b/lib/rack/request.rb index d75a0974..689cd244 100644 --- a/lib/rack/request.rb +++ b/lib/rack/request.rb @@ -1,10 +1,5 @@ # frozen_string_literal: true -require 'rack/utils' -require 'rack/media_type' - -require_relative 'core_ext/regexp' - module Rack # Rack::Request provides a convenient interface to a Rack # environment. It is stateless, the environment +env+ passed to the @@ -15,7 +10,7 @@ module Rack # req.params["data"] class Request - using ::Rack::RegexpExtensions if RUBY_VERSION < '2.4' + (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4' class << self attr_accessor :ip_filter diff --git a/lib/rack/response.rb b/lib/rack/response.rb index a7bece8d..cb76371e 100644 --- a/lib/rack/response.rb +++ b/lib/rack/response.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true -require 'rack/request' -require 'rack/utils' -require 'rack/body_proxy' -require 'rack/media_type' require 'time' module Rack diff --git a/lib/rack/rewindable_input.rb b/lib/rack/rewindable_input.rb index 352bbeaa..91b9d1eb 100644 --- a/lib/rack/rewindable_input.rb +++ b/lib/rack/rewindable_input.rb @@ -2,7 +2,6 @@ # frozen_string_literal: true require 'tempfile' -require 'rack/utils' module Rack # Class which can make any IO object rewindable, including non-rewindable ones. It does diff --git a/lib/rack/runtime.rb b/lib/rack/runtime.rb index d2bca9e5..a0f8ac7f 100644 --- a/lib/rack/runtime.rb +++ b/lib/rack/runtime.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rack/utils' - module Rack # Sets an "X-Runtime" response header, indicating the response # time of the request, in seconds diff --git a/lib/rack/sendfile.rb b/lib/rack/sendfile.rb index 3774b260..3d5e786f 100644 --- a/lib/rack/sendfile.rb +++ b/lib/rack/sendfile.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -require 'rack/files' -require 'rack/body_proxy' - module Rack # = Sendfile diff --git a/lib/rack/server.rb b/lib/rack/server.rb index 306e5746..b7107ed5 100644 --- a/lib/rack/server.rb +++ b/lib/rack/server.rb @@ -3,12 +3,10 @@ require 'optparse' require 'fileutils' -require_relative 'core_ext/regexp' - module Rack class Server - using ::Rack::RegexpExtensions if RUBY_VERSION < '2.4' + (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4' class Options def parse!(args) diff --git a/lib/rack/session/abstract/id.rb b/lib/rack/session/abstract/id.rb index 6ee02502..74fd98f9 100644 --- a/lib/rack/session/abstract/id.rb +++ b/lib/rack/session/abstract/id.rb @@ -3,10 +3,8 @@ # AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net # bugrep: Andreas Zehnder -require 'rack' +require_relative '../../../rack' require 'time' -require 'rack/request' -require 'rack/response' require 'securerandom' require 'digest/sha2' diff --git a/lib/rack/session/cookie.rb b/lib/rack/session/cookie.rb index d110aee2..bb541396 100644 --- a/lib/rack/session/cookie.rb +++ b/lib/rack/session/cookie.rb @@ -2,9 +2,7 @@ require 'openssl' require 'zlib' -require 'rack/request' -require 'rack/response' -require 'rack/session/abstract/id' +require_relative 'abstract/id' require 'json' require 'base64' diff --git a/lib/rack/session/pool.rb b/lib/rack/session/pool.rb index f5b62650..4885605f 100644 --- a/lib/rack/session/pool.rb +++ b/lib/rack/session/pool.rb @@ -5,7 +5,7 @@ # apeiros, for session id generation, expiry setup, and threadiness # sergio, threadiness and bugreps -require 'rack/session/abstract/id' +require_relative 'abstract/id' require 'thread' module Rack diff --git a/lib/rack/show_exceptions.rb b/lib/rack/show_exceptions.rb index 8ca96ef0..f5cc76c3 100644 --- a/lib/rack/show_exceptions.rb +++ b/lib/rack/show_exceptions.rb @@ -2,8 +2,6 @@ require 'ostruct' require 'erb' -require 'rack/request' -require 'rack/utils' module Rack # Rack::ShowExceptions catches all exceptions raised from the app it diff --git a/lib/rack/show_status.rb b/lib/rack/show_status.rb index 3fdfca5e..56034750 100644 --- a/lib/rack/show_status.rb +++ b/lib/rack/show_status.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true require 'erb' -require 'rack/request' -require 'rack/utils' module Rack # Rack::ShowStatus catches all empty responses and replaces them diff --git a/lib/rack/static.rb b/lib/rack/static.rb index 0338073f..8cb58b2f 100644 --- a/lib/rack/static.rb +++ b/lib/rack/static.rb @@ -1,10 +1,5 @@ # frozen_string_literal: true -require "rack/files" -require "rack/utils" - -require_relative 'core_ext/regexp' - module Rack # The Rack::Static middleware intercepts requests for static files @@ -91,7 +86,7 @@ module Rack # ] # class Static - using ::Rack::RegexpExtensions if RUBY_VERSION < '2.4' + (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4' def initialize(app, options = {}) @app = app diff --git a/lib/rack/tempfile_reaper.rb b/lib/rack/tempfile_reaper.rb index 73b6c1c8..9b04fefc 100644 --- a/lib/rack/tempfile_reaper.rb +++ b/lib/rack/tempfile_reaper.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rack/body_proxy' - module Rack # Middleware tracks and cleans Tempfiles created throughout a request (i.e. Rack::Multipart) diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index f60c4919..4dd36e54 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -5,17 +5,16 @@ require 'uri' require 'fileutils' require 'set' require 'tempfile' -require 'rack/query_parser' require 'time' -require_relative 'core_ext/regexp' +require_relative 'query_parser' 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 if RUBY_VERSION < '2.4' + (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4' ParameterTypeError = QueryParser::ParameterTypeError InvalidParameterError = QueryParser::InvalidParameterError |