package TestApache::read;

use strict;
use warnings FATAL => 'all';

use Apache::RequestRec ();
use Apache::RequestIO ();

use Apache::Const -compile => 'OK';

use constant BUFSIZ => 512; #small for testing

sub handler {
    my $r = shift;
    $r->content_type('text/plain');

    my $ct = $r->headers_in->get('content-length');
    my $buffer = "";
    my $bufsiz = $r->args || BUFSIZ;

    my $offset = 0;
    while (my $remain = $ct - $offset) {
        my $len = $remain >= $bufsiz ? $bufsiz : $remain;
        my $read = $r->read($buffer, $len, $offset);
        if ($read != $len) {
            die "read only ${read}b, while ${len}b were requested\n";
        }
        last unless $read > 0;
        $offset += $read;
    }

    #make sure we dont block after all data is read
    my $n = $r->read(my $x, BUFSIZ);
    die unless $n == 0;

    $r->puts($buffer);

    Apache::OK;
}

1;
