summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/gallery/helpers/gallery_rest.php7
-rw-r--r--modules/rest/controllers/rest.php23
-rw-r--r--modules/rest/helpers/rest.php39
-rw-r--r--modules/rest/libraries/Rest_Exception.php41
-rw-r--r--modules/rest/tests/Rest_Controller_Test.php60
-rw-r--r--modules/tag/helpers/tag_rest.php6
6 files changed, 109 insertions, 67 deletions
diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php
index 21e2b939..563a2c7c 100644
--- a/modules/gallery/helpers/gallery_rest.php
+++ b/modules/gallery/helpers/gallery_rest.php
@@ -50,7 +50,7 @@ class gallery_rest_Core {
static function put($request) {
if (empty($request->arguments)) {
- return rest::invalid_request();
+ Rest_Exception::trigger(400, "Bad request");
}
$path = implode("/", $request->arguments);
$item = gallery_rest::_get_item($path, "edit");
@@ -78,7 +78,7 @@ class gallery_rest_Core {
static function post($request) {
if (empty($request->arguments)) {
- return rest::invalid_request();
+ Rest_Exception::trigger(400, "Bad request");
}
$components = $request->arguments;
@@ -125,6 +125,7 @@ class gallery_rest_Core {
static function delete($request) {
if (empty($request->arguments)) {
+ Rest_Exception::trigger(400, "Bad request", $log_message);
return rest::invalid_request();
}
$path = implode("/", $request->arguments);
@@ -132,7 +133,7 @@ class gallery_rest_Core {
$item = gallery_rest::_get_item($path, "edit");
if ($item->id == 1) {
- return rest::invalid_request("Attempt to delete the root album");
+ Rest_Exception::trigger(400, "Bad request", "Attempt to delete the root album");
}
$parent = $item->parent();
diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php
index 6715bc15..b71e60f5 100644
--- a/modules/rest/controllers/rest.php
+++ b/modules/rest/controllers/rest.php
@@ -20,18 +20,17 @@ class Rest_Controller extends Controller {
public function access_key() {
$request = (object)Input::instance()->get();
if (empty($request->user) || empty($request->password)) {
- print rest::forbidden("No user or password supplied");
- return;
+ Rest_Exception::trigger(403, "Forbidden", "No user or password supplied");
}
$user = identity::lookup_user_by_name($request->user);
if (empty($user)) {
- print rest::forbidden("User '{$request->user}' not found");
+ Rest_Exception::trigger(403, "Forbidden", "User '{$request->user}' not found");
return;
}
if (!identity::is_correct_password($user, $request->password)) {
- print rest::forbidden("Invalid password for '{$request->user}'.");
+ Rest_Exception::trigger(403, "Forbidden", "Invalid password for '{$request->user}'.");
return;
}
@@ -55,14 +54,16 @@ class Rest_Controller extends Controller {
$handler_method = $request->method;
if (!method_exists($handler_class, $handler_method)) {
- print rest::not_implemented("$handler_class::$handler_method is not implemented");
- return;
+ Rest_Exception::trigger(501, "Not implemented", "$handler_class::$handler_method");
}
print call_user_func(array($handler_class, $handler_method), $request);
}
+ } catch (Rest_Exception $e) {
+ $e->sendHeaders();
} catch (Exception $e) {
- print rest::internal_error($e->__toString());
+ Kohana_Log::add("error", $e->__toString());
+ header("HTTP/1.1 500 Internal Error");
}
}
@@ -100,12 +101,12 @@ class Rest_Controller extends Controller {
if ($key->loaded()) {
$user = identity::lookup_user($key->user_id);
if (empty($user)) {
- print rest::forbidden("User not found: {$key->user_id}");
- return false;;
+ Rest_Exception::trigger(403, "Forbidden", $log_message,
+ "User not found: {$key->user_id}");
}
} else {
- print rest::forbidden("Invalid user access token supplied: {$key->user_id}");
- return false;
+ Rest_Exception::trigger(403, "Forbidden", $log_message,
+ "Invalid user access token supplied: {$key->user_id}");
}
}
identity::set_active_user($user);
diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php
index 4b3166c0..7684567c 100644
--- a/modules/rest/helpers/rest.php
+++ b/modules/rest/helpers/rest.php
@@ -18,38 +18,22 @@
*/
class rest_Core {
/**
- * Authorization Failure
- */
- static function forbidden($log_message=null) {
- return self::_format_failure_response(t("Authorization failed"), $log_message);
- }
-
- /**
- * Invalid Failure
- */
- static function invalid_request($log_message=null) {
- return self::_format_failure_response(t("Invalid request"), $log_message);
- }
-
- /**
* Not Implemented
*/
static function not_implemented($log_message=null) {
- return self::_format_failure_response(t("Service not implemented"), $log_message);
- }
-
- /**
- * Internal Error
- */
- static function internal_error($log_message=null) {
- return self::_format_failure_response(t("Internal error"), $log_message);
+ Rest_Exception::trigger(501, "Not implemented", $log_message);
}
/**
* Request failed
*/
static function fail($log_message=null) {
- return self::_format_failure_response($log_message, $log_message);
+ if (!empty($log_message)) {
+ Kohana_Log::add("info", $log_message);
+ }
+ // We don't need to save the session for this request
+ Session::abort_save();
+ return json_encode(array("status" => "ERROR", "message" => (string)$message));
}
/**
@@ -78,13 +62,4 @@ class rest_Core {
Session::abort_save();
return json_encode($response);
}
-
- private static function _format_failure_response($message, $log_message) {
- if (!empty($log_message)) {
- Kohana_Log::add("info", $log_message);
- }
- // We don't need to save the session for this request
- Session::abort_save();
- return json_encode(array("status" => "ERROR", "message" => (string)$message));
- }
}
diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php
new file mode 100644
index 00000000..acdcb568
--- /dev/null
+++ b/modules/rest/libraries/Rest_Exception.php
@@ -0,0 +1,41 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Creates a "Page Not Found" exception.
+ *
+ * $Id: Kohana_404_Exception.php 4679 2009-11-10 01:45:52Z isaiah $
+ *
+ * @package Core
+ * @author Kohana Team
+ * @copyright (c) 2007-2009 Kohana Team
+ * @license http://kohanaphp.com/license
+ */
+
+class Rest_Exception_Core extends Exception {
+ /**
+ * Set internal properties.
+ */
+ public function __construct($code, $text) {
+ parent::__construct("$code $text");
+ }
+
+ /**
+ * Throws a new Rest exception.
+ *
+ * @throws Rest_Exception
+ * @return void
+ */
+ public static function trigger($code, $text, $log_message=null) {
+ $message = "$code: $text" . (!empty($log_message) ? "\n$log_message" : "");
+ Kohana_Log::add("info", $message);
+ throw new Rest_Exception($code, $text);
+ }
+
+ /**
+ * Sends the headers, to emulate server behavior.
+ *
+ * @return void
+ */
+ public function sendHeaders() {
+ header('HTTP/1.1 {$this->getMessage()}');
+ }
+} // End Rest Exception \ No newline at end of file
diff --git a/modules/rest/tests/Rest_Controller_Test.php b/modules/rest/tests/Rest_Controller_Test.php
index 6bebc47d..21b83fe6 100644
--- a/modules/rest/tests/Rest_Controller_Test.php
+++ b/modules/rest/tests/Rest_Controller_Test.php
@@ -75,26 +75,38 @@ class Rest_Controller_Test extends Unit_Test_Case {
public function rest_access_key_no_parameters_test() {
$_SERVER["REQUEST_METHOD"] = "GET";
- $this->assert_equal(
- json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))),
- $this->_call_controller());
+ try {
+ $this->_call_controller();
+ } catch (Rest_Exception $e) {
+ $this->assert_equal("403 Forbidden", $e->getMessage());
+ } catch (Exception $e) {
+ $this->assert_false(true, $e->__toString());
+ }
}
public function rest_access_key_user_not_found_test() {
$_SERVER["REQUEST_METHOD"] = "POST";
$_POST["request"] = json_encode(array("user" => "access_test2", "password" => "password"));
- $this->assert_equal(
- json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))),
- $this->_call_controller());
+ try {
+ $this->_call_controller();
+ } catch (Rest_Exception $e) {
+ $this->assert_equal("403 Forbidden", $e->getMessage());
+ } catch (Exception $e) {
+ $this->assert_false(true, $e->__toString());
+ }
}
public function rest_access_key_invalid_password_test() {
$_SERVER["REQUEST_METHOD"] = "POST";
- $this->assert_equal(
- json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))),
- $this->_call_controller());
+ try {
+ $this->_call_controller();
+ } catch (Rest_Exception $e) {
+ $this->assert_equal("403 Forbidden", $e->getMessage());
+ } catch (Exception $e) {
+ $this->assert_false(true, $e->__toString());
+ }
}
public function rest_get_resource_no_request_key_test() {
@@ -114,9 +126,13 @@ class Rest_Controller_Test extends Unit_Test_Case {
$_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = md5($this->_access_key); // screw up the access key;
$_SERVER["REQUEST_METHOD"] = "GET";
- $this->assert_equal(
- json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))),
- $this->_call_controller());
+ try {
+ $this->_call_controller();
+ } catch (Rest_Exception $e) {
+ $this->assert_equal("403 Forbidden", $e->getMessage());
+ } catch (Exception $e) {
+ $this->assert_false(true, $e->__toString());
+ }
}
public function rest_get_resource_no_user_for_key_test() {
@@ -126,9 +142,13 @@ class Rest_Controller_Test extends Unit_Test_Case {
$this->_user->delete();
unset($this->_user);
- $this->assert_equal(
- json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))),
- $this->_call_controller("rest", explode("/", $this->_photo->relative_url())));
+ try {
+ $this->_call_controller("rest", explode("/", $this->_photo->relative_url()));
+ } catch (Rest_Exception $e) {
+ $this->assert_equal("403 Forbidden", $e->getMessage());
+ } catch (Exception $e) {
+ $this->assert_false(true, $e->__toString());
+ }
}
public function rest_get_resource_no_handler_test() {
@@ -136,9 +156,13 @@ class Rest_Controller_Test extends Unit_Test_Case {
$_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = $this->_access_key;
$_SERVER["HTTP_X_GALLERY_REQUEST_METHOD"] = "PUT";
- $this->assert_equal(
- json_encode(array("status" => "ERROR", "message" => (string)t("Service not implemented"))),
- $this->_call_controller("rest", explode("/", $this->_photo->relative_url())));
+ try {
+ $this->_call_controller("rest", explode("/", $this->_photo->relative_url()));
+ } catch (Rest_Exception $e) {
+ $this->assert_equal("501 Not Implemented", $e->getMessage());
+ } catch (Exception $e) {
+ $this->assert_false(true, $e->__toString());
+ }
}
public function rest_get_resource_test() {
diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php
index cfcf93b2..29b74510 100644
--- a/modules/tag/helpers/tag_rest.php
+++ b/modules/tag/helpers/tag_rest.php
@@ -60,7 +60,7 @@ class tag_rest_Core {
static function post($request) {
if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) {
- return rest::invalid_request();
+ Rest_Exception::trigger(400, "Bad request");
}
$path = $request->path;
$tags = explode(",", $request->arguments[0]);
@@ -85,7 +85,7 @@ class tag_rest_Core {
static function put($request) {
if (empty($request->arguments[0]) || empty($request->new_name)) {
- return rest::invalid_request();
+ Rest_Exception::trigger(400, "Bad request");
}
$name = $request->arguments[0];
@@ -105,7 +105,7 @@ class tag_rest_Core {
static function delete($request) {
if (empty($request->arguments[0])) {
- return rest::invalid_request();
+ Rest_Exception::trigger(400, "Bad request");
}
$tags = explode(",", $request->arguments[0]);
if (!empty($request->path)) {