File: byterange.t

package info (click to toggle)
apache2 2.4.66-5
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 59,500 kB
  • sloc: ansic: 212,331; python: 13,830; perl: 11,307; sh: 7,258; php: 1,320; javascript: 1,314; awk: 749; makefile: 715; lex: 374; yacc: 161; xml: 2
file content (57 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use strict;
use warnings FATAL => 'all';

use Apache::Test;
use Apache::TestRequest ();
use Apache::TestCommon ();

Apache::TestCommon::run_files_test(\&verify, 1);

sub verify {
    my($ua, $url, $file) = @_;
    my $debug = $Apache::TestRequest::DebugLWP;

    $url = Apache::TestRequest::resolve_url($url);
    my $req = HTTP::Request->new(GET => $url);

    my $total = 0;
    my $chunk_size = 8192;

    my $wanted = -s $file;

    while ($total < $wanted) {
        my $end = $total + $chunk_size;
        if ($end > $wanted) {
            $end = $wanted;
        }

        my $range = "bytes=$total-$end";
        $req->header(Range => $range);

        print $req->as_string if $debug;

        my $res = $ua->request($req);
        my $content_range = $res->header('Content-Range') || 'NONE';

        $res->content("") if $debug and $debug == 1;
        print $res->as_string if $debug;

        if ($content_range =~ m:^bytes\s+(\d+)-(\d+)/(\d+):) {
            my($start, $end, $total_bytes) = ($1, $2, $3);
            $total += ($end - $start) + 1;
        }
        elsif ($total == 0 && $end == $wanted &&
               $content_range eq 'NONE' && $res->code == 200) {
               $total += $wanted;
        }
        else {
            print "Range:         $range\n";
            print "Content-Range: $content_range\n";
            last;
        }
    }

    print "downloaded $total bytes, file is $wanted bytes\n";

    ok $total == $wanted;
}