Created
September 3, 2010 21:40
-
-
Save wagenet/564615 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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