2010-07-07 16:13:31 -04:00
|
|
|
require 'base64'
|
2010-06-08 17:16:09 -04:00
|
|
|
class BasicMongrelHandler < Mongrel::HttpHandler
|
|
|
|
attr_accessor :content_type, :custom_headers, :response_body, :response_code, :preprocessor, :username, :password
|
2009-01-28 23:37:10 -05:00
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
def initialize
|
|
|
|
@content_type = "text/html"
|
|
|
|
@response_body = ""
|
|
|
|
@response_code = 200
|
|
|
|
@custom_headers = {}
|
|
|
|
end
|
2009-01-28 23:37:10 -05:00
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
def process(request, response)
|
|
|
|
instance_eval &preprocessor if preprocessor
|
|
|
|
reply_with(response, response_code, response_body)
|
|
|
|
end
|
2009-01-28 23:37:10 -05:00
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
def reply_with(response, code, response_body)
|
|
|
|
response.start(code) do |head, body|
|
|
|
|
head["Content-Type"] = content_type
|
|
|
|
custom_headers.each { |k,v| head[k] = v }
|
|
|
|
body.write(response_body)
|
2009-01-28 23:37:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-09 19:00:41 -04:00
|
|
|
class DeflateHandler < BasicMongrelHandler
|
|
|
|
def process(request, response)
|
|
|
|
response.start do |head, body|
|
|
|
|
head['Content-Encoding'] = 'deflate'
|
|
|
|
body.write Zlib::Deflate.deflate(response_body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-12 00:41:11 -04:00
|
|
|
class GzipHandler < BasicMongrelHandler
|
|
|
|
def process(request, response)
|
|
|
|
response.start do |head, body|
|
|
|
|
head['Content-Encoding'] = 'gzip'
|
|
|
|
body.write gzip(response_body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def gzip(string)
|
|
|
|
sio = StringIO.new('', 'r+')
|
|
|
|
gz = Zlib::GzipWriter.new sio
|
|
|
|
gz.write string
|
|
|
|
gz.finish
|
|
|
|
sio.rewind
|
|
|
|
sio.read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
module BasicAuthentication
|
|
|
|
def self.extended(base)
|
|
|
|
base.custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"'
|
|
|
|
end
|
2009-01-28 23:37:10 -05:00
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
def process(request, response)
|
|
|
|
if authorized?(request)
|
|
|
|
super
|
|
|
|
else
|
|
|
|
reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
|
2009-01-28 23:37:10 -05:00
|
|
|
end
|
2010-06-08 17:16:09 -04:00
|
|
|
end
|
2009-01-28 23:37:10 -05:00
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
def authorized?(request)
|
|
|
|
request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip
|
2009-01-28 23:37:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
module DigestAuthentication
|
|
|
|
def self.extended(base)
|
|
|
|
base.custom_headers["WWW-Authenticate"] = 'Digest realm="testrealm@host.com",qop="auth,auth-int",nonce="nonce",opaque="opaque"'
|
|
|
|
end
|
2010-04-26 21:32:30 -04:00
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
def process(request, response)
|
|
|
|
if authorized?(request)
|
|
|
|
super
|
|
|
|
else
|
|
|
|
reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
|
2010-04-26 21:32:30 -04:00
|
|
|
end
|
2010-06-08 17:16:09 -04:00
|
|
|
end
|
2010-04-26 21:32:30 -04:00
|
|
|
|
2010-06-08 17:16:09 -04:00
|
|
|
def authorized?(request)
|
|
|
|
request.params["HTTP_AUTHORIZATION"] =~ /Digest.*uri=/
|
2010-04-26 21:32:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-28 23:37:10 -05:00
|
|
|
def new_mongrel_redirector(target_url, relative_path = false)
|
|
|
|
target_url = "http://#{@host_and_port}#{target_url}" unless relative_path
|
|
|
|
Mongrel::RedirectHandler.new(target_url)
|
|
|
|
end
|