Home > ruby > How to verify the Http-Compression of Static-Content

How to verify the Http-Compression of Static-Content

I know this can be done with Fiddler or Firebug, but i wanted something more faster and simpler, and also it was a perfect excuse for writing my first ruby script. You pass the url, the port, and the path to the element you want to check, and you are done.

Sample run against http://www.ioactive.com/affiliateImg.js

./IsCompressed.rb www.ioactive.com 80 /affiliateImg.js

  WebSite: www.ioactive.com:80
  Path: /affiliateImg.js

  Compression: NO,	1217 bytes,	message: OK ,	code: 200

And the code:

#!/usr/bin/env ruby

require 'net/http'

if ARGV.length != 3
  puts "Mode of Use: script [WebSite] [Port] [Path]"
  exit 1
end

website = ARGV[0]
port = ARGV[1]
path = ARGV[2]

puts "\n"
puts "\tWebSite: " + ARGV[0] + ":" + ARGV[1]
puts "\tPath: " + ARGV[2]
puts "\n"

begin
  http = Net::HTTP.new(website, port)
  resp, data = http.get(path, {'Accept-Encoding' => 'gzip, deflate'})

  encoding = resp.response['content-encoding']
  if encoding.nil?
    encoding = 'NO'
  end

  puts "\tCompression: "+ encoding + ",\t" + data.size.to_s + " bytes,\tmessage: "+ resp.message + ",\tcode: " + resp.code
  puts "\n"
rescue Exception => detail
  puts "An error ocurred. Execution aborted."
  puts detail.message
end
Advertisement
Categories: ruby Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.