Bug in cgi/session

K

Ken Brush

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 =3D CGI.new("html4")
begin
session =3D CGI::Session.new(cgi, 'new_session' =3D> false)
rescue ArgumentError =20
session =3D CGI::Session.new(cgi, 'new_session' =3D> true)
end

session['cart'] ||=3D Hash.new(0);
session['cart'][cgi.params["id"]] +=3D 1
out =3D "#{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:=20
http://localhost/~kbrush/add.rb?id=3Dfoo

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
 
A

Austin Ziegler

Hi All,
=20
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.

The Ruby bug tracker is on RubyForge.org.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
Y

Yukihiro Matsumoto

Hi,

Sorry for being late.

In message "Re: Bug in cgi/session"

|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.
 
A

Ara.T.Howard

Hi,

Sorry for being late.

In message "Re: Bug in cgi/session"

|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.

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
--
===============================================================================
| 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
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top