📄 nb.pm
字号:
package Net::HTTP::NB;# $Id: NB.pm,v 1.5 2001/08/28 03:03:42 gisle Exp $use strict;use vars qw($VERSION @ISA);$VERSION = "0.02";require Net::HTTP;@ISA=qw(Net::HTTP);sub sysread { my $self = $_[0]; if (${*$self}{'httpnb_read_count'}++) { ${*$self}{'http_buf'} = ${*$self}{'httpnb_save'}; die "Multi-read\n"; } my $buf; my $offset = $_[3] || 0; my $n = sysread($self, $_[1], $_[2], $offset); ${*$self}{'httpnb_save'} .= substr($_[1], $offset); return $n;}sub read_response_headers { my $self = shift; ${*$self}{'httpnb_read_count'} = 0; ${*$self}{'httpnb_save'} = ${*$self}{'http_buf'}; my @h = eval { $self->SUPER::read_response_headers(@_) }; if ($@) { return if $@ eq "Multi-read\n"; die; } return @h;}sub read_entity_body { my $self = shift; ${*$self}{'httpnb_read_count'} = 0; ${*$self}{'httpnb_save'} = ${*$self}{'http_buf'}; # XXX I'm not so sure this does the correct thing in case of # transfer-encoding tranforms my $n = eval { $self->SUPER::read_entity_body(@_); }; if ($@) { $_[0] = ""; return -1; } return $n;}1;__END__=head1 NAMENet::HTTP::NB - Non-blocking HTTP client=head1 SYNOPSIS use Net::HTTP::NB; my $s = Net::HTTP::NB->new(Host => "www.perl.com) || die $@; $s->write_request(GET => "/"); use IO::Select; my $sel = IO::Select->new($s); READ_HEADER: { die "Header timeout" unless $sel->can_read(10); my($code, $mess, %h) = $s->read_response_headers; redo READ_HEADER unless $code; } while (1) { die "Body timeout" unless $sel->can_read(10); my $buf; my $n = $s->read_entity_body($buf, 1024); last unless $n; print $buf; }=head1 DESCRIPTIONSame interface as C<Net::HTTP> but it will never try multiple readswhen the read_response_headers() or read_entity_body() methods areinvoked. This make it possible to multiplex multiple Net::HTTP::NBusing select without risk blocking.If read_response_headers() did not see enough data to complete theheaders an empty list is returned.If read_entity_body() did not see new entity data in its readthe value -1 is returned.=head1 SEE ALSOL<Net::HTTP>=head1 COPYRIGHTCopyright 2001 Gisle Aas.This library is free software; you can redistribute it and/ormodify it under the same terms as Perl itself.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -