Skip to content

Commit a4983d1

Browse files
ybiquitousmarcotc
andauthored
Fix Rack integration to tag Content-Type and Content-Length headers (#4359)
The Rack spec says: > The environment must not contain the keys HTTP_CONTENT_TYPE or HTTP_CONTENT_LENGTH (use the versions without HTTP_). See https://github.com/rack/rack/blob/e217a399eb116362710aac7c5b8dc691ea2189b3/SPEC.rdoc?plain=1#L119-L121 Co-authored-by: Marco Costa <[email protected]>
1 parent 6021b3d commit a4983d1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/datadog/tracing/contrib/rack/header_collection.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,17 @@ def key?(header_name)
3131
end
3232

3333
def self.to_rack_header(name)
34-
"HTTP_#{name.to_s.upcase.gsub(/[-\s]/, '_')}"
34+
key = name.to_s.upcase.gsub(/[-\s]/, '_')
35+
case key
36+
when 'CONTENT_TYPE', 'CONTENT_LENGTH'
37+
# NOTE: The Rack spec says:
38+
# > The environment must not contain the keys HTTP_CONTENT_TYPE or HTTP_CONTENT_LENGTH
39+
# > (use the versions without HTTP_).
40+
# See https://github.com/rack/rack/blob/e217a399eb116362710aac7c5b8dc691ea2189b3/SPEC.rdoc?plain=1#L119-L121
41+
key
42+
else
43+
"HTTP_#{key}"
44+
end
3545
end
3646
end
3747
end

spec/datadog/tracing/contrib/rack/header_collection_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,24 @@
3232
expect(collection.get('X-Forwarded-For')).to be_nil
3333
end
3434
end
35+
36+
context 'when Content-Type and Content-Length headers exist in env' do
37+
let(:env) do
38+
{
39+
'CONTENT_TYPE' => 'application/json',
40+
'CONTENT_LENGTH' => '120'
41+
}
42+
end
43+
44+
it 'returns header value' do
45+
expect(collection.get('Content-Type')).to eq('application/json')
46+
expect(collection.get('Content-Length')).to eq('120')
47+
end
48+
49+
it 'returns header value regardless of letter casing in the name' do
50+
expect(collection.get('content-type')).to eq('application/json')
51+
expect(collection.get('content-length')).to eq('120')
52+
end
53+
end
3554
end
3655
end

0 commit comments

Comments
 (0)