纯Perl从另一个HTTP资源流式传输的最简单方法是什么? [英] What is the easiest way in pure Perl to stream from another HTTP resource?

查看:229
本文介绍了纯Perl从另一个HTTP资源流式传输的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中从另一个HTTP资源流式传输的最简单方法是什么(没有打开shell来卷曲和读取stdin)?我在这里假设我正在读取的HTTP资源是一个潜在的无限流(或者只是真的很长)

What is the easiest way (without opening a shell to curl and reading from stdin) in Perl to stream from another HTTP resource? I'm assuming here that the HTTP resource I'm reading from is a potentially infinite stream (or just really, really long)

推荐答案

HTTP :: Lite 请求方法允许您指定回调。

HTTP::Lite's request method allows you to specify a callback.


$ data_callback 参数(如果使用)是一种在接收数据时过滤数据或处理大型传输的方法。它必须是函数引用,并且将被传递:对进行回调的http请求的实例的引用,对将要添加到正文的当前数据块的引用,以及 $ cbargs 参数(可能是任何东西)。它必须返回对要添加到文档正文的数据的引用,或者undef。

The $data_callback parameter, if used, is a way to filter the data as it is received or to handle large transfers. It must be a function reference, and will be passed: a reference to the instance of the http request making the callback, a reference to the current block of data about to be added to the body, and the $cbargs parameter (which may be anything). It must return either a reference to the data to add to the body of the document, or undef.

然而,查找在源头,似乎子请求中的一个错误,因为似乎忽略了传递的回调。 似乎更安全,使用 set_callback

However, looking at the source, there seems to be a bug in sub request in that it seems to ignore the passed callback. It seems safer to use set_callback:

#!/usr/bin/perl

use strict;
use warnings;

use HTTP::Lite;

my $http = HTTP::Lite->new;
$http->set_callback(\&process_http_stream);
$http->http11_mode(1);

$http->request('http://www.example.com/');

sub process_http_stream {
    my ($self, $phase, $dataref, $cbargs) = @_;
    warn $phase, "\n";
    return;
}

输出:


C:\Temp> ht
connect
content-length
done-headers
content
content-done
data
done

它看起来像是一个回传传递给请求方法处理方式不同:

It looks like a callback passed to the request method is treated differently:

#!/usr/bin/perl

use strict;
use warnings;

use HTTP::Lite;

my $http = HTTP::Lite->new;
$http->http11_mode(1);

my $count = 0;
$http->request('http://www.example.com/',
    \&process_http_stream,
    \$count,
);

sub process_http_stream {
    my ($self, $data, $times) = @_;
    ++$$times;
    print "$$times====\n$$data\n===\n";
}

这篇关于纯Perl从另一个HTTP资源流式传输的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