0

这篇文章是关于与LWP GET 大文件下载相关的工作。该帖子是关于尝试错误地在标头中传递参数时 LWP 的错误。现在我发布我所做的更改以及我如何尝试调试该方法。对于那些对 POST 与 GET 标头形成以及服务器在使用 CGI 包时接收到的内容感兴趣的人来说,这个讨论应该非常有用。这不是在网上很容易找到的信息。

这是我的客户端代码片段:

my $bytes_received = 0;  # vars used below are set prior to this point
my $filename = $opt{t}."/$srcfile";
open (FH, ">", "$filename") or $logger->error( "Couldn't open $filename for writing: $!" );
my $ua = LWP::UserAgent->new();
my $target = $srcfile;
my $res = $ua->get(
    $url,
    ':content_cb' => \&callback,
    'api' => 'olfs',  # Note attempted use of different types of quotes had no impact
    "cmd" => 'rfile',
    "target" => $target,
    "bs" => $bs 
    );

    print $logger->info("$bytes_received bytes received");

sub callback{
    my($chunk, $res) = @_;
    $bytes_received += length($chunk);
    print FH $chunk;
}

这是服务器片段(cgi 脚本):

my $query = new CGI;
my $rcvd_data = Dumper($query);
print $rcvd_data;

这是 GET 的输出:

$VAR1 = bless( {
                 '.parameters' => [],
                 'use_tempfile' => 1,
                 '.charset' => 'ISO-8859-1',
                 '.fieldnames' => {},
                 'param' => {},
                 '.header_printed' => 1,
                 'escape' => 1
               }, 'CGI' );

这是一个带有 POST 请求的客户端:

my $ua = new LWP::UserAgent();
local $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1;

    my $req = 
    POST 
    $url,
    'Content_Type' => 'form-data', 
    'Content' => { 
        "api" => 'olfs',
        "cmd" => 'wfile',
        "target" => $target,
        "tsize" => $file_size,
        "bs" => $bs,
        "filename" => [ $file ] };

# HTTP::Message calls set_content, which appears to set the subroutine for content
# LWP::UserAgent 
# LWP::Protocol::file::request sends content in chunks
#

    $req->content( $req->content() );
    $logger->info("Uploading: $file");
    my $resp = $ua->request($req);

这是服务器上的输出,就像以前一样,但现在来自 POST:

         '.parameters' => [
                            'cmd',
                            'bs',
                            'api',
                            'target',
                            'filename',
                            'tsize'
                          ],
         'use_tempfile' => 1,
         '.tmpfiles' => {
                          '*Fh::fh00001random23' => {
                                                      'info' => {
                                                                  'Content-Type' => 'text/plain',
                                                                  'Content-Disposition' => 'form-data; name="filename"; filename="random23"'
                                                                },
                                                      'name' => bless( do{\(my $o = '/usr/tmp/CGItemp33113')}, 'CGITempFile' ),
                                                      'hndl' => bless( \*Fh::fh00001random23, 'Fh' )
                                                    }
                        },
         '.charset' => 'ISO-8859-1',
         '.fieldnames' => {},
         'param' => {
                      'cmd' => [
                                 'wfile'
                               ],
                      'bs' => [
                                'buffer1'
                              ],
                      'api' => [
                                 'olfs'
                               ],
                      'target' => [
                                    'random23'
                                  ],
                      'tsize' => [
                                   '1073741824'
                                 ],
                      'filename' => [
                                      $VAR1->{'.tmpfiles'}{'*Fh::fh00001random23'}{'hndl'}
                    },
         'escape' => 1,
         '.header_printed' => 1
       }, 'CGI' );

简而言之,您可以在 POST 转储中看到“key”/“value”对,即“target => random23”。在 GET 转储中,我没有从我在客户端提交的内容中找到任何键或值。可以解释一下,或者我需要做什么才能在 CGI 脚本中提取键/值对?

4

1 回答 1

1

您将表单变量作为 HTTP 标头传递。

就像我之前提到的,如果你想构建一个 url,你可以使用URI

$url = URI->new($url);
$url->query_form(
   api    => 'olfs',
   cmd    => 'rfile',
   target => $target,
   bs     => $bs,
);
于 2013-10-31T20:58:08.583 回答