Permalink
Browse files

Merge branch 'feature-25-easy-ssl-options'

  • Loading branch information...
2 parents 9c30329 + 398122f commit 7bb62d1372a33a69701f781f6bdbf16e52315c91 @gravis gravis committed Jun 8, 2010
Showing with 83 additions and 11 deletions.
  1. +12 −0 README.textile
  2. +64 −5 lib/typhoeus/easy.rb
  3. +4 −3 lib/typhoeus/multi.rb
  4. +2 −2 spec/typhoeus/easy_spec.rb
  5. +1 −1 spec/typhoeus/remote_spec.rb
View
@@ -204,6 +204,18 @@ Typhoeus::Request.get("https://mail.google.com/mail", :disable_ssl_peer_verifica
*LibCurl*
Typhoeus also has a more raw libcurl interface. These are the Easy and Multi objects. If you're into accessing just the raw libcurl style, those are your best bet.
+SSL Certs can be provided to the Easy interface :
+
+<pre>
+e = Typhoeus::Easy.new
+e.url = "https://example.com/action"
+s.ssl_cacert = "ca_file.cer"
+e.ssl_cert = "acert.crt"
+e.ssl_key = "akey.key"
+[...]
+e.perform
+</pre>
+
h2. NTLM authentication
Thanks for the authentication piece and this description go to Oleg Ivanov (morhekil). The major reason to start this fork was the need to perform NTLM authentication in Ruby. Now you can do it via Typhoeus::Easy interface using the following API.
View
@@ -1,6 +1,6 @@
module Typhoeus
class Easy
- attr_reader :response_body, :response_header, :method, :headers, :url
+ attr_reader :response_body, :response_header, :method, :headers, :url, :params
attr_accessor :start_time
# These integer codes are available in curl/curl.h
@@ -25,7 +25,14 @@ class Easy
:CURLOPT_PROXY => 10004,
:CURLOPT_VERIFYPEER => 64,
:CURLOPT_NOBODY => 44,
- :CURLOPT_ENCODING => 102
+ :CURLOPT_ENCODING => 102,
+ :CURLOPT_SSLCERT => 10025,
+ :CURLOPT_SSLCERTTYPE => 10086,
+ :CURLOPT_SSLKEY => 10087,
+ :CURLOPT_SSLKEYTYPE => 10088,
+ :CURLOPT_KEYPASSWD => 10026,
+ :CURLOPT_CAINFO => 10065,
+ :CURLOPT_CAPATH => 10097
}
INFO_VALUES = {
:CURLINFO_RESPONSE_CODE => 2097154,
@@ -105,7 +112,7 @@ def timed_out?
end
def supports_zlib?
- @supports_zlib ||= !!(version.match(/zlib/))
+ !!(curl_version.match(/zlib/))
end
def request_body=(request_body)
@@ -156,6 +163,7 @@ def post_data=(data)
end
def params=(params)
+ @params = params
params_string = params.keys.collect do |k|
value = params[k]
if value.is_a? Hash
@@ -175,18 +183,68 @@ def params=(params)
end
end
+ # Set SSL certificate
+ # " The string should be the file name of your certificate. "
+ # The default format is "PEM" and can be changed with ssl_cert_type=
+ def ssl_cert=(cert)
+ set_option(OPTION_VALUES[:CURLOPT_SSLCERT], cert)
+ end
+
+ # Set SSL certificate type
+ # " The string should be the format of your certificate. Supported formats are "PEM" and "DER" "
+ def ssl_cert_type=(cert_type)
+ raise "Invalid ssl cert type : '#{cert_type}'..." if cert_type and !%w(PEM DER).include?(cert_type)
+ set_option(OPTION_VALUES[:CURLOPT_SSLCERTTYPE], cert_type)
+ end
+
+ # Set SSL Key file
+ # " The string should be the file name of your private key. "
+ # The default format is "PEM" and can be changed with ssl_key_type=
+ #
+ def ssl_key=(key)
+ set_option(OPTION_VALUES[:CURLOPT_SSLKEY], key)
+ end
+
+ # Set SSL Key type
+ # " The string should be the format of your private key. Supported formats are "PEM", "DER" and "ENG". "
+ #
+ def ssl_key_type=(key_type)
+ raise "Invalid ssl key type : '#{key_type}'..." if key_type and !%w(PEM DER ENG).include?(key_type)
+ set_option(OPTION_VALUES[:CURLOPT_SSLKEYTYPE], key_type)
+ end
+
+ def ssl_key_password=(key_password)
+ set_option(OPTION_VALUES[:CURLOPT_KEYPASSWD], key_password)
+ end
+
+ # Set SSL CACERT
+ # " File holding one or more certificates to verify the peer with. "
+ #
+ def ssl_cacert=(cacert)
+ set_option(OPTION_VALUES[:CURLOPT_CAINFO], cacert)
+ end
+
+ # Set CAPATH
+ # " directory holding multiple CA certificates to verify the peer with. The certificate directory must be prepared using the openssl c_rehash utility. "
+ #
+ def ssl_capath=(capath)
+ set_option(OPTION_VALUES[:CURLOPT_CAPATH], capath)
+ end
+
def set_option(option, value)
if value.class == String
easy_setopt_string(option, value)
- else
+ elsif value
easy_setopt_long(option, value)
end
end
def perform
set_headers()
easy_perform()
- response_code()
+ resp_code = response_code()
+ (resp_code >= 200 and resp_code <= 299) ? success : failure
+ resp_code
end
def set_headers
@@ -262,5 +320,6 @@ def get_info_double(option)
def curl_version
version
end
+
end
end
View
@@ -9,19 +9,20 @@ def initialize
def remove(easy)
multi_remove_handle(easy)
end
-
+
def add(easy)
+ easy.set_headers()
@easy_handles << easy
multi_add_handle(easy)
end
-
+
def perform()
while active_handle_count > 0 do
multi_perform
end
reset_easy_handles
end
-
+
def cleanup()
multi_cleanup
end
@@ -7,12 +7,12 @@
end
it "should return true if the version string has zlib" do
- @easy.stub!(:version).and_return("libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3 libidn/1.16")
+ @easy.stub!(:curl_version).and_return("libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3 libidn/1.16")
@easy.supports_zlib?.should be_true
end
it "should return false if the version string doesn't have zlib" do
- @easy.stub!(:version).and_return("libcurl/7.20.0 OpenSSL/0.9.8l libidn/1.16")
+ @easy.stub!(:curl_version).and_return("libcurl/7.20.0 OpenSSL/0.9.8l libidn/1.16")
@easy.supports_zlib?.should be_false
end
end
@@ -4,7 +4,7 @@
it "should be deprecated" do
fail "This entire interface is deprecated!"
end
-
+
# before(:each) do
# @klass = Class.new do
# include Typhoeus

0 comments on commit 7bb62d1

Please sign in to comment.