Bug in cgi/session

ruby

    Next

  • 1. Debugging wxruby programs.
    Hi, I'm new to Ruby. I'd like to debug my wxruby applications (in Windows XP). What's the best way to do this? I tried putting in 'print' statements and hoping to get some output on the command window. I invoke the ruby application by typing 'rubyw ttc.rbw' in the command window, which of course, does not work. I also tried 'ruby ttc.rbw' but I get: app.rbw:3:in `require': No such file to load -- wxruby (LoadError) from app.rbw:3 I'd very much appreciate some hints. Many thanks, RC
  • 2. Redefining the { } behavior (Hash creation method)
    There are two methods of creating Hash: a = {'a'=>1} a = Hash['a'=>1] but I can only redefine the Hash::[] method, not {} class Hash class << self def [] *args puts "foo" end end end Is it possible to change {}'s behavior? Jan Molic
  • 3. Ruby's FTP incredibly slow - culprit is peeraddr
    I just wrote (in Ruby) a little tool to help with FTPing a bunch of files. It turned out to be ridiculously slow. Upon investigation, it turns out that the culprit is the following line in the "sendport" define of the standard FTP library (i.e. "require 'net/ftp'"): af = (@sock.peeraddr)[0] This line is executed once per file that I'm trying to transfer, and consistently takes (on my machine) five seconds or so each time that it is executed. The purpose of this line is to determine whether the remote host is IP4 or IP6. Commenting out the line, and hardcoding the IP4 path (which is what my test host is) makes the whole program work fast and well. What is the problem with this line, that it takes five seconds? I am using Ruby 1.8, by the way. Thanks, Bob Vesterman.

Bug in cgi/session

Postby Ken Brush » Wed, 17 Aug 2005 01:25:47 GMT

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



Re: Bug in cgi/session

Postby Austin Ziegler » Wed, 17 Aug 2005 03:42:25 GMT



The Ruby bug tracker is on RubyForge.org.

-austin
-- 
Austin Ziegler *  XXXX@XXXXX.COM 
               * Alternate:  XXXX@XXXXX.COM 



Re: Bug in cgi/session

Postby Yukihiro Matsumoto » Tue, 06 Sep 2005 02:29:04 GMT

Hi,

Sorry for being late.

In message "Re: Bug in cgi/session"
    on Tue, 16 Aug 2005 01:25:47 +0900, Ken Brush < XXXX@XXXXX.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.

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



Re: Bug in cgi/session

Postby Ara.T.Howard » Tue, 06 Sep 2005 05:53:18 GMT




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




Similar Threads:

1.[BUG/PATCH] cgi/session.rb, cgi/session/pstore.rb

On Wed, 14 Sep 2005, Ara.T.Howard wrote:


CGI::Session::MemoryStore and CGI::Session::PStore suffer from same issue...
updated patches (in the right order this time):


--- ./cgi/session.rb.org        2005-09-13 20:52:14.000000000 -0600
+++ ./cgi/session.rb    2005-09-13 21:13:22.000000000 -0600
@@ -244,7 +244,7 @@
      #   end
      #
      def initialize(request, option={})
-      @new_session = false
+      @new_session = nil
        session_key = option['session_key'] || '_session_id'
        session_id = option['session_id']
        unless session_id
@@ -381,10 +381,11 @@
          md5 = Digest::MD5.hexdigest(id)[0,16]
         @path = dir+"/"+prefix+md5+suffix
         unless File::exist? @path
-          unless session.new_session
+          unless session.new_session.nil?
              raise CGI::Session::NoSession, "uninitialized session"
            end
           @hash = {}
+          update
         end
        end

@@ -451,7 +452,7 @@
        def initialize(session, option=nil)
         @session_id = session.session_id
          unless GLOBAL_HASH_TABLE.key?(@session_id)
-          unless session.new_session
+          unless session.new_session.nil?
              raise CGI::Session::NoSession, "uninitialized session"
            end
            GLOBAL_HASH_TABLE[@session_id] = {}




--- ./cgi/session/pstore.rb.org 2005-09-13 21:07:52.000000000 -0600
+++ ./cgi/session/pstore.rb     2005-09-13 21:18:21.000000000 -0600
@@ -9,7 +9,7 @@
  # persistent of session data on top of the pstore library.  See
  # cgi/session.rb for more details on session storage managers.

-require 'cgi/session'
+require './cgi/session'
  require 'pstore'

  class CGI
@@ -62,7 +62,7 @@
         path = dir+"/"+prefix+md5
         path.untaint
         unless File::exist?(path)
