Skip to content

Commit

Permalink
Merge pull request #77 from starmask/add_ssl_version_option
Browse files Browse the repository at this point in the history
Add option for specifying ssl_version
  • Loading branch information
Phillip Toland committed Jun 25, 2014
2 parents cb29a02 + 14d72db commit 20875d1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
16 changes: 14 additions & 2 deletions ext/patron/session_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ static void set_options_from_request(VALUE self, VALUE request) {
VALUE ignore_content_length = Qnil;
VALUE insecure = Qnil;
VALUE cacert = Qnil;
VALUE ssl_version = Qnil;
VALUE buffer_size = Qnil;
VALUE action_name = rb_iv_get(request, "@action");

Expand Down Expand Up @@ -471,7 +472,7 @@ static void set_options_from_request(VALUE self, VALUE request) {

proxy = rb_iv_get(request, "@proxy");
if (!NIL_P(proxy)) {
curl_easy_setopt(curl, CURLOPT_PROXY, StringValuePtr(proxy));
curl_easy_setopt(curl, CURLOPT_PROXY, StringValuePtr(proxy));
}

proxy_type = rb_iv_get(request, "@proxy_type");
Expand All @@ -496,6 +497,18 @@ static void set_options_from_request(VALUE self, VALUE request) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
}

ssl_version = rb_iv_get(request, "@ssl_version");
if(!NIL_P(ssl_version)) {
char* version = StringValuePtr(ssl_version);
if(strcmp(version, "SSLv2") == 0) {
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv2);
} else if(strcmp(version, "SSLv3") == 0) {
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
} else if(strcmp(version, "TLSv1") == 0) {
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
}
}

cacert = rb_iv_get(request, "@cacert");
if(!NIL_P(cacert)) {
curl_easy_setopt(curl, CURLOPT_CAINFO, StringValuePtr(cacert));
Expand Down Expand Up @@ -766,4 +779,3 @@ void Init_session_ext() {
rb_define_const(mProxyType, "SOCKS4A", INT2FIX(CURLPROXY_SOCKS4A));
rb_define_const(mProxyType, "SOCKS5_HOSTNAME", INT2FIX(CURLPROXY_SOCKS5_HOSTNAME));
}

5 changes: 3 additions & 2 deletions lib/patron/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ def initialize
READER_VARS = [
:url, :username, :password, :file_name, :proxy, :proxy_type, :insecure,
:ignore_content_length, :multipart, :action, :timeout, :connect_timeout,
:max_redirects, :headers, :auth_type, :upload_data, :buffer_size, :cacert
:max_redirects, :headers, :auth_type, :upload_data, :buffer_size, :cacert,
:ssl_version
]

WRITER_VARS = [
:url, :username, :password, :file_name, :proxy, :proxy_type, :insecure,
:ignore_content_length, :multipart, :cacert
:ignore_content_length, :multipart, :cacert, :ssl_version
]

attr_reader *READER_VARS
Expand Down
4 changes: 4 additions & 0 deletions lib/patron/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class Session
# Does this session stricly verify SSL certificates?
attr_accessor :insecure

# Specifies the ssl version
attr_accessor :ssl_version

# What cacert file should this session use to verify SSL certificates?
attr_accessor :cacert

Expand Down Expand Up @@ -209,6 +212,7 @@ def request(action, url, headers, options = {})
req.proxy_type = options.fetch :proxy_type, self.proxy_type
req.auth_type = options.fetch :auth_type, self.auth_type
req.insecure = options.fetch :insecure, self.insecure
req.ssl_version = options.fetch :ssl_version, self.ssl_version
req.cacert = options.fetch :cacert, self.cacert
req.ignore_content_length = options.fetch :ignore_content_length, self.ignore_content_length
req.buffer_size = options.fetch :buffer_size, self.buffer_size
Expand Down
8 changes: 8 additions & 0 deletions spec/session_ssl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@
body.request_method.should == "GET"
end

it "should work with different SSL versions" do
['SSLv2', 'SSLv3', 'TLSv1'].each do |version|
@session.ssl_version = version
response = @session.get("/test")
response.status.should == 200
end
end

# ------------------------------------------------------------------------
describe 'when debug is enabled' do
it 'it should not clobber stderr' do
Expand Down

0 comments on commit 20875d1

Please sign in to comment.