Skip to content

Commit

Permalink
Merge pull request #2328 from DataDog/fix-uri-exception-again
Browse files Browse the repository at this point in the history
Handle REQUEST_URI with base url
  • Loading branch information
lloeki authored Oct 26, 2022
2 parents 7c03bb3 + e805fd4 commit 0c66c3b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/datadog/tracing/contrib/rack/middlewares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,12 @@ def parse_url(env, original_env)

query_string.empty? ? path : "#{path}?#{query_string}"
else
request_uri
# normally REQUEST_URI starts at the path, but it
# might contain the full URL in some cases (e.g WEBrick)
request_uri.sub(/^#{base_url}/, '')
end

::URI.join(base_url, fullpath).to_s
base_url + fullpath
end

def parse_user_agent_header(headers)
Expand Down
23 changes: 23 additions & 0 deletions spec/datadog/tracing/contrib/rack/middlewares_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,27 @@
end
end
end

# Non-ASCII URLs cannot be tested with `rack-test` as of v2.0.2.
# It would be ideal if that was possible, as we could create integration tests
# for such cases.
#
# As an alternative, we test the parsing method directly.
describe '#parse_url' do
subject(:parse_url) { middleware.send(:parse_url, env, original_env) }
let(:env) { { 'REQUEST_URI' => uri, 'HTTP_HOST' => 'localhost:443', 'rack.url_scheme' => 'https' } }
let(:original_env) { {} }

context 'with Unicode characters' do
let(:uri) { 'https://localhost/success/?繋がってて' }

it { is_expected.to eq(uri) }
end

context 'with unencoded ASCII characters' do
let(:uri) { 'https://localhost/success/|' }

it { is_expected.to eq(uri) }
end
end
end

0 comments on commit 0c66c3b

Please sign in to comment.