无效的下一个编译错误

Invalid next compile error

我有一种方法可以扫描 URL 包含错误的网站:

def begin_vulnerability_check
  info("Checking if sites are vulnerable.")
  IO.read("#{PATH}/temp/SQL_sites_to_check.txt").each_line do |parse|
    Timeout::timeout(10) do
      parsing = Nokogiri::HTML(RestClient.get("#{parse.chomp}"))
      info("Parsing page for SQL syntax error: #{parse.chomp}")
      if parsing.css('html')[0].to_s[/You have an error in your SQL syntax/]
        successful = parse
        success("URL: #{parse.chomp} returned SQL syntax error, dumped to SQL_VULN.txt")
        File.open("#{PATH}/lib/SQL_VULN.txt", "a+"){|s| s.puts(parse)}
        sleep(1)
      else
        err("URL: #{parse.chomp} returned and error, dumped to non_exploitable.txt")
        File.open("#{PATH}/lib/non_exploitable.txt", "a+"){|s| s.puts(parse)}
        sleep(1)
      end
    end
  end
end

在测试期间,我正在扫描这个 URL 列表:

http://www.bible.com/subcat.php?id=2'
http://www.cidko.com/pro_con.php?id=3'
http://www.slavsandtars.com/about.php?id=25'
http://www.police.gov/content.php?id=275'
http://www.icdprague.org/index.php?id=10'
http://huawei.com/en/plugin.php?id=hwdownload'
https://huawei.com/en/plugin.php?id=unlock'
https://facebook.com/profile.php?id'
http://www.footballclub.com.au/index.php?id=43'
http://www.mesrs.gouv/index.php?id=1525'

我还有一个救援块,它应该捕获异常 Timeout::Error 并移动到列表中的下一个 URL:

begin
  begin_vulnerability_check
rescue Timeout::Error
   if Timeout::Error 
     warn("Page timed out, this is usually cause by the page returning a white page, or being non-existent, skipping.")
     next
  end
end

然而,在尝试 运行 这个程序时,我收到以下错误:

whitewidow.rb:130: Invalid next
whitewidow.rb: compile error (SyntaxError)

第 130 行:

rescue Timeout::Error
   if Timeout::Error 
     warn("Page timed out, this is usually cause by the page returning a white page, or being non-existent, skipping.")
     next #<= HERE
  end
end

我的问题是,我是否在错误的意义上使用了 next?在我看来,下一步是,如果发生这种情况,请转到下一行,我这样想是不是错了?我怎样才能重构它以使其工作?

您可以从块中使用 next 到 return。你不能像你想做的那样在块外使用它。

但您甚至不需要 next,因为当您挽救超时错误时,迭代将自动继续下一行。您只需将 rescue 移动到 each_line 迭代中。

您的代码应该是这样的:

def begin_vulnerability_check
  IO.read("#{PATH}/temp/SQL_sites_to_check.txt").each_line do |parse|
    begin
      Timeout::timeout(10) do
        ...
      end
    rescue Timeout::Error
      # Will automatically continue with next line after this
    end
  end
end