Skip to content

Commit

Permalink
Merge pull request igrigorik#192 from ezkl/http-proxy-connopts
Browse files Browse the repository at this point in the history
Authenticated HTTP proxy patch
  • Loading branch information
igrigorik committed May 31, 2012
2 parents 807d013 + 7d5752d commit f090f4b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/em-http/client.rb
Expand Up @@ -133,9 +133,10 @@ def normalize_body(body)

def build_request
head = @req.headers ? munge_header_keys(@req.headers) : {}

if @req.http_proxy?
head['proxy-authorization'] = @req.proxy[:authorization] if @req.proxy[:authorization]

if @conn.connopts.http_proxy?
proxy = @conn.connopts.proxy
head['proxy-authorization'] = proxy[:authorization] if proxy[:authorization]
end

# Set the cookie header if provided
Expand Down
3 changes: 1 addition & 2 deletions lib/em-http/http_client_options.rb
@@ -1,5 +1,5 @@
class HttpClientOptions
attr_reader :uri, :method, :host, :port, :proxy
attr_reader :uri, :method, :host, :port
attr_reader :headers, :file, :body, :query, :path
attr_reader :keepalive, :pass_cookies, :decoding

Expand All @@ -25,7 +25,6 @@ def initialize(uri, options, method)
end

def follow_redirect?; @followed < @redirects; end
def http_proxy?; @proxy && [nil, :http].include?(@proxy[:type]); end
def ssl?; @uri.scheme == "https" || @uri.port == 443; end
def no_body?; @method == "HEAD"; end

Expand Down
2 changes: 2 additions & 0 deletions lib/em-http/http_connection_options.rb
Expand Up @@ -25,4 +25,6 @@ def initialize(uri, options)
@port = uri.port
end
end

def http_proxy?; @proxy && [nil, :http].include?(@proxy[:type]); end
end
16 changes: 16 additions & 0 deletions spec/http_proxy_spec.rb
Expand Up @@ -4,6 +4,7 @@

context "connections via" do
let(:proxy) { {:proxy => { :host => '127.0.0.1', :port => 8083 }} }
let(:authenticated_proxy) { {:proxy => { :host => '127.0.0.1', :port => 8083, :authorization => ["user", "name"] } } }

it "should use HTTP proxy" do
EventMachine.run {
Expand All @@ -12,6 +13,21 @@
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.response_header.should_not include("X_PROXY_AUTH")
http.response.should match('test')
EventMachine.stop
}
}
end

it "should use HTTP proxy with authentication" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/proxyauth?q=test', authenticated_proxy).get

http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.response_header['X_PROXY_AUTH'].should == "Proxy-Authorization: Basic dXNlcjpuYW1l"
http.response.should match('test')
EventMachine.stop
}
Expand Down
7 changes: 6 additions & 1 deletion spec/stallion.rb
Expand Up @@ -243,6 +243,7 @@ def self.call(env)
end
parts = request.split("\r\n")
method, destination, http_version = parts.first.split(' ')
proxy = parts.find { |part| part =~ /Proxy-Authorization/ }
if destination =~ /^http:/
uri = Addressable::URI.parse(destination)
absolute_path = uri.path + (uri.query ? "?#{uri.query}" : "")
Expand All @@ -259,7 +260,11 @@ def self.call(env)

# Take the initial line from the upstream response
session.write client.gets


if proxy
session.write "X-Proxy-Auth: #{proxy}\r\n"
end

# What (absolute) uri was requested? Send it back in a header
session.write "X-The-Requested-URI: #{destination}\r\n"

Expand Down

0 comments on commit f090f4b

Please sign in to comment.