#!/usr/bin/perl # I'd like to give credit to the following people since their code provided me # with enough clues to make this thing work # The author and all the contributors of POE, who's work made this possible, see below for links # http://poe.perl.org/?POE_Cookbook/CGI_Requests # Also the author of CGI_LIB, link below, it was his lib that with quite a bit of work # enabled me to create a HTTP server with multipart form data support # http://www.tneoh.zoneit.com/perl/CGI_LIB/CGI_LIB.pm use POE; use POE::Component::Server::HTTP; # Start an HTTP server. Run it until it's done, typically forever, # and then exit the program. POE::Component::Server::HTTP->new( Port => 2281, ContentHandler => { '/' => \&root_handler, '/post' => \&post_handler, } ); POE::Kernel->run(); exit 0; # Handle root-level requests. Populate the HTTP response with a CGI # form. sub root_handler { my ( $request, $response ) = @_; $response->code(RC_OK); local $/; open(ind,"; close ind; @script = split(/<\?[^\?>]|\?>/,$script); foreach(@script){ print " script = $_\n"; } $response->content_type("text/html; charset=iso-8859-1"); $response->content("@script"); return RC_OK; } # Handle simple CGI parameters. # # This code was contributed by Andrew Chen. It handles GET and POST, # but it does not handle %ENV-based CGI things. It does not handle # cookies, for instance. Neither does it handle file uploads. sub post_handler { my ( $request, $response ) = @_; # OK Parse data, did we get a POST or GET # The rest of this handler displays the values encapsulated by the # object. $response->code(RC_OK); #$outbuffer = $request->as_string; #$outbuffer .= "\n\n"; #$outbuffer .= $request->header('Content-Type'); if($request->header('Content-Type') =~ /multipart\/form-data/){ my($data) = $request->content; my($boundary) = $request->header('Content-Type') =~ /^.*boundary=(.*)$/; warn("boundary = $boundary \n ");#,$request->as_string); #my(@partsArray) = split(/--$boundary/, $submittedData); # @partsArray = splice(@partsArray, 1, (scalar(@partsArray) - 2)); #warn("parts = @partsArray"); @parts = split(/--$boundary/,$data); #$count = 0; #$tcount = 0; @parts = splice(@parts, 1, (scalar(@parts) - 2)); foreach(@parts){ if($_ =~ /filename/){ my($dump, $firstline, $fieldValue) = split(/[\r]\n/, $_, 3); next if $firstline =~ /filename=\"\"/; $firstline =~ s/^Content-Disposition: form-data; //; my(@columns) = split(/;\s+/, $firstline); foreach(@columns){ s/file//; s/name//; s/\=//; s/\"//g; } ($filevar,$filename) = @columns; if (scalar(@columns) > 1) { my($contenttype, $blankline); ($contenttype, $blankline, $filed) = split(/[\r]\n/, $fieldValue, 3); $contenttype =~ /^Content-Type: ([^\s]+)$/; print "ctype = $contenttype , \nfile contents = $filed"; } } } } if($request->header('Content-Type') =~ /^application\/x-www-form-urlencoded$/){ my($submittedData) = $request->content; warn("$submittedData"); my(@fields) = split('&', $submittedData); warn("@fields"); for (@fields) { tr/+/ /; my($fieldName, $fieldValue) = split('=', $_, 2); # The %xx hex numbers are converted to alphanumeric. $fieldName =~ s/%(..)/pack("C", hex($1))/eg; $fieldValue =~ s/%(..)/pack("C", hex($1))/eg; warn("$fieldName"); warn("$fieldValue"); push(@uqry, $fieldName); if($fieldValue eq ""){ push(@uqry, "null"); }else{ push(@uqry, "$fieldValue"); } } } $response->content($request->as_string); return RC_OK; }