Skip to content

Commit

Permalink
Add a cookies option that mirrors the CURLOPT_COOKIE option in libcur…
Browse files Browse the repository at this point in the history
…l. This allows you to pass a simple cookie string like so: "name1=content1; name2=content2;".
  • Loading branch information
chuyeow committed Jun 8, 2009
1 parent 99c71de commit f5992da
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
33 changes: 32 additions & 1 deletion ext/curb_easy.c
Expand Up @@ -128,6 +128,7 @@ void curl_easy_mark(ruby_curl_easy *rbce) {
rb_gc_mark(rbce->userpwd);
rb_gc_mark(rbce->proxypwd);
rb_gc_mark(rbce->headers);
rb_gc_mark(rbce->cookies);
rb_gc_mark(rbce->cookiefile);
rb_gc_mark(rbce->cookiejar);
rb_gc_mark(rbce->cert);
Expand Down Expand Up @@ -191,6 +192,7 @@ static VALUE ruby_curl_easy_new(int argc, VALUE *argv, VALUE klass) {
rbce->userpwd = Qnil;
rbce->proxypwd = Qnil;
rbce->headers = rb_hash_new();
rbce->cookies = Qnil;
rbce->cookiefile = Qnil;
rbce->cookiejar = Qnil;
rbce->cert = Qnil;
Expand Down Expand Up @@ -437,6 +439,29 @@ static VALUE ruby_curl_easy_proxypwd_get(VALUE self) {
CURB_OBJECT_GETTER(ruby_curl_easy, proxypwd);
}


/*
* call-seq:
* easy.cookies = "name1=content1; name2=content2;" => "pwd string"
*
* Set cookies to be sent by this Curl::Easy instance. The format of the string should
* be NAME=CONTENTS, where NAME is the cookie name and CONTENTS is what the cookie should contain.
* Set multiple cookies in one string like this: "name1=content1; name2=content2;" etc.
*/
static VALUE ruby_curl_easy_cookies_set(VALUE self, VALUE cookies) {
CURB_OBJECT_SETTER(ruby_curl_easy, cookies);
}

/*
* call-seq:
* easy.cookies => "name1=content1; name2=content2;"
*
* Obtain the cookies for this Curl::Easy instance.
*/
static VALUE ruby_curl_easy_cookies_get(VALUE self) {
CURB_OBJECT_GETTER(ruby_curl_easy, cookies);
}

/*
* call-seq:
* easy.cookiefile = "cookies.txt" => "pwd string"
Expand All @@ -452,7 +477,7 @@ static VALUE ruby_curl_easy_cookiefile_set(VALUE self, VALUE cookiefile) {

/*
* call-seq:
* easy.cookiefile => "cookies.txt""
* easy.cookiefile => "cookies.txt"
*
* Obtain the cookiefile file for this Curl::Easy instance.
*/
Expand Down Expand Up @@ -1414,6 +1439,10 @@ VALUE ruby_curl_easy_setup( ruby_curl_easy *rbce, VALUE *body_buffer, VALUE *hea
}
}

if (rbce->cookies != Qnil) {
curl_easy_setopt(curl, CURLOPT_COOKIE, StringValuePtr(rbce->cookies));
}

/* Set up HTTPS cert handling if necessary */
if (rbce->cert != Qnil) {
curl_easy_setopt(curl, CURLOPT_SSLCERT, StringValuePtr(rbce->cert));
Expand Down Expand Up @@ -2585,6 +2614,8 @@ void init_curb_easy() {
rb_define_method(cCurlEasy, "userpwd", ruby_curl_easy_userpwd_get, 0);
rb_define_method(cCurlEasy, "proxypwd=", ruby_curl_easy_proxypwd_set, 1);
rb_define_method(cCurlEasy, "proxypwd", ruby_curl_easy_proxypwd_get, 0);
rb_define_method(cCurlEasy, "cookies=", ruby_curl_easy_cookies_set, 1);
rb_define_method(cCurlEasy, "cookies", ruby_curl_easy_cookies_get, 0);
rb_define_method(cCurlEasy, "cookiefile=", ruby_curl_easy_cookiefile_set, 1);
rb_define_method(cCurlEasy, "cookiefile", ruby_curl_easy_cookiefile_get, 0);
rb_define_method(cCurlEasy, "cookiejar=", ruby_curl_easy_cookiejar_set, 1);
Expand Down
1 change: 1 addition & 0 deletions ext/curb_easy.h
Expand Up @@ -32,6 +32,7 @@ typedef struct {
VALUE userpwd;
VALUE proxypwd;
VALUE headers; /* ruby array of strings with headers to set */
VALUE cookies; /* string */
VALUE cookiefile; /* filename */
VALUE cookiejar; /* filename */
VALUE cert;
Expand Down
7 changes: 7 additions & 0 deletions tests/tc_curl_easy.rb
Expand Up @@ -433,6 +433,13 @@ def test_enable_cookies
assert c.enable_cookies?
end

def test_cookies_option
c = Curl::Easy.new
assert_nil c.cookies
assert_equal "name1=content1; name2=content2;", c.cookies = "name1=content1; name2=content2;"
assert_equal "name1=content1; name2=content2;", c.cookies
end

def test_cookiefile
c = Curl::Easy.new
assert_nil c.cookiefile
Expand Down

1 comment on commit f5992da

@taf2
Copy link

@taf2 taf2 commented on f5992da Jun 13, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! thanks for the patch. I'll get this merged with my tree and spin a new release shortly with the changes.

Please sign in to comment.