# Ruby-specific CGI question (I think)

**URL:** <https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849>\
**Category:** ruby-talk\
**Created:** [14 July 2003 23:17 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849 "2003-07-14T23:17:47Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kurt\_M\_Dresner](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kurt\_M\_Dresner](https://rubytalk.org/u/Kurt_M_Dresner)\
**Post date:** [14 July 2003 23:17 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/1 "2003-07-14T23:17:47Z")

</div>

I’m using sessions and forms in my cgi script.

Unfortunately, for some reason, cgi.form seems to be putting some funny  
stuff at the end of each form I make. (I have multiple forms on the  
page, and it puts it on every one.) The funny stuff looks like this:

First of all: Why is it doing this?

Second of all: How do I make it stop?

-Kurt

---

<div class="post-metadata">

**Author:** ![Tim\_Hammerquist2](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@Tim\_Hammerquist2](https://rubytalk.org/u/Tim_Hammerquist2)\
**Post date:** [15 July 2003 00:53 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/2 "2003-07-15T00:53:39Z")

</div>

Kurt M. Dresner graced us by uttering:

> I’m using sessions and forms in my cgi script.
> 
> Unfortunately, for some reason, cgi.form seems to be putting  
> some funny stuff at the end of each form I make. (I have  
> multiple forms on the page, and it puts it on every one.) The  
> funny stuff looks like this:
> 
> First of all: Why is it doing this?

Session management. Session information has to be passed somehow  
to mask the inherent statelessness of the CGI protocol.

> Second of all: How do I make it stop?

Stop using cgi/session?

> -Kurt

Tim Hammerquist

> **···**
>
> –  
> Familiarity breeds contempt – and children.  
> – Mark Twain

---

<div class="post-metadata">

**Author:** ![Kurt\_M\_Dresner](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kurt\_M\_Dresner](https://rubytalk.org/u/Kurt_M_Dresner)\
**Post date:** [15 July 2003 01:36 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/3 "2003-07-15T01:36:22Z")

</div>

The problem is not that it is using session management. The problem is  
that these things are being visually displayed in my web browser as  
rectangles. Any idea how to make /that/ stop?

-Kurt

> **···**
>
> On Tue, Jul 15, 2003 at 09:53:39AM +0900, Tim Hammerquist wrote:
> 
> > Kurt M. Dresner graced us by uttering:
> > 
> > > I’m using sessions and forms in my cgi script.
> > > 
> > > Unfortunately, for some reason, cgi.form seems to be putting  
> > > some funny stuff at the end of each form I make. (I have  
> > > multiple forms on the page, and it puts it on every one.) The  
> > > funny stuff looks like this:
> > > 
> > > First of all: Why is it doing this?
> > 
> > Session management. Session information has to be passed somehow  
> > to mask the inherent statelessness of the CGI protocol.
> > 
> > > Second of all: How do I make it stop?
> > 
> > Stop using cgi/session?
> > 
> > > -Kurt
> > 
> > ## Tim Hammerquist
> > 
> > Familiarity breeds contempt – and children.  
> > – Mark Twain
> > 
> > ======= End of Original Message =======\<

---

<div class="post-metadata">

**Author:** ![Rasputin1](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Rasputin1](https://rubytalk.org/u/Rasputin1)\
**Post date:** [15 July 2003 01:56 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/4 "2003-07-15T01:56:28Z")

</div>

- Tim Hammerquist [tim@vegeta.ath.cx](mailto:tim@vegeta.ath.cx) [0753 01:53]:

> Kurt M. Dresner graced us by uttering:
> 
> > I’m using sessions and forms in my cgi script.
> > 
> > Unfortunately, for some reason, cgi.form seems to be putting  
> > some funny stuff at the end of each form I make. (I have  
> > multiple forms on the page, and it puts it on every one.) The  
> > funny stuff looks like this:
> > 
> > First of all: Why is it doing this?
> 
> Session management. Session information has to be passed somehow  
> to mask the inherent statelessness of the CGI protocol.

Bless you, you have saved my sanity. I _knew_ it had to be sending it  
somehow, but couldn’t figure out why it wasn’t working and just realised I’m  
not using cgi.rb to write the form (I’m using eruby instead).

> > Second of all: How do I make it stop?

My forms look like

- that was generated by :

what does your ruby code look like ? Are you doing something funky?

> **···**
>
> –  
> Q: How many Zen masters does it take to screw in a light bulb?  
> A: None. The Universe spins the bulb, and the Zen master stays out  
> of the way.  
> Rasputin :: Jack of All Trades - Master of Nuns

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [15 July 2003 01:45 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/5 "2003-07-15T01:45:52Z")

</div>

Well… doesn’t fieldset draw a box?? I’ve never  
used it, but I’ve seen documentation that says  
it does that.

Why is ‘fieldset’ used there anyway? Will it work  
if you remove it?

Hal

> **···**
>
> ----- Original Message -----  
> From: “Kurt M. Dresner” [kdresner@cs.utexas.edu](mailto:kdresner@cs.utexas.edu)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Monday, July 14, 2003 8:36 PM  
> Subject: Re: ruby-specific CGI question (I think)
> 
> > The problem is not that it is using session management. The problem is  
> > that these things are being visually displayed in my web browser as  
> > rectangles. Any idea how to make /that/ stop?
> 
> –  
> Hal Fulton  
> [hal9000@hypermetrics.com](mailto:hal9000@hypermetrics.com)

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [15 July 2003 02:00 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/6 "2003-07-15T02:00:17Z")

</div>

Yes, the fieldset is the problem, but I get the impression that Kurt  
isn’t generating that himself, but rather that CGI::Session is  
inserting it for him. It’d help if I could see the code, though.

-Mark

> **···**
>
> On Tue, Jul 15, 2003 at 10:45:52AM +0900, Hal E. Fulton wrote:
> 
> > > The problem is not that it is using session management. The problem is  
> > > that these things are being visually displayed in my web browser as  
> > > rectangles. Any idea how to make /that/ stop?
> > 
> > Well… doesn’t fieldset draw a box?? I’ve never  
> > used it, but I’ve seen documentation that says  
> > it does that.
> > 
> > Why is ‘fieldset’ used there anyway? Will it work  
> > if you remove it?

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [15 July 2003 02:06 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/7 "2003-07-15T02:06:18Z")

</div>

> > > The problem is not that it is using session management. The problem  
> > > is  
> > > that these things are being visually displayed in my web browser as  
> > > rectangles. Any idea how to make /that/ stop?
> > 
> > Well… doesn’t fieldset draw a box?? I’ve never  
> > used it, but I’ve seen documentation that says  
> > it does that.
> > 
> > Why is ‘fieldset’ used there anyway? Will it work  
> > if you remove it?
> 
> Yes, the fieldset is the problem, but I get the impression that Kurt  
> isn’t generating that himself, but rather that CGI::Session is  
> inserting it for him. It’d help if I could see the code, though.

That’s my impression also; but why should the  
session lib use that feature?

By ‘remove it’ I meant either save the HTML and  
edit it out and see how it looks (may not be  
possible) or simply (temporarily) change the  
library source.

Hal

> **···**
>
> ----- Original Message -----  
> From: “Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Monday, July 14, 2003 9:00 PM  
> Subject: Re: ruby-specific CGI question (I think)
> 
> > On Tue, Jul 15, 2003 at 10:45:52AM +0900, Hal E. Fulton wrote:
> 
> –  
> Hal Fulton  
> [hal9000@hypermetrics.com](mailto:hal9000@hypermetrics.com)

---

<div class="post-metadata">

**Author:** ![Kurt\_M\_Dresner](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kurt\_M\_Dresner](https://rubytalk.org/u/Kurt_M_Dresner)\
**Post date:** [15 July 2003 02:10 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/8 "2003-07-15T02:10:55Z")

</div>

Here’s my code folks. Again, I don’t profess to have anything that is  
at all polished - I am just working something out:

-Kurt

#! /usr/bin/env ruby

require “cgi”  
require “cgi/session”  
require “setdeck”  
require “set”  
require “setcollection”  
require “setboard”

cgi = CGI.new(“html4”)  
addr = cgi.remote\_addr.gsub(/./,‘d’)  
sess = CGI::Session.new( cgi, “session\_key” =\> “crappyset”,  
“session\_id” =\> addr,  
“prefix” =\> “setgame.”,  
“tmpdir” =\> “/tmp”)

if sess[“deck”].nil?  
deck = SetDeck.new  
sets = SetCollection.new  
board = SetBoard.new(12)  
deck.shuffle!  
else  
deck = Marshal.load(sess[“deck”])  
sets = Marshal.load(sess[“sets”])  
board = Marshal.load(sess[“board”])  
end

if cgi.has\_key?(‘selections’)  
tomark = cgi[‘selections’][0].split(‘,’).collect { |x| x.to\_i }  
tomark.each do |marked|  
if cgi.has\_key?(‘uncheck’)  
board.deselect(marked)  
else  
board.select(marked)  
end  
end  
end

if cgi.has\_key?(‘findset’)  
board.markoneset  
end

if cgi.has\_key?(‘getset’)  
newset = Set.new(board.removeSelected)  
if newset.valid?  
newset.collect  
sets.add(newset)  
else  
takencards = newset.destroy  
board.place(takencards) unless takencards.nil?  
end  
end

sess[“deck”] = Marshal.dump(deck)  
sess[“sets”] = Marshal.dump(sets)  
sess[“board”] = Marshal.dump(board)  
sess.update

paramstring = “Session name: #{addr}” + cgi.br +  
"Parameters: " +  
(cgi.keys.collect { |key| key.to\_s + ": " +  
cgi[key].to\_s}).join(cgi.br)

board.place(deck.dealn(board.holes.size))

cgi.out {  
cgi.html {  
cgi.head {“\n” + cgi.title {“What a crappy implementation of set!”}}

> **···**
>
> - cgi.body {“\n” + cgi.pre{board.to\_s} +  
> cgi.br +  
> cgi.form {  
> cgi.textarea(“selections”) +  
> cgi.br +  
> cgi.submit(“Check Them”, “check”) +  
> cgi.submit(“Un-Check Them”, “uncheck”)
> 
> On Tue, Jul 15, 2003 at 11:00:17AM +0900, Mark J. Reed wrote:
> 
> > On Tue, Jul 15, 2003 at 10:45:52AM +0900, Hal E. Fulton wrote:
> > 
> > > > The problem is not that it is using session management. The problem is  
> > > > that these things are being visually displayed in my web browser as  
> > > > rectangles. Any idea how to make /that/ stop?
> > > 
> > > Well… doesn’t fieldset draw a box?? I’ve never  
> > > used it, but I’ve seen documentation that says  
> > > it does that.
> > > 
> > > Why is ‘fieldset’ used there anyway? Will it work  
> > > if you remove it?
> > 
> > Yes, the fieldset is the problem, but I get the impression that Kurt  
> > isn’t generating that himself, but rather that CGI::Session is  
> > inserting it for him. It’d help if I could see the code, though.
> > 
> > -Mark
> > 
> > ======= End of Original Message =======\<

---

<div class="post-metadata">

**Author:** ![Rasputin1](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Rasputin1](https://rubytalk.org/u/Rasputin1)\
**Post date:** [15 July 2003 02:14 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/9 "2003-07-15T02:14:10Z")

</div>

- Hal E. Fulton [hal9000@hypermetrics.com](mailto:hal9000@hypermetrics.com) [0706 03:06]:

> From: “Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Monday, July 14, 2003 9:00 PM  
> Subject: Re: ruby-specific CGI question (I think)
> 
> > > > The problem is not that it is using session management. The problem  
> > > > is  
> > > > that these things are being visually displayed in my web browser as  
> > > > rectangles. Any idea how to make /that/ stop?
> > > 
> > > Well… doesn’t fieldset draw a box?? I’ve never  
> > > used it, but I’ve seen documentation that says  
> > > it does that.
> > > 
> > > Why is ‘fieldset’ used there anyway? Will it work  
> > > if you remove it?
> > 
> > Yes, the fieldset is the problem, but I get the impression that Kurt  
> > isn’t generating that himself, but rather that CGI::Session is  
> > inserting it for him. It’d help if I could see the code, though.
> 
> That’s my impression also; but why should the  
> session lib use that feature?
> 
> By ‘remove it’ I meant either save the HTML and  
> edit it out and see how it looks (may not be  
> possible) or simply (temporarily) change the  
> library source.

Even easier:

cgi.form() seems to just return a String - see that snippet I sent earlier:

formString = cgi.form(method=“get”) {  
cgi.text\_field(“key”) +  
cgi.text\_field(“value”) +  
cgi.submit(“add”)  
}

You should be able to work on formString like any other String,  
remove any garbage that got in there, then just puts() it to the browser.

> **···**
>
> > ----- Original Message -----
> > 
> > > On Tue, Jul 15, 2003 at 10:45:52AM +0900, Hal E. Fulton wrote:
> 
> –  
> Boston, n.:  
> Ludwig van Beethoven being jeered by 50,000 sports fans for  
> finishing second in the Irish jig competition.  
> Rasputin :: Jack of All Trades - Master of Nuns

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [15 July 2003 02:43 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/10 "2003-07-15T02:43:28Z")

</div>

Okay, here’s what’s going on. When you create a CGI::Session,  
you pass it a CGI object. The session sets the @output\_hidden instance  
variable within the CGI query object to so that   
elements get output at the end of every form.

Except that, for some strange reason known only to the author of cgi.rb,  
it uses fieldsets:

```
  if @output_hidden
    hidden = @output_hidden.collect{|k,v|
      "<INPUT TYPE=HIDDEN NAME=\"#{k}\" VALUE=\"#{v}\">"
    }.to_s
    if defined? fieldset
      body += fieldset{ hidden }
    else
      body += hidden
    end
  end

```

The defined? test is just checking to see if fieldset is available -  
which it is if you ask for html4, but not if you ask for html3.

I think this is clearly a bug in cgi.rb - there’s no reason to  
put a fieldset around these elements just because you can. So  
your options are:

1. Modify your local copy of cgi.rb so that it doesn’t do that. 🙂  
Great if you have permission to do that, otherwise no help.

2. Ask for html3 instead of html4.  
This is a bad idea for other reasons, though. You don’t want to  
limit yourself to HTML 3, which is years out of date.

3. Create your own modified version of cgi.form within the script that  
munges the result.

That last option goes something like this:

```
def fix_form(cgi, *args, &block)
    return cgi.form(*args, &block).gsub(/<\/?fieldset[^>]*/i, '')
end

```

Then everywhere you currently have

```
cgi.form { blah . . . }

```

Just replace it with

```
fix_form(cgi) { blah . . . }

```

-Mark

> **···**
>
> On Tue, Jul 15, 2003 at 11:10:55AM +0900, Kurt M. Dresner wrote:
> 
> > cgi = CGI.new(“html4”)

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [15 July 2003 02:17 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/11 "2003-07-15T02:17:26Z")

</div>

> > By ‘remove it’ I meant either save the HTML and  
> > edit it out and see how it looks (may not be  
> > possible) or simply (temporarily) change the  
> > library source.
> 
> Even easier:
> 
> cgi.form() seems to just return a String - see that snippet I sent  
> earlier:
> 
> formString = cgi.form(method=“get”) {  
> cgi.text\_field(“key”) +  
> cgi.text\_field(“value”) +  
> cgi.submit(“add”)  
> }
> 
> You should be able to work on formString like any other String,  
> remove any garbage that got in there, then just puts() it to the browser.

Oh, certainly. I’d have seen that if I had  
a little less blood in my caffeine stream.

Hal

> **···**
>
> ----- Original Message -----  
> From: “Rasputin” [rasputin@idoru.mine.nu](mailto:rasputin@idoru.mine.nu)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Monday, July 14, 2003 9:14 PM  
> Subject: Re: ruby-specific CGI question (I think)
> 
> –  
> Hal Fulton  
> [hal9000@hypermetrics.com](mailto:hal9000@hypermetrics.com)

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [15 July 2003 02:50 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/12 "2003-07-15T02:50:38Z")

</div>

Note, however, that this will remove _all_ tags in the  
form, so if you actually have legitimate uses for them, this isn’t  
the way to go. You would need to make a smarter munger in that case.

-Mark

> **···**
>
> On Tue, Jul 15, 2003 at 11:43:28AM +0900, Mark J. Reed wrote:
> 
> > def fix\_form(cgi, \*args, &block)  
> > return cgi.form(_args, &block).gsub(/\</?fieldset[^\>]_/i, ‘’)  
> > end

---

<div class="post-metadata">

**Author:** ![Kurt\_M\_Dresner](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kurt\_M\_Dresner](https://rubytalk.org/u/Kurt_M_Dresner)\
**Post date:** [15 July 2003 05:47 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/13 "2003-07-15T05:47:12Z")

</div>

> I think this is clearly a bug in cgi.rb - there’s no reason to  
> put a fieldset around these elements just because you can. So  
> your options are:

So now the question is, “How do we get this fixed in the real thing?”

-Kurt “doesn’t know about these kinds of things”

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [15 July 2003 03:02 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/14 "2003-07-15T03:02:50Z")

</div>

Typo correction:

> > ```
> > def fix_form(cgi, *args, &block)
> > return cgi.form(*args, &block).gsub(/<\/?fieldset[^>]*/i, '')
> > end
> > 
> > ```

Left off the final \>, which is no good. 🙂

```
def fix_form(cgi, *args, &block)
    return cgi.form(*args, &block).gsub(/<\/?fieldset[^>]*>/i, '')
end

```

> Note, however, that this will remove _all_ tags in the  
> form, so if you actually have legitimate uses for them, this isn’t  
> the way to go. You would need to make a smarter munger in that case.

-Mark

> **···**
>
> On Mon, Jul 14, 2003 at 10:49:41PM -0400, Mark J. Reed wrote:
> 
> > On Tue, Jul 15, 2003 at 11:43:28AM +0900, Mark J. Reed wrote:

---

<div class="post-metadata">

**Author:** ![Rasputin1](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Rasputin1](https://rubytalk.org/u/Rasputin1)\
**Post date:** [15 July 2003 09:22 UTC](https://rubytalk.org/t/ruby-specific-cgi-question-i-think/6849/15 "2003-07-15T09:22:39Z")

</div>

- Hal E. Fulton [hal9000@hypermetrics.com](mailto:hal9000@hypermetrics.com) [0717 03:17]:

> **···**
>
> > ----- Original Message -----  
> > From: “Rasputin” [rasputin@idoru.mine.nu](mailto:rasputin@idoru.mine.nu)  
> > To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> > Sent: Monday, July 14, 2003 9:14 PM  
> > Subject: Re: ruby-specific CGI question (I think)
> > 
> > > > By ‘remove it’ I meant either save the HTML and  
> > > > edit it out and see how it looks (may not be  
> > > > possible) or simply (temporarily) change the  
> > > > library source.
> > > 
> > > Even easier:
> > > 
> > > cgi.form() seems to just return a String - see that snippet I sent  
> > > earlier:
> > > 
> > > formString = cgi.form(method=“get”) {  
> > > cgi.text\_field(“key”) +  
> > > cgi.text\_field(“value”) +  
> > > cgi.submit(“add”)  
> > > }
> > > 
> > > You should be able to work on formString like any other String,  
> > > remove any garbage that got in there, then just puts() it to the browser.
> > 
> > Oh, certainly. I’d have seen that if I had  
> > a little less blood in my caffeine stream.
> 
> 🙂
> 
> I’d never have seen it in a million years if cgi/session.rb didn’t  
> have lots of head-shaped dents in it after me spending all night  
> trying to get sessions in .rhtml files working…
> 
> –  
> How much does it cost to entice a dope-smoking UNIX system guru to Dayton?  
> – Brian Boyle, UNIX/WORLD’s First Annual Salary Survey  
> Rasputin :: Jack of All Trades - Master of Nuns
