From 33e9f2d6a474329d3f1d2bb3b6f80f0901eb868b Mon Sep 17 00:00:00 2001 From: Frederik Erbs Spang Thomsen Date: Tue, 29 Oct 2024 22:39:23 +0100 Subject: [PATCH] Borrow Rack::Utils for building nested query --- sentry-ruby/lib/sentry/excon/middleware.rb | 2 +- sentry-ruby/lib/sentry/utils/http_tracing.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/sentry-ruby/lib/sentry/excon/middleware.rb b/sentry-ruby/lib/sentry/excon/middleware.rb index 0bb86e734..9c6e59467 100644 --- a/sentry-ruby/lib/sentry/excon/middleware.rb +++ b/sentry-ruby/lib/sentry/excon/middleware.rb @@ -65,7 +65,7 @@ def extract_request_info(env) result[:query] = env[:query] # Handle excon 1.0.0+ - result[:query] = ::Rack::Utils.build_nested_query(result[:query]) unless result[:query].is_a?(String) + result[:query] = build_nested_query(result[:query]) unless result[:query].is_a?(String) result[:body] = env[:body] end diff --git a/sentry-ruby/lib/sentry/utils/http_tracing.rb b/sentry-ruby/lib/sentry/utils/http_tracing.rb index abf6a9141..e250d7160 100644 --- a/sentry-ruby/lib/sentry/utils/http_tracing.rb +++ b/sentry-ruby/lib/sentry/utils/http_tracing.rb @@ -36,6 +36,25 @@ def propagate_trace?(url) Sentry.configuration.propagate_traces && Sentry.configuration.trace_propagation_targets.any? { |target| url.match?(target) } end + + # Kindly borrowed from Rack::Utils + def build_nested_query(value, prefix = nil) + case value + when Array + value.map { |v| + build_nested_query(v, "#{prefix}[]") + }.join("&") + when Hash + value.map { |k, v| + build_nested_query(v, prefix ? "#{prefix}[#{k}]" : k) + }.delete_if(&:empty?).join("&") + when nil + URI.encode_www_form_component(prefix) + else + raise ArgumentError, "value must be a Hash" if prefix.nil? + "#{URI.encode_www_form_component(prefix)}=#{URI.encode_www_form_component(value)}" + end + end end end end