Created
March 9, 2015 18:08
-
-
Save jduck/9b5880ba4f4d5b791d84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# PoC to show bug in ProgressBar/Zlib handling | |
# | |
# Joshua J. Drake (jduck) | |
# | |
COMPRESSED_WRONG_SIZE_URL = "http://sourceforge.net/projects/pocfiles/files/" | |
require 'uri' | |
require 'net/http' | |
require 'progressbar' | |
def get_with_bug(url) | |
uri = URI.parse(url) | |
gr = Net::HTTP::Get.new(uri.request_uri) | |
data = '' | |
Net::HTTP.start(uri.host, uri.port) { |http| | |
http.request(gr) { |res| | |
case res.code | |
when "200" | |
pbar = nil | |
len = res['content-length'].to_i | |
if len > 0 | |
puts "expecting #{len} bytes" | |
pbar = ProgressBar.new(" Progress", len) | |
pbar.file_transfer_mode | |
end | |
count = 0 | |
res.read_body { |chunk| | |
count += chunk.length | |
puts "#{count} / #{len} received" | |
if pbar | |
pbar.set(count) | |
end | |
data << chunk | |
} | |
puts "" | |
if (not (data and data.length > 0)) | |
raise RuntimeError, "Unable to open #{url.to_s}" | |
end | |
end | |
} | |
} | |
return data | |
end | |
def get_without_bug(url) | |
uri = URI.parse(url) | |
gr = Net::HTTP::Get.new(uri.request_uri) | |
data = '' | |
Net::HTTP.start(uri.host, uri.port) { |http| | |
http.request(gr) { |res| | |
case res.code | |
when "200" | |
pbar = nil | |
len = res['content-length'].to_i | |
if len > 0 | |
puts "expecting #{len} bytes" | |
pbar = ProgressBar.new(" Progress", len) | |
pbar.file_transfer_mode | |
end | |
count = 0 | |
res.read_body { |chunk| | |
count += chunk.length | |
puts "#{count} / #{len} received" | |
if pbar | |
pbar.set(count) rescue nil | |
end | |
data << chunk | |
} | |
puts "" | |
if (not (data and data.length > 0)) | |
raise RuntimeError, "Unable to open #{url.to_s}" | |
end | |
end | |
} | |
} | |
return data | |
end | |
data = get_without_bug(COMPRESSED_WRONG_SIZE_URL) | |
puts "without bug, we got #{data.length} bytes" | |
data = get_with_bug(COMPRESSED_WRONG_SIZE_URL) | |
puts "with bug, we got #{data.length} bytes" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment