This repository has been archived by the owner on Mar 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
httpResponse.class.php
executable file
·483 lines (408 loc) · 12.1 KB
/
httpResponse.class.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
/*
This file is part of Vosbox.
Vosbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Vosbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Vosbox. If not, see <http://www.gnu.org/licenses/>.
Vosbox copyright Callan Bryant 2011-2012 <callan.bryant@gmail.com> http://callanbryant.co.uk/
TODO: proper range support http://stackoverflow.com/questions/2209204/parsing-http-range-header-in-php
*/
/**
* http server class
*
* Allows voswork to serve files other than normal script output.
* For example, It can be used to download images, CSS or javascript - or more
* importantly, dynamically served files, possibly with authentication
* It makes use of caching and acts as a normal webserver.
*
* Internal attributes must be set up. Then run serve() to commit and serve.
* For local files, use load_local_file() to set sensible attibutes.
* They can be overidden after, if necessary.
*
* @todo http resume
* @package core
* @final
* @see core.class.php
* @author Callan Bryant
*/
class httpResponse
{
public $handle,$name,$size,$etag;
// default mimetype - set later!
public $mimetype = 'application/octet-stream';
public $status = null;
public $position = 0;
// inline or attachment (browser view or force download)
public $inline = false;
// should the client keep the file indefinitey?
// if the file never changes, tell the client by setting this!
public $persistent = false;
/**
* constructor
*/
public function __construct()
{
// filestats are cached, which may cause problems
clearstatcache();
// automatic seeking from range request (partial support TODO full support)
// take start offset from bytes=start,finish
if ( $range = @$_SERVER['HTTP_RANGE']){
$range = end(explode('bytes=',$range));
$range = current(explode('-',$range));
if (is_numeric($range))
$this->position = $range;
}
}
/**
* (magic)
* verfy the attributes given when they are set
*
* @param $attr string attribute to set
*/
public function __set($attr,$value)
{
switch ($attr)
{
case 'handle':
//no test...?
break;
case 'mimetype':
if (!is_string($attr))
throw new Exception('mimetype must be a string');
break;
case 'name':
if (!is_string($attr))
throw new Exception('name must be a string');
break;
case 'size':
if (!is_int($attr))
throw new Exception('size must be an integer');
break;
case 'etag':
if (!is_string($attr))
throw new Exception('etag must be a string');
break;
case 'status':
if (!is_int($attr))
throw new Exception('status must be a HTTP response code (integer)');
break;
case 'position':
if (!is_int($attr))
throw new Exception('name must be an integer');
break;
}
// set it, it passed the tests
$this->$attr = $value;
}
/**
* Sets and checks all attributes to serve a file
* @param $filepath string local path of file to serve
*/
public function load_local_file($path)
{
if (!file_exists($path))
{
$this->status = 404;
return;
}
if (!is_readable($path))
{
$this->status = 403;
return;
}
// get file extension from path
// automatically generate it
$extension = self::extension($path);
// set appropiate mimetype header
$this->mimetype = $this->mimetype($extension);
// guess based on extension wether the file should be inline or an attachment
// can be changed after file is loaded
if ($extension =='html' or $extension =='txt')
$this->inline = true;
else
$this->inline = false;
// set an appropiate name
$this->name = basename($path);
// allow the browser to calculate file progress
$this->size = filesize($path);
// sprintf: show correct file size over 2GB (convert to unsigned)
$this->size = sprintf( "%u",$this->size);
// get Etags for 304 handling
$current_etag = self::etag($path);
$client_etag =& $_SERVER['HTTP_IF_NONE_MATCH'];
// see if the client already has a current version of the file
if ( $current_etag === $client_etag )
{
//client has same file
//tell the client to use cache
$this->status= 304;
return;
}
// give the client an Etag for the file
header('Etag: '.$current_etag);
// try to open a handle
$this->handle = @fopen($path, 'rb');
if ($this->handle === false)
throw new Exception('could not open file');
$this->status = 200;
}
/**
* Sets and checks all attributes to serve a variable as a file
* other attibutes must be manually set: mimetype, name
* FOR SMALL FILES! MAX 2MB! (Uses RAM, limited deliberatley by php://temp/)
* @param &$data string contents of file to serve
*/
public function load_string(&$data)
{
// allow the browser to calculate file progress
$this->size = strlen($data);
// get Etags for 304 handling
$current_etag = sha1($data);
$client_etag =& $_SERVER['HTTP_IF_NONE_MATCH'];
// see if the client already has a current version of the file
if ( $current_etag === $client_etag )
{
//client has same file
//tell the client to use cache
$this->status= 304;
return;
}
// give the client an Etag for the file
header('Etag: '.$current_etag);
// open a new, temporary handle
$this->handle = fopen('php://temp', 'r+');
// fill it up with the data
fputs($this->handle,$data);
// set the pointer back to the beginning
rewind($this->handle);
$this->status = 200;
}
/**
* returns the extension of a file
*/
public static function extension($file)
{
$matches = array();
preg_match('/\.([[:alnum:]]+)$/',$file,$matches);
return $matches[1];
}
/**
* returns the mimetype given a file extension
* @param $ext string file extension
* @return string mime type
*/
public static function mimetype($extension)
{
switch($extension)
{
case 'js' :return 'text/javascript'; break;
case 'html':return 'text/html'; break;
case 'mp3':return 'audio/mpeg3'; break;
case 'flv':return 'video/x-flv'; break;
case 'ogg':return 'audio/ogg'; break;
case 'ogv':return 'video/ogg'; break;
case 'css':return 'text/css'; break;
case '7z' :return 'application/x-7z-compressed'; break;
case 'exe':return 'application/octet-stream'; break;
case 'zip':return 'application/zip'; break;
case 'mp4':return 'video/mp4'; break;
case 'pdf':return 'application/pdf'; break;
case 'txt':return 'text/plain'; break;
case 'php':return 'text/plain'; break;
case 'doc':return 'application/msword'; break;
case 'xls':return 'application/vnd.ms-excel'; break;
case 'ppt':return 'application/vnd.ms-powerpoint'; break;
case 'gif':return 'image/gif'; break;
case 'png':return 'image/png'; break;
case 'jpg':return 'image/jpeg'; break;
case 'svg':return 'image/svg+xml'; break;
//if the ext is not recognised, force the browser to download it as a binary file
default: return 'application/octet-stream';
}
}
/**
* echos http status code page
*
* (200, 404, 403, 304 supported)
* @param integer status code
*/
public static function status($code)
{
header('Content-Type: text/plain');
switch($code)
{
case 200:
header('HTTP/1.1 200 OK');
return;
case 206:
header('HTTP/1.1 206 Partial Content');
return;
case 304:
header('HTTP/1.1 304 Not Modified');
return;
case 404:
header('HTTP/1.1 404 Not Found');
echo('404: file not found!');
return;
case 403:
header('HTTP/1.1 403 Forbidden');
echo('403: ACCESS DENIED!');
return;
case 301:
header('HTTP/1.1 301 Moved Permanently');
return;
// unknown default to 500 to at least give something relevant
default:
case 500:
header('HTTP/1.1 500 Internal Server Error');
echo ('An internal, undefined, error occurred.');
return;
}
}
/**
* main server method - outputs a file by the handle, without headers
*
* low memory usage
*
* @param string handle of file
*/
protected function read()
{
// stop all output buffering, to decrease memory usage and increase speed
while (@ob_end_flush());
// set the position of the file pointer (for HTTP resume)
if (@fseek($this->handle,$this->position) !== 0 )
throw new Exception('Seek/handle error');
// loop through 4K at a time
while (!feof($this->handle))
{
$chunk = fread($this->handle, 4096);
echo($chunk);
// reset watchdog so script doesn't time out with slow connections
set_time_limit(30);
}
fclose($this->handle);
}
/**
* returns an Etag for a file - that changes if modified
*
* is fast (does not hash the file itself)
* @param $file string filepath
* @return string Etag
*/
public static function etag($filepath)
{
$mtime = filemtime($filepath);
$size = filesize($filepath);
$inode = fileinode($filepath);
// hash them together with the file path
return sha1($mtime.$size.$filepath);
}
/**
* commit - send the file with the object's attributes
* only for when all attributes are valid, and there is a handle!
*/
protected function commit()
{
// it is important to unlock the session data to allow other
// pages to use it and hence be able to load whilst the data
// is being served by this class
session_write_close();
if (!$this->position)
self::status(200);
else
self::status(206);
header('Content-type:'.$this->mimetype);
header('Accept-Ranges: bytes');
// if html or text, it must be displayed inline
if ($this->inline)
header('Content-Disposition: inline;');
else
//tell the browser the filename and to download it as an attachment
header('Content-Disposition: attachment; filename="' . $this->name. '"');
if (isset($this->size))
// this breaks compatability with gz handling
header('Content-Length: '.($this->size-$this->position));
// if the file never changes, tell the client!!
$this->dictate_cache();
// Etag
if (isset($this->etag))
header('Etag: '.$this->etag);
// serve the actual file
$this->read();
}
/**
* redirects a relative or absolute url
*
* @param $url string abs. or rel. url to redirect to
*/
public static function redirect($url)
{
if (strpos($url,'http://') !== 0)
// relative URL (must be converted)
$url = 'http://'.$_SERVER['HTTP_HOST'].'/'.$url;
// must provide a full absolute URL
self::status(301);
header('Location: '.$url);
}
/**
* tells the client to cache or not (none or ~infinite) based on
* $this->persistent flag
*/
protected function dictate_cache()
{
if ($this->persistent)
{
// cache it client side for about 3 years, effectively ~infinite!
header('Cache-Control: public, maxage=99999999');
// depreciated old way for HTTP/1.0 (absolute, therefore flawed)
header('Expires: '.date('D, d M Y H:i:s', (time()+99999999)).' GMT');
}
else
{
// make it explicitly non-cachable
header('Cache-Control: no-cache,must-revalidate');
// depreciated old way for HTTP/1.0 (absolute, therefore flawed)
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
}
}
/**
* decide what to do based on the status code - call LAST
*/
public function serve()
{
// does not work with cgi
//if (headers_sent() )
//throw new Exception('headers already sent, incorrect usage');
switch($this->status)
{
case 200:
case 206:
// actually serve the file based on attributes
// (the idea is to try to avoid this where possible for speed!)
$this->commit();
break;
case 304:
// if the persistent option is enabled after the first transfer
// the client will only receive 304s and therefore will need to
// told as well! (not just on 200)
$this->dictate_cache();
$this->status(304);
break;
case null:
throw new Exception('(HTTP) $this->status must be set');
break;
// catch all
default:
self::status($this->status);
}
}
}
?>