<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -20,6 +20,7 @@
 require &quot;rubygems&quot;
 require &quot;hpricot&quot;
 require &quot;open-uri&quot;
+require &quot;net/http&quot;
 
 module Feedbag
 
@@ -32,23 +33,32 @@ module Feedbag
 		'application/rdf+xml',
 	]
 
+	@debug = 1
+
 	$feeds = []
+	$base_uri = nil
 
 	def self.find(url)
 		$feeds = []
 		begin
 			html = open(url) do |f|
 				if @content_types.include?(f.content_type.downcase)
-					return self.add_feed(url)
+					return self.add_feed(url, nil)
 				end
 
 				doc = Hpricot(f.read)
 
+				if doc.at(&quot;base&quot;) and doc.at(&quot;base&quot;)[&quot;href&quot;]
+					$base_uri = doc.at(&quot;base&quot;)[&quot;href&quot;]
+				else
+					$base_uri = nil
+				end
+
 				# first with links
 				(doc/&quot;link&quot;).each do |l|
 					next unless l[&quot;rel&quot;]
 					if l[&quot;type&quot;] and @content_types.include?(l[&quot;type&quot;].downcase.strip) and (l[&quot;rel&quot;].downcase =~ /alternate/i or l[&quot;rel&quot;] == &quot;service.feed&quot;)
-						self.add_feed(l[&quot;href&quot;], url)
+						self.add_feed(l[&quot;href&quot;], url, $base_uri)
 					end
 				end
 				
@@ -59,7 +69,7 @@ module Feedbag
 						a[&quot;href&quot;] =~ /feed=(rss2|atom)/i or 
 						a[&quot;href&quot;] =~ /(atom|feed)\/$/i)
 
-						self.add_feed(a[&quot;href&quot;], url)
+						self.add_feed(a[&quot;href&quot;], url, $base_uri)
 					end
 				end
 
@@ -71,9 +81,17 @@ module Feedbag
 		$feeds
 	end
 
-	def self.add_feed(feed_url, orig_url)
+	def self.add_feed(feed_url, orig_url, base_uri = nil)
+		puts &quot;#{feed_url} - #{orig_url}&quot;
 		url = feed_url.sub(/^feed:/, '').strip
 
+		if base_uri
+			url = base_uri + feed_url
+			puts &quot;base_uri: #{base_uri}&quot; if @debug
+			puts &quot;orig_url: #{orig_url}&quot; if @debug
+			puts &quot;feed_url: #{feed_url}&quot; if @debug
+		end
+
 		begin
 			uri = URI.parse(url)
 		rescue
@@ -84,7 +102,21 @@ module Feedbag
 			orig = URI.parse(orig_url)
 			url = orig.merge(url).to_s
 		end
-		$feeds.push(url)
+
+		# verify url is really valid
+		$feeds.push(url) if self._is_http_valid(URI.parse(url), url)
+	end
+
+	def self._is_http_valid(uri, url)
+		req = Net::HTTP.get_response(uri)
+		case req
+			when Net::HTTPSuccess then
+				return true
+			else
+				guess_url = &quot;#{uri.scheme}://#{uri.host}#{uri.path}&quot;
+				return false if guess_url == url
+				return true if self._is_http_valid(URI.parse(guess_url), guess_url)
+		end
 	end
 end
 </diff>
      <filename>feedbag.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,6 @@
 #!/usr/bin/ruby
 
-require &quot;../feedbag&quot;
+require &quot;#{File.dirname(__FILE__)}/../feedbag&quot;
 require &quot;test/unit&quot;
 require &quot;open-uri&quot;
 require &quot;hpricot&quot;
@@ -9,35 +9,36 @@ require &quot;pp&quot;
 class AtomAutoDiscoveryTest &lt; Test::Unit::TestCase
 	def test_autodisc
 		base_url = &quot;http://diveintomark.org/tests/client/autodiscovery/&quot;
-		url = &quot;html4-001.html&quot;
+		url = base_url + &quot;html4-001.html&quot;
 
 		i = 1
 		puts &quot;trying now with #{url}&quot;
 		while(i)
+			puts
 			i = 0 # unless otherwise found
 
-			f = Feedbag.find base_url + url
+			f = Feedbag.find url
 
 			assert_instance_of Array, f
-			assert f.size == 1, &quot;Feedbag didn't find a feed on #{base_url + url} or found more than one&quot;
+			assert f.size == 1, &quot;Feedbag didn't find a feed on #{url} or found more than one&quot;
 
 			puts &quot; found #{f[0]}&quot;
 			feed = Hpricot(open(f[0]))
 	
 			(feed/&quot;link&quot;).each do |l|
 				next unless l[&quot;rel&quot;] == &quot;alternate&quot;
-				assert_equal l[&quot;href&quot;], base_url + url
+				assert_equal l[&quot;href&quot;], url
 			end
 
 			# ahora me voy al siguiente
-			html = Hpricot(open(base_url + url))
+			html = Hpricot(open(url))
 			(html/&quot;link&quot;).each do |l|
 				next unless l[&quot;rel&quot;] == &quot;next&quot;
-				url = l[&quot;href&quot;]
+				url = URI.parse(base_url).merge(l[&quot;href&quot;]).to_s
 				puts &quot;trying now with #{url}&quot;
 				i = 1
 			end
-
+		
 		end	
 	end
 </diff>
      <filename>test/atom_autodiscovery_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>6bda23094a9639cff53fc27cb2ba320cceca02c2</id>
    </parent>
  </parents>
  <author>
    <name>David Moreno</name>
    <email>david@axiombox.com</email>
  </author>
  <url>http://github.com/damog/feedbag/commit/98c2023b540b8d6d7931bd8df1ddfe1edd81ec3c</url>
  <id>98c2023b540b8d6d7931bd8df1ddfe1edd81ec3c</id>
  <committed-date>2008-12-25T09:18:04-08:00</committed-date>
  <authored-date>2008-12-25T09:18:04-08:00</authored-date>
  <message>adding _is_http_valid for unsuccessful testing: undone</message>
  <tree>784d2c25262846f71c27fdb0f87b6be6488e05ce</tree>
  <committer>
    <name>David Moreno</name>
    <email>david@axiombox.com</email>
  </committer>
</commit>
