lib/css_parser/parser.rb in css_parser-1.4.2 vs lib/css_parser/parser.rb in css_parser-1.4.3
- old
+ new
@@ -20,11 +20,13 @@
STRIP_HTML_COMMENTS_RX = /\<\!\-\-|\-\-\>/m
# Initial parsing
RE_AT_IMPORT_RULE = /\@import\s*(?:url\s*)?(?:\()?(?:\s*)["']?([^'"\s\)]*)["']?\)?([\w\s\,^\]\(\))]*)\)?[;\n]?/
- # Array of CSS files that have been loaded.
+ MAX_REDIRECTS = 3
+
+ # Array of CSS files that have been loaded.
attr_reader :loaded_uris
#--
# Class variable? see http://www.oreillynet.com/ruby/blog/2007/01/nubygems_dont_use_class_variab_1.html
#++
@@ -436,12 +438,26 @@
# Returns the file's data and character set in an array.
#--
# TODO: add option to fail silently or throw and exception on a 404
#++
def read_remote_file(uri) # :nodoc:
- return nil, nil unless circular_reference_check(uri.to_s)
+ if @redirect_count.nil?
+ @redirect_count = 0
+ else
+ @redirect_count += 1
+ end
+ unless circular_reference_check(uri.to_s)
+ @redirect_count = nil
+ return nil, nil
+ end
+
+ if @redirect_count > MAX_REDIRECTS
+ @redirect_count = nil
+ return nil, nil
+ end
+
src = '', charset = nil
begin
uri = Addressable::URI.parse(uri.to_s)
@@ -464,12 +480,17 @@
res = http.get(uri.request_uri, {'User-Agent' => USER_AGENT, 'Accept-Encoding' => 'gzip'})
src = res.body
charset = fh.respond_to?(:charset) ? fh.charset : 'utf-8'
if res.code.to_i >= 400
+ @redirect_count = nil
raise RemoteFileError if @options[:io_exceptions]
return '', nil
+ elsif res.code.to_i == 301 or res.code.to_i == 302
+ if res.response['Location'] != nil
+ return read_remote_file Addressable::URI.parse(URI::encode(res.response['Location']))
+ end
end
case res['content-encoding']
when 'gzip'
io = Zlib::GzipReader.new(StringIO.new(res.body))
@@ -487,13 +508,15 @@
ic = Iconv.new('UTF-8//IGNORE', charset)
src = ic.iconv(src)
end
end
rescue
+ @redirect_count = nil
raise RemoteFileError if @options[:io_exceptions]
return nil, nil
end
+ @redirect_count = nil
return src, charset
end
private
# Save a folded declaration block to the internal cache.