Skip to content

Commit

Permalink
initial pass at implementing a easy.setopt to allow ruby to call curl…
Browse files Browse the repository at this point in the history
…_easy_setopt
  • Loading branch information
taf2 committed Mar 21, 2011
1 parent 659a45a commit 621f54f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ext/curb.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ void Init_curb_core() {
rb_define_const(mCurl, "CURLAUTH_ANY", INT2FIX(0));
#endif

rb_define_const(mCurl, "CURLOPT_VERBOSE", INT2FIX(CURLOPT_VERBOSE));
rb_define_const(mCurl, "CURLOPT_HEADER", INT2FIX(CURLOPT_HEADER));
rb_define_const(mCurl, "CURLOPT_NOPROGRESS", INT2FIX(CURLOPT_NOPROGRESS));
rb_define_const(mCurl, "CURLOPT_NOSIGNAL", INT2FIX(CURLOPT_NOSIGNAL));
rb_define_const(mCurl, "CURLOPT_URL", INT2FIX(CURLOPT_URL));

rb_define_singleton_method(mCurl, "ipv6?", ruby_curl_ipv6_q, 0);
rb_define_singleton_method(mCurl, "kerberos4?", ruby_curl_kerberos4_q, 0);
rb_define_singleton_method(mCurl, "ssl?", ruby_curl_ssl_q, 0);
Expand Down
53 changes: 52 additions & 1 deletion ext/curb_easy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2867,7 +2867,10 @@ static VALUE ruby_curl_easy_ssl_verify_result_get(VALUE self) {

/* TODO CURLINFO_SSL_ENGINES
Pass the address of a 'struct curl_slist *' to receive a linked-list of OpenSSL crypto-engines supported. Note that engines are normally implemented in separate dynamic libraries. Hence not all the returned engines may be available at run-time. NOTE: you must call curl_slist_free_all(3) on the list pointer once you're done with it, as libcurl will not free the data for you. (Added in 7.12.3)
Pass the address of a 'struct curl_slist *' to receive a linked-list of OpenSSL crypto-engines supported.
Note that engines are normally implemented in separate dynamic libraries.
Hence not all the returned engines may be available at run-time.
NOTE: you must call curl_slist_free_all(3) on the list pointer once you're done with it, as libcurl will not free the data for you. (Added in 7.12.3)
*/

/*
Expand Down Expand Up @@ -3073,6 +3076,51 @@ static VALUE ruby_curl_easy_last_result(VALUE self) {
Data_Get_Struct(self, ruby_curl_easy, rbce);
return INT2FIX(rbce->last_result);
}

/*
* call-seq:
* easy.setopt Fixnum, value => value
*
* Iniital access to libcurl curl_easy_setopt
*/
static VALUE ruby_curl_easy_set_opt(VALUE self, VALUE opt, VALUE val) {
ruby_curl_easy *rbce;
long option = FIX2LONG(opt);

Data_Get_Struct(self, ruby_curl_easy, rbce);

switch (option) {
/* BEHAVIOR OPTIONS */
case CURLOPT_VERBOSE: {
VALUE verbose = val;
CURB_BOOLEAN_SETTER(ruby_curl_easy, verbose);
} break;
case CURLOPT_HEADER:
case CURLOPT_NOPROGRESS:
case CURLOPT_NOSIGNAL:
curl_easy_setopt(rbce->curl, CURLOPT_NOSIGNAL, val == Qtrue ? 1 : 0);
break;
/* TODO: CALLBACK OPTIONS */
/* TODO: ERROR OPTIONS */
/* NETWORK OPTIONS */
case CURLOPT_URL: {
VALUE url = val;
CURB_OBJECT_HSETTER(ruby_curl_easy, url);
} break;
}
return val;
}

/*
* call-seq:
* easy.getinfo Fixnum => value
*
* Iniital access to libcurl curl_easy_getinfo, remember getinfo doesn't return the same values as setopt
*/
static VALUE ruby_curl_easy_get_opt(VALUE self, VALUE opt) {

}

/*
* call-seq:
* easy.inspect => "#<Curl::Easy http://google.com/>"
Expand Down Expand Up @@ -3356,4 +3404,7 @@ void init_curb_easy() {
rb_define_method(cCurlEasy, "multi", ruby_curl_easy_multi_get, 0);
rb_define_method(cCurlEasy, "multi=", ruby_curl_easy_multi_set, 1);
rb_define_method(cCurlEasy, "last_result", ruby_curl_easy_last_result, 0);

rb_define_method(cCurlEasy, "setopt", ruby_curl_easy_set_opt, 2);
rb_define_method(cCurlEasy, "getinfo", ruby_curl_easy_get_opt, 1);
}
14 changes: 14 additions & 0 deletions lib/curl/easy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
module Curl
class Easy

#
# call-seq:
# easy.set :sym, value
#
# translates ruby symbols to libcurl options
#
def set(opt,val)
setopt(sym2curl(opt), val)
end

def sym2curl(opt)
Curl.const_get("CURLOPT_#{opt.to_s.upcase}")
end

#
# call-seq:
# easy.perform => true
Expand Down
31 changes: 31 additions & 0 deletions tests/tc_curl_easy_setopt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))

class TestCurbCurlEasySetOpt < Test::Unit::TestCase
def setup
@easy = Curl::Easy.new
end

def test_opt_verbose
@easy.set :verbose, true
assert @easy.verbose?
end

def test_opt_header
@easy.set :header, true
end

def test_opt_noprogress
@easy.set :noprogress, true
end

def test_opt_nosignal
@easy.set :nosignal, true
end

def test_opt_url
url = "http://google.com/"
@easy.set :url, url
assert_equal url, @easy.url
end

end

0 comments on commit 621f54f

Please sign in to comment.