Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
#!/usr/bin/ruby -w # This is a Pavatar check script written in ruby by Jeena Paradies # I hereby place this script into the public domain. # # This is version 0.2 and it could include some bugs I haven't found yet. # Because the program is licensed free of charge, there is no warranty # for the program, but it works for me, try it if you want. # # If you have some questions contact me: pavatar@jeenaparadies.net # include some libraries require 'cgi' require 'uri' require 'net/http' require 'image_size' # see http://www.rubycgi.org/archive/ class Pavatar # content types defined by the spec http://pavatar.com/spec#technical-definition @@content_types = ['image/png', 'image/jpeg', 'image/gif'] # fetch the pavatar url on initialize def initialize url @url = url @purl = URI.parse(@url) # check for the first real pavatar url and save it into a class variable if (@pavatar = xHeader).nil? and (@pavatar = link).nil? and (@pavatar = path).nil? @pavatar = favicon end end # Autodiscovery methods # get url from header if there is one def xHeader begin res = FetchHelper.head(@url) res['X-Pavatar'] unless res['X-Pavatar'].nil? rescue end end # get url from <link> if there is one def link begin res = FetchHelper.get(@url) pavatar = /<link rel="pavatar" href="([^"]+)" ?\/?>/.match(res.body) pavatar[1] unless pavatar.nil? rescue end end # look in users path if there is a pavatar def path begin # to discover the given directory is a little bit tricky in ruby path = @purl.path.to_s unless path[-1..-1] == '/' path = File.dirname(path) unless path == '.' or path == '/' path << '/' end end if path == '.' path = '/' end url = 'http://' + @purl.host.to_s + path + 'pavatar.png' # if would be ok to look on the header here but Apache has a problem with # head requests so we use a get request to workaround it res = FetchHelper.get(url) # some people redirect to a 404 page and send status 200 so we need to check the content type url unless res['content-type'].nil? or not @@content_types.include?(res['content-type']) rescue end end # look in users home directory if there is a pavatar def favicon begin url = 'http://' + @purl.host.to_s + '/pavatar.png' # same problem with apache here res = FetchHelper.get(url) url unless res['content-type'].nil? or not @@content_types.include?(res['content-type']) rescue end end # url getter def get_url @pavatar end # get a whole html string to show a pavatar def get_html (attr = '') attr = ' ' + attr unless attr.length > 0 '<img src="' + get_url + '"' + attr + ' />' end end # helps us handle redirections module FetchHelper def FetchHelper.head(url_str, limit = 10) # stop if there is a loop raise ArgumentError, 'HTTP redirect too deep' if limit == 0 url = URI.parse(url_str) path = File.dirname(url.path.to_s) response = nil Net::HTTP.start(url.host, url.port) { |http| response = http.head( path == '.' ? '/' : url.path.to_s ) } case response when Net::HTTPSuccess then response when Net::HTTPRedirection then FetchHelper.head(response['location'], limit - 1) else response.error! end end def FetchHelper.get(url_str, limit = 10) raise ArgumentError, 'HTTP redirect too deep.' if limit == 0 response = Net::HTTP.get_response(URI.parse(url_str)) case response when Net::HTTPSuccess then response when Net::HTTPRedirection then FetchHelper.get(response['location'], limit - 1) else response.error! end end def FetchHelper.validate(url, what) begin uri = URI.parse(url) if uri.class != URI::HTTP raise ArgumentError, "Only HTTP protocol addresses can be used in the #{what} URL." end rescue URI::InvalidURIError raise ArgumentError, "The format of the #{what} URL is not valid." end end end # ------------------------------------- # HTML output # ------------------------------------- cgi = CGI.new url = cgi['hp'] # print our HTML header print "Content-type: text/html\r\n\r\n" print <<HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Check your Pavatar!</title> <style type="text/css"> body { font-family: verdana, arial, sans-serif; text-align: center; background: url(http://jeenaparadies.net/webdesign/jlog/personal/css/img/body.png) repeat-x white; font-size: 90%; color: black; } h1 { font-family: "Trebuchet MS"; font-size: 350%; } a { color: black; text-decoration: none; } a:hover { text-decoration: underline; } form { margin-top: 5em; } .pavatar { min-height: 300px; background: url(http://jeenaparadies.net/img/pavatar-logo.png) no-repeat bottom center; padding-top: 50px; } * html .pavatar { height: 300px; } /* IE hack */ </style> </head> <body> <h1>Check your <a href="http://pavatar.com">Pavatar</a>!</h1> <form action=""> <p>Your homepage:</p> <p><input type="text" name="hp" value="#{ cgi['hp'] }" size="25" /> <input type="submit" value="Check it now!" /></p> <p class="pavatar"> HTML types = { 'PNG' => 'image/png', 'GIF' => 'image/gif', 'JPEG' => 'image/jpeg' } unless cgi['hp'].length == 0 then begin FetchHelper.validate(url, 'homepage') pavatar_url = Pavatar.new(cgi['hp']) raise ArgumentError, "No pavatar here." if pavatar_url.get_url.nil? or pavatar_url.get_url == "none" FetchHelper.validate(pavatar_url.get_url, 'Pavatar') pavatar = FetchHelper.get(pavatar_url.get_url) raise ArgumentError, "File size too big (#{(pavatar.body.to_s.size * 8)}) max: 409600." if (pavatar.body.to_s.size * 8) > 409600 img = ImageSize.new(pavatar.body) raise ArgumentError, "Width is not 80px but #{img.get_width}px." unless img.get_width == 80 raise ArgumentError, "Height is not 80px but #{img.get_height}px." unless img.get_height == 80 raise ArgumentError, "False image type: #{img.get_type} #{pavatar_url.get_url}." unless types.has_key? img.get_type print pavatar_url.get_html('alt="My Pavatar"') rescue Exception => e print "<strong>Error:</strong><br />#{e}\r\n" end end print <<HTML </p> </form> </body> </html> HTML
This paste will be private.
From the Design Piracy series on my blog: