Bug in cgi/session

Hi All,

I couldn't find a bug tracker for ruby, to see if this was known or
not. Anyhow, there is a problem with cgi/session. It'll convert a
value of a hash stored in the session to an array, I think on close.

Here's the test case I'm using:

--- Start ruby code ---
#!/usr/bin/ruby

# Test case for cgi session
require 'cgi'
require 'cgi/session'

cgi = CGI.new("html4")
begin
    session = CGI::Session.new(cgi, 'new_session' => false)
    rescue ArgumentError
    session = CGI::Session.new(cgi, 'new_session' => true)
end

session['cart'] ||= Hash.new(0);
session['cart'][cgi.params["id"]] += 1
out = "#{cgi.params["id"]} -- #{session['cart'][cgi.params["id"]]}"

print cgi.header
print cgi.out() { out }
session.close
-- End of ruby code ---

When accessed with something like:
http://localhost/~kbrush/add.rb?id=foo

it'll print 'foo -- 1'
then access it again and you'll get an 'Internal Error'

This is the error in error.log:

[Mon Aug 15 09:25:27 2005] [error] mod_ruby: error in ruby
[Mon Aug 15 09:25:27 2005] [error] mod_ruby:
/home/kbrush/public_html/add.rb:19:in `[]': cannot convert Array into
Integer (TypeError)
[Mon Aug 15 09:25:27 2005] [error] mod_ruby: from
/home/kbrush/public_html/add.rb:19
[Mon Aug 15 09:25:27 2005] [error] mod_ruby: from
/usr/lib/ruby/1.8/apache/ruby-run.rb:53:in `load'
[Mon Aug 15 09:25:27 2005] [error] mod_ruby: from
/usr/lib/ruby/1.8/apache/ruby-run.rb:53:in `handler'
[Mon Aug 15 09:25:28 2005] [error] [client 127.0.0.1] File does not
exist: /var/www/favicon.ico

you get the same error if you use mod_ruby or not.

-Ken

The Ruby bug tracker is on RubyForge.org.

-austin

···

On 8/15/05, Ken Brush <kbrush@gmail.com> wrote:

Hi All,

I couldn't find a bug tracker for ruby, to see if this was known or
not. Anyhow, there is a problem with cgi/session. It'll convert a
value of a hash stored in the session to an array, I think on close.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Hi,

Sorry for being late.

Here's the test case I'm using:

They are not bugs in cgi/session.

  * session data should be strings; non string values are converted
    using to_s in 1.8, so that session['cart'] ||= Hash.new(0) won't
    work. You need to pack it explicitly, perhaps using Marshal.

  * cgi.params returns arrays, since there's possibility of multiple
    values for a form id. If you want to use first value, try
    cgi.params["id"][0].

  * since session data are strings

          session['cart'][cgi.params["id"][0]] += 1

    would not work.

              matz.

···

In message "Re: Bug in cgi/session" on Tue, 16 Aug 2005 01:25:47 +0900, Ken Brush <kbrush@gmail.com> writes:

fyi - that's __unless__ you use cgi/session/pstore:

   harp:~ > cat a.rb
   require 'cgi'
   require 'cgi/session/pstore'

   STDIN.reopen '/dev/null'

   session = CGI::Session::new CGI::new, 'database_manager' => CGI::Session::PStore

   session['key'] = 0

   p(session['key'] += 42)

   harp:~ > ruby a.rb
   42

cheers.

-a

···

On Mon, 5 Sep 2005, Yukihiro Matsumoto wrote:

Hi,

Sorry for being late.

In message "Re: Bug in cgi/session" > on Tue, 16 Aug 2005 01:25:47 +0900, Ken Brush <kbrush@gmail.com> writes:

>Here's the test case I'm using:

They are not bugs in cgi/session.

* session data should be strings; non string values are converted
   using to_s in 1.8, so that session['cart'] ||= Hash.new(0) won't
   work. You need to pack it explicitly, perhaps using Marshal.

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================