home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _482543dbe0a9d0dc714e9030be0d0b0a < prev    next >
Text File  |  2004-06-01  |  2KB  |  107 lines

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