Skip to content

Commit

Permalink
Initial commit used to trace 302 redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTruncer committed Oct 9, 2014
1 parent 61118be commit 2a20a0a
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions WebTrace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env ruby

require 'net/http'
require 'net/https'
require 'uri'

# The fetch function was based off of the function at the following URL
# http://stackoverflow.com/questions/6934185/ruby-net-http-following-redirects

def fetch(uri_str, url_list, limit = 10)
# You should choose better exception.
raise ArgumentError, 'HTTP redirect too deep' if limit == 0

uri = URI.parse(uri_str)

if uri_str.start_with?("http://")
# code came from - http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
elsif uri_str.start_with?("https://")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
end

response = http.request(request)
case response
when Net::HTTPSuccess
url_list.push("#{uri_str} <- Final URL")
when Net::HTTPRedirection
url_list.push("#{uri_str} redirects to...")
fetch(response['location'], url_list, limit - 1)
else
response.error!
end
end

# Check to make sure we have only one argument, the URL
if ARGV.length != 1
puts "[*] Error: Please provide a URL to check for redirects!"
puts "[*] Usage: ./WebTrace.rb <URL>"
exit
end

# Check to make sure it's a valid URL
if ARGV[0] =~ URI::regexp
else
puts "[*] Error: Please provide a valid URL!"
puts "[*] Usage: ./WebTrace.rb <URL>"
exit
end

# Array which will store all redirects
all_urls = []

# Function that checks for redirects
fetch(ARGV[0], all_urls)

# If no redirects, say so. Otherwise, list all redirects
if all_urls.length == 1
puts "No Redirection"
else
all_urls.each do |ind_url|
puts ind_url
end
end

0 comments on commit 2a20a0a

Please sign in to comment.