Skip to content

Latest commit

 

History

History
executable file
·
1035 lines (865 loc) · 30.9 KB

offload.php

File metadata and controls

executable file
·
1035 lines (865 loc) · 30.9 KB
 
Jul 17, 2005
Jul 17, 2005
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
<?php
// This is a PHP script that handles offloading of bandwidth from a web
// server. It's a sort of poor-man's Akamai. It doesn't need anything
// terribly complex (Apache, PHP, a writable directory).
//
// It works like this:
// - You have a webserver with dynamic content, and static content that
// may change arbitrarily (i.e. - various users making changes to their
// homepages, etc). This server is under a lot of load, mostly from
// the static content, which tends to be big. There may be multiple virtual
// hosts on this machine. We call this the "base" server.
// - You have at least one other webserver that you can use to offload some
// of the bandwidth. We call this the "offload" server.
// - You set up an Apache module (mod_offload) on the first server.
// mod_offload inserts itself into the request chain, and decides if a
// given file is safe static content (real file, not a script/cgi, no
// password, etc). In those cases, it sends a 302 redirect, pointing the
// client to the offload server.
// - The offload server gets a request from the redirected client. It then
// sends an HTTP HEAD request for the file in question to the base server
// while the client waits. It decides if it has the right file based on
// the HEAD. If it does, it serves the cached file.
// - If the file is out of date, or doesn't exist on the offload server, it
// sends a regular HTTP request for it to the base server and
// begins caching it. While caching it, it also feeds it to the client
// that has been waiting.
// - If another request comes in while the file is being cached, it will
// stream what is already there from disk, and then continue to feed as
// the rest shows up.
// !!! FIXME: issues to work out.
// - Could have a partial file cached if server crashes or power goes out.
// Add a "cacher's process id" to the metadata, and have those feeding
// from the cache decide if this process died...if so, wipe the entry and
// recache it.
// - Need to have a way to clean out old files. If x.zip is on the base,
// gets cached, and then is deleted, it'll stay on the offload server
// forever. Getting a 404 from the HEAD request will clean it out, but
// the offload server needs to know to do that.
//
// Installation:
// You need PHP with --enable-sysvsem support. You should configure PHP to not
Aug 2, 2005
Aug 2, 2005
47
48
49
50
// have a time limit on script execution (max_execution_time setting, or
// just don't run this script in safe mode and it'll handle it). PHP for
// Windows currently doesn't support sysvsem, so until someone writes me
// a mutex implementation, we assume you'll use a Unix box for this script.
Jul 17, 2005
Jul 17, 2005
51
52
53
54
55
56
57
58
59
//
// You need Apache to push every web request to this script, presumably in a
// virtual host, if not the entire server.
//
// Assuming this script was at /www/scripts/index.php, you would want to add
// this to Apache's config:
//
// AliasMatch ^.*$ "/www/scripts/index.php"
//
Jul 14, 2007
Jul 14, 2007
60
61
62
63
64
65
66
// If you don't have control over the virtual host's config file, you can't
// use AliasMatch, but if you can put an .htaccess file in the root of the
// virtual host, you can get away with this:
//
// ErrorDocument 404 /index.php
//
// This will make all missing files (everything) run the script, which will
Jul 15, 2007
Jul 15, 2007
67
68
69
// then cache and distribute the correct content, including overriding the
// 404 status code with the correct one. Be careful about files that DO exist
// in that vhost directory, though. They won't offload.
Jul 14, 2007
Jul 14, 2007
70
//
Jul 17, 2005
Jul 17, 2005
71
72
73
74
// You can offload multiple base servers with one box: set up one virtual host
// on the offload server for each base server. This lets each base server
// have its own cache and configuration.
//
Aug 1, 2005
Aug 1, 2005
75
76
77
// Then edit offload_server_config.php to fit your needs.
//
// Restart the server so the AliasMatch configuration tweak is picked up.
Jul 17, 2005
Jul 17, 2005
78
79
80
//
// This file is written by Ryan C. Gordon (icculus@icculus.org).
Aug 1, 2005
Aug 1, 2005
81
require_once './offload_server_config.php';
Jul 17, 2005
Jul 17, 2005
82
83
require_once 'PEAR.php';
Aug 31, 2008
Aug 31, 2008
84
define('GVERSION', '1.0.1');
Aug 31, 2008
Aug 31, 2008
85
$GServerString = 'offload.php/' . GVERSION;
Jul 17, 2005
Jul 17, 2005
86
87
88
89
90
91
92
93
$Guri = $_SERVER['REQUEST_URI'];
if (strcmp($Guri{0}, '/') != 0)
$Guri = '/' . $Guri;
$GFilePath = NULL;
$GMetaDataPath = NULL;
$GSemaphore = NULL;
$GSemaphoreOwned = 0;
Jul 21, 2005
Jul 21, 2005
94
$GDebugFilePointer = NULL;
Dec 9, 2005
Dec 9, 2005
95
96
$GLockDir = GOFFLOADDIR . '/lock-';
$GEtagFname = NULL;
Jul 21, 2005
Jul 21, 2005
97
98
99
100
101
102
103
function getDebugFilePointer()
{
global $GDebugFilePointer;
if ((!GDEBUG) || (!GDEBUGTOFILE))
return(NULL);
if (!isset($GDebugFilePointer))
Jul 21, 2005
Jul 21, 2005
104
{
Jul 21, 2005
Jul 21, 2005
105
$GDebugFilePointer = fopen(GOFFLOADDIR . '/debug-' . getmypid(), 'a');
Jul 21, 2005
Jul 21, 2005
106
107
108
if ($GDebugFilePointer === false)
$GDebugFilePointer = NULL;
} // if
Jul 21, 2005
Jul 21, 2005
109
110
return($GDebugFilePointer);
} // getDebugFilePointer
Jul 17, 2005
Jul 17, 2005
111
112
113
114
115
116
function debugEcho($str)
{
if (GDEBUG)
{
Jul 21, 2005
Jul 21, 2005
117
118
119
120
121
if (!is_array($str))
$str = $str . "\n";
if (!GDEBUGTOFILE)
print($str);
Jul 17, 2005
Jul 17, 2005
122
else
Jul 21, 2005
Jul 21, 2005
123
124
125
126
127
128
129
130
{
$fp = getDebugFilePointer();
if (isset($fp))
{
@fputs($fp, print_r($str, true));
@fflush($fp);
} // if
} // else
Jul 17, 2005
Jul 17, 2005
131
132
133
134
135
136
137
138
139
140
141
142
} // if
} // debugEcho
function etagToCacheFname($etag)
{
return(trim($etag, " \t\n\r\0\x0B\"'"));
} // etagToCacheFname
function getSemaphore()
{
Dec 9, 2005
Dec 9, 2005
143
global $GSemaphore, $GSemaphoreOwned, $GLockDir, $GEtagFname;
Jul 17, 2005
Jul 17, 2005
144
145
146
147
148
debugEcho("grabbing semaphore...(owned $GSemaphoreOwned time(s).)");
if ($GSemaphoreOwned++ > 0)
return;
Dec 9, 2005
Dec 9, 2005
149
if (GUSESEMAPHORE)
Jul 17, 2005
Jul 17, 2005
150
{
Dec 9, 2005
Dec 9, 2005
151
152
153
154
155
156
157
158
if (!isset($GSemaphore))
{
debugEcho('(have to create semaphore...)');
$GSemaphore = sem_get(0x8267bc62); // !!! FIXME: good value?
if ($GSemaphore === false)
failure('503 Service Unavailable', "Couldn't allocate semaphore.");
} // if
sem_acquire($GSemaphore);
Jul 17, 2005
Jul 17, 2005
159
} // if
Dec 9, 2005
Dec 9, 2005
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
else
{
if ($GEtagFname == NULL)
failure('503 Service Unavailable', 'Semaphore init failed');
$dir = $GLockDir . $GEtagFname;
$max = 100;
$count = 0;
while (($count < $max) && (@mkdir($dir) === false))
{
usleep(10000);
$count++;
} // while
if ($count == $max) // didn't get lock...force it. So nasty.
{
@rmdir($dir);
$GSemaphoreOwned--;
getSemaphore();
} // if
} // else
Jul 17, 2005
Jul 17, 2005
181
182
183
184
185
} // getSemaphore
function putSemaphore()
{
Dec 9, 2005
Dec 9, 2005
186
187
global $GSemaphore, $GSemaphoreOwned, $GLockDir, $GEtagFname;
if ($GSemaphoreOwned == 0)
Jul 17, 2005
Jul 17, 2005
188
189
190
return;
if (--$GSemaphoreOwned == 0)
Dec 9, 2005
Dec 9, 2005
191
192
193
194
195
196
197
198
199
200
201
202
{
if (GUSESEMAPHORE)
{
if (isset($GSemaphore))
sem_release($GSemaphore);
} // if
else
{
if ($GEtagFname != NULL)
@rmdir($GLockDir . $GEtagFname);
} // else
} // if
Jul 17, 2005
Jul 17, 2005
203
204
205
206
207
208
debugEcho("released semaphore...(now owned $GSemaphoreOwned time(s).)");
} // putSemaphore
function terminate()
{
Aug 2, 2005
Aug 2, 2005
209
global $GDebugFilePointer, $GSemaphoreOwned;
Jul 21, 2005
Jul 21, 2005
210
Jul 17, 2005
Jul 17, 2005
211
212
213
debugEcho('offload script is terminating...');
while ($GSemaphoreOwned > 0)
putSemaphore();
Jul 21, 2005
Jul 21, 2005
214
215
if (isset($GDebugFilePointer))
Aug 2, 2005
Aug 2, 2005
216
@fclose($GDebugFilePointer);
Jul 17, 2005
Jul 17, 2005
217
218
219
220
221
222
exit();
} // terminate
function doHeader($str)
{
Jul 21, 2005
Jul 21, 2005
223
if ((!GDEBUG) || (GDEBUGTOFILE))
Jul 15, 2007
Jul 15, 2007
224
225
226
227
228
229
{
header($str, true);
if (headers_sent($filename, $linenum))
debugEcho("Headers already sent in $filename on line $linenum");
}
Jul 17, 2005
Jul 17, 2005
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
debugEcho("header('$str');");
} // doHeader
function sanestrpos($haystack, $needle)
{
$rc = strpos($haystack, $needle);
return(($rc === false) ? -1 : $rc);
} // sanestrpos
function loadMetadata($fname)
{
$retval = array();
$lines = @file($fname);
if ($lines === false)
return($retval);
$max = count($lines);
for ($i = 0; $i < $max; $i += 2)
{
$key = trim($lines[$i]);
$val = trim($lines[$i+1]);
debugEcho("Loaded metadata '$key' => '$val'");
$retval[$key] = $val;
} // for
debugEcho("Loaded $max metadata pair(s).");
return($retval);
} // loadMetadata
function cachedMetadataMostRecent($metadata, $head)
{
Jul 21, 2005
Jul 21, 2005
264
265
global $GFilePath;
Jul 17, 2005
Jul 17, 2005
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
if (!isset($metadata['Content-Length']))
return(false);
if (!isset($metadata['ETag']))
return(false);
if (!isset($metadata['Last-Modified']))
return(false);
if (strcmp($metadata['Content-Length'], $head['Content-Length']) != 0)
return(false);
if (strcmp($metadata['ETag'], $head['ETag']) != 0)
return(false);
if (strcmp($metadata['Last-Modified'], $head['Last-Modified']) != 0)
Aug 1, 2005
Aug 1, 2005
282
283
284
285
286
287
{
if (!isset($metadata['X-Offload-Is-Weak']))
return(false);
if (($metadata['X-Offload-Is-Weak']) == 0)
return(false);
} // if
Jul 17, 2005
Jul 17, 2005
288
Jul 21, 2005
Jul 21, 2005
289
290
291
292
293
294
295
296
297
298
// See if file size != Content-Length, and if it isn't,
// see if X-Offload-Caching-PID still exists. If process
// is missing, assume transfer died and recache.
$stat = @stat($GFilePath);
if ($stat === false)
return(false);
$fsize = $stat['size'];
if ($fsize != $metadata['Content-Length'])
{
Jul 21, 2005
Jul 21, 2005
299
300
301
// whoa, we were supposed to cache this!
if ($metadata['X-Offload-Caching-PID'] == getmypid())
return(false);
Aug 31, 2008
Aug 31, 2008
302
303
else if ($metadata['X-Offload-Caching-PID'] <= 0)
return(false);
Jul 21, 2005
Jul 21, 2005
304
Aug 31, 2008
Aug 31, 2008
305
306
// !!! FIXME: Unix specific!
if (!posix_kill($metadata['X-Offload-Caching-PID'], 0))
Jul 21, 2005
Jul 21, 2005
307
{
Aug 31, 2008
Aug 31, 2008
308
309
debugEcho('Caching process ID died!');
return(false);
Jul 21, 2005
Jul 21, 2005
310
311
} // if
} // if
Jul 17, 2005
Jul 17, 2005
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
return(true);
} // cachedMetadataMostRecent
function nukeRequestFromCache()
{
global $GMetaDataPath, $GFilePath;
debugEcho('Nuking request from cache...');
getSemaphore();
if (isset($GMetaDataPath))
@unlink($GMetaDataPath);
if (isset($GFilePath))
@unlink($GFilePath);
putSemaphore();
} // nukeRequestFromCache
function failure($httperr, $errmsg, $location = NULL)
{
global $GServerString;
if (strncasecmp($httperr, 'HTTP', 4) == 0)
{
$pos = sanestrpos($httperr, ' ');
if ($pos >= 0)
$httperr = substr($httperr, $pos+1);
} // if
$responseStr = "HTTP/1.0 $httperr";
debugEcho('failure() called:');
debugEcho(' ' . $httperr);
debugEcho(' ' . $errmsg);
doHeader($responseStr);
doHeader('Server: ' . $GServerString);
doHeader('Date: ' . HTTP::date());
if (isset($location))
doHeader('Location: ' . $location);
doHeader('Connection: close');
Aug 30, 2008
Aug 30, 2008
352
doHeader('Content-type: text/plain; charset=utf-8');
Jul 17, 2005
Jul 17, 2005
353
354
355
356
print("$errmsg\n");
terminate();
} // failure
Aug 2, 2005
Aug 2, 2005
357
358
359
function invalidContentRange($startRange, $endRange, $max)
{
if (($startRange < 0) || ($startRange >= $max))
Aug 2, 2005
Aug 2, 2005
360
return(true);
Aug 2, 2005
Aug 2, 2005
361
if (($endRange < 0) || ($endRange >= $max))
Aug 2, 2005
Aug 2, 2005
362
return(true);
Aug 2, 2005
Aug 2, 2005
363
if ($startRange > $endRange)
Aug 2, 2005
Aug 2, 2005
364
365
return(true);
return(false);
Aug 2, 2005
Aug 2, 2005
366
367
} // invalidContentRange
Jul 17, 2005
Jul 17, 2005
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
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
} // microtime_float
function stopwatch($id = NULL)
{
static $storedid = NULL;
static $tod = NULL;
if (!GDEBUG)
return;
$now = microtime_float();
if (isset($id))
$storedid = $id;
if (!isset($tod))
$tod = $now;
else
{
debugEcho("Stopwatch [$storedid]: " . ($now - $tod) . ' seconds.');
$tod = NULL;
} // else
} // stopwatch
Aug 2, 2005
Aug 2, 2005
399
400
401
402
403
404
405
406
407
408
409
410
411
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno)
{
case E_USER_ERROR:
debugEcho("PHP ERROR TRIGGERED: [$errno] $errstr");
debugEcho(" Fatal error in line $errline of file $errfile");
debugEcho(", PHP " . PHP_VERSION . " (" . PHP_OS . ")");
debugEcho("Aborting...");
exit(1);
break;
case E_USER_WARNING:
Aug 2, 2005
Aug 2, 2005
412
debugEcho("PHP WARNING TRIGGERED: [$errno] $errstr");
Aug 2, 2005
Aug 2, 2005
413
414
415
416
417
418
419
420
421
422
423
break;
case E_USER_NOTICE:
debugEcho("PHP NOTICE TRIGGERED:</b> [$errno] $errstr");
break;
default:
debugEcho("Unknown PHP error triggered!: [$errno] $errstr");
break;
} // switch
} // myErrorHandler
Jul 17, 2005
Jul 17, 2005
424
425
426
427
428
function debugInit()
{
global $Guri;
if (GDEBUG)
{
Aug 30, 2008
Aug 30, 2008
429
header('Content-type: text/plain; charset=utf-8');
Jul 21, 2005
Jul 21, 2005
430
431
432
debugEcho('');
debugEcho('');
debugEcho('');
Jul 17, 2005
Jul 17, 2005
433
434
debugEcho('Offload Debug Run!');
debugEcho('');
Jul 21, 2005
Jul 21, 2005
435
debugEcho('Timestamp: ' . date('D M j G:i:s T Y'));
Jul 17, 2005
Jul 17, 2005
436
debugEcho('Base server:' . GBASESERVER);
Jul 21, 2005
Jul 21, 2005
437
438
439
440
debugEcho('User wants to get: ' . $Guri);
debugEcho('Request from address: ' . $_SERVER['REMOTE_ADDR'] . '.');
debugEcho('Client User-Agent: "' . $_SERVER['HTTP_USER_AGENT'] . '".');
debugEcho('Referrer string: "' . $_SERVER['HTTP_REFERER'] . '".');
Jul 17, 2005
Jul 17, 2005
441
442
debugEcho('Timeout for HTTP HEAD request is ' . GTIMEOUT . '.');
debugEcho('Data cache goes in "' . GOFFLOADDIR . '".');
Aug 2, 2005
Aug 2, 2005
443
debugEcho('My PID: ' . getmypid());
Jul 17, 2005
Jul 17, 2005
444
445
446
debugEcho('');
debugEcho('');
} // if
Aug 2, 2005
Aug 2, 2005
447
448
449
450
// force PHP errors to not go through debug system and not to user.
error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
set_error_handler('myErrorHandler');
Jul 17, 2005
Jul 17, 2005
451
452
453
454
455
456
457
458
} // debugInit
// The mainline...
debugInit();
Aug 2, 2005
Aug 2, 2005
459
460
461
// try to prevent script timeout.
set_time_limit(0);
Jul 17, 2005
Jul 17, 2005
462
// Feed a fake robots.txt to keep webcrawlers out of the offload server.
Aug 2, 2005
Aug 2, 2005
463
if (strcmp($Guri, "/robots.txt") == 0)
Jul 17, 2005
Jul 17, 2005
464
465
466
467
468
failure('200 OK', "User-agent: *\nDisallow: /");
if (sanestrpos($Guri, '?') >= 0)
failure('403 Forbidden', "Offload server doesn't do dynamic content.");
Aug 31, 2008
Aug 31, 2008
469
470
471
472
473
474
475
476
477
$reqmethod = $_SERVER['REDIRECT_REQUEST_METHOD'];
if (!isset($reqmethod)
$reqmethod = $_SERVER['REQUEST_METHOD'];
if (!isset($reqmethod)
$reqmethod = 'GET';
$ishead = (strcasecmp($reqmethod, 'HEAD') == 0);
$isget = (strcasecmp($reqmethod, 'GET') == 0);
if ((!ishead) && (!isget))
Jul 17, 2005
Jul 17, 2005
478
479
480
481
482
483
484
485
486
failure('403 Forbidden', "Offload server doesn't do dynamic content.");
$origurl = 'http://' . GBASESERVER . $Guri;
stopwatch('HEAD transaction');
$head = HTTP::head($origurl, GTIMEOUT);
stopwatch();
if (PEAR::isError($head))
failure('503 Service Unavailable', 'Error: ' . $head->getMessage());
Aug 1, 2005
Aug 1, 2005
487
debugEcho('The HTTP HEAD from ' . GBASESERVER . ' ...');
Jul 17, 2005
Jul 17, 2005
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
debugEcho($head);
if (($head['response_code'] == 401) || (isset($head['WWW-Authenticate'])))
failure('403 Forbidden', "Offload server doesn't do protected content.");
else if ($head['response_code'] != 200)
failure($head['response'], $head['response'], $head['Location']);
if ( (!isset($head['ETag'])) ||
(!isset($head['Content-Length'])) ||
(!isset($head['Last-Modified'])) )
{
failure('403 Forbidden', "Offload server doesn't do dynamic content.");
} // if
Aug 1, 2005
Aug 1, 2005
503
$head['X-Offload-Orig-ETag'] = $head['ETag'];
Aug 1, 2005
Aug 1, 2005
504
$head['X-Offload-Is-Weak'] = '0';
Aug 1, 2005
Aug 1, 2005
505
506
507
if (strlen($head['ETag']) > 2)
{
// a "weak" ETag?
Aug 1, 2005
Aug 1, 2005
508
509
if (strncasecmp($head['ETag'], "W/", 2) == 0)
{
Aug 2, 2005
Aug 2, 2005
510
debugEcho("There's a weak ETag on this request.");
Aug 1, 2005
Aug 1, 2005
511
$head['X-Offload-Is-Weak'] = '1';
Aug 1, 2005
Aug 1, 2005
512
$head['ETag'] = substr($head['ETag'], 2);
Aug 1, 2005
Aug 1, 2005
513
514
debugEcho('Chopped ETag to be [' . $head['ETag'] . ']');
} // if
Aug 1, 2005
Aug 1, 2005
515
516
} // if
Jul 17, 2005
Jul 17, 2005
517
518
519
520
521
522
523
524
// !!! FIXME: Check Cache-Control, Pragma no-cache
$cacheio = NULL; // will be non-NULL if we're WRITING to the cache...
$frombaseserver = false;
$io = NULL; // read from this. May be file or HTTP connection.
// HTTP HEAD requests for PHP scripts otherwise run fully and throw away the
// results: http://www.figby.com/archives/2004/06/01/2004-06-01-php/
Aug 2, 2005
Aug 2, 2005
525
526
if ($ishead)
debugEcho('This is a HEAD request to the offload server.');
Jul 17, 2005
Jul 17, 2005
527
Aug 2, 2005
Aug 2, 2005
528
529
530
531
532
// Partial content:
// Does client want a range (download resume, "web accelerators", etc)?
$max = $head['Content-Length'];
$startRange = 0;
$endRange = $max-1;
Jul 15, 2007
Jul 15, 2007
533
$responseCode = '200 OK';
Aug 2, 2005
Aug 2, 2005
534
$reportRange = 0;
Aug 2, 2005
Aug 2, 2005
535
536
537
538
539
540
541
542
543
544
if (isset($HTTP_SERVER_VARS['HTTP_IF_RANGE']))
{
// !!! FIXME: handle this.
$ifrange = $HTTP_SERVER_VARS['HTTP_IF_RANGE'];
debugEcho("Client set If-Range: [$ifrange]...unsupported!");
if (isset($HTTP_SERVER_VARS['HTTP_RANGE']))
unset($HTTP_SERVER_VARS['HTTP_RANGE']);
} // if
Aug 2, 2005
Aug 2, 2005
545
546
547
548
549
550
if (isset($HTTP_SERVER_VARS['HTTP_RANGE']))
{
$range = $HTTP_SERVER_VARS['HTTP_RANGE'];
debugEcho("There's a HTTP_RANGE specified: [$range].");
if (strncasecmp($range, 'bytes=', 6) != 0)
failure('400 Bad Request', 'Only ranges of "bytes" accepted.');
Aug 2, 2005
Aug 2, 2005
551
552
else if (strpos($range, ',') !== false)
failure('400 Bad Request', 'Multiple ranges not currently supported');
Aug 2, 2005
Aug 2, 2005
553
554
555
556
557
558
559
560
561
562
563
564
else
{
$range = substr($range, 6);
$pos = strpos($range, '-');
if ($pos !== false)
{
$startRange = trim(substr($range, 0, $pos));
$endRange = trim(substr($range, $pos + 1));
if (strcmp($startRange, '') == 0)
$startRange = 0;
if (strcmp($endRange, '') == 0)
$endRange = $max-1;
Jul 15, 2007
Jul 15, 2007
565
$responseCode = '206 Partial Content';
Aug 2, 2005
Aug 2, 2005
566
567
568
569
570
$reportRange = 1;
} // if
} // else
} // if
Aug 2, 2005
Aug 2, 2005
571
572
573
if ($endRange >= $max) // apparently, this is legal to request.
$endRange = $max - 1;
Aug 2, 2005
Aug 2, 2005
574
debugEcho("We are feeding the client bytes $startRange to $endRange of $max");
Aug 2, 2005
Aug 2, 2005
575
576
577
if (invalidContentRange($startRange, $endRange, $max))
failure('400 Bad Request', 'Bad content range requested.');
Dec 9, 2005
Dec 9, 2005
578
579
580
$GEtagFname = etagToCacheFname($head['ETag']);
$GFilePath = GOFFLOADDIR . '/filedata-' . $GEtagFname;
$GMetaDataPath = GOFFLOADDIR . '/metadata-' . $GEtagFname;
Jul 17, 2005
Jul 17, 2005
581
$head['X-Offload-Orig-URL'] = $Guri;
Jul 21, 2005
Jul 21, 2005
582
$head['X-Offload-Hostname'] = GBASESERVER;
Jul 17, 2005
Jul 17, 2005
583
Jul 21, 2005
Jul 21, 2005
584
585
586
debugEcho('metadata cache is ' . $GMetaDataPath);
debugEcho('file cache is ' . $GFilePath);
Aug 2, 2005
Aug 2, 2005
587
588
589
if ($ishead)
$metadata = $head;
else
Jul 17, 2005
Jul 17, 2005
590
{
Aug 2, 2005
Aug 2, 2005
591
getSemaphore();
Jul 17, 2005
Jul 17, 2005
592
Aug 2, 2005
Aug 2, 2005
593
594
595
596
597
598
599
600
$metadata = loadMetadata($GMetaDataPath);
if (cachedMetadataMostRecent($metadata, $head))
{
$io = @fopen($GFilePath, 'rb');
if ($io === false)
failure('500 Internal Server Error', "Couldn't access cached data.");
debugEcho('File is cached.');
} // else if
Jul 17, 2005
Jul 17, 2005
601
Aug 2, 2005
Aug 2, 2005
602
603
604
else
{
// we need to pull a new copy from the base server...
Jul 17, 2005
Jul 17, 2005
605
Aug 2, 2005
Aug 2, 2005
606
ignore_user_abort(true); // if we're caching, we MUST run to completion!
Jul 17, 2005
Jul 17, 2005
607
Aug 2, 2005
Aug 2, 2005
608
$frombaseserver = true;
Jul 14, 2007
Jul 14, 2007
609
610
$io = NULL;
$getheaders = HTTP::get($io, $origurl, GTIMEOUT); // !!! FIXME: may block, don't hold semaphore here!
Aug 2, 2005
Aug 2, 2005
611
612
613
614
if ($io === false)
failure('503 Service Unavailable', "Couldn't stream file to cache.");
stream_set_blocking($io, false);
stream_set_timeout($io, 60);
Jul 17, 2005
Jul 17, 2005
615
Aug 2, 2005
Aug 2, 2005
616
617
618
619
620
621
$cacheio = @fopen($GFilePath, 'wb');
if ($cacheio === false)
{
fclose($io);
failure('500 Internal Server Error', "Couldn't update cached data.");
} // if
Jul 17, 2005
Jul 17, 2005
622
Aug 2, 2005
Aug 2, 2005
623
624
625
626
627
628
629
630
$metaout = @fopen($GMetaDataPath, 'wb');
if ($metaout === false)
{
fclose($cacheio);
fclose($io);
nukeRequestFromCache();
failure('500 Internal Server Error', "Couldn't update metadata.");
} // if
Jul 17, 2005
Jul 17, 2005
631
Aug 2, 2005
Aug 2, 2005
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// !!! FIXME: This is a race condition...may change between HEAD
// !!! FIXME: request and actual HTTP grab. We should really
// !!! FIXME: just use this for comparison once, and if we are
// !!! FIXME: recaching, throw this out and use the headers from the
// !!! FIXME: actual HTTP grab when really updating the metadata.
//
// !!! FIXME: Also, write to temp file and rename in case of write failure!
if (!isset($head['Content-Type'])) // make sure this is sane.
$head['Content-Type'] = 'application/octet-stream';
$head['X-Offload-Caching-PID'] = getmypid();
foreach ($head as $key => $val)
fputs($metaout, $key . "\n" . $val . "\n");
fclose($metaout);
$metadata = $head;
debugEcho('Cache needs refresh...pulling from base server...');
} // else
putSemaphore();
} // else
Jul 17, 2005
Jul 17, 2005
653
Jul 15, 2007
Jul 15, 2007
654
doHeader('Status: ' . $responseCode);
Jul 17, 2005
Jul 17, 2005
655
656
657
658
659
doHeader('Date: ' . HTTP::date());
doHeader('Server: ' . $GServerString);
doHeader('Connection: close');
doHeader('ETag: ' . $metadata['ETag']);
doHeader('Last-Modified: ' . $metadata['Last-Modified']);
Aug 2, 2005
Aug 2, 2005
660
doHeader('Content-Length: ' . (($endRange - $startRange) + 1));
Aug 2, 2005
Aug 2, 2005
661
doHeader('Accept-Ranges: bytes');
Jul 17, 2005
Jul 17, 2005
662
doHeader('Content-Type: ' . $metadata['Content-Type']);
Aug 2, 2005
Aug 2, 2005
663
664
if ($reportRange)
doHeader("Content-Range: bytes $startRange-$endRange/$max");
Jul 17, 2005
Jul 17, 2005
665
666
if ($ishead)
Aug 2, 2005
Aug 2, 2005
667
668
{
debugEcho('This was a HEAD request to offload server, so it is done.');
Jul 17, 2005
Jul 17, 2005
669
terminate();
Aug 2, 2005
Aug 2, 2005
670
} // if
Jul 17, 2005
Jul 17, 2005
671
672
$br = 0;
Aug 2, 2005
Aug 2, 2005
673
$endRange++;
Aug 2, 2005
Aug 2, 2005
674
while ($br < $endRange)
Jul 17, 2005
Jul 17, 2005
675
{
Aug 2, 2005
Aug 2, 2005
676
677
678
679
680
681
682
683
684
685
$readsize = $startRange - $br;
if (($readsize <= 0) || ($readsize > 8192))
$readsize = 8192;
if ($readsize > ($endRange - $br))
$readsize = ($endRange - $br);
if ($readsize == 0)
break; // Shouldn't hit, but just in case...
Jul 21, 2005
Jul 21, 2005
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
if (feof($io))
{
debugEcho('feof() triggered.');
break;
} // if
if ($frombaseserver)
{
$info = stream_get_meta_data($io);
if ($info['eof'])
{
debugEcho('socket meta data has eof flag.');
break;
} // if
else if ($info['timed_out'])
{
debugEcho('socket meta data has timed_out flag.');
break;
} // if
} // if
else
Jul 17, 2005
Jul 17, 2005
709
710
711
712
713
714
715
716
{
$stat = @fstat($io);
if ($stat === false)
break;
$cursize = $stat['size'];
if ($cursize < $max)
{
Aug 2, 2005
Aug 2, 2005
717
if (($cursize - $br) <= $readsize) // may be caching on another process.
Jul 17, 2005
Jul 17, 2005
718
719
720
721
722
723
724
{
sleep(1);
continue;
} // if
} // if
} // if
Aug 2, 2005
Aug 2, 2005
725
$data = @fread($io, $readsize);
Jul 21, 2005
Jul 21, 2005
726
727
$len = strlen($data);
if ($len > 0)
Jul 17, 2005
Jul 17, 2005
728
{
Jul 21, 2005
Jul 21, 2005
729
730
731
732
733
if (isset($cacheio))
{
fwrite($cacheio, $data); // !!! FIXME: check for errors!
fflush($cacheio);
} // if
Jul 17, 2005
Jul 17, 2005
734
Jul 21, 2005
Jul 21, 2005
735
736
737
if (!connection_aborted())
{
if ((!GDEBUG) || (GDEBUGTOFILE))
Aug 2, 2005
Aug 2, 2005
738
739
{
if (($br >= $startRange) && ($br < $endRange))
Aug 2, 2005
Aug 2, 2005
740
741
742
{
$verb = GDEBUGTOFILE ? 'Wrote ' : 'Would have written ';
debugEcho($verb . $len . ' bytes.');
Aug 2, 2005
Aug 2, 2005
743
print($data);
Aug 2, 2005
Aug 2, 2005
744
} // if
Aug 2, 2005
Aug 2, 2005
745
} // if
Jul 21, 2005
Jul 21, 2005
746
} // if
Aug 2, 2005
Aug 2, 2005
747
$br += $len;
Aug 2, 2005
Aug 2, 2005
748
Aug 2, 2005
Aug 2, 2005
749
750
751
752
753
754
// If this connection is cacheing from base server, we have to keep going.
if (($br == $endRange) && (isset($cacheio)) && ($br != $max))
{
debugEcho('Sent complete request, but am pulling from base server!');
$endRange = $max;
} // if
Aug 2, 2005
Aug 2, 2005
755
} // if
Jul 17, 2005
Jul 17, 2005
756
757
758
759
} // while
debugEcho('Transfer is complete.');
Aug 31, 2008
Aug 31, 2008
760
Aug 2, 2005
Aug 2, 2005
761
762
763
764
if (isset($cacheio))
@fclose($cacheio);
if ($br != $endRange)
Jul 17, 2005
Jul 17, 2005
765
{
Aug 2, 2005
Aug 2, 2005
766
debugEcho("Bogus transfer! Sent $br, wanted to send $endRange!");
Aug 31, 2008
Aug 31, 2008
767
768
if ($frombaseserver)
nukeRequestFromCache();
Jul 17, 2005
Jul 17, 2005
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
} // if
terminate();
// end of offload script ...
// This is HTTP from PEAR. Copied here for my convenience.
// I trimmed some stuff out and hacked on some other code.
// --ryan.
class HTTP
{
Jul 14, 2007
Jul 14, 2007
784
static function Date($time = null)
Jul 17, 2005
Jul 17, 2005
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
{
if (!isset($time)) {
$time = time();
} elseif (!is_numeric($time) && (-1 === $time = strtotime($time))) {
return(false);
}
// RFC822 or RFC850
$format = ini_get('y2k_compliance') ? 'D, d M Y' : 'l, d-M-y';
return gmdate($format .' H:i:s \G\M\T', $time);
}
function negotiateLanguage($supported, $default = 'en-US')
{
$supp = array();
foreach ($supported as $lang => $isSupported) {
if ($isSupported) {
$supp[strToLower($lang)] = $lang;
}
}
if (!count($supp)) {
return $default;
}
$matches = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
$lang = array_map('trim', explode(';', $lang));
if (isset($lang[1])) {
$l = strtolower($lang[0]);
$q = (float) str_replace('q=', '', $lang[1]);
} else {
$l = strtolower($lang[0]);
$q = null;
}
if (isset($supp[$l])) {
$matches[$l] = isset($q) ? $q : 1000 - count($matches);
}
}
}
if (count($matches)) {
asort($matches, SORT_NUMERIC);
return $supp[array_pop(array_keys($matches))];
}
if (isset($_SERVER['REMOTE_HOST'])) {
$lang = strtolower(array_pop(explode('.', $_SERVER['REMOTE_HOST'])));
if (isset($supp[$lang])) {
return $supp[$lang];
}
}
return $default;
}
Jul 14, 2007
Jul 14, 2007
843
static function head($url, $timeout = 10)
Jul 17, 2005
Jul 17, 2005
844
845
846
847
848
849
850
851
852
853
854
{
$p = parse_url($url);
if (!isset($p['scheme'])) {
$p = parse_url(HTTP::absoluteURI($url));
} elseif ($p['scheme'] != 'http') {
return HTTP::raiseError('Unsupported protocol: '. $p['scheme']);
}
$port = isset($p['port']) ? $p['port'] : 80;
//debugEcho(array($p['host'], $port, $eno, $estr, $timeout));
Jul 19, 2005
Jul 19, 2005
855
$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout);
Jul 19, 2005
Jul 19, 2005
856
if ($fp === false) {
Jul 17, 2005
Jul 17, 2005
857
858
if ($eno == 0) { // dns lookup failure seems to trigger this. --ryan.
sleep(3);
Jul 19, 2005
Jul 19, 2005
859
$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout);
Jul 19, 2005
Jul 19, 2005
860
if ($fp === false) {
Jul 17, 2005
Jul 17, 2005
861
862
863
864
865
866
867
868
return HTTP::raiseError("Connection error: $estr ($eno)");
}
}
}
$path = !empty($p['path']) ? $p['path'] : '/';
$path .= !empty($p['query']) ? '?' . $p['query'] : '';
Jul 19, 2005
Jul 19, 2005
869
870
871
872
873
874
875
876
if (@fputs($fp, "HEAD $path HTTP/1.0\r\n") === false)
return HTTP::raiseError("i/o error");
if (@fputs($fp, 'Host: ' . $p['host'] . ':' . $port . "\r\n") === false)
return HTTP::raiseError("i/o error");
if (@fputs($fp, "Connection: close\r\n\r\n") === false)
return HTTP::raiseError("i/o error");
Jul 17, 2005
Jul 17, 2005
877
878
879
880
881
882
883
$response = rtrim(fgets($fp, 4096));
if (preg_match("|^HTTP/[^\s]*\s(.*?)\s|", $response, $status)) {
$headers['response_code'] = $status[1];
}
$headers['response'] = $response;
Jul 19, 2005
Jul 19, 2005
884
while ($line = @fgets($fp, 4096)) {
Jul 17, 2005
Jul 17, 2005
885
886
887
888
889
890
891
892
893
894
895
896
897
if (!trim($line)) {
break;
}
if (($pos = strpos($line, ':')) !== false) {
$header = substr($line, 0, $pos);
$value = trim(substr($line, $pos + 1));
$headers[$header] = $value;
}
}
fclose($fp);
return $headers;
}
Jul 14, 2007
Jul 14, 2007
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
static function get(&$fp, $url, $timeout = 10)
{
$p = parse_url($url);
if (!isset($p['scheme'])) {
$p = parse_url(HTTP::absoluteURI($url));
} elseif ($p['scheme'] != 'http') {
return HTTP::raiseError('Unsupported protocol: '. $p['scheme']);
}
$port = isset($p['port']) ? $p['port'] : 80;
//debugEcho(array($p['host'], $port, $eno, $estr, $timeout));
$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout);
if ($fp === false) {
if ($eno == 0) { // dns lookup failure seems to trigger this. --ryan.
sleep(3);
$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout);
if ($fp === false) {
return HTTP::raiseError("Connection error: $estr ($eno)");
}
}
}
$path = !empty($p['path']) ? $p['path'] : '/';
$path .= !empty($p['query']) ? '?' . $p['query'] : '';
if (@fputs($fp, "GET $path HTTP/1.0\r\n") === false)
return HTTP::raiseError("i/o error");
if (@fputs($fp, 'Host: ' . $p['host'] . ':' . $port . "\r\n") === false)
return HTTP::raiseError("i/o error");
if (@fputs($fp, "Connection: close\r\n") === false)
return HTTP::raiseError("i/o error");
if (@fputs($fp, "X-Mod-Offload-Bypass: true\r\n\r\n") === false)
return HTTP::raiseError("i/o error");
$response = rtrim(fgets($fp, 4096));
if (preg_match("|^HTTP/[^\s]*\s(.*?)\s|", $response, $status)) {
$headers['response_code'] = $status[1];
}
$headers['response'] = $response;
while ($line = @fgets($fp, 4096)) {
if (trim($line) == '') {
break;
}
if (($pos = strpos($line, ':')) !== false) {
$header = substr($line, 0, $pos);
$value = trim(substr($line, $pos + 1));
$headers[$header] = $value;
}
}
return $headers;
}
Jul 17, 2005
Jul 17, 2005
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function absoluteURI($url = null, $protocol = null, $port = null)
{
// filter CR/LF
$url = str_replace(array("\r", "\n"), ' ', $url);
// Mess around with already absolute URIs
if (preg_match('!^([a-z0-9]+)://!i', $url)) {
if (empty($protocol) && empty($port)) {
return $url;
}
if (!empty($protocol)) {
$url = $protocol .':'. array_pop(explode(':', $url, 2));
}
if (!empty($port)) {
$url = preg_replace('!^(([a-z0-9]+)://[^/:]+)(:[\d]+)?!i',
'\1:'. $port, $url);
}
return $url;
}
$host = 'localhost';
if (!empty($_SERVER['HTTP_HOST'])) {
list($host) = explode(':', $_SERVER['HTTP_HOST']);
} elseif (!empty($_SERVER['SERVER_NAME'])) {
list($host) = explode(':', $_SERVER['SERVER_NAME']);
}
if (empty($protocol)) {
if (isset($_SERVER['HTTPS']) && !strcasecmp($_SERVER['HTTPS'], 'on')) {
$protocol = 'https';
} else {
$protocol = 'http';
}
if (!isset($port) || $port != intval($port)) {
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80;
}
}
if ($protocol == 'http' && $port == 80) {
unset($port);
}
if ($protocol == 'https' && $port == 443) {
unset($port);
}
$server = $protocol .'://'. $host . (isset($port) ? ':'. $port : '');