2016-04-08 1 views
1

wavファイルの生データ(リーフヘッダなし)のサーバへのRFC 1867アップロードが必要です。私はこれを行うためにperlスクリプトを書いています。RFC1867ファイルのperlからのアップロード

my $wav = new Audio::Wav; 
my $read = $wav->read($wavtoupload); 
my $rawdata = $read->read_raw($read->length()); #raw data w/o riff 

my $url = "http://thehost.com"; 
my $req = HTTP::Request->new(); 
$req = POST $url, 
    Referer => 'hxxp://www.mydomain.com/some.php3', 
    Accept_Language => 'en-us', 
    Content_Type => 'form-data', 
    Accept_Encoding => 'wav', 
    User_Agent => 'Mozilla/4.0', 
    Host => 'www.mydomain.com', 
    Connection => 'Keep-Alive', 
    Content => [ $rawdata ]; 
print $req->as_string; 

my $ua = LWP::UserAgent->new; 
my $resp = $ua->request($req); 

if ($resp->is_success) { 
    #... 
} 

http://search.cpan.org/~ether/HTTP-Message-6.11/lib/HTTP/Request/Common.pmによると::

RFC 1867あなたに指定されている「POSTメソッドは、フォームベースのファイルアップロードのために使用さマルチパート/フォームデータコンテンツをサポートし、このように見えますリクエストヘッダーの1つとして 'form-data'のコンテンツタイプを指定することによって、このコンテンツフォーマットをトリガーします。

しかし、サーバーはContent_Type = "form-data ="を受信して​​いますが、 "multipart/form-data"を期待しているので、サーバーが好きではありません。また、Content_Lengthは0で、ボディはアップロードされていません。

私は間違っていますか?すべてのHTTP要求がそのデータを整理する方法であるあなたは間違ってやっている

print $req->as_string; 

ことの一つは、あなたがキー/値のペアにデータを組み立てていないです、:ありがとう

+0

http://stackoverflow.com/questions/21273071/how-does-rfc-2854-obsolete-rfc-1867は読む価値があります – Quentin

+0

これは役に立たないと思います。 RFC1867のアップロード –

答えて

0

あなたはの出力を投稿しませんでした。

またCONTENT_LENGTHは、すべてのデータは、このように値がない、キー/値ペアのキー一部として使用され、そしていかなる値は、コンテンツの長さを有していないためだ0

あります0

のにもかかわらず、サーバはヘッダを受信すべきではありません。

Content_Type = "form-data" 

生の要求のために、私が手:

POST http://thehost.com 
Connection: Keep-Alive 
Accept-Encoding: wav 
Accept-Language: en-us 
Host: www.mydomain.com 
Referer: hxxp://www.mydomain.com/some.php3 
User-Agent: Mozilla/4.0 
Content-Length: 82 
Content-Type: multipart/form-data; boundary=xYzZY 

--xYzZY 
Content-Disposition: form-data; name="mydata" 

hello world 
--xYzZY-- 

明らかにわかるように、Content-Typeヘッダーはmultipart/form-dataです。これは、

のコンテンツタイプをリクエストヘッダーの1つとして指定することによってトリガーします。

つまり、perlモジュールは、実際の要求にヘッダContent-Type: multipart/form-dataを作成します。ここで

は、私が使用したコードです:

use strict; 
use warnings; 
use 5.020; 
use Data::Dumper; 
use HTTP::Message; 
use HTTP::Request::Common; 

my $rawdata = "hello world"; 
my $url = "http://thehost.com"; 

my $requ = POST $url, 
    Referer => 'hxxp://www.mydomain.com/some.php3', 
    Accept_Language => 'en-us', 
    Content_Type => 'form-data', 
    Accept_Encoding => 'wav', 
    User_Agent => 'Mozilla/4.0', 
    Host => 'www.mydomain.com', 
    Connection => 'Keep-Alive', 
    Content => [ mydata => $rawdata ]; 

say $requ->as_string; 

次のキー/値のペアvを指定するとの違いを示すだけの値を指定:。

use strict; 
use warnings; 
use 5.020; 
use Data::Dumper; 
use HTTP::Message; 
use HTTP::Request::Common; 

my $rawdata = "hello world"; 
my $url = "http://thehost.com"; 

my $requ = POST $url, 
    Referer => 'hxxp://www.mydomain.com/some.php3', 
    Accept_Language => 'en-us', 
    Content_Type => 'form-data', 
    Accept_Encoding => 'wav', 
    User_Agent => 'Mozilla/4.0', 
    Host => 'www.mydomain.com', 
    Connection => 'Keep-Alive', 
    Content => [ 
     mykey => 'myValue', 
     $rawdata, 
    ]; 


say $requ->as_string; 

--output:-- 
POST http://thehost.com 
Connection: Keep-Alive 
Accept-Encoding: wav 
Accept-Language: en-us 
Host: www.mydomain.com 
Referer: hxxp://www.mydomain.com/some.php3 
User-Agent: Mozilla/4.0 
Content-Length: 142 
Content-Type: multipart/form-data; boundary=xYzZY 

--xYzZY 
Content-Disposition: form-data; name="mykey" #<===HERE 

myValue #<======AND HERE 
--xYzZY 
Content-Disposition: form-data; name="hello world" #<===COMPARED TO 

     #<======COMPARED TO 
--xYzZY-- 

