/
offload.php
executable file
·1035 lines (865 loc) · 30.9 KB
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
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.
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"
//
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
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.
70
//
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.
//
75
76
77
// Then edit offload_server_config.php to fit your needs.
//
// Restart the server so the AliasMatch configuration tweak is picked up.
78
79
80
//
// This file is written by Ryan C. Gordon (icculus@icculus.org).
81
require_once './offload_server_config.php';
82
83
require_once 'PEAR.php';
84
define('GVERSION', '1.0.1');
85
$GServerString = 'offload.php/' . GVERSION;
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;
94
$GDebugFilePointer = NULL;
95
96
$GLockDir = GOFFLOADDIR . '/lock-';
$GEtagFname = NULL;
97
98
99
100
101
102
103
function getDebugFilePointer()
{
global $GDebugFilePointer;
if ((!GDEBUG) || (!GDEBUGTOFILE))
return(NULL);
if (!isset($GDebugFilePointer))
104
{
105
$GDebugFilePointer = fopen(GOFFLOADDIR . '/debug-' . getmypid(), 'a');
106
107
108
if ($GDebugFilePointer === false)
$GDebugFilePointer = NULL;
} // if
109
110
return($GDebugFilePointer);
} // getDebugFilePointer
111
112
113
114
115
116
function debugEcho($str)
{
if (GDEBUG)
{
117
118
119
120
121
if (!is_array($str))
$str = $str . "\n";
if (!GDEBUGTOFILE)
print($str);
122
else
123
124
125
126
127
128
129
130
{
$fp = getDebugFilePointer();
if (isset($fp))
{
@fputs($fp, print_r($str, true));
@fflush($fp);
} // if
} // else
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()
{
143
global $GSemaphore, $GSemaphoreOwned, $GLockDir, $GEtagFname;
144
145
146
147
148
debugEcho("grabbing semaphore...(owned $GSemaphoreOwned time(s).)");
if ($GSemaphoreOwned++ > 0)
return;
149
if (GUSESEMAPHORE)
150
{
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);
159
} // if
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
181
182
183
184
185
} // getSemaphore
function putSemaphore()
{
186
187
global $GSemaphore, $GSemaphoreOwned, $GLockDir, $GEtagFname;
if ($GSemaphoreOwned == 0)
188
189
190
return;
if (--$GSemaphoreOwned == 0)
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
203
204
205
206
207
208
debugEcho("released semaphore...(now owned $GSemaphoreOwned time(s).)");
} // putSemaphore
function terminate()
{
209
global $GDebugFilePointer, $GSemaphoreOwned;
210
211
212
213
debugEcho('offload script is terminating...');
while ($GSemaphoreOwned > 0)
putSemaphore();
214
215
if (isset($GDebugFilePointer))
216
@fclose($GDebugFilePointer);
217
218
219
220
221
222
exit();
} // terminate
function doHeader($str)
{
223
if ((!GDEBUG) || (GDEBUGTOFILE))
224
225
226
227
228
229
{
header($str, true);
if (headers_sent($filename, $linenum))
debugEcho("Headers already sent in $filename on line $linenum");
}
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)
{
264
265
global $GFilePath;
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)
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
288
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'])
{
299
300
301
// whoa, we were supposed to cache this!
if ($metadata['X-Offload-Caching-PID'] == getmypid())
return(false);
302
303
else if ($metadata['X-Offload-Caching-PID'] <= 0)
return(false);
304
305
306
// !!! FIXME: Unix specific!
if (!posix_kill($metadata['X-Offload-Caching-PID'], 0))
307
{
308
309
debugEcho('Caching process ID died!');
return(false);
310
311
} // if
} // if
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');
352
doHeader('Content-type: text/plain; charset=utf-8');
353
354
355
356
print("$errmsg\n");
terminate();
} // failure
357
358
359
function invalidContentRange($startRange, $endRange, $max)
{
if (($startRange < 0) || ($startRange >= $max))
360
return(true);
361
if (($endRange < 0) || ($endRange >= $max))
362
return(true);
363
if ($startRange > $endRange)
364
365
return(true);
return(false);
366
367
} // invalidContentRange
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
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:
412
debugEcho("PHP WARNING TRIGGERED: [$errno] $errstr");
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
424
425
426
427
428
function debugInit()
{
global $Guri;
if (GDEBUG)
{
429
header('Content-type: text/plain; charset=utf-8');
430
431
432
debugEcho('');
debugEcho('');
debugEcho('');
433
434
debugEcho('Offload Debug Run!');
debugEcho('');
435
debugEcho('Timestamp: ' . date('D M j G:i:s T Y'));
436
debugEcho('Base server:' . GBASESERVER);
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'] . '".');
441
442
debugEcho('Timeout for HTTP HEAD request is ' . GTIMEOUT . '.');
debugEcho('Data cache goes in "' . GOFFLOADDIR . '".');
443
debugEcho('My PID: ' . getmypid());
444
445
446
debugEcho('');
debugEcho('');
} // if
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');
451
452
453
454
455
456
457
458
} // debugInit
// The mainline...
debugInit();
459
460
461
// try to prevent script timeout.
set_time_limit(0);
462
// Feed a fake robots.txt to keep webcrawlers out of the offload server.
463
if (strcmp($Guri, "/robots.txt") == 0)
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.");
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))
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());
487
debugEcho('The HTTP HEAD from ' . GBASESERVER . ' ...');
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
503
$head['X-Offload-Orig-ETag'] = $head['ETag'];
504
$head['X-Offload-Is-Weak'] = '0';
505
506
507
if (strlen($head['ETag']) > 2)
{
// a "weak" ETag?
508
509
if (strncasecmp($head['ETag'], "W/", 2) == 0)
{
510
debugEcho("There's a weak ETag on this request.");
511
$head['X-Offload-Is-Weak'] = '1';
512
$head['ETag'] = substr($head['ETag'], 2);
513
514
debugEcho('Chopped ETag to be [' . $head['ETag'] . ']');
} // if
515
516
} // if
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/
525
526
if ($ishead)
debugEcho('This is a HEAD request to the offload server.');
527
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;
533
$responseCode = '200 OK';
534
$reportRange = 0;
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
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.');
551
552
else if (strpos($range, ',') !== false)
failure('400 Bad Request', 'Multiple ranges not currently supported');
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;
565
$responseCode = '206 Partial Content';
566
567
568
569
570
$reportRange = 1;
} // if
} // else
} // if
571
572
573
if ($endRange >= $max) // apparently, this is legal to request.
$endRange = $max - 1;
574
debugEcho("We are feeding the client bytes $startRange to $endRange of $max");
575
576
577
if (invalidContentRange($startRange, $endRange, $max))
failure('400 Bad Request', 'Bad content range requested.');
578
579
580
$GEtagFname = etagToCacheFname($head['ETag']);
$GFilePath = GOFFLOADDIR . '/filedata-' . $GEtagFname;
$GMetaDataPath = GOFFLOADDIR . '/metadata-' . $GEtagFname;
581
$head['X-Offload-Orig-URL'] = $Guri;
582
$head['X-Offload-Hostname'] = GBASESERVER;
583
584
585
586
debugEcho('metadata cache is ' . $GMetaDataPath);
debugEcho('file cache is ' . $GFilePath);
587
588
589
if ($ishead)
$metadata = $head;
else
590
{
591
getSemaphore();
592
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
601
602
603
604
else
{
// we need to pull a new copy from the base server...
605
606
ignore_user_abort(true); // if we're caching, we MUST run to completion!
607
608
$frombaseserver = true;
609
610
$io = NULL;
$getheaders = HTTP::get($io, $origurl, GTIMEOUT); // !!! FIXME: may block, don't hold semaphore here!
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);
615
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
622
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
631
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
653
654
doHeader('Status: ' . $responseCode);
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']);
660
doHeader('Content-Length: ' . (($endRange - $startRange) + 1));
661
doHeader('Accept-Ranges: bytes');
662
doHeader('Content-Type: ' . $metadata['Content-Type']);
663
664
if ($reportRange)
doHeader("Content-Range: bytes $startRange-$endRange/$max");
665
666
if ($ishead)
667
668
{
debugEcho('This was a HEAD request to offload server, so it is done.');
669
terminate();
670
} // if
671
672
$br = 0;
673
$endRange++;
674
while ($br < $endRange)
675
{
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...
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
709
710
711
712
713
714
715
716
{
$stat = @fstat($io);
if ($stat === false)
break;
$cursize = $stat['size'];
if ($cursize < $max)
{
717
if (($cursize - $br) <= $readsize) // may be caching on another process.
718
719
720
721
722
723
724
{
sleep(1);
continue;
} // if
} // if
} // if
725
$data = @fread($io, $readsize);
726
727
$len = strlen($data);
if ($len > 0)
728
{
729
730
731
732
733
if (isset($cacheio))
{
fwrite($cacheio, $data); // !!! FIXME: check for errors!
fflush($cacheio);
} // if
734
735
736
737
if (!connection_aborted())
{
if ((!GDEBUG) || (GDEBUGTOFILE))
738
739
{
if (($br >= $startRange) && ($br < $endRange))
740
741
742
{
$verb = GDEBUGTOFILE ? 'Wrote ' : 'Would have written ';
debugEcho($verb . $len . ' bytes.');
743
print($data);
744
} // if
745
} // if
746
} // if
747
$br += $len;
748
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
755
} // if
756
757
758
759
} // while
debugEcho('Transfer is complete.');
760
761
762
763
764
if (isset($cacheio))
@fclose($cacheio);
if ($br != $endRange)
765
{
766
debugEcho("Bogus transfer! Sent $br, wanted to send $endRange!");
767
768
if ($frombaseserver)
nukeRequestFromCache();
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
{
784
static function Date($time = null)
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;
}
843
static function head($url, $timeout = 10)
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));
855
$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout);
856
if ($fp === false) {
857
858
if ($eno == 0) { // dns lookup failure seems to trigger this. --ryan.
sleep(3);
859
$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout);
860
if ($fp === false) {
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'] : '';
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");
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;
884
while ($line = @fgets($fp, 4096)) {
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;
}
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;
}
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 : '');