From: "Eustaquio Rangel de Oliveira Jr." Date: 2004-12-23T05:37:38+09:00 Subject: cgi.rb GET and POST values patch --------------050808070301030607000002 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello there. :-) First of all, let me say that I'm just learning Ruby and think that is really a wonderful language and have a very fine community. People are cool here. :-) I'm just reading and learning how to program in Ruby for some few days, and as I use PHP as my main web development language, I'm testing mod_ruby and eruby. One thing I missed is a way to put the GET and POST values on differente places there. I think it's more safe to keep it separated, there is a good discussion about this here: http://simon.incutio.com/archive/2003/10/25/difference I use to always refer to my posted form values in different way I get the parameters that came from the URL. Don't know what you guys think about it, but I think is a good way to *try* to avoid some "dirty tricks" that people can do. Let me hear what you think about that. :-) But, I just took my chances here and developed (!) a patch (!!) for the cgi.rb module, that allow to reference to the POST and GET parameters values on different places. There was CGI.params and it is still there, no need to changes, but there is now CGI.get_params and CGI.post_params and I made this based on the env_method value. Making a little test here with a eruby file, I attached the file eruby_form.rhtml. Tell me what you think about. I'm attaching also the patch file, I really don't know if I did that the right way, forgive me some mistakes but I'm still (and will keep) learning about Ruby. Thanks for your attention and keep the good work! :-) - ---------------------------- Eust�quio "TaQ" Rangel eustaquiorangel@yahoo.com http://beam.to/taq Usu�rio GNU/Linux no. 224050 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBydlBb6UiZnhJiLsRAmZ2AKCl5f8K7+skL2hmxR77AEzr8FGjuACdGMtj NyUvEofUqvktFCxWrac5JMU= =ODr9 -----END PGP SIGNATURE----- --------------050808070301030607000002 Content-Type: text/plain; name="eruby_form.rhtml" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="eruby_form.rhtml" <% require 'cgi' # Require the CGI library cgi = CGI.new() # New CGI object username = cgi.post_params['username'] # post username get_test = cgi.get_params['get_test_param'] # get test param %> eRuby Test

Testing eRuby

<% if username.empty? # Print out the form asking for the username %>
Please enter your username:
<% if !get_test.empty? %> You told me that the get test parameter is <%= get_test %> <% end %> <% else %> Welcome <%= username %>! <% end %> --------------050808070301030607000002 Content-Type: text/plain; name="cgi.rb.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="cgi.rb.patch" --- cgi.rb.old 2004-12-22 13:30:36.000000000 -0200 +++ cgi.rb 2004-12-22 14:54:13.000000000 -0200 @@ -891,7 +891,7 @@ # # "name2" => ["value1", "value2", ...], ... } # def CGI::parse(query) - params = Hash.new([].freeze) + params = Hash.new([].freeze) query.split(/[&;]/n).each do |pairs| key, value = pairs.split('=',2).collect{|v| CGI::unescape(v) } @@ -956,6 +956,8 @@ # Get the parameters as a hash of name=>values pairs, where # values is an Array. attr("params") + attr("get_params") + attr("post_params") # Set all the parameters. def params=(hash) @@ -963,6 +965,16 @@ @params.update(hash) end + def get_params=(hash) + @get_params.clear + @get_params.update(hash) + end + + def post_params=(hash) + @post_params.clear + @post_params.update(hash) + end + def read_multipart(boundary, content_length) params = Hash.new([]) boundary = "--" + boundary @@ -1117,6 +1129,9 @@ ) end + @get_params = env_table['REQUEST_METHOD'] == "GET" ? @params : Hash.new([]) + @post_params = env_table['REQUEST_METHOD'] == "POST" ? @params : Hash.new([]) + @cookies = CGI::Cookie::parse((env_table['HTTP_COOKIE'] or env_table['COOKIE'])) end private :initialize_query @@ -2265,11 +2280,15 @@ initialize_query() # set @params, @cookies else if defined?(CGI_PARAMS) - @params = CGI_PARAMS.nil? ? nil : CGI_PARAMS.dup - @cookies = CGI_COOKIES.nil? ? nil : CGI_COOKIES.dup + @params = CGI_PARAMS.nil? ? nil : CGI_PARAMS.dup + @cookies = CGI_COOKIES.nil? ? nil : CGI_COOKIES.dup + @get_params = CGI_PARAMS.nil? ? nil : (env_table['REQUEST_METHOD'] == "GET" ? CGI_PARAMS.dup : nil) + @post_params = CGI_PARAMS.nil? ? nil : (env_table['REQUEST_METHOD'] == "POST" ? CGI_PARAMS.dup : nil) else initialize_query() # set @params, @cookies - params = @params.nil? ? nil : @params.dup + params = @params.nil? ? nil : @params.dup + get_params = env_table['REQUEST_METHOD'] == "GET" ? params.dup : nil + post_params = env_table['REQUEST_METHOD'] == "POST" ? params.dup : nil cookies = @cookies.nil? ? nil : @cookies.dup (class << self; self; end).class_eval do const_set(:CGI_PARAMS, params) --------------050808070301030607000002--