Bump trunk version of HTTP::Body to 0.8
[catagits/HTTP-Body.git] / lib / HTTP / Body / Parser / OctetStream.pm
1 package HTTP::Body::Parser::OctetStream;
2
3 use strict;
4 use bytes;
5 use base 'HTTP::Body::Parser';
6
7 use Carp       qw[];
8 use Errno      qw[];
9 use File::Temp qw[];
10
11 sub parse {
12     my $self = shift;
13
14     if ( $self->seen_eos && length $self->buffer || length $self->buffer >= $self->bufsize ) {
15
16         unless ( $self->context->content ) {
17             $self->context->content( File::Temp->new );
18         }
19
20         my ( $r, $w, $s ) = ( length $self->buffer, 0, 0 );
21
22         for ( $w = 0; $w < $r; $w += $s || 0 ) {
23
24             $s = $self->context->content->syswrite( $self->buffer, $r - $w, $w );
25
26             Carp::croak qq/Failed to syswrite buffer to temporary file. Reason: $!./
27               unless defined $s || $! == Errno::EINTR;
28         }
29
30         $self->buffer = '';
31     }
32
33     if ( $self->seen_eos && $self->context->content ) {
34
35         sysseek( $self->context->content, 0, 0 )
36           or Carp::croak qq/Failed to sysseek temporary handle./;
37     }
38 }
39
40 1;