Ejemplo n.º 1
0
Archivo: Router.php Proyecto: Zvax/Tier
 /**
  * @param Request $request
  * @return Executable
  */
 public function routeRequest(Request $request)
 {
     $path = $request->getPath();
     $queryPosition = strpos($path, '?');
     if ($queryPosition !== false) {
         $path = substr($path, 0, $queryPosition);
     }
     $routeInfo = $this->dispatcher->dispatch($request->getMethod(), $path);
     $dispatcherResult = $routeInfo[0];
     if ($dispatcherResult == \FastRoute\Dispatcher::FOUND) {
         $handler = $routeInfo[1];
         $vars = $routeInfo[2];
         $params = InjectionParams::fromParams($vars);
         return new Executable($handler, $params, null);
     } else {
         if ($dispatcherResult == \FastRoute\Dispatcher::METHOD_NOT_ALLOWED) {
             //TODO - need to embed allowedMethods....theoretically.
             return new Executable([$this, 'serve405ErrorPage']);
         }
     }
     $templateName = $this->templateExists($path, $this->jigConfig);
     if ($templateName != false) {
         return $this->tierJig->createJigExecutable($templateName);
     }
     return new Executable([$this, 'serve404ErrorPage']);
 }
Ejemplo n.º 2
0
function checkIfModifiedHeader(Request $request, $lastModifiedTime)
{
    if ($lastModifiedTime === false) {
        return false;
    }
    if (!$request->hasHeader('If-Modified-Since')) {
        return false;
    }
    $header = $request->getHeader('If-Modified-Since');
    $clientModifiedTime = @strtotime($header);
    if ($clientModifiedTime == false) {
        return false;
    }
    if ($clientModifiedTime < $lastModifiedTime) {
        return false;
    }
    return true;
}
Ejemplo n.º 3
0
function createImageTask(VariableMap $variableMap, ImagickTaskQueue $taskQueue, PageInfo $pageInfo, Request $request, Response $response, $customImage, $params)
{
    $job = $variableMap->getVariable('job', false);
    if ($job === false) {
        if ($taskQueue->isActive() == false) {
            //Queue isn't active - don't bother queueing a task
            return false;
        }
        $task = new \ImagickDemo\Queue\ImagickTask($pageInfo, $params, $customImage, $request->getPath());
        $taskQueue->addTask($task);
    }
    if ($variableMap->getVariable('noredirect') == true) {
        return new \ImagickDemo\Response\ErrorResponse(503, "image still processing {$job} is " . $job);
    }
    $caching = new \Room11\Caching\LastModified\Disabled();
    foreach ($caching->getHeaders(time()) as $key => $value) {
        $response->addHeader($key, $value);
    }
    $response->setStatus(420);
    return new TextBody("Image is generating.");
}
Ejemplo n.º 4
0
/**
 * @param Request $request
 * @param Response $response
 * @param bool $autoAddReason
 * @throws TierException
 */
function sendResponse(Request $request, Response $response, $autoAddReason = true)
{
    $statusCode = $response->getStatus();
    $reason = $response->getReasonPhrase();
    if ($autoAddReason && empty($reason)) {
        $reasonConstant = "Arya\\Reason::HTTP_{$statusCode}";
        $reason = defined($reasonConstant) ? constant($reasonConstant) : '';
        $response->setReasonPhrase($reason);
    }
    if ($response->hasHeader('Date') == false) {
        $response->addHeader("Date", gmdate("D, d M Y H:i:s", time()) . " UTC");
    }
    $statusLine = sprintf("HTTP/%s %s", $request->getProtocol(), $statusCode);
    if (isset($reason[0])) {
        $statusLine .= " {$reason}";
    }
    $file = null;
    $line = null;
    if (headers_sent($file, $line)) {
        //TODO - this is not optimal
        throw new TierException("Headers already sent by File " . $file . " line " . $line);
    }
    header($statusLine);
    $headers = $response->getAllHeaderLines();
    foreach ($headers as $headerLine) {
        header($headerLine, $replace = false);
    }
    flush();
    // Force header output
    $body = $response->getBody();
    if (method_exists($body, '__toString')) {
        echo $body->__toString();
        return;
    } else {
        if (is_string($body)) {
            echo $body;
            return;
        } elseif (is_callable($body)) {
            $body();
            return;
        }
    }
    //this is bad.
    throw new TierException("Unknown body type.");
}