Skip to content

Instantly share code, notes, and snippets.

@wagenet
Created September 3, 2010 21:40
Show Gist options
  • Save wagenet/564615 to your computer and use it in GitHub Desktop.
Save wagenet/564615 to your computer and use it in GitHub Desktop.
use Rack::ShowExceptions
use Rack::CommonLogger
require 'rack/request'
require 'rack/response'
require 'open-uri'
class HeaderProxy
def call(env)
req = Rack::Request.new(env)
Rack::Response.new.finish do |res|
url = req.path[1..-1]
type = req.params['type'] || 'text/html'
begin
file = open(url).read;
# Special case for gists
if url.include?('gist.github.com')
raw_url = file.match(/\/raw[^"]+/);
url = 'http://gist.github.com'+raw_url[0];
file = open(url).read
end
res.write file
res.headers['ContentType'] = type;
rescue OpenURI::HTTPError => e
res.write "Error opening: #{url}\n"
res.write e.inspect
end
end
end
end
run HeaderProxy.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment