CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Upload with CURL FAIL

 



OpenSys
Novice

Apr 4, 2009, 4:59 PM

Post #1 of 10 (1608 views)
Upload with CURL FAIL Can't Post

Hello,

I trying to make a simple post file upload to one HTTP page using the CURL,
But do not work at all. My code freezes the script and then i need to stop it with ctrl+c. I tested a identical script build in PHP that's works fine and upload my file.
I need the do it with curl, because curl have options to limit the bandwidth.

Please helps.

My perl code:


#!/usr/bin/perl
use strict;
use warnings;
use File::stat;
use WWW::Curl::Easy;
use IO::File;
use FileHandle;

my $myfile = '/test.txt';

my @upload_headers = (
'Connection: Keep-Alive',
'Keep-Alive: 300',
'Referer: ',
'Expect: ',
'Content-Type: multipart/form-data'
);

my $user_agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20091107 Firefox/3.0'";

my $request_url = "http://www.mysite.net/upload/index.php";

sub builduploadfile {
my ($file)=@_;

my $fh = new FileHandle;
if ($fh->open("< $file")) {
undef $fh;
}

return $fh;

}



my @extra_vars = ( 'ligado'=> '1', 'file'=>$myfile );
#my $extra_vars2 = "ligado=1&file=$myfile";

my $curl = new WWW::Curl::Easy;

$curl->setopt(CURLOPT_HEADER,0);
$curl->setopt(CURLOPT_HTTPHEADER, \@upload_headers);
$curl->setopt(CURLOPT_USERAGENT, $user_agent);
$curl->setopt(CURLOPT_URL, $request_url);
$curl->setopt(CURLOPT_POST, 1);
$curl->setopt(CURLOPT_POSTFIELDS, \@extra_vars);
$curl->setopt(CURLOPT_FAILONERROR, 1);
$curl->setopt(CURLOPT_UPLOAD, 1);
$curl->setopt(CURLOPT_INFILE, builduploadfile($myfile));
$curl->setopt(CURLOPT_INFILESIZE,length(builduploadfile($myfile)));
$curl->setopt(CURLOPT_CUSTOMREQUEST, "POST");
$curl->setopt(CURLOPT_SSL_VERIFYPEER, 0);
$curl->setopt(CURLOPT_SSL_VERIFYHOST, 0);
$curl->setopt(CURLOPT_FOLLOWLOCATION, 1);
#$curl->setopt(CURLOPT_AUTOREFERER, 1);
$curl->setopt(CURLOPT_COOKIESESSION, 1);
$curl->setopt(CURLOPT_COOKIEFILE, "cookiefile.txt");

$curl->setopt(CURLOPT_TIMEOUT,30);
$curl->setopt(CURLOPT_VERBOSE, 1);

my $response_body;

open (my $fileb, ">", \$response_body);
$curl->setopt(CURLOPT_WRITEDATA,$fileb);

my $retcode = $curl->perform;

my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);

if ($retcode == 0)
{
print("Transfer went ok ($response_code)\n");
} else {
print("An error happened: ".$curl->strerror($retcode)." ($retcode)\n");
}


FishMonger
Veteran / Moderator

Apr 4, 2009, 9:04 PM

Post #2 of 10 (1598 views)
Re: [OpenSys] Upload with CURL FAIL [In reply to] Can't Post

I've never used that Curl module, but I do see several problems.


Code
if ($fh->open("< $file")) { 
undef $fh;
}

If the open call is successful (i.e. $fh is now defined), you undef $fh which essentially closes the file handle.


Code
my @extra_vars = ( 'ligado'=> '1', 'file'=>$myfile );

Here you're defining an array, but you're attempting to assign hash values. Which did you want, an array, a hash, an array of hashes?

Start by fixing those issues, then run your script under the debugger (i.e., the -d switch) to see what it's doing and where it hangs.


OpenSys
Novice

Apr 5, 2009, 1:30 AM

Post #3 of 10 (1592 views)
Re: [FishMonger] Upload with CURL FAIL [In reply to] Can't Post

Tanks for the help FishMonger,

As i read the "undef $fh;" close the file when the script exit or something like that, but i comment the line for now.
The "extra_vars" i remove it from array and set "my $extra_vars2 = "ligado=1&file=$myfile";"

now the script ends with:
The requested URL returned error: 403


OpenSys
Novice

Apr 5, 2009, 3:40 AM

Post #4 of 10 (1589 views)
Re: Upload with CURL FAIL [In reply to] Can't Post

Seams that it is not sending the" Content-Type: multipart/mixed; boundary " header

my apache log
ModSecurity: Multipart parsing error (init): Multipart: Boundary not found in C-T.


FishMonger
Veteran / Moderator

Apr 5, 2009, 6:18 AM

Post #5 of 10 (1586 views)
Re: [OpenSys] Upload with CURL FAIL [In reply to] Can't Post

Try changing
$curl->setopt(CURLOPT_HEADER,0);

to
$curl->setopt(CURLOPT_HEADER,1);


OpenSys
Novice

Apr 5, 2009, 8:38 AM

Post #6 of 10 (1580 views)
Re: [FishMonger] Upload with CURL FAIL [In reply to] Can't Post

Tanks, but do not work.

with this body I send the file, but the file not show up on server:


Content-Type: multipart/form-data; boundary=---------------------------69389555486682998970487794

-----------------------------69389555486682998970487794
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream

dfgdgdfgdfgddfgdfgd
dgdgdfgdgdggggggggdf
-----------------------------69389555486682998970487794
Content-Disposition: form-data; name="ligado"

1
-----------------------------6938955548


FishMonger
Veteran / Moderator

Apr 5, 2009, 9:26 AM

Post #7 of 10 (1579 views)
Re: [OpenSys] Upload with CURL FAIL [In reply to] Can't Post

Since I haven't used this module nor CURL itself, I don't think I'll be able to provide much help. I can't say for sure, but I doubt that others here on this forum have used this module, so it might be best to post your question on one of the other forums. I suggest:
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/


KevinR
Veteran


Apr 5, 2009, 10:18 AM

Post #8 of 10 (1576 views)
Re: [OpenSys] Upload with CURL FAIL [In reply to] Can't Post

I have never used the module or CURL either, but this sub needs to be changedL


Code
sub builduploadfile {  
my ($file)=@_;

my $fh = new FileHandle;
if ($fh->open("< $file")) {
undef $fh;
}

return $fh;

}


It looks like you need to return the contents of the file, so try this:


Code
sub builduploadfile {  
my ($file)=@_;
my $content;
my $fh = new FileHandle;
if ($fh->open("< $file")) {
$content = do {local $/; <$fh>};
undef $fh;
}
return $content;
}

-------------------------------------------------


(This post was edited by KevinR on Apr 5, 2009, 10:18 AM)


OpenSys
Novice

Apr 5, 2009, 11:02 AM

Post #9 of 10 (1573 views)
Re: [KevinR] Upload with CURL FAIL [In reply to] Can't Post

The option CURLOPT_INFILE require a file handle not the the file content.


KevinR
Veteran


Apr 5, 2009, 11:54 AM

Post #10 of 10 (1572 views)
Re: [OpenSys] Upload with CURL FAIL [In reply to] Can't Post


In Reply To
The option CURLOPT_INFILE require a file handle not the the file content.


OK.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives