public function handle(Event $event) { if (!$event->getParameter('main_request')) { return false; } $exception = $event->getParameter('exception'); if (null !== $this->logger) { $this->logger->err(sprintf('%s (uncaught %s exception)', $exception->getMessage(), get_class($exception))); } $parameters = array('_bundle' => $this->bundle, '_controller' => $this->controller, '_action' => $this->action, 'exception' => $exception, 'originalRequest' => $event->getParameter('request'), 'logs' => $this->container->hasService('zend.logger.writer.debug') ? $this->container->getService('zend.logger.writer.debug')->getLogs() : array()); $request = $event->getParameter('request')->duplicate(array('path' => $parameters)); try { $response = $event->getSubject()->handleRaw($request, false); } catch (\Exception $e) { return false; } $event->setReturnValue($response); return true; }
public function handle(Event $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) { return false; } $exception = $event->getParameter('exception'); if (null !== $this->logger) { $this->logger->err(sprintf('%s (uncaught %s exception)', $exception->getMessage(), get_class($exception))); } $parameters = array('_controller' => $this->controller, 'exception' => $exception, 'originalRequest' => $event->getParameter('request'), 'logs' => $this->container->hasService('zend.logger.writer.debug') ? $this->container->getService('zend.logger.writer.debug')->getLogs() : array()); $request = $event->getParameter('request')->duplicate(null, null, $parameters); try { $response = $event->getSubject()->handle($request, HttpKernelInterface::FORWARDED_REQUEST, true); error_log(sprintf('%s: %s', get_class($exception), $exception->getMessage())); } catch (\Exception $e) { return false; } $event->setReturnValue($response); return true; }
/** * @param Symfony\Components\EventDispatcher\Event $event * @param Bundle\ServerBundle\ResponseInterface $response * @return Bundle\ServerBundle\ResponseInterface * * @see Symfony\Components\EventDispatcher\EventDispatcher::filter() */ public function filter(Event $event, ResponseInterface $response) { $request = $event->getSubject(); if ($request->hasHeader('Accept-Encoding')) { foreach ($request->splitHttpAcceptHeader($request->getHeader('Accept-Encoding')) as $encoding) { $encoding = strtolower($encoding); if (in_array($encoding, $this->encodings)) { $response->setBody($compressed = call_user_func(array($this, $encoding), $body = $response->getBody())); $response->setHeader('Content-Encoding', $encoding); $response->setHeader('Content-Length', $length = strlen($compressed)); // add compression statistics $event->setParameter('compression', true); $event->setParameter('compression.encoding', $encoding); $event->setParameter('compression.level', $this->level); $event->setParameter('compression.before', strlen($body)); $event->setParameter('compression.after', $length); return $response; } } } return $response; }
/** * @param Symfony\Components\EventDispatcher\Event $event * * @see Symfony\Components\EventDispatcher\EventDispatcher::notifyUntil() */ public function handle(Event $event) { // get HttpMessage request $request = $event->getSubject(); $code = 404; $status = SymfonyResponse::$statusTexts[$code]; $headers = array(); $content = '<h1>Error 404 - Not Found</h1>'; // ExceptionController-like view renderer would be cool // add Date header $date = new \DateTime(); $headers['Date'] = $date->format(DATE_RFC822); // add Content-Length header $headers['Content-Length'] = strlen($content); // build Response $response = $this->container->getServer_ResponseService(); $response->setHttpVersion($request->getHttpVersion()); $response->setStatusCode($code, $status); $response->addHeaders($headers); $response->setBody($content); $event->setReturnValue($response); return true; }
/** * @param Symfony\Components\EventDispatcher\Event $event * * @see Symfony\Components\EventDispatcher\EventDispatcher::notifyUntil() */ public function handle(Event $event) { /** @var Bundle\ServerBundle\RequestInterface $request */ $request = $event->getSubject(); /** @var Bundle\ServerBundle\Socket\ServerSocket $server */ $server = $event->getParameter('server'); /** @var Bundle\ServerBundle\Socket\ClientSocket $client */ $client = $event->getParameter('client'); // collect parameters $requestMethod = $request->getRequestMethod(); $requestUrl = $request->getRequestUrl(); $url = parse_url($requestUrl); $queryString = isset($url['path']) ? $url['path'] : null; // @TODO: fix script name, script filename & path_translated $scriptName = '/index.php'; // GET & POST parameters $getParameters = array(); parse_str($queryString, $getParameters); $postParameters = array(); if ($request->getRequestMethod() == Request::METHOD_POST) { parse_str($request->getBody(), $postParameters); } // local & remote address:port list($address, $port) = explode(':', $server->getName()); list($remoteAddress, $remotePort) = explode(':', $client->getPeerName()); // fake _SERVER // @see http://php.net/manual/de/reserved.variables.server.php $server = array('SERVER_SIGNATURE' => sprintf('<address>Symfony/%s (ServerBundle) Server at %s Port %d</address>', Kernel::VERSION, $this->options['hostname'], $port), 'SERVER_SOFTWARE' => sprintf('Symfony/%s (ServerBundle)', Kernel::VERSION), 'SERVER_NAME' => $this->options['hostname'], 'SERVER_ADDR' => $address, 'SERVER_PORT' => $port, 'SERVER_ADMIN' => $this->options['admin'], 'GATEWAY_INTERFACE' => 'CGI/1.1', 'SERVER_PROTOCOL' => 'HTTP/' . $request->getHttpVersion(), 'REQUEST_METHOD' => $requestMethod, 'REMOTE_ADDR' => $remoteAddress, 'REMOTE_PORT' => $remotePort, 'DOCUMENT_ROOT' => $this->options['document_root'], 'QUERY_STRING' => $queryString, 'REQUEST_URI' => $requestUrl, 'REQUEST_TIME' => $client->getAccepted(), 'PHP_SELF' => $url['path'], 'SCRIPT_NAME' => $scriptName, 'SCRIPT_FILENAME' => $scriptFilename = $this->options['document_root'] . $scriptName, 'PATH_INFO' => str_replace($scriptName, '', $url['path']), 'PATH_TRANSLATED' => $scriptFilename); // @TODO: PATH // @TODO: AUTH > AUTH_TYPE, REMOTE_USER, REMOTE_IDENT // @TODO: POST, PUT > CONTENT_TYPE, CONTENT_LENGTH // extend _SERVER if ($request->getRequestMethod() == Request::METHOD_GET) { $server['argv'] = $queryString; $server['argc'] = count($getParameters); } if ($this->options['hostname_lookups']) { $server['REMOTE_HOST'] = gethostbyaddr($remoteAddress); } if ($request->hasHeader('Host')) { $server['HTTP_HOST'] = $request->getHeader('Host'); } if ($request->hasHeader('Connection')) { $server['HTTP_CONNECTION'] = $request->getHeader('Connection'); } if ($request->hasHeader('User-Agent')) { $server['HTTP_USER_AGENT'] = $request->getHeader('User-Agent'); } if ($request->hasHeader('Accept')) { $server['HTTP_ACCEPT'] = $request->getHeader('Accept'); } if ($request->hasHeader('Accept-Encoding')) { $server['HTTP_ACCEPT_ENCODING'] = $request->getHeader('Accept-Encoding'); } if ($request->hasHeader('Accept-Language')) { $server['HTTP_ACCEPT_LANGUAGE'] = $request->getHeader('Accept-Language'); } if ($request->hasHeader('Accept-Charset')) { $server['HTTP_ACCEPT_CHARSET'] = $request->getHeader('Accept-Charset'); } if ($request->hasHeader('Cookie')) { $server['HTTP_COOKIE'] = $request->getHeader('Cookie'); } if ($request->hasHeader('Referer')) { $server['HTTP_REFERER'] = $request->getHeader('Referer'); } // fake _COOKIE $cookies = array(); if ($request->hasHeader('Cookie')) { parse_str($request->getHeader('Cookie'), $cookies); } // @TODO: php.ini - request_order > _REQUEST $parameters = array_merge($getParameters, $postParameters); // @TODO: fake _FILES $files = array(); // initialize SymfonyRequest $sfRequest = SymfonyRequest::create($requestUrl, $requestMethod, $parameters, $cookies, $files, $server); try { if (null !== $this->customKernel) { /** @var $sfResponse Symfony\Components\HttpKernel\Response */ $sfResponse = $this->customKernel->handle($sfRequest); } else { /** @var $sfResponse Symfony\Components\HttpKernel\Response */ $sfResponse = $this->kernel->handle($sfRequest); } } catch (\Exception $e) { $code = 500; $status = SymfonyResponse::$statusTexts[$code]; $headers = array(); $content = sprintf('<h1>Error %d - %s</h1>', $code, $status); // ExceptionController-like view renderer would be cool // add Date header $date = new \DateTime(); $headers['Date'] = $date->format(DATE_RFC822); // add Content-Length header $headers['Content-Length'] = strlen($content); // build Response $response = $this->container->getServer_ResponseService(); $response->setHttpVersion($request->getHttpVersion()); $response->setStatusCode($code, $status); $response->addHeaders($headers); $response->setBody($content); $event->setReturnValue($response); return true; } // add Date header $date = new \DateTime(); $sfResponse->headers->set('Date', $date->format(DATE_RFC822)); // add Content-Length header $sfContent = $sfResponse->getContent(); $sfResponse->headers->set('Content-Length', strlen($sfContent)); // build Response $response = $this->container->getServer_ResponseService(); $response->setHttpVersion($sfResponse->getProtocolVersion()); $response->setStatusCode($sfResponse->getStatusCode()); $response->addHeaders($sfResponse->headers->all()); $response->setBody($sfContent); $event->setReturnValue($response); return true; }
/* * This file is part of the symfony package. * (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once __DIR__ . '/../../../bootstrap.php'; use Symfony\Components\EventDispatcher\Event; $t = new LimeTest(11); $subject = new stdClass(); $parameters = array('foo' => 'bar'); $event = new Event($subject, 'name', $parameters); // ->getSubject() $t->diag('->getSubject()'); $t->is($event->getSubject(), $subject, '->getSubject() returns the event subject'); // ->getName() $t->diag('->getName()'); $t->is($event->getName(), 'name', '->getName() returns the event name'); // ->getParameters() $t->diag('->getParameters()'); $t->is($event->getParameters(), $parameters, '->getParameters() returns the event parameters'); // ->getReturnValue() ->setReturnValue() $t->diag('->getReturnValue() ->setReturnValue()'); $event->setReturnValue('foo'); $t->is($event->getReturnValue(), 'foo', '->getReturnValue() returns the return value of the event'); // ->setProcessed() ->isProcessed() $t->diag('->setProcessed() ->isProcessed()'); $event->setProcessed(true); $t->is($event->isProcessed(), true, '->isProcessed() returns true if the event has been processed'); $event->setProcessed(false);
/** * @param Symfony\Components\EventDispatcher\Event $event * * @see Symfony\Components\EventDispatcher\EventDispatcher::notifyUntil() */ public function handle(Event $event) { $request = $event->getSubject(); $url = trim($request->getRequestUrl(), '/'); $name = basename($url); $dir = trim(substr($url, 0, strlen($url) - strlen($name)), '/'); $path = $this->documentRoot . '/' . ($dir == $name ? $name : $dir . '/' . $name); $path = realpath($path); // skip document root if ($path == $this->documentRoot) { return false; } // path hacking if (substr($path, 0, strlen($this->documentRoot)) !== $this->documentRoot) { return false; } $headers = array(); // add Date header $date = new \DateTime(); $headers['Date'] = $date->format(DATE_RFC822); $path = new \SplFileInfo($path); // is file if ($path->isFile()) { // get mime type $info = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($info, $path); finfo_close($info); // get mime encoding $info = finfo_open(FILEINFO_MIME_ENCODING); $encoding = finfo_file($info, $path); finfo_close($info); // add Content-Type/Encoding header $headers['Content-Type'] = $mime; $headers['Content-Encoding'] = $encoding; // add Content-Length header $headers['Content-Length'] = filesize($path); // build Response $response = $this->container->getServer_ResponseService(); $response->setHttpVersion($request->getHttpVersion()); $response->setStatusCode(200); $response->addHeaders($headers); $response->setBody(file_get_contents($path)); $event->setReturnValue($response); return true; } // is dir if ($path->isDir()) { $dir = trim(substr($path, strlen($this->documentRoot)), '/'); $finder = new Finder(); $finder->depth(0); // @TODO add ExceptionHandler-like rendering $item = <<<EOF <tr> <td valign="top">%s</td> <td valign="top"><a href="/%s">%s</a></td> <td align="right">%s</td> <td align="right">%s%s</td> </tr> EOF; $list = array(); if ($path != $this->documentRoot) { $list[] = sprintf($item, 'parent', $dir . '/../', 'Parent directory', '', '-', ''); } foreach ($finder->in($path) as $file) { $date = new \DateTime(); $date->setTimestamp($file->getMTime()); $list[] = sprintf($item, $file->getType(), ltrim(substr($file->getRealpath(), strlen($this->documentRoot)), '/'), $file->getFilename(), $date->format('d-M-Y H:i'), $file->getSize(), ''); } $layout = <<<EOF <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Index of /{$dir}</title> </head> <body> <h1>Index of /{$dir}</h1> <table><thead> <tr><th></th><th>Name</th><th>Last modified</th><th>Size</th></tr> </thead> <tbody>%s</tbody> </table> </body></html> EOF; $content = sprintf($layout, implode("\n", $list)); // add Content-Type header $headers['Content-Type'] = 'text/html; charset=UTF-8'; // add Content-Length header $headers['Content-Length'] = strlen($content); // build Response $response = $this->container->getServer_ResponseService(); $response->setHttpVersion($request->getHttpVersion()); $response->setStatusCode(200); $response->addHeaders($headers); $response->setBody($content); $event->setReturnValue($response); return true; } return false; }