move information from HTTP::Request to CGI.pm

M

Miroslav Suchy

Hello,
I have following code:

my $daemon = HTTP::Daemon->new(
ReuseAddr => 1,
LocalAddr => 'mydomain.com',
LocalPort => 8888,
) or die "Cannot start daemon: $!\n";
while (my $conn = $daemon->accept()) {
while (my $req = $conn->get_request()) {
if ($req->method() eq 'GET') { # I do not use GET
$q = CGI->new($url->query());
} elsif ($req->method() eq 'POST') { # I only use POST method
$q = CGI->new($req->content);
open FF, ">/tmp/debug"; #output to FF just for debug
print FF $req->content;
close FF;
}
print STDERR "params: ", join(',', $q->param), "\n";
}
}

And it does not work as I expect. This is content of file "/tmp/debug":
-----------------------------18004688081790386843384315354
Content-Disposition: form-data; name="id"

6
-----------------------------18004688081790386843384315354
Content-Disposition: form-data; name="pw"

aa
-----------------------------18004688081790386843384315354--

I expect following output from script:
params: id,pw
but I get this output:
params: -----------------------------18004688081790386843384315354
Content-Disposition: form-data, name

What I'm doing wrong? Thank you.

Miroslav Suchy
 
M

Miroslav Suchy

Hello again,
I was hard working on my problem (I've HTTP::Request and I to move its
iformation to CGI object) and I come to the smallest code. But it still does not
work and I do not know where can be a problem.

8<--cut
#!/usr/bin/perl
use strict;
use HTTP::Daemon ();
use CGI ();

my $daemon = HTTP::Daemon->new(
LocalAddr => 'my.domain.com',
LocalPort => 8888,
) or die "Cannot start daemon: $!\n";
my $i;
while (my $conn = $daemon->accept()) {
while (my $req = $conn->get_request()) {
my $url = $req->uri();
$ENV{'REQUEST_METHOD'}=$req->method();
$ENV{'CONTENT_TYPE'}=$req->header('Content-Type');
$ENV{'CONTENT_LENGTH'}=$req->header('Content-Length');
my $filename="/dev/shm/tmp.$$.".++$i;
print STDERR "$filename\n";
open INPUT, ">$filename" or die; # prepare stdin for CGI.pm
binmode INPUT;
print INPUT $req->content;
close INPUT;
open STDIN, "<$filename" or die; # redirect stdin
my $q = CGI->new;
my $id=$q->param('id');
my $ff=$q->param('file');
print STDERR "$id-$ff----\n";
my $res = HTTP::Response->new();
$res->header('Content-Type'=> 'text/html');
my $content=$q->start_html.$q->end_html;
$res->content_length(length($content));
$res->content($content);
$conn->send_response($res);
close STDIN;
}
$conn->close();
}
8<---cut

In first while loop is everything correct. But second and all additional loops
have the same content of $q as in first loop!!!!
STDERR look like this:
/dev/shm/tmp.18353.1
4159616-4159616.jpg----
/dev/shm/tmp.18353.2
4159616-4159616.jpg----
/dev/shm/tmp.18353.3
4159616-4159616.jpg----

It means $q->param('id') returns id from the first file, which contain:
$ head -n5 /dev/shm/tmp.18353.1
--xYzZY
Content-Disposition: form-data; name="id"

4159616
--xYzZY
but it should return id from the second file, which contain:
$ head -n5 /dev/shm/tmp.18353.2
--xYzZY
Content-Disposition: form-data; name="id"

4159620
--xYzZY

If I try to dump STDIN after the line:
open STDIN, "<$filename" or die; # redirect stdin
it is the same as content of file $filename
But CGI module somehow remember its state from first reading!!
How can I force CGI to re-read STDIN?
Or make I something wrong?

BTW: The request for this daemon is created using this code:
....
my $ua = LWP::UserAgent->new;
my $response = $ua->post('http://my.domain.com:8888/',
[
id => $id,
file => ["$path$id.jpg"] ,
],
'Content_Type' => 'form-data',
);

Thanks in advance for any suggestion

Miroslav Suchy
 
M

Miroslav Suchy

Hello again,
I solved my problem. For archive purpose:
If you want to read STDIN again with CGI you must undef @CGI::QUERY_PARAM (Yeah,
that one with comment "Other globals that you shouldn't worry about." in CGI.pm :) )

Miroslav Suchy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top