diff --git a/ext/curb_easy.c b/ext/curb_easy.c index f962e9f9..58c8a9b9 100644 --- a/ext/curb_easy.c +++ b/ext/curb_easy.c @@ -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); @@ -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; @@ -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" @@ -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. */ @@ -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)); @@ -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); diff --git a/ext/curb_easy.h b/ext/curb_easy.h index 0ffb3610..f21df0bf 100644 --- a/ext/curb_easy.h +++ b/ext/curb_easy.h @@ -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; diff --git a/tests/tc_curl_easy.rb b/tests/tc_curl_easy.rb index c5c373a8..642e8002 100644 --- a/tests/tc_curl_easy.rb +++ b/tests/tc_curl_easy.rb @@ -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