#!/usr/local/bin/ruby require 'resolv-replace' require 'timeout' require 'net/http' require 'open-uri' require 'cgi' REMOTE_CONTENT_TIMEOUT = 10 REMOTE_CONTENT_MAX_LENGTH = 4 * 1024 * 1024 def get_remote_content(uri) meta = nil timeout(REMOTE_CONTENT_TIMEOUT) do size = file_size_by_uri(uri) if (size && size > REMOTE_CONTENT_MAX_LENGTH) raise RuntimeError end meta = uri.read end if (meta.length > REMOTE_CONTENT_MAX_LENGTH) raise RuntimeError end meta end def file_size_by_uri(uri) s = nil Net::HTTP.start( uri.host, uri.port ) do |http| file = uri.path if (uri.query) file += "?" + uri.query end response = http.head(file) s = response['content-length'] end if (s) s.to_i else nil end end cgi = CGI.new url = cgi['url'] if (url) c = get_remote_content(URI.parse(url)) mime = c.content_type if (mime =~ /^image/) cgi.out(mime) do c end end end