added flag for timeout and added error handling for if the config.txt is missing.

This commit is contained in:
2026-07-16 01:02:45 -06:00
parent 0a4e9e1617
commit e764dcceb3
+17 -8
View File
@@ -3,18 +3,21 @@ require "http/client"
require "validator" require "validator"
output = "passed.txt" output = "passed.txt"
urlTimeout = 1
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.banner = "Usage: rete [arguments] [urls/file]" parser.banner = "Usage: rete [arguments] [file/urls]"
parser.on("-t TIMEOUT", "--timeout=TIMEOUT", "Sets how many seconds until rete declares a timeout") {|timeout|
urlTimeout = timeout}
parser.on("-f FILE", "--file=FILE", "Checks all urls in a txt file.") { |file| parser.on("-f FILE", "--file=FILE", "Checks all urls in a txt file.") { |file|
urls = File.read(file) urls = File.read(file)
checkUrlFromFile urls, output checkUrlFromFile urls, output, urlTimeout.to_i
exit 0} exit 0}
parser.on("-o FILENAME", "--output=FILENAME", "Sets the name of the outputted file. -f flag required") {|filename|
output = filename}
parser.on("-v", "--version", "Displays current version of rete.") { parser.on("-v", "--version", "Displays current version of rete.") {
puts "Rete 1.0 by koffee.zip" puts "Rete 1.0 by koffee.zip"
exit(0)} exit(0)}
parser.on("-o FILENAME", "--output=FILENAME", "Sets teh name of the outputted file. -f flag required") {|filename|
output = filename}
parser.on("-h", "--help", "Show this help") do parser.on("-h", "--help", "Show this help") do
puts parser puts parser
exit exit
@@ -26,12 +29,20 @@ OptionParser.parse do |parser|
end end
end end
def checkUrlFromFile(content, output) # Sorry for the crappy method name. def checkUrlFromFile(content, output, urlTimeout) # Sorry for the crappy method name.
repeat = 0 repeat = 0
working = 0 working = 0
notWorking = 0 notWorking = 0
exist = true exist = true
begin
config = File.read("config.txt") config = File.read("config.txt")
rescue File::NotFoundError
puts "Oh No! It seems that your config.txt file does not exist!"
puts "This is required for rete to know what classifies if a url is working or not!"
puts "You can fix this by creating a config.txt file and by then entering status codes you classify as \"working\""
exit(1)
end
urls = content.split("\n") urls = content.split("\n")
@@ -47,9 +58,7 @@ def checkUrlFromFile(content, output) # Sorry for the crappy method name.
STDIN.read_timeout = 1 STDIN.read_timeout = 1
uri = URI.parse(urls[repeat]) uri = URI.parse(urls[repeat])
client = HTTP::Client.new(uri) client = HTTP::Client.new(uri)
client.connect_timeout = 5.seconds client.connect_timeout = urlTimeout.seconds
client.read_timeout = 10.seconds
client.write_timeout = 5.seconds
response = client.get("/") response = client.get("/")
if config.includes?(response.status_code.to_s) if config.includes?(response.status_code.to_s)