Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

CGI::Application and start_multipart_form stumper

by dmorgo (Pilgrim)
on Dec 06, 2005 at 04:23 UTC ( [id://514320]=perlquestion: print w/replies, xml ) Need Help??

dmorgo has asked for the wisdom of the Perl Monks concerning the following question:

Alas, my fellow travellers, an upload script with which young supplicants were to submit mp3 files of single hands clapping has stopped working after I installed Firefox 1.5 on Windows XP. The vexing thing is, it does work on a Windows 2000 machine with Firefox 1.5, just not on my Win XP machine.

Jokes about upgrading to Linux are appreciated here just as much as at any monastery, but unfortunately not just supplicants, but masses of aspiring supplicants will be using this and they could be running just about anything.

I think this is a problem with run_mode in CGI::Application. Here's what the script is supposed to do, and what it does:

What it's supposed to do:
Display a form. Supplicant enters data into form, including a filename, and clicks 'Upload.' Then it displays the values of field1 and field2. For the purpose of this test, it just ignores the value of the filename field.

What it actually does:
Display a form. After supplicant enters data and submits, it redisplays the same empty form.

It only misbehaves like this when the filename field is populated, and only with Firefox 1.5 on Win XP. And the real (full) script that actually does something with the file behaves the same way. Apparently the run_mode (rm) parameter is getting cleared when there is a file uploaded. So instead of going to save_mode and storing the file to the disk, it acts as if it got no input at all, and goes to the default mode, list_mode in this case.

Your most inferior servant has cut the code down to the minimum that reproduces this.

Question is, what in this code could be leading to this issue? Also, if anyone suspects this is a Firefox 1.5 on Win XP bug, do you have any suggestions for how to search out the real truth on this deep matter? We have several large prayer wheels standing by, powered by Makita 9V cordless drills with extra batteries donated by the Mill Valley Green Gulch Zen Center, ready to make thousands of revolutions on your behalf in return for any wisdom you would be willing to bestow.

Here's the wrapper code:

#!/usr/bin/perl -T use strict; use warnings; BEGIN { push @INC, "."; } use Min; my $minimal_uploader = Min->new(); $minimal_uploader->run();

And with our most humble obiences, here's where most of the action is:

#!/usr/bin/perl -T package Min; use base 'CGI::Application'; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use HTML::Template; use strict; use warnings; $ENV{'PATH'} = ''; sub setup { my $self = shift; $self->start_mode('list_mode'); $self->mode_param('rm'); $self->run_modes( 'list_mode' => \&list, 'save_mode' => \&save ); } sub list { my $self = shift; my $q = $self->query(); $q->param('rm', 'save_mode'); # hack? my $template = HTML::Template->new(filename => 'templates/test.tmp +l'); my $output = $q->b("Field1: ") . $q->textfield(-name=>'field1',-si +ze=>20) . $q->p() . $q->b("MP3:") . $q->filefield(-name=>'mymp3', -size=> +20) . $q->p() . $q->b("Field2:") . $q->textarea(-name=>'field2',-rows +=>4,-cols\ =>20); $output = $q->start_multipart_form() . $q->hidden(-name=>'rm',-value=>'save_mode') . $output . $q->br() . $q->submit(-label=>'Upload') . $q->end_form(); $template->param(LISTFORM => $output); return $template->output; } sub save { my $self = shift; my $q = $self->query(); my $field1 = $q->param('field1'); my $mp3 = $q->param('mymp3'); my $field2 = $q->param('field2'); # actual "SAVE" code has been deleted for this perlmonks post. # note this part does NOT use the template file # yes we aren't printing out the mp3 filename. # in the real code the file gets uploaded and saved # to disk, but not with Firefox 1.5 on Win XP. my $stuff; ($stuff = <<" END_STUFF")=~s/^\s+//gm; <html> <body> <p>field1 is $field1</p> <p>field2 is $field2</p> </html> END_STUFF return $stuff; } 1;

And here's a simple HTML::Template file being used for this test:

<html> <head> <title>testing</title> </head> <body> <TMPL_VAR NAME="LISTFORM"> </body> </html>

Replies are listed 'Best First'.
Re: CGI::Application and start_multipart_form stumper
by cees (Curate) on Dec 06, 2005 at 18:55 UTC

    Are you sure you are not just getting bit by the sticky fields in CGI.pm?

    $q->param('rm', 'save_mode'); # hack?

    If 'rm' already has a value, then CGI.pm will not overwrite it unless you also pass the -override => 1 option.

    $q->param(-name => 'rm', -value => 'save_mode' -override => 1);

    You can easily check this by looking at the generated HTML and making sure the runmode is what you expect it to be.

Re: CGI::Application and start_multipart_form stumper
by Belgarion (Chaplain) on Dec 06, 2005 at 05:13 UTC

    In list(), I tested just the form generation code and it doesn't look like it's producing valid HTML.

    The code I used was:

    #!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new(); my $output = $q->b("Field1: ") . $q->textfield(-name=>'field1',-size=> +20) . $q->p() . $q->b("MP3:") . $q->filefield(-name=>'mymp3', -size=>20) . $q->p() . $q->b("Field2:") . $q->textarea(-name=>'field2',-rows=>4,-cols=>20 +); $output = $q->start_multipart_form() . $q->hidden(-name=>'rm',-value=>'save_mode') . $output . $q->br() . $q->submit(-label=>'Upload') . $q->end_form(); print $output; exit; __DATA__ <form method="post" action="/./html.pl" enctype="multipart/form-data"> <input type="hidden" name="rm" value="save_mode" /><b>Field1: </b><in +put type="text" name="field1" tabindex="1" size="20" /><p /><b>MP3:< +/b><input type="file" name="mymp3" tabindex="2" size="20" /><p /><b> +Field2:</b><textarea name="field2" tabindex="3" rows="4" cols="20"></ +textarea><br /><input type="submit" tabindex="4" name=".submit" value +="Upload" /><div></div> </form>

    As you can see, there is no closing form tag. (Also, some of the fields are in XHTML rather than HTML, so that could be causing problems as well.) I don't have a full answer at the moment, but Firefox 1.5 might be more strict about valid HTML.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://514320]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-12-08 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.