<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -380,6 +380,17 @@ VALUE session_handle_request(VALUE self, VALUE request) {
   return rb_ensure(&amp;perform_request, self, &amp;cleanup, self);
 }
 
+VALUE enable_cookie_session(VALUE self, VALUE file) {
+	struct curl_state *state;
+	Data_Get_Struct(self, struct curl_state, state);
+	CURL* curl = state-&gt;handle;
+	char *file_path = RSTRING_PTR(file);
+	if (file_path != &quot;&quot;)
+		curl_easy_setopt(curl, CURLOPT_COOKIEJAR, file_path);
+	curl_easy_setopt(curl, CURLOPT_COOKIEFILE, file_path);
+	return Qnil;
+}
+
 //------------------------------------------------------------------------------
 // Extension initialization
 //
@@ -411,6 +422,7 @@ void Init_session_ext() {
   rb_define_method(cSession, &quot;escape&quot;,         session_escape,         1);
   rb_define_method(cSession, &quot;unescape&quot;,       session_unescape,       1);
   rb_define_method(cSession, &quot;handle_request&quot;, session_handle_request, 1);
+  rb_define_method(cSession, &quot;enable_cookie_session&quot;, enable_cookie_session, 1);
 
 	rb_define_const(cRequest, &quot;AuthBasic&quot;, 	CURLAUTH_BASIC);
 	rb_define_const(cRequest, &quot;AuthDigest&quot;, CURLAUTH_DIGEST);</diff>
      <filename>ext/patron/session_ext.c</filename>
    </modified>
    <modified>
      <diff>@@ -60,7 +60,7 @@ module Patron
     # @see Patron::Request#auth_type
     attr_accessor :auth_type
 
-    private :ext_initialize, :handle_request
+    private :ext_initialize, :handle_request, :enable_cookie_session
 
     # Create a new Session object.
     def initialize
@@ -72,6 +72,23 @@ module Patron
       @auth_type = :basic
     end
 
+    # Makes this session handle cookies and store them in in +file+.
+    # If file is nil they will be stored in memory. Otherwise the +file+
+    # must be readable and writable. Calling multiple times will add more files.
+    def handle_cookies(file = nil)
+      if file
+        path = Pathname(file).expand_path
+        unless File.exists?(file) and File.writable?(path.dirname)
+          raise ArgumentError, &quot;Can't create file #{path} (permission error)&quot;
+        end
+        unless File.readable?(file) or File.writable?(path)
+          raise ArgumentError, &quot;Cant read or write file #{path} (permission error)&quot;
+        end
+      end
+      enable_cookie_session(path.to_s)
+      self
+    end
+
     ###################################################################
     ### Standard HTTP methods
     ###</diff>
      <filename>lib/patron/session.rb</filename>
    </modified>
    <modified>
      <diff>@@ -67,10 +67,19 @@ class RedirectServlet &lt; HTTPServlet::AbstractServlet
   end
 end
 
+class SetCookieServlet &lt; HTTPServlet::AbstractServlet
+  def do_GET(req, res)
+    res['Set-Cookie'] = &quot;session_id=foo123&quot;
+    res['Location'] = &quot;http://localhost:9001/test&quot;
+    res.status = 301
+  end
+end
+
 server = WEBrick::HTTPServer.new :Port =&gt; 9001
 server.mount(&quot;/test&quot;, TestServlet)
 server.mount(&quot;/timeout&quot;, TimeoutServlet)
 server.mount(&quot;/redirect&quot;, RedirectServlet)
+server.mount(&quot;/setcookie&quot;, SetCookieServlet)
 
 exit_code = lambda do
   begin</diff>
      <filename>script/test_server</filename>
    </modified>
    <modified>
      <diff>@@ -177,6 +177,21 @@ describe Patron::Session do
     body.header['authorization'].should == [encode_authz(&quot;foo&quot;, &quot;bar&quot;)]
   end
 
+  it &quot;should handle cookies if set&quot; do
+    @session.handle_cookies
+    response = @session.get(&quot;/setcookie&quot;).body
+    YAML::load(response).header['cookie'].first.should == &quot;session_id=foo123&quot;
+  end
+
+  it &quot;should not handle cookies by default&quot; do
+    response = @session.get(&quot;/setcookie&quot;).body
+    YAML::load(response).header.should_not include('cookie')
+  end
+
+  it &quot;should raise exception if cookie store is not writable or readable&quot; do
+    lambda { @session.handle_cookies(&quot;/trash/clash/foo&quot;) }.should raise_error(ArgumentError)
+  end
+  
   def encode_authz(user, passwd)
     &quot;Basic &quot; + Base64.encode64(&quot;#{user}:#{passwd}&quot;).strip
   end</diff>
      <filename>spec/session_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>e814e55dc6e898217f2de62d8c9b19d0855cf7ab</id>
    </parent>
  </parents>
  <author>
    <name>Jannik Sch&#252;rg</name>
    <email>jannschu@gmail.com</email>
  </author>
  <url>http://github.com/toland/patron/commit/82f185e257ab81bd5ac32ace324333bb8bedcfb3</url>
  <id>82f185e257ab81bd5ac32ace324333bb8bedcfb3</id>
  <committed-date>2009-08-13T08:45:18-07:00</committed-date>
  <authored-date>2009-08-13T03:38:06-07:00</authored-date>
  <message>simple cookie handling added

Signed-off-by: Phillip Toland &lt;phil.toland@gmail.com&gt;</message>
  <tree>81277ce170666e293142488ebd47359d07c998cd</tree>
  <committer>
    <name>Phillip Toland</name>
    <email>phil.toland@gmail.com</email>
  </committer>
</commit>
