lib/nanoc/extra/validators/links.rb in nanoc-3.3.7 vs lib/nanoc/extra/validators/links.rb in nanoc-3.4.0
- old
+ new
@@ -166,11 +166,11 @@
# Skip non-HTTP URLs
return true if uri.scheme !~ /^https?$/
# Get status
status = fetch_http_status_for(uri)
- is_valid = !!(status && status >= 200 && status <= 299)
+ is_valid = !status.nil? && status == 200
# Notify
@delegate && @delegate.send(:external_href_validated, href, is_valid)
# Done
@@ -220,12 +220,23 @@
Timeout::timeout(10) do
res = request_url_once(url)
end
if res.code =~ /^3..$/
- url = URI.parse(res['location'])
return nil if i == 5
+
+ # Find proper location
+ location = res['Location']
+ if location !~ /^https?:\/\//
+ base_url = url.dup
+ base_url.path = (location =~ /^\// ? '' : '/')
+ base_url.query = nil
+ base_url.fragment = nil
+ location = base_url.to_s + location
+ end
+
+ url = URI.parse(location)
else
return res.code.to_i
end
rescue
return nil
@@ -236,10 +247,10 @@
def request_url_once(url)
require 'net/https'
path = (url.path.nil? || url.path.empty? ? '/' : url.path)
req = Net::HTTP::Head.new(path)
- http=Net::HTTP.new(url.host, url.port)
+ http = Net::HTTP.new(url.host, url.port)
if url.instance_of? URI::HTTPS
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
res = http.request(req)