Skip to content

Commit 0502cbc

Browse files
committed
Merge pull request #296 from pusher/fix-basic-auth-and-proxy
Fix basic auth and proxy
2 parents 912fc07 + 2c51402 commit 0502cbc

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

lib/em-http/http_encoding.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ def encode_request(method, uri, query, connopts)
5353
# uri in request header, as opposed to a relative path.
5454
# Don't modify the header with CONNECT proxies. It's unneeded and will
5555
# cause 400 Bad Request errors with many standard setups.
56-
query = uri.join(query) if connopts.proxy && !connopts.connect_proxy?
56+
if connopts.proxy && !connopts.connect_proxy?
57+
query = uri.join(query)
58+
# Drop the userinfo, it's been converted to a header and won't be
59+
# accepted by the proxy
60+
query.userinfo = nil
61+
end
5762

5863
HTTP_REQUEST_HEADER % [method.to_s.upcase, query]
5964
end

spec/http_proxy_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@
5252
}
5353
end
5454

55+
it "should strip basic auth from before the host in URI sent to proxy" do
56+
EventMachine.run {
57+
58+
http = EventMachine::HttpRequest.new('http://user:pass@127.0.0.1:8090/echo_authorization_header', proxy).get
59+
60+
http.errback { failed(http) }
61+
http.callback {
62+
http.response_header.status.should == 200
63+
# The test proxy server gives the requested uri back in this header
64+
http.response_header['X_THE_REQUESTED_URI'].should == 'http://127.0.0.1:8090/echo_authorization_header'
65+
# Ensure the basic auth was converted to a header correctly
66+
http.response.should match('authorization:Basic dXNlcjpwYXNz')
67+
EventMachine.stop
68+
}
69+
}
70+
end
71+
5572
it "should include query parameters specified in the options" do
5673
EventMachine.run {
5774
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/', proxy).get :query => { 'q' => 'test' }

spec/stallion.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def ride
1717

1818
def match?(request)
1919
method = request['REQUEST_METHOD']
20-
right_method = @methods.empty? or @methods.include?(method)
20+
@methods.empty? or @methods.include?(method)
2121
end
2222
end
2323

@@ -36,7 +36,7 @@ def in(path, *methods, &block)
3636

3737
def call(request, response)
3838
@request, @response = request, response
39-
@boxes.each do |(path, methods), mount|
39+
@boxes.each do |_, mount|
4040
if mount.match?(request)
4141
mount.ride
4242
end
@@ -96,6 +96,9 @@ def self.call(env)
9696
elsif stable.request.path_info == '/echo_content_length_from_header'
9797
stable.response.write "content-length:#{stable.request.env["CONTENT_LENGTH"]}"
9898

99+
elsif stable.request.path_info == '/echo_authorization_header'
100+
stable.response.write "authorization:#{stable.request.env["HTTP_AUTHORIZATION"]}"
101+
99102
elsif stable.request.head? && stable.request.path_info == '/'
100103
stable.response.status = 200
101104

0 commit comments

Comments
 (0)