Sha256: 1c8762afaa0fc704a315dc8872928ada005f5a08f8fcb516728cd818d61be713

Contents?: true

Size: 1.57 KB

Versions: 47

Compression:

Stored size: 1.57 KB

Contents

def basic_mongrel_handler
  Class.new(Mongrel::HttpHandler) do
    attr_writer :content_type, :response_body, :response_code

    def initialize
      @content_type = "text/html"
      @response_body = ""
      @response_code = 200
      @custom_headers = {}
    end

    def process(request, response)
      reply_with(response, @response_code, @response_body)
    end

    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)
      end
    end
  end
end

def new_mongrel_handler
  basic_mongrel_handler.new
end

def add_basic_authentication_to(handler)
  m = Module.new do
    attr_writer :username, :password

    def self.extended(base)
      base.instance_eval { @custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"' }
      base.class_eval { alias_method_chain :process, :basic_authentication }
    end

    def process_with_basic_authentication(request, response)
      if authorized?(request) then process_without_basic_authentication(request, response)
      else reply_with(response, 401, "Incorrect.  You have 20 seconds to comply.")
      end
    end

    def authorized?(request)
      request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip
    end
  end
  handler.extend(m)
end

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

Version data entries

47 entries across 47 versions & 14 rubygems

Version Path
alexvollmer-httparty-0.3.1 features/steps/mongrel_helper.rb
alexvollmer-httparty-0.4.3 features/steps/mongrel_helper.rb
cluon-httparty-0.4.3 features/steps/mongrel_helper.rb
dbalatero-httparty-0.4.4 features/steps/mongrel_helper.rb
geetarista-httparty-0.4.5 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.2.10 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.2.9 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.3.0 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.3.1 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.4.0 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.4.1 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.4.2 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.4.3 features/steps/mongrel_helper.rb
jnunemaker-httparty-0.4.4 features/steps/mongrel_helper.rb
kerryb-httparty-0.3.2 features/steps/mongrel_helper.rb
kerryb-httparty-0.3.3 features/steps/mongrel_helper.rb
kerryb-httparty-0.4.3 features/steps/mongrel_helper.rb
leh-httparty-0.3.1.1 features/steps/mongrel_helper.rb
mfilej-httparty-0.4.3 features/steps/mongrel_helper.rb
nullstyle-httparty-0.4.4 features/steps/mongrel_helper.rb