Skip to content

Instantly share code, notes, and snippets.

@sentientmonkey
Created March 25, 2013 00:16
Show Gist options
  • Select an option

  • Save sentientmonkey/5234186 to your computer and use it in GitHub Desktop.

Select an option

Save sentientmonkey/5234186 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/http'
require 'pp'
string_output = StringIO.new
http = Net::HTTP.new('www.iana.org')
http.set_debug_output string_output
http.start do |http|
request = Net::HTTP::Get.new('/domains/example')
response = http.request request
end
lines = string_output.string.split("\n")
request_headers = Hash[lines.select{|line| line =~ /-> "[\w\-]*:/ }.map{|line| line.gsub(/-> \"(.*) *: *(.*)\"$/, '\1:\2').split(':', 2) }]
pp reqest_headers
# {"Date"=>" Mon, 25 Mar 2013 00:15:05 GMT\\r\\n",
# "Server"=>"Apache/2.2.3 (CentOS)\\r\\n",
# "Last-Modified"=>" Fri, 04 Jan 2013 01:17:22 GMT\\r\\n",
# "Vary"=>"Accept-Encoding\\r\\n",
# "Connection"=>"close\\r\\n",
# "Transfer-Encoding"=>"chunked\\r\\n",
# "Content-Type"=>"text/html; charset=UTF-8\\r\\n"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment