def html_or_feed str
s = str[0, 512]
pos = 0
while pos < s.length
case s[pos]
when 0x09, 0x20, 0x0A, 0x0D
pos += 1
when 0x3C
pos += 1
if s[pos..pos+2] == "!--"
pos += 3
until s[pos..pos+2] == "-->" or pos >= s.length
pos += 1
end
pos += 3
elsif s[pos] == 0x21
pos += 1
until s[pos] == 0x3E or pos >= s.length
pos += 1
end
pos += 1
elsif s[pos] == 0x3F
until s[pos..pos+1] == "?>" or pos >= s.length
pos += 1
end
pos += 2
elsif s[pos..pos+2] == "rss"
return "application/rss+xml"
elsif s[pos..pos+3] == "feed"
return "application/atom+xml"
elsif s[pos..pos+6] == "rdf:RDF"
raise NotImplementedError
end
else
break
end
end
"text/html"
end