POE-Component-Server-HTTP

 view release on metacpan or  search on metacpan

t/20_stream.t  view on Meta::CPAN


    $response->code(RC_OK);
    $response->content_type('text/html; charset=iso-8859-1');

    $response->content(<<HTML);
<html>
<head>
<title>Hello world</title>
</head>
<body>
<h1>Hello world from POE::Component::Server::HTTP</h1>

<ul>
    <li><a href="/last.txt">Text</a></li>
    <li><a href="/multipart.txt">Multipart text</a></li>
    <li><a href="/last.gif">Image</a></li>
    <li><a href="/multipart.gif">Multipart image</a></li>
    <li><a href="/multipart.mixed">Text, then image</a></li>
</ul>
    

</body>
</html>
HTML
    return RC_OK;
}

#######################################
# Called as ContentHandler
sub favicon
{
    my($self, $request, $response)=@_;

    DEBUG and warn "favicon\n";

    $response->code(RC_NOT_FOUND);
    $response->content_type('text/html; charset=iso-8859-1');

    $response->content(<<HTML);
<html>
<head>
<title>Go away</title>
</head>
<body>
<h1>Go away</h1>
</body>
</html>
HTML
    return RC_NOT_FOUND;
}


#######################################
# Called as ContentHandler
sub multipart
{
    my($self, $request, $response)=@_;

    DEBUG and warn "multipart\n";
    
    # Send an HTTP header and turn streaming on
    $self->multipart_start($request, $response);
    # After the HTTP header is sent, our StreamHandler will be called
    # Save the values that stream_start needs to do its work
    push @{$self->{stream_todo}}, [$request, $response, 
                                        'first.gif', 'last.gif'];

    return RC_OK;
}

#######################################
# Called as ContentHandler
sub multipart_mixed
{
    my($self, $request, $response)=@_;

    DEBUG and warn "multipart\n";

    $self->multipart_start($request, $response);
    push @{$self->{stream_todo}}, [$request, $response, 
                                        'first.txt', 'last.gif'];

    return RC_OK;
}

#######################################
# Called as ContentHandler
sub last
{
    my($self, $request, $response)=@_;

    DEBUG and warn "last\n";
    $response->code(RC_OK);
    $response->content_type('image/gif');
    $response->content($self->data('last.gif'));
    return RC_OK;
}

#######################################
# Called as ContentHandler
sub multipart_txt
{
    my($self, $request, $response)=@_;

    DEBUG and warn "multipart_txt\n";

    $self->multipart_start($request, $response);
    push @{$self->{stream_todo}}, [$request, $response, 
                                        'first.txt', 'last.txt'];

    return RC_OK;
}

#######################################
# Called as ContentHandler
sub last_txt
{
    my($self, $request, $response)=@_;

    DEBUG and warn "last_txt\n";
    $response->code(RC_OK);

t/20_stream.t  view on Meta::CPAN

    open FILE, $file or die "Can't open $file: $!";
    {
        local $/; 
        $file = <FILE>;
    }
    close FILE;
    return $file;
}


####################################################################

#######################################
# This function sends a file over the connection
# We create a new HTTP response, with content and content_length
# Because HTTP response->as_string sends HTTP status line, we hide it
#   behind a X-HTTP-Status header, just after the boundary.
# This means that this part of the response looks like:
#
# --BoundaryString
# X-HTTP-Status: HTTP/1.0 200 (OK)
# Content-Type: text/plain
# Content-Length: 13
#
# Content here
#
# Setting Content-Length is important for images
sub multipart_send
{
    my($self, $response, $file)=@_;

    DEBUG and warn "multipart_send $file\n";

    my $ct = 'image/gif';
    $ct = 'text/plain' if $file =~ /txt$/;

    my $resp =  $self->multipart_response($ct);

    my $data=$self->data($file);
    $resp->content($data);
    $resp->content_length(length($data));

    $response->send("--$self->{boundary}\cM\cJX-HTTP-Status: ");
    $response->send($resp->as_string);
    return;
}

#######################################
# Create a HTTP::Response object to be sent as a part of the response
sub multipart_response
{
    my($self, $ct, $resp)=@_;
    $resp ||= HTTP::Response->new;
    $resp->content_type($ct||'text/plain');
    $resp->code(200);
    return $resp;
}

#######################################
# Send an HTTP header that sets up multipart/mixed response
# Also turns on streaming.
#
# PoCo::Server::HTTP will send the $response object, then run PostHandler
# then switch to Streaming mode.
sub multipart_start
{
    my($self, $request, $response)=@_;

    $response->code(RC_OK);
    $self->{boundary} ||= 'ThisRandomString';
    $response->content_type("multipart/mixed;boundary=$self->{boundary}");

    $response->streaming(1);
}

#######################################
# The request is done.  Turn off streaming and end the multipart response
# Setting the header Connection to 'close' forces PoCo::Server::HTTP to
# close the socket.  This is needed so that the browsers stop "twirling".
sub multipart_end
{
    my($self, $request, $response)=@_;
    DEBUG and warn "Closing connection\n";
    $response->close;
    $request->header(Connection => 'close');
    $response->send("--$self->{boundary}--\cM\cJ");
}



( run in 0.377 second using v1.01-cache-2.11-cpan-39bf76dae61 )