Permalink
Browse files

Merge pull request #192 from ezkl/http-proxy-connopts

Authenticated HTTP proxy patch
  • Loading branch information...
2 parents 807d013 + 7d5752d commit f090f4b5ade2ffe0c1c49936190145c9038160c3 @igrigorik committed May 31, 2012
View
@@ -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
@@ -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
@@ -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
@@ -25,4 +25,6 @@ def initialize(uri, options)
@port = uri.port
end
end
+
+ def http_proxy?; @proxy && [nil, :http].include?(@proxy[:type]); end
end
View
@@ -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 {
@@ -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
}
View
@@ -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}" : "")
@@ -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"

0 comments on commit f090f4b

Please sign in to comment.