-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbyte-serving.php
204 lines (185 loc) · 7.17 KB
/
byte-serving.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
// Originally by [email protected].
// Re-factorized and enhanced by DannyNiu/NJF once Jan. 2020.
/*
* MIT License
*
* Copyright (c) 2018 Răzvan Valentin Florian
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
function byteserve_set_range($range, $filesize, &$first, &$last)
{
# Sets the first and last bytes of a range, given a range expressed as a string
# and the size of the file.
#
# If the end of the range is not specified, or the end of the range is greater
# than the length of the file, $last is set as the end of the file.
#
# If the begining of the range is not specified, the meaning of the value after
# the dash is "get the last n bytes of the file".
#
# If $first is greater than $last, the range is not satisfiable, and we should
# return a response with a status of 416 (Requested range not satisfiable).
#
# Examples:
# $range='0-499', $filesize=1000 => $first=0, $last=499 .
# $range='500-', $filesize=1000 => $first=500, $last=999 .
# $range='500-1200', $filesize=1000 => $first=500, $last=999 .
# $range='-200', $filesize=1000 => $first=800, $last=999 .
$dash = strpos($range, '-');
$first = trim(substr($range, 0, $dash));
$last = trim(substr($range, $dash+1));
if( $first == '' )
{
//suffix byte range: gets last n bytes
$suffix = $last;
$last = $filesize-1;
$first = $filesize-$suffix;
if( $first < 0 ) $first = 0;
}
else if( $last=='' || $last > $filesize-1 ) $last = $filesize-1;
if( $first > $last )
{
//unsatisfiable range
http_response_code(416);
header("Status: 416 Requested range not satisfiable");
header("Content-Range: */$filesize");
die(); // My (DannyNiu's) coding style is to differenciate success exits from failure dies [diff-exit-die].
}
}
function byteserve_buffered_read($file, $bytes, $buffer_size=1024)
{
# Outputs up to $bytes from the file $file to standard output, $buffer_size bytes at a time.
# ``$file'' may be pre-seeked to a sub-range of a larger (say, uncompressed archive) file.
$bytes_left=$bytes;
while( $bytes_left > 0 && !feof($file) )
{
$bytes_to_read = min($buffer_size, $bytes_left);
$bytes_left-=$bytes_to_read;
$contents=fread($file, $bytes_to_read);
echo $contents;
flush();
}
}
function byteserve($filename, $fileoffset=0, $filesize=-1, $mimetype="application/octet-stream")
{
// 2019-08-22: added by DannyNiu/NJF.
// Indicating potentially cacheable results.
clearstatcache();
$mt = stat($filename)['mtime'];
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mt)." GMT");
header("Cache-Control: public, max-age=3600");
if( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) )
{
$rt = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
if( $rt > $mt ) {
http_response_code(304);
return;
}
}
/*
Byteserves the file $filename.
When there is a request for a single range, the content is transmitted
with a Content-Range header, and a Content-Length header showing the number
of bytes actually transferred.
When there is a request for multiple ranges, these are transmitted as a
multipart message. The multipart media type used for this purpose is
"multipart/byteranges".
*/
if( $filesize == -1 )
{
if( $fileoffset > 0 )
{
http_response_code(500);
die(); // [diff-exit-die]
}
$filesize = filesize($filename);
$mimetype = mime_content_type($filename);
}
$file = fopen($filename, "rb");
fseek($file, $fileoffset, SEEK_SET);
$ranges=NULL;
if( $_SERVER['REQUEST_METHOD']=='GET' &&
isset($_SERVER['HTTP_RANGE']) &&
$range = stristr(trim($_SERVER['HTTP_RANGE']), 'bytes=') )
{
$range = substr($range, 6); // 6 == strlen("bytes=")
$boundary = bin2hex(random_bytes(48)); // set a random boundary.
$ranges = explode(',', $range);
}
if( $ranges && count($ranges) ){
http_response_code(206);
header("Accept-Ranges: bytes");
if( count($ranges) > 1 )
{
## More than one range is requested. ##
//compute content length
$content_length=0;
foreach ($ranges as $range){
byteserve_set_range($range, $filesize, $first, $last);
$content_length+=strlen("\r\n--$boundary\r\n");
$content_length+=strlen("Content-Type: $mimetype\r\n");
$content_length+=strlen("Content-Range: bytes $first-$last/$filesize\r\n\r\n");
$content_length+=$last-$first+1;
}
$content_length+=strlen("\r\n--$boundary--\r\n");
//output headers
header("Content-Length: $content_length");
// see http://httpd.apache.org/docs/misc/known_client_problems.html
// and https://docs.oracle.com/cd/B14098_01/web.1012/q20206/misc/known_client_problems.html
// for an discussion of x-byteranges vs. byteranges
header("Content-Type: multipart/x-byteranges; boundary=$boundary");
//output the content
foreach ($ranges as $range){
byteserve_set_range($range, $filesize, $first, $last);
echo "\r\n--$boundary\r\n";
echo "Content-Type: $mimetype\r\n";
echo "Content-Range: bytes $first-$last/$filesize\r\n\r\n";
fseek($file, $first+$fileoffset);
byteserve_buffered_read ($file, $last-$first+1);
}
echo "\r\n--$boundary--\r\n";
}
else
{
## A single range is requested. ##
$range=$ranges[0];
byteserve_set_range($range, $filesize, $first, $last);
header("Content-Length: ".($last-$first+1) );
header("Content-Range: bytes $first-$last/$filesize");
header("Content-Type: $mimetype");
fseek($file, $first+$fileoffset);
byteserve_buffered_read($file, $last-$first+1);
}
}
else
{
## no byteserving ##
header("Accept-Ranges: bytes");
header("Content-Length: $filesize");
header("Content-Type: $mimetype");
fseek($file, $fileoffset);
byteserve_buffered_read($file, $filesize);
}
fclose($file);
return; // let caller do other back-stage processing.
}
//do not send cache limiter header
# ini_set('session.cache_limiter','none');