/** * Preflight checks. */ public function before() { parent::before(); // Defaulting output content type to text/plain - will hopefully be overriden later $this->response->headers('Content-Type', 'text/plain'); $method_override = $this->request->headers('X-HTTP-Method-Override'); $method = strtoupper(empty($method_override) ? $this->request->method() : $method_override); // Checking requested method if (!isset($this->_action_map[$method])) { return $this->request->action('invalid'); } elseif (!method_exists($this, 'action_' . $this->_action_map[$method])) { throw HTTP_Exception::factory(500, 'METHOD_MISCONFIGURED'); } else { $this->request->action($this->_action_map[$method]); } // Checking Content-Type. Considering only POST and PUT methods as other // shouldn't have any content. if (in_array($method, array(HTTP_Request::POST, HTTP_Request::PUT))) { $request_content_type = $this->request->headers('Content-Type'); if (empty($request_content_type)) { throw HTTP_Exception::factory(400, 'NO_CONTENT_TYPE_PROVIDED'); } if (RESTful_Request::get_parser($request_content_type) === FALSE) { throw HTTP_Exception::factory(415); } else { $request_body = $this->request->body(); if (strlen($request_body) > 0) { $request_data = call_user_func(RESTful_Request::get_parser($request_content_type), $request_body); } else { $request_data = $_POST; } } if ($request_data !== FALSE and !empty($request_data)) { $this->request->body($request_data); } else { throw HTTP_Exception::factory(400, 'MALFORMED_REQUEST_BODY'); } } // Determining response content type $registered_response_content_types = array_keys(RESTful_Response::get_renderer()); // Checking Accept mime-types $this->_preferred_response_content_type = $this->request->headers()->preferred_accept($registered_response_content_types); // Fail in no preferred response type could be determined. if ($this->_preferred_response_content_type === FALSE) { throw HTTP_Exception::factory(406, 'This service delivers following types: :types.', array(':types' => implode(', ', array_keys($this->_response_types)))); } }