Skip to content

Commit 965be77

Browse files
committed
Image scraping: evaluate Content-Length header
1 parent 255f71b commit 965be77

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/docs/core/response.rb

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ def blank?
1212
body.blank?
1313
end
1414

15+
def content_length
16+
value = headers['Content-Length'] || '0'
17+
value.to_i
18+
end
19+
1520
def mime_type
1621
headers['Content-Type'] || 'text/plain'
1722
end

lib/docs/filters/core/images.rb

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def call
4646
next
4747
end
4848

49+
size = response.content_length
50+
51+
if size > (context[:max_image_size] || DEFAULT_MAX_SIZE)
52+
instrument 'too_big.image', url: url, size: size
53+
next
54+
end
55+
4956
image = response.body
5057

5158
unless context[:optimize_images] == false

test/lib/docs/core/response_test.rb

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ class DocsResponseTest < MiniTest::Spec
6363
end
6464
end
6565

66+
describe "#content_length" do
67+
it "returns the content type" do
68+
options.headers['Content-Length'] = '188420'
69+
assert_equal 188420, response.content_length
70+
end
71+
72+
it "defaults to 0" do
73+
assert_equal 0, response.content_length
74+
end
75+
end
76+
6677
describe "#mime_type" do
6778
it "returns the content type" do
6879
options.headers['Content-Type'] = 'type'

0 commit comments

Comments
 (0)