-          unless session.new_session
+          unless session.new_session.nil?
              raise CGI::Session::NoSession, "uninitialized session"
            end
           @hash = {}


now i'm noticing that the create of session is not atomic... but that's for
another day.

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



2.[BUG/PATCH] cgi/session.rb (SOLVED?)

3.[BUG/PATCH] cgi/session.rb

the following program with fail both the way it is, and using the commented
section:


   ~ > cat a.cgi
   #! /usr/local/bin/ruby

   require 'cgi'
   require 'cgi/session'

   cgi = CGI::new

   database_manager = CGI::Session::FileStore

   session =
   =begin
     begin
       s = CGI::Session::new cgi, 'database_manager' => database_manager, 'new_session' => false
       s.delete
       s
     rescue ArgumentError
       CGI::Session::new cgi, 'database_manager' => database_manager, 'new_session' => true
     end
   =end
       CGI::Session::new cgi, 'database_manager' => database_manager

   session.close

   this_time = Time::now
   last_time = session['last_time'] || this_time
   session['last_time'] = this_time
   session.close

   content = ''
   content << "<hr> THIS_TIME @ <b>#{ this_time }</b> <hr>"
   content << "<hr> LAST_TIME @ <b>#{ last_time }</b> <hr>"

   cgi.out{ content }


in either case a new session is __always__ created.  the docs say:

..
   # A CGI::Session instance is created from a CGI object.  By default,
   # this CGI::Session instance will start a new session if none currently
   # exists, or continue the current session for this client if one does
   # exist.  The +new_session+ option can be used to either always or
   # never create a new session.  See #new() for more details.
..

so there is no way to re-use a session attm.  this seems serious.  i think this
patch addresses the issue:


[ahoward@localhost build/ruby-1.8.2/lib/] diff -u -b -B cgi/session.rb.org cgi/session.rb
--- cgi/session.rb.org  2005-09-13 20:52:14.000000000 -0600
+++ cgi/session.rb      2005-09-13 20:53:32.000000000 -0600
@@ -244,7 +244,7 @@
      #   end
      #
      def initialize(request, option={})
-      @new_session = false
+      @new_session = nil
        session_key = option['session_key'] || '_session_id'
        session_id = option['session_id']
        unless session_id
@@ -381,10 +381,11 @@
          md5 = Digest::MD5.hexdigest(id)[0,16]
         @path = dir+"/"+prefix+md5+suffix
         unless File::exist? @path
-          unless session.new_session
+          unless session.new_session.nil?
              raise CGI::Session::NoSession, "uninitialized session"
            end
           @hash = {}
+          update
         end
        end


and, indeed, with the change the following works as expected/doccumented:

   #! /usr/local/bin/ruby
   require 'cgi'
   require './cgi/session'

   cgi = CGI::new
   content = ''
   database_manager = CGI::Session::FileStore
   this_time = Time::now

   begin
     session = CGI::Session::new cgi, 'database_manager' => database_manager
     last_time = session['last_time'] || this_time
     session['last_time'] = this_time
     session.close

     content << "<hr> THIS_TIME @ <b>#{ this_time }</b> <hr>"
     content << "<hr> LAST_TIME @ <b>#{ last_time }</b> <hr>"
   rescue Exception => e
     m, c, b = e.message, e.class, e.backtrace.join("\n")
     content << "<pre>#{ m } (#{ c })\n#{ b }</pre>"
   end
   cgi.out{ content }


hopefully this hasn't already been fixed - i searched for a while....

regards.


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



4.BUG: CGI.Session on Windows

If a newbie tries to get CGI Sessions working, he will run in the
following bug. No code example that i have seen so far starts with supplying
the temporary diectory as an option.

s = CGI::Session.new(cgi,{"tmpdir" => "z:/temp/", "session_path" => "/"})

And if a user does not do this it will result in an error because the
default in "session.rb:105" is

"dir = option['tmpdir'] || ENV['TMP'] || '/tmp'"

'/tmp' is not valid under windows and normally (standart
configuration) CGI's don't get the environment variables from the
system. So could we change this into
 
"dir = option['tmpdir'] || ENV['TMP'] || Dir::temp_directory

and add a "temp_directory" method to the Dir class that returns '/tmp'
or on windows the value of the "GetTempPath" function.

I would highly recommand this change. It makes the newbies life much
easier.

BTW is there any public bug tracking system where i can add such
requests.



5.cgi/session , deleting old sessions.

6. multipart forms and cgi - cgi.rb bug?

7. Help needed with CGI Sessions

8. CGI::Session Question



Return to ruby

 

Who is online

Users browsing this forum: No registered users and 52 guest