Cgi redirect

in perl, this is easy:

my $cgi = new CGI;
print $cgi->redirect(“http://foo.bar.baz/”);’

gives:
Status: 302 Moved
location: http://foo.bar.baz/

in ruby there is no CGI#redirect, the only way i could think of was:

print cgi.header({‘Status’ => ‘302 Moved’, ‘location’ =>
http://foo.bar.baz/’})

gives:
Content-Type: text/html
Status: 302 Moved
location: http://foo.bar.baz/

nil
irb(main):004:0>

but it still prints the content-type header. any good way around this
without doing it manually? i know there’s a solution for mod_ruby,
but cgi.rb really should have a redirect method as well i think.

···


tom@alkali.spamfree.org
remove ‘spamfree.’ to respond

In Ruby, you can always add the method you want to CGI dynamically:

require ‘cgi’

class CGI
def redirect( where )
print this.header( { ‘Status’ => ‘302 Moved’, ‘location’ =>
’#{where}’ } )
end
end

Now whenever you create a new instance of CGI, you have that method, in
addition to every other method in CGI. It’s OK to complain in some langauges
when a built in or library class doesn’t have particular functionality you
want/expect. But in Ruby it’s much more productive to just add it when you
need it. You can put those 7 grueling lines of code (including the space
between “require” and “class CGI”) into a separate file called mycgi.rb,
plop it in your local site_ruby directory, and from now on all you need to
write is:

require ‘mycgi’

cgi = CGI.new
cgi.redirect( “http://foo.bar.baz/” )

That’s what I love about Ruby’s OO model. Complain less, get more work done.
Of course you could always subclass CGI, but then you lose CGI’s private
methods and data. Usually I’ll only subclass when I need polymorphism or the
problem domain cries out for a non-trivial object hierarchy. Otherwise, for
most utilitatian purposes, just tweak an existing class using Ruby’s various
metaprogramming and dynamic OOP techniques.

:wink:

Sincerely,

Bob Calco

%% -----Original Message-----
%% From: Tom Robinson [mailto:tom@alkali.spamfree.org]
%% Sent: Tuesday, September 03, 2002 8:42 AM
%% To: ruby-talk ML
%% Subject: cgi redirect
%%
%%
%% in perl, this is easy:
%%
%% my $cgi = new CGI;
%% print $cgi->redirect(“http://foo.bar.baz/”);’
%%
%% gives:
%% Status: 302 Moved
%% location: http://foo.bar.baz/
%%
%% in ruby there is no CGI#redirect, the only way i could think of was:
%%
%% print cgi.header({‘Status’ => ‘302 Moved’, ‘location’ =>
%% ‘http://foo.bar.baz/’})
%%
%% gives:
%% Content-Type: text/html
%% Status: 302 Moved
%% location: http://foo.bar.baz/
%%
%% nil
%% irb(main):004:0>
%%
%% but it still prints the content-type header. any good way around this
%% without doing it manually? i know there’s a solution for mod_ruby,
%% but cgi.rb really should have a redirect method as well i think.
%%
%% –
%% tom@alkali.spamfree.org
%% remove ‘spamfree.’ to respond
%%
%%

Tom Robinson tom@alkali.spamfree.org wrote in message news:aka9nu0ie49m1hob6h9oohlp7isdrqe4m9@4ax.com

in perl, this is easy:

my $cgi = new CGI;
print $cgi->redirect(“http://foo.bar.baz/”);’

gives:
Status: 302 Moved
location: http://foo.bar.baz/

NARF should. http://narf-lib.sourceforge.net http://www.sourceforge.net/projects/narf-lib

require ‘narf’
output = “”
CGI.process( :out => output){ |cgi|
cgi.set_redirect( “new_location.com” )
}
puts output

this script outputs:

Content-Type: text/html
Location: new_location.com
Status: 302
Set-Cookie: _session_id=545508609cdfcd19; path=

hmmmmm is this correct? Narf automagically creates sessions for you,
that’s what the Set-Cookie is for. But is the content-type header
harmful? What about a status of “302” rather than “302 Moved”?

Let me know so I can fix the behavior!

Thanks,

Patrick May

In my previous example, I got a little overzealous. I wanted to show
off how NARF can redirect the output stream. This script:

require 'narf’
CGI.process() { |cgi|
cgi.set_redirect( “new_location.com” )
}

will also return:

Content-Type: text/html
Location: new_location.com
Status: 302
Set-Cookie: _session_id=e14f006c450db0a4; path=

Narf borrows much from Wakou’s cgi, so it should send headers
correctly in mod_ruby. Still, I am concerned about the other issues:

  • does the extra Content-Type header matter?
  • does the order of the headers matter?
  • does the fact that status is “302” instead of “302 Moving” matter?
  • does the cookie for the session matter?

Anyways, I appreciate any feedback.

thx in advance,

Patrick

In Ruby, you can always add the method you want to CGI dynamically:

How do you guys manage your own “enhancements” to ruby? Say you’ve
got a number of “pet” definitions, redefinitions, etc. to standard
ruby library classes; where do you put them? In separate files that
you include later, or 1 big one, or something else?

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com

That’s what I love about Ruby’s OO model. Complain less, get more
work done.

This seems like it leads to each person reinventing the same new methods (or
hacks), but not sharing them, rather than enhancing the public code base.

I don’t understand why an observation on the lack of a built-in way to emit
a common HTTP header is considered a complaint.

James

But that doesn’t give the desired result, as I said, using CGI#header
in this way also prints out the content-type header - which CGI.pm
doesn’t. You would want:

class CGI
def redirect(url)
“Status: 302 Moved\nlocation: #{url}\n\n”
end
end

which would then be used as similar to CGI.pm:

print cgi.redirect(‘http://location.com/’)

You would also have to consider that the above is incompatible with
the way the header handling works under mod_ruby (header is sent
straight away rather than returned, I think).

I agree with you that the way you can ‘dip in’ to existing classes
from anywhere in ruby is great, but I still think CGI#header should be
in the standard cgi.rb, as it’s very commonly required.

···

On Tue, 03 Sep 2002 14:50:25 GMT, “Bob Calco” robert.calco@verizon.net wrote:

In Ruby, you can always add the method you want to CGI dynamically:

require ‘cgi’

class CGI
def redirect( where )
print this.header( { ‘Status’ => ‘302 Moved’, ‘location’ =>
‘#{where}’ } )
end
end


tom@alkali.spamfree.org
remove ‘spamfree.’ to respond

correctly in mod_ruby. Still, I am concerned about the other issues:

  • does the extra Content-Type header matter?
  • does the order of the headers matter?
  • does the fact that status is “302” instead of “302 Moving” matter?

These all depend on the browser, and you know how diverse those are!
I think the main thing to consider is that CGI.pm does it in a certain
way and that (I’ve found) always works, so the safest thing is going
to be to copy the CGI.pm behaviour exactly. I’ve tried changing some
of this stuff before and I’ve run into problems more often than not.
I think the content-type header breaks the redirection in internet
explorer but I’m not 100% sure.

  • does the cookie for the session matter?

In my experience, this shouldn’t matter, but the above stuff still
applies.

···

On 4 Sep 2002 18:22:04 -0700, patrick-may@monmouth.com (Patrick May) wrote:


tom@alkali.spamfree.org
remove ‘spamfree.’ to respond

How do you guys manage your own “enhancements” to ruby? Say you’ve
got a number of “pet” definitions, redefinitions, etc. to standard
ruby library classes; where do you put them? In separate files that
you include later, or 1 big one, or something else?

til/

I organize them all into their own files, then include them when I need
them.

For example, I’ve got a small collection of things handy for windows – I
have them in a file called win.rb, and my installer puts them in
site_ruby\1.6\cl\util, so whenever I need them:

require ‘cl/util/win’

I’ve also got an in-house library on the job that also has an installer and
what-not. This also allows me to include the latest win32ole.so when I need
it, plus other libs and blah blah blah.

Chris
http://clabs.org

How do I do it? Answer: Badly.

I’ve thought about organizing all these tidbits,
but have never done it. Usually I just think:
When was the last time I used that trick? Then
I go and cut/paste the code.

Hope Dave Thomas isn’t reading this. DRY, and
all that…

I do like the idea of doing this well, but my
meta-coding usually takes a back seat to my
coding.

Hal

···

----- Original Message -----
From: “Michael Campbell” michael_s_campbell@yahoo.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, September 03, 2002 9:47 AM
Subject: How do you manage your own library? was: RE: cgi redirect

In Ruby, you can always add the method you want to CGI dynamically:

How do you guys manage your own “enhancements” to ruby? Say you’ve
got a number of “pet” definitions, redefinitions, etc. to standard
ruby library classes; where do you put them? In separate files that
you include later, or 1 big one, or something else?

James:

%%
%% > That’s what I love about Ruby’s OO model. Complain less, get more
%% > work done.
%%
%% This seems like it leads to each person reinventing the same new
%% methods (or
%% hacks), but not sharing them, rather than enhancing the public code base.
%%

Good point, I wasn’t trying to comment on the virtues of open source
development, though. Specifically I was just trying to coo about Ruby’s
dynamicity. The lack of a commonly accepted “redirect” method in CGI is not
a showstopper and very little code is needed to remedy the situation, was my
only point. And you don’t have to wait till the community adopts your fix,
I’ll add to that, or mess with the original public codebase to do it, I’ll
add to that.

%% I don’t understand why an observation on the lack of a built-in
%% way to emit
%% a common HTTP header is considered a complaint.

OK, strike that wording and change “complain” to “despair” or even, more
innocuously, “frown disapprovingly”. My point was merely that unlike in
other languages, where builtins are hard and fast and you need to wait till
the next release for your feature to appear (maybe), you can fairly easily
modify the behavior of even the most fundamental objects in Ruby. And as
long as you modifying these objects in your application code or libraries,
you aren’t going to muck up the public codebase, and if a fix or enhancement
comes along, you shouldn’t have to change much at all if anything but a line
or two in your code to take advantage of the new stuff.

I apologize for the wording; I did not mean it the way it came out.

%%
%% James
%%
%%
%%

Hi –

In Ruby, you can always add the method you want to CGI dynamically:

How do you guys manage your own “enhancements” to ruby? Say you’ve
got a number of “pet” definitions, redefinitions, etc. to standard
ruby library classes; where do you put them? In separate files that
you include later, or 1 big one, or something else?

I know I’ll sound like a broken record, at least to long-time
ruby-talk residents, if I mention Ruby Behaviors, or
[ruby-talk:14556], or any such thing :slight_smile: So I’ll just say that, in the
absence of having really made Behaviors work, and probably for other
reasons, I tend to be pretty scrappy when it comes to this kind of
thing. For instance, in a recent project I wanted to make use of
Enumerable#map_with_indices, so I rewrote it for the nth time…
Luckily Ruby makes that relatively easy :slight_smile: though it would in theory
be easier if I were more organized about packaging things, even for
private use.

David

···

On Tue, 3 Sep 2002, Michael Campbell wrote:


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

Tom:

[very good, valid technical points snipped]

%% I agree with you that the way you can ‘dip in’ to existing classes
%% from anywhere in ruby is great, but I still think CGI#header should be
%% in the standard cgi.rb, as it’s very commonly required.

I agree. :slight_smile:

%% –
%% tom@alkali.spamfree.org
%% remove ‘spamfree.’ to respond
%%

Tom Robinson tom@alkali.spamfree.org wrote in message news:2tednuc3rnsp70p23vjf7rhim4rn86o9gf@4ax.com

···

On 4 Sep 2002 18:22:04 -0700, patrick-may@monmouth.com (Patrick May) > wrote:

correctly in mod_ruby. Still, I am concerned about the other issues:

  • does the extra Content-Type header matter?
  • does the order of the headers matter?
  • does the fact that status is “302” instead of “302 Moving” matter?

These all depend on the browser, and you know how diverse those are!
I think the main thing to consider is that CGI.pm does it in a certain
way and that (I’ve found) always works, so the safest thing is going
to be to copy the CGI.pm behaviour exactly. I’ve tried changing some
of this stuff before and I’ve run into problems more often than not.
I think the content-type header breaks the redirection in internet
explorer but I’m not 100% sure.

Hmmm… guess it’s time to do some browser testing! I appreciate the heads up.

~ Patrick

Hi –

In Ruby, you can always add the method you want to CGI dynamically:

How do you guys manage your own “enhancements” to ruby? Say you’ve
got a number of “pet” definitions, redefinitions, etc. to standard
ruby library classes; where do you put them? In separate files that
you include later, or 1 big one, or something else?

I know I’ll sound like a broken record, at least to long-time
ruby-talk residents, if I mention Ruby Behaviors, or
[ruby-talk:14556], or any such thing :slight_smile: So I’ll just say that, in the
absence of having really made Behaviors work, and probably for other
reasons, I tend to be pretty scrappy when it comes to this kind of
thing. For instance, in a recent project I wanted to make use of
Enumerable#map_with_indices, so I rewrote it for the nth time…
Luckily Ruby makes that relatively easy :slight_smile: though it would in theory
be easier if I were more organized about packaging things, even for
private use.

I haven’t followed all of this thread yet, so I apologize if I too sound
like a broken record (!). In order to package things effectively, we need
a damn good - and standard - way to manage packages.

Now I know we have rpkg (is that what it’s called?), and it seems to be
rather good, not that I’ve used it, but it needs to become ingrained into
the very nature of Ruby, like rdoc does, and so on.

There’s little point writing one’s own packages if it’s difficult to
manage them. For really simple things, “difficult” means “any obstacle at
all”, since it’s all too often easier to rewrite methods.

I intend to look at rpkg real soon to see if it can help me manipulate my
own lame packages.


David Alan Black | Register for RubyConf 2002!

Cheerio,
Gavin

···

On Tue, 3 Sep 2002, Michael Campbell wrote: