Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose .peer_cert on Response #634

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ def perform(&block)
validate
setup_raw_request
chunked_body = nil
peer_cert = nil
current_http = http

self.last_response = current_http.request(@raw_request) do |http_response|
peer_cert ||= current_http.peer_cert

self.last_response = http.request(@raw_request) do |http_response|
if block
chunks = []

Expand All @@ -155,10 +159,9 @@ def perform(&block)
end
end


handle_host_redirection if response_redirects?
result = handle_unauthorized
result ||= handle_response(chunked_body, &block)
result ||= handle_response(chunked_body, peer_cert, &block)
result
end

Expand Down Expand Up @@ -340,7 +343,7 @@ def encode_body(body)
end
end

def handle_response(body, &block)
def handle_response(body, peer_cert, &block)
if response_redirects?
options[:limit] -= 1
if options[:logger]
Expand All @@ -363,7 +366,7 @@ def handle_response(body, &block)
else
body ||= last_response.body
body = body.nil? ? body : encode_body(body)
Response.new(self, last_response, lambda { parse_response(body) }, body: body)
Response.new(self, last_response, lambda { parse_response(body) }, body: body, peer_cert: peer_cert)
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/httparty/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ def self._load(data)
new(req, resp, -> { parsed_resp }, body: resp_body)
end

attr_reader :request, :response, :body, :headers
attr_reader :request, :response, :body, :headers, :peer_cert

def initialize(request, response, parsed_block, options = {})
@request = request
@response = response
@body = options[:body] || response.body
@parsed_block = parsed_block
@headers = Headers.new(response.to_hash)
@peer_cert = options[:peer_cert]

if request.options[:logger]
logger = ::HTTParty::Logger.build(
Expand Down
8 changes: 6 additions & 2 deletions spec/httparty/ssl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
RSpec.describe HTTParty::Request do
context "SSL certificate verification" do
before do
WebMock.allow_net_connect!
WebMock.disable!
end

after do
WebMock.disable_net_connect!
WebMock.enable!
end

it "should fail when no trusted CA list is specified, by default" do
Expand Down Expand Up @@ -70,5 +70,9 @@
ssl_verify_test(:ssl_ca_path, ".", "bogushost.crt")
end.to raise_error(OpenSSL::SSL::SSLError)
end

it "should provide the certificate used by the server via peer_cert" do
expect(ssl_verify_test(:ssl_ca_file, "ca.crt", "server.crt").peer_cert).to be_a OpenSSL::X509::Certificate
end
end
end
2 changes: 2 additions & 0 deletions spec/support/stub_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def stub_http_response_with(filename)

http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', format: format)
allow(http_request).to receive_message_chain(:http, :request).and_return(response)
allow(http_request).to receive_message_chain(:http, :peer_cert).and_return(nil)

expect(HTTParty::Request).to receive(:new).and_return(http_request)
end
Expand All @@ -22,6 +23,7 @@ def response.read_body(&block)

http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', options)
allow(http_request).to receive_message_chain(:http, :request).and_yield(response).and_return(response)
allow(http_request).to receive_message_chain(:http, :peer_cert).and_return(nil)

expect(HTTParty::Request).to receive(:new).and_return(http_request)
end
Expand Down