| 1 | package Net::HTTP::NB; | ||||||||||||
| 2 | 1 | our $VERSION = '6.24'; | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use base 'Net::HTTP'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 3 | ▶ | sub can_read { | ||||||||||
|
Subroutine
can_read: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 9 | 3 | return 1; | |||||||||||
| 10 | } | ||||||||||||
| 11 | |||||||||||||
| 12 | 3 | ▶ | sub sysread { | ||||||||||
|
Subroutine
sysread: called CC=3 SCAR=12.2
Pod
undocumented
| |||||||||||||
| 13 | 3 | my $self = $_[0]; | |||||||||||
| 14 | 3 | ▶ | if (${*$self}{'httpnb_read_count'}++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | 0 | ${*$self}{'http_buf'} = ${*$self}{'httpnb_save'}; | |||||||||||
| 16 | 0 | die "Multi-read\n"; | |||||||||||
| 17 | } | ||||||||||||
| 18 | 3 | my $buf; | |||||||||||
| 19 | 3 | ▶ | my $offset = $_[3] || 0; | ||||||||||
|
Condition50%1/2
$_[3] || 0
MC/DC0%0/1
$_[3] || 0
$_[3]
Truth table50%1/2
A
A= $_[3]
| |||||||||||||
| 20 | 3 | my $n = sysread($self, $_[1], $_[2], $offset); | |||||||||||
| 21 | 3 | ${*$self}{'httpnb_save'} .= substr($_[1], $offset); | |||||||||||
| 22 | 3 | return $n; | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | 1 | ▶ | sub read_response_headers { | ||||||||||
|
Subroutine
read_response_headers: called CC=3 SCAR=11.9
Pod
documented
| |||||||||||||
| 26 | 1 | my $self = shift; | |||||||||||
| 27 | 1 | ${*$self}{'httpnb_read_count'} = 0; | |||||||||||
| 28 | 1 | ${*$self}{'httpnb_save'} = ${*$self}{'http_buf'}; | |||||||||||
| 29 | 1 | my @h = eval { $self->SUPER::read_response_headers(@_) }; | |||||||||||
| 30 | 1 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 0 | ▶ | return if $@ eq "Multi-read\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | die; | |||||||||||
| 33 | } | ||||||||||||
| 34 | 1 | return @h; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 3 | ▶ | sub read_entity_body { | ||||||||||
|
Subroutine
read_entity_body: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 38 | 3 | my $self = shift; | |||||||||||
| 39 | 3 | ${*$self}{'httpnb_read_count'} = 0; | |||||||||||
| 40 | 3 | ${*$self}{'httpnb_save'} = ${*$self}{'http_buf'}; | |||||||||||
| 41 | # XXX I'm not so sure this does the correct thing in case of | ||||||||||||
| 42 | # transfer-encoding transforms | ||||||||||||
| 43 | 3 | my $n = eval { $self->SUPER::read_entity_body(@_); }; | |||||||||||
| 44 | 3 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | $_[0] = ""; | |||||||||||
| 46 | 0 | return -1; | |||||||||||
| 47 | } | ||||||||||||
| 48 | 3 | return $n; | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | 1 | 1; | |||||||||||
| 52 | |||||||||||||
| 53 | =pod | ||||||||||||
| 54 | |||||||||||||
| 55 | =encoding UTF-8 | ||||||||||||
| 56 | |||||||||||||
| 57 | =head1 NAME | ||||||||||||
| 58 | |||||||||||||
| 59 | Net::HTTP::NB - Non-blocking HTTP client | ||||||||||||
| 60 | |||||||||||||
| 61 | =head1 VERSION | ||||||||||||
| 62 | |||||||||||||
| 63 | version 6.24 | ||||||||||||
| 64 | |||||||||||||
| 65 | =head1 SYNOPSIS | ||||||||||||
| 66 | |||||||||||||
| 67 | use Net::HTTP::NB; | ||||||||||||
| 68 | my $s = Net::HTTP::NB->new(Host => "www.perl.com") || die $@; | ||||||||||||
| 69 | $s->write_request(GET => "/"); | ||||||||||||
| 70 | |||||||||||||
| 71 | use IO::Select; | ||||||||||||
| 72 | my $sel = IO::Select->new($s); | ||||||||||||
| 73 | |||||||||||||
| 74 | READ_HEADER: { | ||||||||||||
| 75 | die "Header timeout" unless $sel->can_read(10); | ||||||||||||
| 76 | my($code, $mess, %h) = $s->read_response_headers; | ||||||||||||
| 77 | redo READ_HEADER unless $code; | ||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | while (1) { | ||||||||||||
| 81 | die "Body timeout" unless $sel->can_read(10); | ||||||||||||
| 82 | my $buf; | ||||||||||||
| 83 | my $n = $s->read_entity_body($buf, 1024); | ||||||||||||
| 84 | last unless $n; | ||||||||||||
| 85 | print $buf; | ||||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | =head1 DESCRIPTION | ||||||||||||
| 89 | |||||||||||||
| 90 | Same interface as C<Net::HTTP> but it will never try multiple reads | ||||||||||||
| 91 | when the read_response_headers() or read_entity_body() methods are | ||||||||||||
| 92 | invoked. This make it possible to multiplex multiple Net::HTTP::NB | ||||||||||||
| 93 | using select without risk blocking. | ||||||||||||
| 94 | |||||||||||||
| 95 | If read_response_headers() did not see enough data to complete the | ||||||||||||
| 96 | headers an empty list is returned. | ||||||||||||
| 97 | |||||||||||||
| 98 | If read_entity_body() did not see new entity data in its read | ||||||||||||
| 99 | the value -1 is returned. | ||||||||||||
| 100 | |||||||||||||
| 101 | =head1 SEE ALSO | ||||||||||||
| 102 | |||||||||||||
| 103 | L<Net::HTTP> | ||||||||||||
| 104 | |||||||||||||
| 105 | =head1 AUTHOR | ||||||||||||
| 106 | |||||||||||||
| 107 | Gisle Aas <gisle@activestate.com> | ||||||||||||
| 108 | |||||||||||||
| 109 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 110 | |||||||||||||
| 111 | This software is copyright (c) 2001 by Gisle Aas. | ||||||||||||
| 112 | |||||||||||||
| 113 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 114 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 115 | |||||||||||||
| 116 | =cut | ||||||||||||
| 117 | |||||||||||||
| 118 | __END__ | ||||||||||||