裸値が使用されてしまいます値として存在しないため、Content-Lengthは0になります。docsによると、ここでファイルのアップロードを指定する必要がありますどのように

されています:

use strict; 
use warnings; 
use 5.020; 
use Data::Dumper; 
use HTTP::Message; 
use HTTP::Request::Common; 

my $rawdata = "hello world"; 
my $url = "http://thehost.com"; 

my $requ = POST $url, 
    Referer => 'hxxp://www.mydomain.com/some.php3', 
    Accept_Language => 'en-us', 
    Content_Type => 'form-data', 
    Accept_Encoding => 'wav', 
    User_Agent => 'Mozilla/4.0', 
    Host => 'www.mydomain.com', 
    Connection => 'Keep-Alive', 
    Content => [ 
     mykey => 'myValue',   #some other key/value pair 
     cool_sounds => [undef,  #file upload key/value pair 
         'myfile.wav', 
         Content => $rawdata 
         ] 

    ]; 


say $requ->as_string; 

--output:-- 
POST http://thehost.com 
Connection: Keep-Alive 
Accept-Encoding: wav 
Accept-Language: en-us 
Host: www.mydomain.com 
Referer: hxxp://www.mydomain.com/some.php3 
User-Agent: Mozilla/4.0 
Content-Length: 176 
Content-Type: multipart/form-data; boundary=xYzZY 

--xYzZY 
Content-Disposition: form-data; name="mykey" 

myValue 
--xYzZY 
Content-Disposition: form-data; name="cool_sounds"; filename="myfile.wav" 

hello world 
--xYzZY-- 

ファイルセクション用のContent-Typeを設定する必要がある場合、あなたはこのようにそれを行うことができます。

use strict; 
use warnings; 
use 5.020; 
use Data::Dumper; 
use HTTP::Message; 
use HTTP::Request::Common; 

my $rawdata = "hello world"; 
my $url = "http://thehost.com"; 

my $requ = POST $url, 
    Referer => 'hxxp://www.mydomain.com/some.php3', 
    Accept_Language => 'en-us', 
    Content_Type => 'form-data', 
    Accept_Encoding => 'wav', 
    User_Agent => 'Mozilla/4.0', 
    Host => 'www.mydomain.com', 
    Connection => 'Keep-Alive', 
    Content => [ 
     mykey => 'myValue',   #some other key/value pair 
     cool_sounds => [undef,  #file upload key/value pair 
         'myfile.wav', 
         'Content-Type' => 'audio/x-wav', 
         Content => $rawdata 
         ] #=>If one of the values in the $form_ref is an array reference... 

    ]; 


say $requ->as_string; 

--output:-- 
OST http://thehost.com 
Connection: Keep-Alive 
Accept-Encoding: wav 
Accept-Language: en-us 
Host: www.mydomain.com 
Referer: hxxp://www.mydomain.com/some.php3 
User-Agent: Mozilla/4.0 
Content-Length: 203 
Content-Type: multipart/form-data; boundary=xYzZY 

--xYzZY 
Content-Disposition: form-data; name="mykey" 

myValue 
--xYzZY 
Content-Disposition: form-data; name="cool_sounds"; filename="myfile.wav" 
Content-Type: audio/x-wav 

hello world 
--xYzZY-- 

そして、あなたは他の何かにファイルセクションの内容-処分を設定する必要がある場合:

use strict; 
use warnings; 
use 5.020; 
use Data::Dumper; 
use HTTP::Message; 
use HTTP::Request::Common; 

my $rawdata = "hello world"; 
my $url = "http://thehost.com"; 

my $requ = POST $url, 
    Referer => 'hxxp://www.mydomain.com/some.php3', 
    Accept_Language => 'en-us', 
    Content_Type => 'form-data', 
    Accept_Encoding => 'wav', 
    User_Agent => 'Mozilla/4.0', 
    Host => 'www.mydomain.com', 
    Connection => 'Keep-Alive', 
    Content => [ 
     mykey => 'myValue',   #some other key/value pair 
     cool_sounds => [undef,  #file upload key/value pair 
         'myfile.wav', 
         'Content-Type' => 'audio/x-wav', 
         'Content-Disposition' => 'file', 
         Content => $rawdata 
         ] 

    ]; 


say $requ->as_string; 

--output:-- 
POST http://thehost.com 
Connection: Keep-Alive 
Accept-Encoding: wav 
Accept-Language: en-us 
Host: www.mydomain.com 
Referer: hxxp://www.mydomain.com/some.php3 
User-Agent: Mozilla/4.0 
Content-Length: 155 
Content-Type: multipart/form-data; boundary=xYzZY 

--xYzZY 
Content-Disposition: form-data; name="mykey" 

myValue 
--xYzZY 
Content-Disposition: file 
Content-Type: audio/x-wav 

hello world 
--xYzZY-- 

この場合、ファイルセクションの名前属性はありません。

ところで、use statementsを投稿していないと助けてくれたと思いますか?それをしないでください。

+0

を「すべてのHTTP要求がどのようにデータを整理するか」を望むサーバーを制御できません。これは真実ではありません。標準のHTMLフォームからトリガーできる2つの標準要求エンコーディングデータ。 – Quentin

+0

ありがとう、それは多くの助けになります。私の問題は、単に「コンテンツ」セクションが適切な形式でないことでした。 –