Here's code for a simple, single-threaded HTTP server which you can use to debug your perl CGI scripts. All requests will be directed to your code. I've tested it with file uploads, and it seems to work just fine.

You can also use it to save requests to a file which can be useful for generating the input files required to test your CGI scripts off-line. See the comment near the end of the main loop.

To use, insert your perl code in the call_cgi routine. It will listen on port 9000 (which can be changed by supplying a port number on the command line.) You'll also have change the ACTION parameter of your FORM to direct requests to this server (e.g. ACTION="http://localhost:9000/".)

#!/usr/bin/perl # # quick and dirty signle threaded CGI executive # can also be used to capture requests use strict; use warnings; use IO::Socket::INET; use IO::String; my $port = shift(@ARGV) || 9000; my $listen = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => $port, Proto => 'tcp', ReuseAddr => 1 ); unless ($listen) { die "unable to listen on port $port: $!\n" }; while (1) { print STDERR "waiting for connection on port $port\n"; my $s = $listen->accept(); open(STDOUT, ">&=".fileno($s)); open(STDIN, "<&=".fileno($s)); my ($req, $content); delete $ENV{CONTENT_LENGTH}; { local ($/) = "\r\n"; while (<STDIN>) { $req .= $_; chomp; # print STDERR "got: $_\n"; last unless /\S/; if (/^GET\s*(\S+)/) { $ENV{REQUEST_METHOD} = 'GET'; (my $qs = $1) =~ m/\?(.*)/; $ENV{'QUERY_STRING'} = $1; } elsif (/^POST/) { $ENV{REQUEST_METHOD} = 'POST'; $ENV{'QUERY_STRING'} = ''; } elsif (/^Content-Type:\s*(.*)/) { $ENV{CONTENT_TYPE} = $1; } elsif (/^Content-Length:\s*(.*)/) { $ENV{CONTENT_LENGTH} = $1; } } } if (my $size = $ENV{CONTENT_LENGTH}) { $content = ''; while (length($content) < $size) { my $nr = read(STDIN, $content, $size-length($content), length($content)); die "read error" unless $nr; } } # can save $req, $content here: # open(F, ">request"); print F $req, $content; close(F); close(STDIN); # n.b.: does not close socket tie *STDIN, 'IO::String', $content; undef @CGI::QUERY_PARAM; call_cgi(); untie *STDIN; close(STDOUT); close($s); } sub call_cgi { print "HTTP/1.0 200\r\n"; # your CGI code goes here, example code follows use CGI; my $q = new CGI(); print $q->header(-type => 'text/plain'); print "Color = ", $q->param('color'), "\n"; my $file = $q->param('myfile'); print "myfile = ", $file, "\n"; print "myfile contents:\n"; while (<$file>) { print; } print "end of file\n"; }

In reply to Re: CGI.pm debugging with TYPE=FILE & ENCTYPE="multipart/form-data" by pc88mxer
in thread CGI.pm debugging with TYPE=FILE & ENCTYPE="multipart/form-data" by jae_63

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.