Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / media / video-throttled-load.cgi
1 #!/usr/bin/perl -wT
2
3 use strict;
4
5 use CGI;
6 use File::stat;
7
8 use constant CHUNK_SIZE_BYTES => 1024;
9
10 my $query = new CGI;
11
12 my $name = $query->param('name');
13 my $filesize = stat($name)->size;
14
15 # Get throttling rate, assuming parameter is in kilobytes per second.
16 my $kbPerSec = $query->param('throttle');
17 my $chunkPerSec = $kbPerSec * 1024 / CHUNK_SIZE_BYTES;
18
19 # Get MIME type if provided.  Default to video/mp4.
20 my $type = $query->param('type') || "video/mp4";
21
22 my $nph = $query->param('nph') || 0;
23 CGI->nph($nph);
24
25 my $contentRange = $ENV{'HTTP_RANGE'};
26
27 my $rangeEnd = $filesize - 1;
28 my @parsedRange = (0, $rangeEnd);
29
30 if ($nph) {
31     # Handle HTTP Range requests.
32     my $httpContentRange;
33     my $httpStatus;
34
35     if ($contentRange) {
36         my @values = split('=', $contentRange);
37         my $rangeType = $values[0];
38         @parsedRange = split("-", $values[1]);
39
40         if (!$parsedRange[1]) {
41             $parsedRange[1] = $rangeEnd;
42         }
43         $httpStatus = "206 Partial Content";
44         $httpContentRange = "bytes " . $parsedRange[0] . "-" . $parsedRange[1] . "/" . $filesize;
45     } else {
46         $httpStatus = "200 OK";
47     }
48
49     print "Status: " . $httpStatus . "\n";
50     print "Connection: close\n";
51     print "Content-Length: " . $filesize . "\n";
52     print "Content-Type: " . $type . "\n";
53     print "Accept-Ranges: bytes\n";
54     if ($httpContentRange) {
55         print "Content-Range: " . $httpContentRange . "\n";
56     }
57 } else {
58     # Print HTTP Header, disabling cache.
59     print "Cache-Control: no-cache\n";
60     print "Content-Length: " . $filesize . "\n";
61     print "Content-Type: " . $type . "\n";
62 }
63
64 print "\n";
65
66 open FILE, $name or die;
67 binmode FILE;
68 my ($data, $n);
69 my $total = 0;
70
71 seek(FILE, $parsedRange[0], 0);
72
73 while (($n = read FILE, $data, 1024) != 0) {
74     print $data;
75
76     $total += $n;
77     if (($total >= $filesize) || ($total > $parsedRange[1])) {
78         last;
79     }
80
81     # Throttle if there is some.
82     if ($chunkPerSec > 0) {
83         select(undef, undef, undef, 1.0 / $chunkPerSec);
84     }
85 }
86 close(FILE);