Beavis has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use warnings; use strict; use Socket; my $buffersize = 2 * 1024 * 1024; my $host = "127.0.0.1"; my $path = "/debug.php"; my $url = "http://".$host.$path; my $local_file = "smallishtestfile.tar.gz"; my $user = "cardman"; my $pass = "yvan eht nioj"; my $serverid = 666; my $upfile = shift || 'F:\\skript\\'.$local_file; system("cls"); print STDOUT "Starting upload to ".$url."\n\n"; $| = 1; my ($iaddr, $paddr, $proto); $iaddr = inet_aton($host); $paddr = sockaddr_in(80, $iaddr); $proto = getprotobyname('tcp'); unless(socket(SOCK, PF_INET, SOCK_STREAM, $proto)) { die "Couldn't ini +t socket: $!"; } unless(connect(SOCK, $paddr)) { die "Couldn't connect: $!\n"; } my $boundary = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; my @data = ( "--".$boundary."", "Content-Disposition: form-data; name=\"username\"", "", "".$user."", "--".$boundary."", "Content-Disposition: form-data; name=\"password\"", "", "".$pass."", "--".$boundary."", "Content-Disposition: form-data; name=\"serverid\"", "", "".$serverid."", "--".$boundary."", "Content-Disposition: form-data; name=\"file\"; filename=\"".$loca +l_file."\"", "Content-Type: application/octet-stream", "", ); open (FH,"< $upfile") or die "$!\n"; binmode FH; my $data = join("\r\n", @data); my $length = 0; $length += length($data); # length of the data to be POST'ed $length += -s FH; # filesize $length += length($boundary); # boundary is added once more at the +end of all the file-chunks my @head = ( "POST ".$path." HTTP/1.1", "Host: ".$host."", "Content-Length: $length", "Connection: keep-alive", "Content-Type: multipart/form-data; boundary=".$boundary."", "", ); my $header = join("\r\n", @head).$data; select SOCK; $| = 1; binmode SOCK; print SOCK $header; while(sysread(FH, my $buf, $buffersize)) { if(length($buf) < $buffersize) { $buf = $buf."\r\n--".$boundary."--"; syswrite SOCK, $buf, length($buf); } else { syswrite SOCK, $buf, $buffersize; } } close FH; my @response = (<SOCK>); shutdown SOCK, 1; print STDOUT "Result:\n-------\n @response"; close SOCK;
HTTP/1.1 400 Bad Request Date: Tue, 21 Jun 2011 21:01:32 GMT Server: Apache Content-Length: 408 Connection: close Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>400 Bad Request</title> </head><body> <h1>Bad Request</h1> <p>Your browser sent a request that this server could not understand.<br /> Request header field is missing ':' separator.<br /> <pre> --zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz</pre> </p> <hr> <address>Apache Server at localhost Port 80</address> </body></html>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTTP-POST with IO::Socket -- Header problem
by Anonymous Monk on Jun 21, 2011 at 21:58 UTC | |
by onelesd (Pilgrim) on Jun 22, 2011 at 00:16 UTC | |
|
SOLVED: HTTP-POST with IO::Socket -- Header problem
by Beavis (Initiate) on Jun 22, 2011 at 12:07 UTC | |
by Corion (Patriarch) on Jun 22, 2011 at 12:14 UTC | |
by Anonymous Monk on Jun 23, 2011 at 09:58 UTC | |
by Corion (Patriarch) on Jun 23, 2011 at 10:13 UTC |