lib/hanami/routing/http_router.rb in hanami-router-0.6.2 vs lib/hanami/routing/http_router.rb in hanami-router-0.7.0
- old
+ new
@@ -1,5 +1,6 @@
+require 'uri'
require 'http_router'
require 'hanami/utils/io'
require 'hanami/routing/endpoint_resolver'
require 'hanami/routing/route'
require 'hanami/routing/parsers'
@@ -35,10 +36,28 @@
#
# @since 0.5.0
# @api private
SCRIPT_NAME = 'SCRIPT_NAME'.freeze
+ # Path info - rack environment variable
+ #
+ # @since 0.7.0
+ # @api private
+ PATH_INFO = 'PATH_INFO'.freeze
+
+ # Default PATH_INFO for Rack requests
+ #
+ # @since 0.7.0
+ # @api private
+ DEFAULT_PATH_INFO = '/'.freeze
+
+ # URL separator
+ #
+ # @since 0.7.0
+ # @api private
+ URL_SEPARATOR = '/'.freeze
+
# @since 0.5.0
# @api private
attr_reader :namespace
# Initialize the router.
@@ -46,10 +65,12 @@
# @see Hanami::Router#initialize
#
# @since 0.1.0
# @api private
def initialize(options = {}, &blk)
+ @compiled = false
+ @uri_parser = URI::Parser.new
super(options, &nil)
@namespace = options[:namespace] if options[:namespace]
@default_scheme = options[:scheme] if options[:scheme]
@default_host = options[:host] if options[:host]
@@ -120,11 +141,11 @@
# @see Hanami::Router#mount
#
# @since 0.1.1
# @api private
def mount(app, options)
- add("#{ options.fetch(:at) }*").to(
+ add("#{ options.fetch(:at) }*", host: options[:host]).to(
@resolver.resolve(to: app)
)
end
# @api private
@@ -156,13 +177,18 @@
@default_app.call(env)
end
end
# @api private
- # @since 0.5.0
- def rewrite_path_info(env, request)
- super
- env[SCRIPT_NAME] = @prefix.join(env[SCRIPT_NAME])
+ def rewrite_partial_path_info(env, request)
+ if request.path.empty?
+ env[SCRIPT_NAME] += env[PATH_INFO]
+ env[PATH_INFO] = DEFAULT_PATH_INFO
+ else
+ path_info_before = env[PATH_INFO].dup
+ env[PATH_INFO] = "/#{@uri_parser.escape(request.path.join(URL_SEPARATOR))}"
+ env[SCRIPT_NAME] += path_info_before[0, path_info_before.bytesize - env[PATH_INFO].bytesize]
+ end
end
private
def _rescue_url_recognition