Created
May 16, 2013 13:08
-
-
Save typhonius/5591581 to your computer and use it in GitHub Desktop.
My first ruby script. Is a webserver where you pass the URL like localhost:8080/http://google.com and it'll tell you if google is up and what response given etc. It's terrible but it's a start.
This file contains 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/ruby | |
#require "net/http" | |
require "net/https" | |
require "uri" | |
require "socket" | |
require "yaml" | |
$redirects = Array.new | |
def fetch(uri_str, limit = 10) | |
raise ArgumentError, 'too many HTTP redirects' if limit == 0 | |
response = Net::HTTP.get_response(URI(uri_str)) | |
case response | |
when Net::HTTPSuccess then | |
response | |
when Net::HTTPRedirection then | |
location = response['location'] | |
#warn "redirected to #{location}" | |
$redirects << location | |
fetch(location, limit - 1) | |
else | |
response.value | |
end | |
end | |
webserver = TCPServer.new('0.0.0.0', 8080) | |
while (session = webserver.accept) | |
while line = session.gets | |
md = line.match(/^GET\s\/(\S+)\s/) | |
if !md.nil? && md[1] =~ /^#{URI::regexp}$/ | |
puts md[1] | |
puts 'cows' | |
result = fetch(md[1]) | |
output = { | |
'isup_status' => 'success', | |
'uri' => md[1], | |
'code' => result.code, | |
'status' => result.message, | |
'redirects' => $redirects, | |
} | |
session.write(YAML.dump(output)) | |
$redirects.clear | |
break | |
else | |
output = { | |
'isup_status' => 'error', | |
} | |
session.write(YAML.dump(output)) | |
break | |
end | |
end | |
session.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment