Skip to content

Commit

Permalink
Add a cookiefile option that allows a file that contains cookies to b…
Browse files Browse the repository at this point in the history
…e sent in curl requests.

This is different from the cookiejar setting which tells curl where to store cookies in responses, and augments the enable_cookies setting with a specific file to read cookies from.
  • Loading branch information
chuyeow committed Jun 7, 2009
1 parent 5c8080d commit 99c71de
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
35 changes: 33 additions & 2 deletions 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->cookiefile);
rb_gc_mark(rbce->cookiejar);
rb_gc_mark(rbce->cert);
rb_gc_mark(rbce->encoding);
Expand Down Expand Up @@ -190,6 +191,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->cookiefile = Qnil;
rbce->cookiejar = Qnil;
rbce->cert = Qnil;
rbce->encoding = Qnil;
Expand Down Expand Up @@ -435,12 +437,35 @@ static VALUE ruby_curl_easy_proxypwd_get(VALUE self) {
CURB_OBJECT_GETTER(ruby_curl_easy, proxypwd);
}

/*
* call-seq:
* easy.cookiefile = "cookies.txt" => "pwd string"
*
* Set a file that contains cookies to be sent in subsequent requests by this Curl::Easy instance.
*
* *Note* that you must set enable_cookies true to enable the cookie
* engine, or this option will be ignored.
*/
static VALUE ruby_curl_easy_cookiefile_set(VALUE self, VALUE cookiefile) {
CURB_OBJECT_SETTER(ruby_curl_easy, cookiefile);
}

/*
* call-seq:
* easy.cookiefile => "cookies.txt""
*
* Obtain the cookiefile file for this Curl::Easy instance.
*/
static VALUE ruby_curl_easy_cookiefile_get(VALUE self) {
CURB_OBJECT_GETTER(ruby_curl_easy, cookiefile);
}

/*
* call-seq:
* easy.cookiejar = "cookiejar.file" => "pwd string"
*
* Set a cookiejar file to use for this Curl::Easy instance. This file
* will be used to persist cookies.
* Set a cookiejar file to use for this Curl::Easy instance.
* Cookies from the response will be written into this file.
*
* *Note* that you must set enable_cookies true to enable the cookie
* engine, or this option will be ignored.
Expand Down Expand Up @@ -1380,6 +1405,10 @@ VALUE ruby_curl_easy_setup( ruby_curl_easy *rbce, VALUE *body_buffer, VALUE *hea
if (rbce->enable_cookies) {
if (rbce->cookiejar != Qnil) {
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, StringValuePtr(rbce->cookiejar));
}

if (rbce->cookiefile != Qnil) {
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, StringValuePtr(rbce->cookiefile));
} else {
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* "" = magic to just enable */
}
Expand Down Expand Up @@ -2556,6 +2585,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, "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);
rb_define_method(cCurlEasy, "cookiejar", ruby_curl_easy_cookiejar_get, 0);
rb_define_method(cCurlEasy, "cert=", ruby_curl_easy_cert_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 cookiefile; /* filename */
VALUE cookiejar; /* filename */
VALUE cert;
VALUE encoding;
Expand Down
9 changes: 8 additions & 1 deletion tests/tc_curl_easy.rb
Expand Up @@ -432,7 +432,14 @@ def test_enable_cookies
assert c.enable_cookies = true
assert c.enable_cookies?
end


def test_cookiefile
c = Curl::Easy.new
assert_nil c.cookiefile
assert_equal "some.file", c.cookiefile = "some.file"
assert_equal "some.file", c.cookiefile
end

def test_cookiejar
c = Curl::Easy.new
assert_nil c.cookiejar
Expand Down

0 comments on commit 99c71de

Please sign in to comment.