From 99cfee3976c085808ed212c58ef2f7ce54e2d482 Mon Sep 17 00:00:00 2001 From: Magnus Holm Date: Mon, 2 Nov 2009 16:24:47 +0100 Subject: [PATCH] Cookies are now only set when changed (closes #1) --- lib/camping-unabridged.rb | 5 +++-- lib/camping.rb | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/camping-unabridged.rb b/lib/camping-unabridged.rb index 00dfe00..999c726 100644 --- a/lib/camping-unabridged.rb +++ b/lib/camping-unabridged.rb @@ -356,7 +356,8 @@ def to_a @env['rack.session'] = @state r = Rack::Response.new(@body, @status, @headers) @cookies.each do |k, v| - v = {:value => v, :path => self / "/"} if String === v + next if @old_cookies[k] == v + v = { :value => v, :path => self / "/" } if String === v r.set_cookie(k, v) end r.to_a @@ -367,7 +368,7 @@ def initialize(env, m) #:nodoc: @root, @input, @cookies, @state, @headers, @status, @method = r.script_name.sub(/\/$/,''), n(r.params), - H[r.cookies], H[r.session], + H[@old_cookies = r.cookies], H[r.session], {}, m =~ /r(\d+)/ ? $1.to_i : 200, m end diff --git a/lib/camping.rb b/lib/camping.rb index 2e0a318..8e64d7a 100644 --- a/lib/camping.rb +++ b/lib/camping.rb @@ -19,13 +19,13 @@ def r s,b,h={};b,h=h,b if Hash===b;@status=s; to_s;end;def r404 p;P%"#{p} not found"end;def r500 k,m,e;raise e;end def r501 m;P%"#{m.upcase} not implemented"end;def to_a @env['rack.session']=@state;r=Rack::Response.new(@body,@status,@headers) -@cookies.each{|k,v|v={:value=>v,:path=>self/"/"} if String===v -r.set_cookie(k,v)} +@cookies.each{|k,v|next if @old_cookies[k]==v;v={:value=>v,:path=>self/"/"} if +String===v;r.set_cookie(k,v)} r.to_a;end;def initialize(env,m) r=@request=Rack::Request.new(@env=env) @root,@input,@cookies,@state,@headers,@status,@method= r.script_name.sub(/\/$/,''),n(r.params), -H[r.cookies],H[r.session],{},m=~/r(\d+)/?$1.to_i: 200,m +H[@old_cookies = r.cookies],H[r.session],{},m=~/r(\d+)/?$1.to_i: 200,m end;def n h;Hash===h ?h.inject(H[]){|m,(k,v)|m[k]=n(v);m}: h end;def service *a r=catch(:halt){send(@method,*a)};@body||=r self;end;end;module Controllers;@r=[];class<