Skip to content

Instantly share code, notes, and snippets.

@radist2s
Last active December 29, 2015 16:09
Show Gist options
  • Save radist2s/7695705 to your computer and use it in GitHub Desktop.
Save radist2s/7695705 to your computer and use it in GitHub Desktop.
Simple Web Server file throttle sending via PHP. (Low bandwidth emulation)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.+\.(?:jpg|jpeg|gif|png))$ /throttle.php?$1 [L]
</IfModule>
<?php
if (!empty($_SERVER['REDIRECT_QUERY_STRING']) AND $filepath = $_SERVER['REDIRECT_QUERY_STRING'] AND is_file($filepath))
{
if ($mime = detect_mime($filepath))
{
header("Content-type: $mime");
srand(time());
$sleep = rand(0, 1);
sleep($sleep);
echo file_get_contents($filepath);
}
else
{
fallback();
}
}
else
{
fallback();
}
function fallback()
{
header("Location: {$_SERVER['REDIRECT_QUERY_STRING']}");
exit;
}
function detect_mime($filepath)
{
if(function_exists('mime_content_type'))
{
return mime_content_type($filepath);
}
$filename = basename($filepath);
$extension = array_pop(explode('.', $filename));
switch($extension)
{
case "js":
return "application/x-javascript";
case "json":
return "application/json";
case "jpg":
case "jpeg":
case "jpe":
return "image/jpg";
case "png":
return "image/png";
case "gif":
return "image/gif";
case "css":
return "text/css";
case "xml":
return "application/xml";
case "doc":
case "docx":
return "application/msword";
case "xls":
case "xlt":
case "xlm":
case "xld":
case "xla":
case "xlc":
case "xlw":
case "xll":
return "application/vnd.ms-excel";
case "ppt":
case "pps":
return "application/vnd.ms-powerpoint";
case "rtf":
return "application/rtf";
case "pdf":
return "application/pdf";
case "html":
case "htm":
case "php":
return "text/html";
case "txt":
return "text/plain";
case "mpeg":
case "mpg":
case "mpe":
return "video/mpeg";
case "mp3":
return "audio/mpeg3";
case "wav":
return "audio/wav";
case "aiff":
case "aif":
return "audio/aiff";
case "avi":
return "video/msvideo";
case "wmv":
return "video/x-ms-wmv";
case "mov":
return "video/quicktime";
case "zip":
return "application/zip";
case "tar":
return "application/x-tar";
case "swf":
return "application/x-shockwave-flash";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment