diff --git a/.rubocop.yml b/.rubocop.yml index c0f965fe..fb6a20fb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -31,10 +31,10 @@ Metrics/ClassLength: # TODO: Lower to 6 Metrics/CyclomaticComplexity: - Max: 8 + Max: 9 Metrics/PerceivedComplexity: - Max: 8 + Max: 9 # TODO: Lower to 80 Metrics/LineLength: diff --git a/lib/http/client.rb b/lib/http/client.rb index 5b9aa4fa..bb417764 100644 --- a/lib/http/client.rb +++ b/lib/http/client.rb @@ -43,13 +43,15 @@ def build_request(verb, uri, opts = {}) # rubocop:disable Style/OptionHash headers = make_request_headers(opts) body = make_request_body(opts, headers) proxy = opts.proxy + normalize_uri = opts.features[:normalize_uri]&.method(:normalize_uri) req = HTTP::Request.new( :verb => verb, :uri => uri, :headers => headers, :proxy => proxy, - :body => body + :body => body, + :normalize_uri => normalize_uri ) opts.features.inject(req) do |request, (_name, feature)| diff --git a/lib/http/feature.rb b/lib/http/feature.rb index 3c279e97..53d33811 100644 --- a/lib/http/feature.rb +++ b/lib/http/feature.rb @@ -20,3 +20,4 @@ def wrap_response(response) require "http/features/auto_deflate" require "http/features/logging" require "http/features/instrumentation" +require "http/features/normalize_uri" diff --git a/lib/http/features/auto_deflate.rb b/lib/http/features/auto_deflate.rb index 2a2b5300..342ad3cf 100644 --- a/lib/http/features/auto_deflate.rb +++ b/lib/http/features/auto_deflate.rb @@ -32,7 +32,8 @@ def wrap_request(request) :uri => request.uri, :headers => request.headers, :proxy => request.proxy, - :body => deflated_body(request.body) + :body => deflated_body(request.body), + :normalize_uri => request.normalize_uri ) end diff --git a/lib/http/features/normalize_uri.rb b/lib/http/features/normalize_uri.rb new file mode 100644 index 00000000..805843ae --- /dev/null +++ b/lib/http/features/normalize_uri.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module HTTP + module Features + class NormalizeUri < Feature + attr_reader :normalizer + + def initialize(normalizer: Normalizer) + @normalizer = normalizer + end + + def normalize_uri(uri) + normalizer.call(uri) + end + + module Normalizer + def self.call(uri) + HTTP::URI::NORMALIZER.call(uri) + end + end + + HTTP::Options.register_feature(:normalize_uri, self) + end + end +end diff --git a/lib/http/request.rb b/lib/http/request.rb index e29c9bda..8f5c0888 100644 --- a/lib/http/request.rb +++ b/lib/http/request.rb @@ -66,6 +66,8 @@ class UnsupportedSchemeError < RequestError; end # Scheme is normalized to be a lowercase symbol e.g. :http, :https attr_reader :scheme + attr_reader :normalize_uri + # "Request URI" as per RFC 2616 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html attr_reader :uri @@ -73,13 +75,15 @@ class UnsupportedSchemeError < RequestError; end # @option opts [String] :version # @option opts [#to_s] :verb HTTP request method + # @option opts [#to_s] :normalize_uri # @option opts [HTTP::URI, #to_s] :uri # @option opts [Hash] :headers # @option opts [Hash] :proxy # @option opts [String, Enumerable, IO, nil] :body def initialize(opts) @verb = opts.fetch(:verb).to_s.downcase.to_sym - @uri = normalize_uri(opts.fetch(:uri)) + normalizer = opts.fetch(:normalize_uri, nil) || HTTP::URI::NORMALIZER + @uri = normalizer.call(opts.fetch(:uri)) @scheme = @uri.scheme.to_s.downcase.to_sym if @uri.scheme raise(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb) @@ -212,18 +216,5 @@ def port def default_host_header_value PORTS[@scheme] != port ? "#{host}:#{port}" : host end - - # @return [HTTP::URI] URI with all componentes but query being normalized. - def normalize_uri(uri) - uri = HTTP::URI.parse uri - - HTTP::URI.new( - :scheme => uri.normalized_scheme, - :authority => uri.normalized_authority, - :path => uri.normalized_path, - :query => uri.query, - :fragment => uri.normalized_fragment - ) - end end end diff --git a/lib/http/uri.rb b/lib/http/uri.rb index 52f49a81..372f19b7 100644 --- a/lib/http/uri.rb +++ b/lib/http/uri.rb @@ -26,6 +26,17 @@ class URI # @private HTTPS_SCHEME = "https" + NORMALIZER = lambda do |uri| + uri = HTTP::URI.parse uri + HTTP::URI.new( + :scheme => uri.normalized_scheme, + :authority => uri.normalized_authority, + :path => uri.normalized_path, + :query => uri.query, + :fragment => uri.normalized_fragment + ) + end + # Parse the given URI string, returning an HTTP::URI object # # @param [HTTP::URI, String, #to_str] uri to parse diff --git a/spec/lib/http_spec.rb b/spec/lib/http_spec.rb index 403c5da5..da0c17e4 100644 --- a/spec/lib/http_spec.rb +++ b/spec/lib/http_spec.rb @@ -430,6 +430,26 @@ def setsockopt(*args) expect(response.to_s).to eq("#{body}-deflated") end end + + context "with :normalize_uri" do + it "normalizes URI" do + response = HTTP.get "#{dummy.endpoint}/hello world" + expect(response.to_s).to eq("hello world") + end + + it "uses the custom URI Normalizer method" do + client = HTTP.use(:normalize_uri => {:normalizer => :itself.to_proc}) + response = client.get("#{dummy.endpoint}/hello world") + expect(response.status).to eq(400) + end + + it "uses the default URI normalizer" do + client = HTTP.use :normalize_uri + expect(HTTP::URI::NORMALIZER).to receive(:call).and_call_original + response = client.get("#{dummy.endpoint}/hello world") + expect(response.to_s).to eq("hello world") + end + end end it "unifies socket errors into HTTP::ConnectionError" do diff --git a/spec/support/dummy_server/servlet.rb b/spec/support/dummy_server/servlet.rb index 1ca05653..7b0bb955 100644 --- a/spec/support/dummy_server/servlet.rb +++ b/spec/support/dummy_server/servlet.rb @@ -148,6 +148,11 @@ def do_#{method.upcase}(req, res) res.body = req.body end + get "/hello world" do |_req, res| + res.status = 200 + res.body = "hello world" + end + post "/encoded-body" do |req, res| res.status = 200