3 * Utilities for handling HTTP related tasks
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
9 define('HTTP_MULTIPART_BOUNDARY','D0KuW1K1B0uNDARY');
10 define('HTTP_HEADER_LF',"\r\n");
11 define('HTTP_CHUNK_SIZE',16*1024);
14 * Checks and sets HTTP headers for conditional HTTP requests
16 * @author Simon Willison <swillison@gmail.com>
17 * @link http://simon.incutio.com/archive/2003/04/23/conditionalGet
18 * @param timestamp $timestamp lastmodified time of the cache file
19 * @returns void or exits with previously header() commands executed
21 function http_conditionalRequest($timestamp){
22 // A PHP implementation of conditional get, see
23 // http://fishbowl.pastiche.org/archives/001132.html
24 $last_modified = substr(gmdate('r', $timestamp), 0, -5).'GMT';
25 $etag = '"'.md5($last_modified).'"';
27 header("Last-Modified: $last_modified");
28 header("ETag: $etag");
29 // See if the client has provided the required headers
30 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
31 $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
33 $if_modified_since = false;
36 if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
37 $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
39 $if_none_match = false;
42 if (!$if_modified_since && !$if_none_match){
46 // At least one of the headers is there - check them
47 if ($if_none_match && $if_none_match != $etag) {
48 return; // etag is there but doesn't match
51 if ($if_modified_since && $if_modified_since != $last_modified) {
52 return; // if-modified-since is there but doesn't match
55 // Nothing has changed since their last request - serve a 304 and exit
56 header('HTTP/1.0 304 Not Modified');
58 // don't produce output, even if compression is on
64 * Let the webserver send the given file vi x-sendfile method
66 * @author Chris Smith <chris.eureka@jalakai.co.uk>
67 * @returns void or exits with previously header() commands executed
69 function http_sendfile($file) {
72 //use x-sendfile header to pass the delivery to compatible webservers
73 if($conf['xsendfile'] == 1){
74 header("X-LIGHTTPD-send-file: $file");
77 }elseif($conf['xsendfile'] == 2){
78 header("X-Sendfile: $file");
81 }elseif($conf['xsendfile'] == 3){
82 header("X-Accel-Redirect: $file");
91 * Send file contents supporting rangeRequests
93 * This function exits the running script
95 * @param ressource $fh - file handle for an already open file
96 * @param int $size - size of the whole file
97 * @param int $mime - MIME type of the file
99 * @author Andreas Gohr <andi@splitbrain.org>
101 function http_rangeRequest($fh,$size,$mime){
105 header('Accept-Ranges: bytes');
107 if(!isset($_SERVER['HTTP_RANGE'])){
108 // no range requested - send the whole file
109 $ranges[] = array(0,$size,$size);
111 $t = explode('=', $_SERVER['HTTP_RANGE']);
112 if (!$t[0]=='bytes') {
113 // we only understand byte ranges - send the whole file
114 $ranges[] = array(0,$size,$size);
117 // handle multiple ranges
118 $r = explode(',',$t[1]);
120 $p = explode('-', $x);
123 if (!$end) $end = $size - 1;
124 if ($start > $end ||
$start > $size ||
$end > $size){
125 header('HTTP/1.1 416 Requested Range Not Satisfiable');
126 print 'Bad Range Request!';
129 $len = $end - $start +
1;
130 $ranges[] = array($start,$end,$len);
134 $parts = count($ranges);
136 // now send the type and length headers
138 header("Content-Type: $mime",true);
140 header('HTTP/1.1 206 Partial Content');
142 header("Content-Type: $mime",true);
144 header('Content-Type: multipart/byteranges; boundary='.HTTP_MULTIPART_BOUNDARY
,true);
149 for($i=0; $i<$parts; $i++
){
150 list($start,$end,$len) = $ranges[$i];
152 // multipart or normal headers
154 echo HTTP_HEADER_LF
.'--'.HTTP_MULTIPART_BOUNDARY
.HTTP_HEADER_LF
;
155 echo "Content-Type: $mime".HTTP_HEADER_LF
;
156 echo "Content-Range: bytes $start-$end/$size".HTTP_HEADER_LF
;
159 header("Content-Length: $len");
161 header("Content-Range: bytes $start-$end/$size");
166 fseek($fh,$start); //seek to start of range
167 $chunk = ($len > HTTP_CHUNK_SIZE
) ? HTTP_CHUNK_SIZE
: $len;
168 while (!feof($fh) && $chunk > 0) {
169 @set_time_limit
(30); // large files can take a lot of time
170 print fread($fh, $chunk);
173 $chunk = ($len > HTTP_CHUNK_SIZE
) ? HTTP_CHUNK_SIZE
: $len;
177 echo HTTP_HEADER_LF
.'--'.HTTP_MULTIPART_BOUNDARY
.'--'.HTTP_HEADER_LF
;
180 // everything should be done here, exit
185 * Check for a gzipped version and create if necessary
187 * return true if there exists a gzip version of the uncompressed file
188 * (samepath/samefilename.sameext.gz) created after the uncompressed file
190 * @author Chris Smith <chris.eureka@jalakai.co.uk>
192 function http_gzip_valid($uncompressed_file) {
193 $gzip = $uncompressed_file.'.gz';
194 if (filemtime($gzip) < filemtime($uncompressed_file)) { // filemtime returns false (0) if file doesn't exist
195 return copy($uncompressed_file, 'compress.zlib://'.$gzip);