无效的下一个编译错误

时间:2016-04-02 14:15:23

标签: ruby next

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

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

在测试过程中,我正在浏览此网址列表:

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并转移到列表中的下一个网址:

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?在我看来,接下来会发生这种情况,如果发生这种情况,请转到下一行,我是否认为这样做错了?我怎样才能重构这个呢?

1 个答案:

答案 0 :(得分:1)

您可以使用next从块中返回。你不能像你想要的那样在街区之外使用它。

但是你甚至不需要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