From 95239ad21e113d7d25d5844b0ac5f15a606f2140 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Thu, 22 Jan 2015 20:26:46 +0100 Subject: AppFramework StreamResponse First stab at the StreamResponse, see #12988 The idea is to use an interface ICallbackResponse (I'm not 100% happy with the name yet, suggestions?) that allow the response to output things in its own way, for instance stream the file using readfile Unittests are atm lacking, plan is to check if a mock of ICallbackResponse will be used by calling its callback (also unhappy with this name) method Usage is: $response = new StreamResponse('path/to/file'); rename io to output, add additional methods and handle error and not modified cases when using StreamResponse fix indention and uppercasing, also handle forbidden cases fix indention fix indention no forbidden, figuring out if a file is really readable is too complicated to get to work across OSes and streams remove useless import remove useless import fix intendation --- lib/public/appframework/http/icallbackresponse.php | 28 +++++++++++ lib/public/appframework/http/ioutput.php | 57 ++++++++++++++++++++++ lib/public/appframework/http/streamresponse.php | 48 ++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 lib/public/appframework/http/icallbackresponse.php create mode 100644 lib/public/appframework/http/ioutput.php create mode 100644 lib/public/appframework/http/streamresponse.php (limited to 'lib/public/appframework') diff --git a/lib/public/appframework/http/icallbackresponse.php b/lib/public/appframework/http/icallbackresponse.php new file mode 100644 index 00000000000..4a392ed081d --- /dev/null +++ b/lib/public/appframework/http/icallbackresponse.php @@ -0,0 +1,28 @@ + + * + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\AppFramework\Http; + + +/** + * Interface ICallbackResponse + * + * @package OCP\AppFramework\Http + */ +interface ICallbackResponse { + + /** + * Outputs the content that should be printed + * + * @param IOutput a small wrapper that handles output + */ + function callback(IOutput $output); + +} diff --git a/lib/public/appframework/http/ioutput.php b/lib/public/appframework/http/ioutput.php new file mode 100644 index 00000000000..191f84374dc --- /dev/null +++ b/lib/public/appframework/http/ioutput.php @@ -0,0 +1,57 @@ + + * + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\AppFramework\Http; + + +/** + * Very thin wrapper class to make output testable + */ +interface IOutput { + + /** + * @param string $out + */ + public function setOutput($out); + + /** + * @param string $path + * + * @return bool false if an error occured + */ + public function setReadfile($path); + + /** + * @param string $header + */ + public function setHeader($header); + + /** + * @return int returns the current http response code + */ + public function getHttpResponseCode(); + + /** + * @param int $code sets the http status code + */ + public function setHttpResponseCode($code); + + /** + * @param string $name + * @param string $value + * @param int $expire + * @param string $path + * @param string $domain + * @param bool $secure + * @param bool $httponly + */ + public function setCookie($name, $value, $expire, $path, $domain, $secure, $httponly); + +} diff --git a/lib/public/appframework/http/streamresponse.php b/lib/public/appframework/http/streamresponse.php new file mode 100644 index 00000000000..870eb95cc1a --- /dev/null +++ b/lib/public/appframework/http/streamresponse.php @@ -0,0 +1,48 @@ + + * + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\AppFramework\Http; + +use OCP\AppFramework\Http; + +/** + * Class StreamResponse + * + * @package OCP\AppFramework\Http + */ +class StreamResponse extends Response implements ICallbackResponse { + /** @var string */ + private $filePath; + + /** + * @param string $filePath the path to the file which should be streamed + */ + public function __construct ($filePath) { + $this->filePath = $filePath; + } + + + /** + * Streams the file using readfile + * + * @param IOutput a small wrapper that handles output + */ + public function callback (IOutput $output) { + // handle caching + if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) { + if (!file_exists($this->filePath)) { + $output->setHttpResponseCode(Http::STATUS_NOT_FOUND); + } elseif ($output->setReadfile($this->filePath) === false) { + $output->setHttpResponseCode(Http::STATUS_BAD_REQUEST); + } + } + } + +} -- cgit v1.2.3