diff --git a/.gitignore b/.gitignore index 2e83ba2..fa91dff 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ config.txt test.txt rete-test.sh -rh.txt \ No newline at end of file +rh.txt +.vscode/ \ No newline at end of file diff --git a/src/rete.cr b/src/rete.cr index 25280d5..6fbe43f 100644 --- a/src/rete.cr +++ b/src/rete.cr @@ -2,6 +2,26 @@ require "option_parser" require "http/client" require "validator" +OptionParser.parse do |parser| + parser.banner = "Usage: rete [arguments] [urls]" + parser.on("-f FILE", "--file=FILE", "Checks all urls in a txt file.") { |file| + urls = File.read(file) + checkUrlFromFile urls + exit 0} + parser.on("-v", "--version", "Displays current version of rete.") { + puts "Rete 1.0 by koffee.zip" + exit(0)} + parser.on("-h", "--help", "Show this help") do + puts parser + exit + end + parser.invalid_option do |flag| + STDERR.puts "ERROR: #{flag} is not a valid option." + STDERR.puts parser + exit(1) + end +end + def checkUrlFromFile(content) # Sorry for the crappy method name. repeat = 0 working = 0 @@ -12,9 +32,22 @@ def checkUrlFromFile(content) # Sorry for the crappy method name. urls = content.split("\n") while repeat < urls.size - if Valid.url? urls[repeat] + begin + validUrl = Valid.url? urls[repeat] + rescue Regex::Error + validUrl = false + end + + if validUrl == true begin - response = HTTP::Client.get(urls[repeat]) + STDIN.read_timeout = 1 + uri = URI.parse(urls[repeat]) + client = HTTP::Client.new(uri) + client.connect_timeout = 5.seconds + client.read_timeout = 10.seconds + client.write_timeout = 5.seconds + + response = client.get("/") if config.includes?(response.status_code.to_s) puts "#{urls[repeat]} is a valid URL and works. Status code: #{response.status_code}" working += 1 @@ -22,7 +55,7 @@ def checkUrlFromFile(content) # Sorry for the crappy method name. puts "#{urls[repeat]} is a valid URL but does not work. Status Code: #{response.status_code}" notWorking += 1 end - rescue Socket::Addrinfo::Error + rescue puts "#{urls[repeat]} is a valid URL but does not exist." notWorking += 1 end @@ -41,24 +74,6 @@ def checkUrlFromFile(content) # Sorry for the crappy method name. exit 0 end -OptionParser.parse do |parser| - parser.banner = "Usage: rete [arguments] [urls]" - parser.on("-f FILE", "--file=FILE", "Checks all urls in a txt file.") { |file| - urls = File.read(file) - checkUrlFromFile urls - exit 0} - parser.on("-v", "--version", "Displays current version of rete.") {puts "Rete 1.0 by koffee.zip"} - parser.on("-h", "--help", "Show this help") do - puts parser - exit - end - parser.invalid_option do |flag| - STDERR.puts "ERROR: #{flag} is not a valid option." - STDERR.puts parser - exit(1) - end -end - repeat = 0 working = 0 notWorking = 0