Last active
December 5, 2017 13:54
-
-
Save wolffc/62860f06e5ec012e4dca05621150ea67 to your computer and use it in GitHub Desktop.
Script to delay an http resource for debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Aerticket\delay; | |
/** | |
* Script for Delaying Content Delivery for certain Resources | |
*/ | |
/** | |
* Created by PhpStorm. | |
* User: cwolff | |
* Date: 05.12.2017 | |
* Time: 09:48 | |
*/ | |
class delay | |
{ | |
const HELP_MESSAGE_HTML = '<h1>Delay Script</h1> | |
<pre>GET parameters: | |
<b>url</b> <i>required</i> url which should be fetched. | |
<b>delay</b> <i>optional</i> delay in millisecond 1000 equals 1 Second. | |
<b>chunk_size</b> <i>optional</i> chunksize, in bytes, every chunk will be delayed by <i>delay</i>. | |
</pre>'; | |
/** | |
* example usage: | |
* http://extranet-aerticket-de.local/packit/delay.php?delay=10&src=http%3A%2F%2Fextranet-aerticket-de.local%2Fpackit%2Fscripts%2Fpackit.js | |
*/ | |
public function run() | |
{ | |
$delay = isset($_GET['delay']) ? (int)$_GET['delay'] * 1000 : 0; | |
$chunkLength = isset($_GET['chunk_size']) ? (int)$_GET['chunk_size'] : 0; | |
$src = isset($_GET['url']) ? $_GET['url'] : null; | |
/** | |
* Main Programm | |
*/ | |
if (empty($src)) { | |
http_response_code(400); | |
echo self::HELP_MESSAGE_HTML; | |
exit(); | |
} else { | |
$response = $this->fetchHttpResource($src); | |
usleep($delay); | |
$body = $this->sendHeaderAndGetBody($response); | |
$this->sendBodyPartsDelayed($body, $delay, $chunkLength); | |
} | |
} | |
/** | |
* Sends the HTTP headers and Returns the Body | |
* | |
* @param $httpResponse | |
* | |
* @return string | |
*/ | |
function sendHeaderAndGetBody($httpResponse) | |
{ | |
// Split the headers from the body and fetch the headers | |
$parts = explode("\r\n\r\n", $httpResponse); | |
$headers = array_shift($parts); | |
// Send headers | |
foreach (explode("\r\n", $headers) as $header) { | |
$header = trim($header); | |
if ($header) header($header); | |
} | |
return implode("\r\n\r\n", $parts); // return body Part | |
} | |
/** | |
* Sends the Body Parts with a delay if we send Multiple Parts | |
* | |
* @param $body | |
* @param $delay | |
* @param $chunkLength | |
*/ | |
function sendBodyPartsDelayed($body, $delay, $chunkLength = 0) | |
{ | |
if ($chunkLength === 0) { | |
echo $body; | |
} else { | |
$bodyChunks = str_split($body, $chunkLength); | |
foreach ($bodyChunks as $chunk) { | |
usleep($delay); | |
echo $chunk; | |
} | |
} | |
} | |
/** | |
* Fetches the Remote Resource | |
* | |
* @param $src | |
* | |
* @return mixed | |
*/ | |
function fetchHttpResource($src) | |
{ | |
$curlHandle = curl_init(); | |
curl_setopt_array($curlHandle, [ | |
CURLOPT_URL => $src, | |
CURLOPT_HEADER => true, | |
CURLOPT_RETURNTRANSFER => true, | |
]); | |
$response = curl_exec($curlHandle); | |
return $response; | |
} | |
} | |
$delay = new delay(); | |
$delay->run(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment