/** * Sends chunked response to output. * * @param \MouseOver\Storage\IStorageFile $storageFile Storage file * @param \Nette\Http\IRequest $httpRequest HTTP request * @param \Nette\Http\IResponse $httpResponse HTTP response * * @return void */ protected function sendStorageFile($storageFile, Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { $httpResponse->setHeader('Accept-Ranges', 'bytes'); $reader = $storageFile->getReader(); if (preg_match('#^bytes=(\\d*)-(\\d*)\\z#', $httpRequest->getHeader('Range'), $matches)) { list(, $start, $end) = $matches; if ($start === '') { $start = null; } if ($end === '') { $end = null; } try { $reader->setRange($start, $end); } catch (\InvalidArgumentException $invalidArgumentException) { $httpResponse->setCode(416); // requested range not satisfiable return; } $httpResponse->setCode(206); $httpResponse->setHeader('Content-Range', 'bytes ' . $reader->getRangeStart() . '-' . $reader->getRangeEnd() . '/' . $reader->getFileSize()); $reader->setRange($start, $end); $httpResponse->setHeader('Content-Length', $reader->getLength()); } else { $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($reader->getFileSize() - 1) . '/' . $reader->getFileSize()); $httpResponse->setHeader('Content-Length', $reader->getLength()); } while ($reader->hasContent()) { echo $reader->read(); } }
public function generateImage(ImageRequest $request) { $width = $request->getWidth(); $height = $request->getHeight(); $format = $request->getFormat(); if (!$this->validator->validate($width, $height)) { throw new Application\BadRequestException(); } $image = NULL; foreach ($this->providers as $provider) { $image = $provider->getImage($request); if ($image) { break; } } if (!$image) { $this->httpResponse->setHeader('Content-Type', 'image/jpeg'); $this->httpResponse->setCode(Http\IResponse::S404_NOT_FOUND); exit; } $destination = $this->wwwDir . '/' . $this->httpRequest->getUrl()->getRelativeUrl(); $dirname = dirname($destination); if (!is_dir($dirname)) { $success = @mkdir($dirname, 0777, TRUE); if (!$success) { throw new Application\BadRequestException(); } } $success = $image->save($destination, 90, $format); if (!$success) { throw new Application\BadRequestException(); } $image->send(); exit; }
/** * {inheritDoc} */ public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { $httpResponse->setContentType($this->getContentType(), 'utf-8'); $httpResponse->setExpiration(FALSE); $httpResponse->setCode($this->code); echo Nette\Utils\Json::encode($this->getPayload(), Nette\Utils\Json::PRETTY); }
/** * @param Nette\Http\IRequest $httpRequest * @param Nette\Http\IResponse $httpResponse */ function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { $response = $this->getResponse(); $httpResponse->setCode(200); $httpResponse->setContentType('text/plain'); $httpResponse->setHeader('Content-Length', strlen($response)); echo $response; }
/** * @param IRequest $httpRequest * @param IResponse $httpResponse * @return void */ public function send(IRequest $httpRequest, IResponse $httpResponse) { $httpResponse->setCode($this->code); foreach ($this->headers as $name => $value) { $httpResponse->setHeader($name, $value); } echo (string) $this->getBody(); }
/** * @param Http\IRequest $request * @param Http\IResponse $response */ public function send(Http\IRequest $request, Http\IResponse $response) { $response->setContentType($this->contentType); $response->setCode($this->code ?: Http\IResponse::S200_OK); $response->setExpiration($this->expiration); $response->setHeader('Pragma', $this->expiration ? 'cache' : 'no-cache'); echo Json::encode($this->data); }
/** * {@inheritdoc} */ public function send(IRequest $httpRequest, IResponse $httpResponse) { $httpResponse->setContentType('text/xml'); $httpResponse->setExpiration(false); $httpResponse->setCode($this->getCode()); $httpResponse->setHeader('Content-Length', strlen($this->response)); echo $this->response; }
/** * Terminates current request and sends HTTP response with * code and optionaly payload formatted for requested output content type * * @throws AbortException */ public function terminateWithCode($code, $payload = NULL) { $this->httpResponse->setCode($code); if (func_num_args() == 1) { $this->response = $this->createResponse(); } else { $this->response = $this->createResponse($payload); } throw new AbortException(); }
/** * Attempts to cache the sent entity by its last modification date. * @param string|int|\DateTime last modified time * @param string strong entity tag validator * @return bool */ public function isModified($lastModified = NULL, $etag = NULL) { if ($lastModified) { $this->response->setHeader('Last-Modified', $this->response->date($lastModified)); } if ($etag) { $this->response->setHeader('ETag', '"' . addslashes($etag) . '"'); } $ifNoneMatch = $this->request->getHeader('If-None-Match'); if ($ifNoneMatch === '*') { $match = TRUE; // match, check if-modified-since } elseif ($ifNoneMatch !== NULL) { $etag = $this->response->getHeader('ETag'); if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === FALSE) { return TRUE; } else { $match = TRUE; // match, check if-modified-since } } $ifModifiedSince = $this->request->getHeader('If-Modified-Since'); if ($ifModifiedSince !== NULL) { $lastModified = $this->response->getHeader('Last-Modified'); if ($lastModified != NULL && strtotime($lastModified) <= strtotime($ifModifiedSince)) { $match = TRUE; } else { return TRUE; } } if (empty($match)) { return TRUE; } $this->response->setCode(IResponse::S304_NOT_MODIFIED); return FALSE; }
public function doProcessException($e) { if (!$e instanceof BadRequestException && $this->httpResponse instanceof Response) { $this->httpResponse->warnOnBuffer = false; } if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() ?: 404 : 500); } $requests = $this->getRequests(); $request = end($requests) ?: $this->initialRequest; $args = ['exception' => $e, 'request' => $request]; $errorPresenter = $request ? $this->findErrorPresenter($request->getPresenterName()) : $this->errorPresenter; if ($this->getPresenter() instanceof Presenter) { try { $this->getPresenter()->forward(":{$errorPresenter}:", $args); } catch (AbortException $_) { $this->processRequest($this->getPresenter()->getLastCreatedRequest()); } } else { $this->processRequest(new Request($errorPresenter, Request::FORWARD, $args)); } }
/** * Sends response to output. * * @param \Nette\Http\IRequest $httpRequest * @param \Nette\Http\IResponse $httpResponse */ public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse) { if (strlen($this->etag)) { $httpResponse->setHeader('Etag', $this->etag); } $httpResponse->setExpiration(\Nette\Http\IResponse::PERMANENT); if (($inm = $httpRequest->getHeader('if-none-match')) && $inm == $this->etag) { $httpResponse->setCode(\Nette\Http\IResponse::S304_NOT_MODIFIED); return; } $httpResponse->setContentType($this->contentType); echo $this->content; }
/** * Sends response to output. * * @param Http\IRequest $httpRequest * @param Http\IResponse $httpResponse */ public function send(Http\IRequest $httpRequest, Http\IResponse $httpResponse) { $httpResponse->setExpiration(Http\IResponse::PERMANENT); if (($inm = $httpRequest->getHeader('if-none-match')) && $inm == $this->etag) { $httpResponse->setCode(Http\IResponse::S304_NOT_MODIFIED); return; } $httpResponse->setContentType(AssetsLoader\Files\MimeMapper::getMimeFromFilename($this->filePath)); $httpResponse->setHeader('Content-Transfer-Encoding', 'binary'); $httpResponse->setHeader('Content-Length', filesize($this->filePath)); $httpResponse->setHeader('Content-Disposition', 'attachment; filename="' . basename($this->filePath) . '"'); $httpResponse->setHeader('Access-Control-Allow-Origin', '*'); // Read the file readfile($this->filePath); }
/** * Sends response to output. * @return void */ public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { $httpResponse->setContentType($this->contentType); $httpResponse->setHeader('Content-Disposition', 'inline; filename="' . $this->name . '"'); $filesize = $length = filesize($this->file); $handle = fopen($this->file, 'r'); if ($this->resuming) { $httpResponse->setHeader('Accept-Ranges', 'bytes'); if (preg_match('#^bytes=(\\d*)-(\\d*)\\z#', $httpRequest->getHeader('Range'), $matches)) { list(, $start, $end) = $matches; if ($start === '') { $start = max(0, $filesize - $end); $end = $filesize - 1; } elseif ($end === '' || $end > $filesize - 1) { $end = $filesize - 1; } if ($end < $start) { $httpResponse->setCode(416); // requested range not satisfiable return; } $httpResponse->setCode(206); $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize); $length = $end - $start + 1; fseek($handle, $start); } else { $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize); } } $httpResponse->setHeader('Content-Length', $length); while (!feof($handle) && $length > 0) { echo $s = fread($handle, min(4000000.0, $length)); $length -= strlen($s); } fclose($handle); }
/** * @return void */ public function processException(\Exception $e) { if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() ?: 404 : 500); } $args = array('exception' => $e, 'request' => end($this->requests) ?: NULL); if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", $args); } catch (AbortException $foo) { $this->processRequest($this->presenter->getLastCreatedRequest()); } } else { $this->processRequest(new Request($this->errorPresenter, Request::FORWARD, $args)); } }
/** * Send error to image * * @param \Nette\Http\IResponse $response * @param string|\Exception $error */ private function sendError(HttpResponse $response, $error, $width, $height) { $response->setCode(HttpResponse::S500_INTERNAL_SERVER_ERROR); $response->setContentType('image/gif'); if (!$error instanceof \Exception) { $response->setHeader('X-Imager-Error-Message', $error); } else { $response->setHeader('X-Imager-Error-Message', get_class($error) . ': ' . $error->getMessage()); // detailed information only in debug mode if ($this->factory->getDebugger()) { $response->setHeader('X-Imager-Error-File', $error->getFile() . ' (' . $error->getLine() . ')'); $trace = $error->getTraceAsString(); $trace = Strings::replace($trace, '~[\\n|\\n\\r]~', '>>>'); $response->setHeader('X-Imager-Error-Trace', $trace); } } $this->factory->sendErrorImage($width, $height); }
/** * Create new api response * @param IResource $resource * @param string|null $contentType * @return IResponse * * @throws InvalidStateException */ public function create(IResource $resource, $contentType = NULL) { if ($contentType === NULL) { $contentType = $this->jsonp === FALSE || !$this->request->getQuery($this->jsonp) ? $this->getPreferredContentType($this->request->getHeader('Accept')) : IResource::JSONP; } if (!isset($this->responses[$contentType])) { throw new InvalidStateException('Unregistered API response for ' . $contentType); } if (!class_exists($this->responses[$contentType])) { throw new InvalidStateException('API response class does not exist.'); } if (!$resource->getData()) { $this->response->setCode(204); // No content } $responseClass = $this->responses[$contentType]; $response = new $responseClass($resource->getData(), $this->mapperContext->getMapper($contentType), $contentType); if ($response instanceof BaseResponse) { $response->setPrettyPrint($this->isPrettyPrint()); } return $response; }
/** * Dispatch a HTTP request to a front controller. * @return void */ public function run() { $request = NULL; $repeatedError = FALSE; do { try { if (count($this->requests) > self::$maxLoop) { throw new ApplicationException('Too many loops detected in application life cycle.'); } if (!$request) { $this->onStartup($this); $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { $request = NULL; throw new BadRequestException('No route for HTTP request.'); } if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } } $this->requests[] = $request; $this->onRequest($this, $request); // Instantiate presenter $presenterName = $request->getPresenterName(); try { $this->presenter = $this->presenterFactory->createPresenter($presenterName); } catch (InvalidPresenterException $e) { throw new BadRequestException($e->getMessage(), 404, $e); } $this->presenterFactory->getPresenterClass($presenterName); $request->setPresenterName($presenterName); $request->freeze(); // Execute presenter $response = $this->presenter->run($request); if ($response) { $this->onResponse($this, $response); } // Send response if ($response instanceof Responses\ForwardResponse) { $request = $response->getRequest(); continue; } elseif ($response instanceof IResponse) { $response->send($this->httpRequest, $this->httpResponse); } break; } catch (\Exception $e) { $this->onError($this, $e); if ($repeatedError) { $e = new ApplicationException("An error occurred while executing error-presenter '{$this->errorPresenter}'.", 0, $e); } if ($repeatedError || !$this->catchExceptions) { $this->onShutdown($this, $e); throw $e; } $repeatedError = TRUE; $this->errorPresenter = $this->errorPresenter ?: 'Nette:Error'; if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500); } if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e)); } catch (AbortException $foo) { $request = $this->presenter->getLastCreatedRequest(); } } else { $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e)); } // continue } } while (1); $this->onShutdown($this, isset($e) ? $e : NULL); }
/** * Dispatch a HTTP request to a front controller. * * @return void */ public function run() { $request = null; $repeatedError = false; do { try { if (count($this->requests) > self::$maxLoop) { throw new ApplicationException('Too many loops detected in application life cycle.'); } if (!$request) { $this->onStartup($this); $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { $request = null; throw new BadRequestException('No route for HTTP request.'); } if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } } $this->requests[] = $request; $this->onRequest($this, $request); // Instantiate presenter $presenterName = $request->getPresenterName(); try { $this->presenter = $this->presenterFactory->createPresenter($presenterName); } catch (InvalidPresenterException $e) { throw new BadRequestException($e->getMessage(), 404, $e); } $this->presenterFactory->getPresenterClass($presenterName); $request->setPresenterName($presenterName); $request->freeze(); // Execute presenter $response = $this->presenter->run($request); if ($response) { $this->onResponse($this, $response); } // Send response if ($response instanceof Responses\ForwardResponse) { $request = $response->getRequest(); continue; } elseif ($response instanceof IResponse) { $response->send($this->httpRequest, $this->httpResponse); } break; } catch (\Exception $e) { // fault barrier $this->onError($this, $e); if (!$this->catchExceptions) { $this->onShutdown($this, $e); throw $e; } if ($repeatedError) { $e = new ApplicationException('An error occurred while executing error-presenter', 0, $e); } if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500); } if (!$repeatedError && $this->errorPresenter) { $repeatedError = true; if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e)); } catch (AbortException $foo) { $request = $this->presenter->getLastCreatedRequest(); } } else { $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e)); } // continue } else { // default error handler if ($e instanceof BadRequestException) { $code = $e->getCode(); } else { $code = 500; Nette\Diagnostics\Debugger::log($e, Nette\Diagnostics\Debugger::ERROR); } require __DIR__ . '/templates/error.phtml'; break; } } } while (1); $this->onShutdown($this, isset($e) ? $e : null); }
/** * Sends response to output. * @return void */ public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { $httpResponse->setContentType($this->contentType); $httpResponse->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"'); $filesize = $length = filesize($this->file); $handle = fopen($this->file, 'r'); if ($this->resuming) { $httpResponse->setHeader('Accept-Ranges', 'bytes'); $range = $httpRequest->getHeader('Range'); if ($range !== NULL) { $range = substr($range, 6); // 6 == strlen('bytes=') list($start, $end) = explode('-', $range); if ($start == NULL) { $start = 0; } if ($end == NULL) { $end = $filesize - 1; } if ($start < 0 || $end <= $start || $end > $filesize - 1) { $httpResponse->setCode(416); // requested range not satisfiable return; } $httpResponse->setCode(206); $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize); $length = $end - $start + 1; fseek($handle, $start); } else { $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize); } } $httpResponse->setHeader('Content-Length', $length); while (!feof($handle)) { echo fread($handle, 4000000.0); } fclose($handle); }
/** * Sets HTTP response code * * @param int code * @return self * @throws Nette\InvalidStateException if HTTP headers have been sent */ public function setCode($code) { $this->httpResponse->setCode($code); return $this; }
/** * @param IHttpRequest $httpRequest * @param IHttpResponse $httpResponse */ public function send(IHttpRequest $httpRequest, IHttpResponse $httpResponse) { $httpResponse->addHeader(self::HADER_MESSAGE, $this->message); $httpResponse->setCode($this->code); }
/** * @param Http\IRequest $httpRequest * @param Http\IResponse $httpResponse */ public function send(Http\IRequest $httpRequest, Http\IResponse $httpResponse) { $httpResponse->setContentType($this->contentType); $httpResponse->setCode($this->code); $httpResponse->setExpiration($this->expiration); $httpResponse->setHeader('Pragma', $this->expiration ? 'cache' : 'no-cache'); $response = Json::encode($this->data); if (is_callable($this->postProcessor)) { $response = call_user_func($this->postProcessor, $response); } echo $response; }
public function send(Http\IRequest $request, Http\IResponse $response) { $response->setCode($this->code ?: Http\IResponse::S204_NO_CONTENT); }
/** * Sends response to output. * @return void */ public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { $lastMTime = \filemtime($this->getFile()); // Pokud je povoleno cachovani, podrzim to po dobu 14ti dnu (nebo dokud se soubor zmeni) if ($this->isCachingAllowed()) { $httpResponse->setExpiration(time() + 60 * 60 * 24 * 14); $cachedTime = $httpRequest->getHeader('If-Modified-Since'); if ($cachedTime >= $lastMTime) { $httpResponse->setCode(304); return; } } if (!$this->dontSetContentType) { $httpResponse->setContentType($this->getContentType()); } $httpResponse->addHeader("Last-Modified", gmdate("U", $lastMTime)); $httpResponse->setHeader('Content-Disposition', $this->getContentDisposition() . '; filename="' . $this->getName() . '"'); $filesize = $length = filesize($this->getFile()); //$handle = fopen($this->getFile(), 'r'); if (false && $this->resuming) { $httpResponse->setHeader('Accept-Ranges', 'bytes'); $range = $httpRequest->getHeader('Range'); if ($range !== NULL) { $range = substr($range, 6); // 6 == strlen('bytes=') list($start, $end) = explode('-', $range); if ($start == NULL) { $start = 0; } if ($end == NULL) { $end = $filesize - 1; } if ($start < 0 || $end <= $start || $end > $filesize - 1) { $httpResponse->setCode(416); // requested range not satisfiable return; } $httpResponse->setCode(206); $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize); $length = $end - $start + 1; fseek($handle, $start); } else { $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize); } } $httpResponse->setHeader('Content-Length', $length); readfile($this->getFile()); //while(!feof($handle)) { // echo fread($handle, 4e6); //} //fclose($handle); }
/** * @param Nette\Http\IRequest $httpRequest * @param Nette\Http\IResponse $httpResponse */ function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { $httpResponse->setCode(204); }
/** * Sends response to output. * @return void */ public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { $httpResponse->setContentType($this->contentType); $httpResponse->setHeader('Content-Disposition', ($this->forceDownload ? 'attachment' : 'inline') . '; filename="' . $this->name . '"'); $output = NULL; if ($this->precalculateFileSize) { ob_start(); Nette\Utils\Callback::invokeArgs($this->outputGenerator); $output = ob_get_clean(); $filesize = $length = strlen($output); } if ($this->resuming && $this->precalculateFileSize) { $httpResponse->setHeader('Accept-Ranges', 'bytes'); if (preg_match('#^bytes=(\\d*)-(\\d*)\\z#', $httpRequest->getHeader('Range'), $matches)) { list(, $start, $end) = $matches; if ($start === '') { $start = max(0, $filesize - $end); $end = $filesize - 1; } elseif ($end === '' || $end > $filesize - 1) { $end = $filesize - 1; } if ($end < $start) { $httpResponse->setCode(416); // requested range not satisfiable return; } $httpResponse->setCode(206); $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize); $length = $end - $start + 1; } else { $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize); } } if ($this->precalculateFileSize) { $httpResponse->setHeader('Content-Length', $length); } if (isset($start)) { echo substr($output, $start, $length); } elseif (isset($output)) { echo $output; } else { Nette\Utils\Callback::invoke($this->outputGenerator); } }
/** * Clears all http headers * @param IResponse $res * @return IResponse */ static function clearHeaders(IResponse $res, $setContentType = false) { $res->setCode(IResponse::S200_OK); foreach ($res->getHeaders() as $key => $val) { $res->setHeader($key, null); } if ($setContentType === true) { $res->setContentType("text/html", "UTF-8"); } return $res; }
/** * Sends response to output * @param Http\IRequest $httpRequest * @param Http\IResponse $httpResponse */ public function send(Http\IRequest $httpRequest, Http\IResponse $httpResponse) { $httpResponse->setCode($this->code); $this->response->send($httpRequest, $httpResponse); }
/** * Odesílá response * * @param IRequest * @param IResponse */ public function send(IRequest $httpRequest, IResponse $httpResponse) { $httpResponse->setCode($this->responseCode); parent::send($httpRequest, $httpResponse); }