diff --git a/lib/rackup/handler/webrick.rb b/lib/rackup/handler/webrick.rb index 7a7070f..280555f 100644 --- a/lib/rackup/handler/webrick.rb +++ b/lib/rackup/handler/webrick.rb @@ -23,7 +23,7 @@ def self.run(app, **options) if !options[:BindAddress] || options[:Host] options[:BindAddress] = options.delete(:Host) || default_host end - options[:Port] ||= 8080 + options[:Port] = options[:port] || 8080 if options[:SSLEnable] require 'webrick/https' end diff --git a/lib/rackup/lobster.rb b/lib/rackup/lobster.rb index 8502ba0..d136ba1 100644 --- a/lib/rackup/lobster.rb +++ b/lib/rackup/lobster.rb @@ -75,7 +75,7 @@ def call(env) require_relative 'show_exceptions' require_relative 'lint' Rackup::Server.start( - app: Rack::ShowExceptions.new(Rack::Lint.new(Rackup::Lobster.new)), Port: 9292 + app: Rack::ShowExceptions.new(Rack::Lint.new(Rackup::Lobster.new)), port: 9292 ) # :nocov: end diff --git a/lib/rackup/server.rb b/lib/rackup/server.rb index 3304b87..5604e59 100644 --- a/lib/rackup/server.rb +++ b/lib/rackup/server.rb @@ -66,11 +66,11 @@ def parse!(args) } opts.on("-o", "--host HOST", "listen on HOST (default: localhost)") { |host| - options[:Host] = host + options[:host] = host } opts.on("-p", "--port PORT", "use PORT (default: 9292)") { |port| - options[:Port] = port + options[:port] = port } opts.on("-O", "--option NAME[=VALUE]", "pass VALUE to the server as option NAME. If no VALUE, sets it to true. Run '#{$0} -s SERVER -h' to get a list of options for SERVER") { |name| @@ -238,6 +238,7 @@ def initialize(options = nil) @use_default_options = true @options = parse_options(ARGV) end + normalize_legacy_options end def options @@ -252,9 +253,9 @@ def default_options { environment: environment, pid: nil, - Port: 9292, - Host: default_host, - AccessLog: [], + port: 9292, + host: default_host, + access_log: [], config: "config.ru" } end @@ -457,6 +458,43 @@ def exit_with_pid(pid) $stderr.puts "A server is already running (pid: #{pid}, file: #{options[:pid]})." exit(1) end - end + module LegacyOptionsNormalizer + def normalize_legacy_options + if @options && @options.is_a?(Hash) + @options.keys.each do |key| + snakeified_key = snakeify(key).to_sym + camelized_key = camelize(snakeified_key).to_sym + normalized_value = + if @options.key?(snakeified_key) + @options[snakeified_key] + elsif @options.key?(camelized_key) + @options[camelized_key] + else + @options[key] + end + @options[key] = @options[camelized_key] = @options[snakeified_key] = normalized_value + end + end + end + + def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) + if first_letter_in_uppercase + lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } + else + lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1] + end + end + + def snakeify(word) + word.to_s.gsub(/::/, '/'). + gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). + gsub(/([a-z\d])([A-Z])/,'\1_\2'). + tr("-", "_"). + downcase + end + end + + include LegacyOptionsNormalizer + end end diff --git a/test/spec_server.rb b/test/spec_server.rb index c5cebdb..c74c5e6 100644 --- a/test/spec_server.rb +++ b/test/spec_server.rb @@ -53,8 +53,8 @@ def with_stderr end it "Options#parse parses -p and --port options into :Port" do - Rackup::Server::Options.new.parse!(%w[-p 1234]).must_equal :Port => '1234' - Rackup::Server::Options.new.parse!(%w[--port 1234]).must_equal :Port => '1234' + Rackup::Server::Options.new.parse!(%w[-p 1234]).must_equal :port => '1234' + Rackup::Server::Options.new.parse!(%w[--port 1234]).must_equal :port => '1234' end it "Options#parse parses -D and --daemonize option into :daemonize" do @@ -482,9 +482,9 @@ def start(*) [self.class, :started] end t = Thread.new { server.start { |s| Thread.current[:server] = s } } t.join(0.01) until t[:server] && t[:server].status != :Stop body = if URI.respond_to?(:open) - URI.open("http://localhost:#{server.options[:Port]}/") { |f| f.read } + URI.open("http://localhost:#{server.options[:port]}/") { |f| f.read } else - open("http://localhost:#{server.options[:Port]}/") { |f| f.read } + open("http://localhost:#{server.options[:port]}/") { |f| f.read } end body.must_equal 'success' @@ -512,7 +512,7 @@ def start(*) [self.class, :started] end t = Thread.new { server.start { |s| Thread.current[:server] = s } } t.join(0.01) until t[:server] && t[:server].status != :Stop - uri = URI.parse("https://localhost:#{server.options[:Port]}/") + uri = URI.parse("https://localhost:#{server.options[:port]}/") Net::HTTP.start("localhost", uri.port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| @@ -599,4 +599,41 @@ def start(*) [self.class, :started] end end end + it "normalize simple (non-camelized) words" do + options = {port: 8081} + server = Rackup::Server.new(options) + server.options[:port].must_equal 8081 + server.options[:Port].must_equal 8081 + end + + it "normalize simple (non-camelized) words with snake-case having priority" do + options = {port: 8081, Port: 8082} + server = Rackup::Server.new(options) + server.options[:port].must_equal 8081 + server.options[:Port].must_equal 8081 + end + + it "normalizes complex (camelized) words" do + options = {SSLEnable: true} + server = Rackup::Server.new(options) + server.options[:SSLEnable].must_equal true + server.options[:ssl_enable].must_equal true + server.options[:SslEnable].must_equal true + end + + it "normalizes complex (camelized) words with camel-case having priority over actual key" do + options = {SSLEnable: true, ssl_enable: false} + server = Rackup::Server.new(options) + server.options[:SSLEnable].must_equal false + server.options[:ssl_enable].must_equal false + server.options[:SslEnable].must_equal false + end + + it "normalizes complex (camelized) words with snake-case having priority over all" do + options = {SSLEnable: true, SslEnable: nil, ssl_enable: false} + server = Rackup::Server.new(options) + server.options[:SSLEnable].must_equal false + server.options[:ssl_enable].must_equal false + server.options[:SslEnable].must_equal false + end end