Skip to content

Instantly share code, notes, and snippets.

@bwoebi
Last active December 13, 2015 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwoebi/df934c4e0629a2dccb66 to your computer and use it in GitHub Desktop.
Save bwoebi/df934c4e0629a2dccb66 to your computer and use it in GitHub Desktop.
Local Aerys Redirect
<?php
$dispatcher = new class implements \Aerys\Middleware {
public function __invoke(\Aerys\Request $req, \Aerys\Response $res) {
/* dispatch */
if (/* $local_request === */ true) {
$started = false;
$oldireq = $req->getLocalVar("cgihandler.ireq");
$ireq = clone $oldireq;
$ireq->locals = [];
/* manipulate URI here */
$ireq->uri = "{$ireq->protocol}://{$ireq->uriHost}:{$ireq->uriPort}/filterstream";
$ireq->uriPath = "/filterstream";
$ireq->uriQuery = "";
try {
$filters = $ireq->client->httpDriver->filters($ireq);
$filters = array_merge([function() use (&$started) {
$headers = yield;
$started = true;
return $headers;
}], $filters);
$filter = \Aerys\responseFilter($filters, $ireq);
$filter->current(); // initialize filters
$codec = \Aerys\responseCodec($filter, $ireq);
$ireq->response = new \Aerys\StandardResponse($codec);
$gen = $ireq->vhost->getApplication()(new \Aerys\StandardRequest($ireq), $ireq->response);
if ($gen instanceof \Generator) {
yield from $gen;
}
// prevent any writes for this response...
$oldireq->responseWriter = (function() { while (1) yield; })();
} catch (\Throwable $e) {
if ($started) {
// force append
$res->stream("");
}
throw $e;
}
}
}
public function do(\Aerys\InternalRequest $ireq) {
$ireq->locals["cgihandler.ireq"] = $ireq;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment