At wits end! LWP and IIS(?)

C

Calvine Chew

I've been trying to get a script to post to another script for some time now
but to no avail! Sending the LWP POST request to the receiving script and
printing out the request/results, I get the following:

###################
POST http://llc.compsys.org/up.cgi Content-Length: 177 Content-Type:
multipart/form-data; boundary=xYzZY --xYzZY Content-Disposition: form-data;
name="upload"; filename="success.txt" Content-Length: 36 Content-Type:
text/plain IT WORKED!!! 5th May 2004 11:24am --xYzZY--

It didn't work. 501 (Not Implemented) syntax error Content-Type: text/plain
Client-Date: Wed, 05 May 2004 03:37:10 GMT Client-Warning: Internal response
501 syntax error

501 syntax error
###################
Both sending and receiving scripts are on the same server. I'm a relative
newbie to Perl so I desperately need help on this! Any URLs or FAQs I can
refer to to get more info on my troubles? Is it a Perl problem or a
webserver one?

Much appreciated in advance!


Below is my POSTer code:
###################
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use CGI qw:)standard);
use lib 'modules';
use LWP::UserAgent 2.031;
use HTTP::Request::Common 1.22 qw/POST/;
my $ua= LWP::UserAgent->new();
my $url = "http://llc.compsys.org/up.cgi";
my $fullpath_filename = param("upload");
my $filename = lc($fullpath_filename);
$filename =~ s/.*[\/\\](.*)/$1/;

print (header());
print (start_html( -title => 'Upload' ));

if (!$filename) {
print ("No file specified.<br>\n");
print qq~
<form action="upload.cgi" method="post" enctype="multipart/form-data">
File upload:
<input type="file" name="upload" size="60"><br>
<br>
<input type="submit" name="Submit" value="Upload">
</form>
</body></html>
~;
}
else {
open UPLOADFILE, ">$filename";
binmode(UPLOADFILE);
while ( <$fullpath_filename> ){ print UPLOADFILE; }
close UPLOADFILE;

print "$filename uploaded!<br>";
my $fullpath = $ENV{'DOCUMENT_ROOT'}."/cgi-bin/$filename";
print "FULLPATH = $fullpath<br>";
print "Above file exists!<br><br>" if -e $fullpath;
print "Above file does not exist!<br><br>" if !-e $fullpath;

my $request=POST $url , Content_Type=>'form-data',
Content => [upload=>[$filename]];
print $request->as_string; print "<br><br>";

my $results=$ua->request($request);
if($results->is_success){
print "It's good!\n";
print $results->as_string; print "<br><br>";
} else {
print "It didn't work.\n";
print $results->as_string; print "<br><br>";
print $results->status_line();
}
###################

This is my server code:
###################
#!/usr/bin/perl

use CGI qw:)standard);

$filename = param("upload");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = param("upload");

print (header());
print (start_html( -title => 'Upload' ));

if (!$filename) {
print ("No file specified.<br>\n");
}
else {
$filename = lc($filename);
open UPLOADFILE, ">$filename";
binmode(UPLOADFILE);
while ( <$upload_filehandle> ){ print UPLOADFILE; }
close UPLOADFILE;
print ("<b>$filename</b> upload successful!<br>\n");
}
print ("</BODY>\n</HTML>");
###################
 
G

gnari

Calvine Chew said:
I've been trying to get a script to post to another script for some time now
but to no avail! Sending the LWP POST request to the receiving script and
printing out the request/results, I get the following:

###################
POST http://llc.compsys.org/up.cgi Content-Length: 177 Content-Type:
multipart/form-data; boundary=xYzZY --xYzZY Content-Disposition: form-data;
name="upload"; filename="success.txt" Content-Length: 36 Content-Type:
text/plain IT WORKED!!! 5th May 2004 11:24am --xYzZY--

It didn't work. 501 (Not Implemented) syntax error Content-Type: text/plain
Client-Date: Wed, 05 May 2004 03:37:10 GMT Client-Warning: Internal response
501 syntax error

501 syntax error
###################
Both sending and receiving scripts are on the same server. I'm a relative
newbie to Perl so I desperately need help on this! Any URLs or FAQs I can
refer to to get more info on my troubles? Is it a Perl problem or a
webserver one?


this means that your original upload (from browser
to upload.cgi) worked, but the second one (from
upload.cgi to up.cgi) failed, probably because it
was not correctly done.

did you try to upload directly to the second script?
gnari
 
C

Calvine Chew

gnari said:
this means that your original upload (from browser
to upload.cgi) worked, but the second one (from
upload.cgi to up.cgi) failed, probably because it
was not correctly done.

did you try to upload directly to the second script?
gnari
Yes indeed. The 2nd script (up.cgi) works perfectly and is able to save the
file (I verified this via a multipart/form-data html form to up.cgi). I
checked the server log and there is no reference to any 501 errors or even
requests to up.cgi!!

I conclude that this means the request is not even going out. I understand
that the header Client-Warning is inserted by LWP. Could this mean that the
script is failing at upload.cgi (1st script)?
 
G

gnari

Calvine Chew said:
gnari said:
[problems with relayed upload]

did you try to upload directly to the second script?
gnari
Yes indeed. The 2nd script (up.cgi) works perfectly and is able to save the
file (I verified this via a multipart/form-data html form to up.cgi). I
checked the server log and there is no reference to any 501 errors or even
requests to up.cgi!!

I conclude that this means the request is not even going out. I understand
that the header Client-Warning is inserted by LWP. Could this mean that the
script is failing at upload.cgi (1st script)?

yes. it means that the LWP::UserAgent request call (or
the HTTP::Request::Common POST call) is failing

I have never used these for file upload, and can't be bothered to do
research,
but I suspect that you at least need to specify multipart/form-data in
the POST call, and probably some more work. an upload post is in a pretty
different format that regular posts, and I am not sure
HTTP::Request::Common
supports it, thus the "501 (Not Implemented)" message.

good luck

gnari
 
G

Gisle Aas

gnari said:
I have never used these for file upload, and can't be bothered to do
research, but I suspect that you at least need to specify
multipart/form-data in the POST call, and probably some more
work. an upload post is in a pretty different format that regular
posts, and I am not sure HTTP::Request::Common supports it, thus the
"501 (Not Implemented)" message.

HTTP::Request::Common certainly do support file uploads.
 
G

gnari

Gisle Aas said:
HTTP::Request::Common certainly do support file uploads.

so it does!

and after a quick look at the OP's code, I see that the filename
passed to POST is not the same that was tested with -e.
in addition, it looks to me that unsafe assumptions about current
directory are made.

gnari
 
C

Calvine Chew

gnari said:
so it does!

and after a quick look at the OP's code, I see that the filename
passed to POST is not the same that was tested with -e.
in addition, it looks to me that unsafe assumptions about current
directory are made.

gnari
Hi, apologies, the code I pasted is indeed wrong. However, I changed the
POST to reflect the correct variable, ie

my $fullpath = $ENV{'DOCUMENT_ROOT'}."/cgi-bin/$filename";
my $request=POST $url , Content_Type=>'form-data',
Content => [upload=>[$fullpath]];
print $request->as_string; print;

But strangely enough, in the POST request headers, the field "upload" still
shows $filename, instead of $fullpath, in other words, LWP stripped off the
path...

Or did I do something wrong again?

Appreciate the help accorded!!
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top