Example #1
0
 /**
  * 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))));
     }
 }
Example #2
0
<?php

defined('SYSPATH') or die('No direct script access.');
RESTful_Request::register_parser('application/json', 'RESTful_Request_Parser_JSON::parse');
RESTful_Request::register_parser('application/php-serialized', 'RESTful_Request_Parser_PHP::parse');
RESTful_Request::register_parser('application/x-www-form-urlencoded', 'RESTful_Request_Parser_URLENC::parse');
RESTful_Request::register_parser('application/x-www-form-urlencoded; charset=UTF-8', 'RESTful_Request_Parser_URLENC::parse');
//In order to make work HMVC requests
RESTful_Request::register_parser('text/plain', 'RESTful_Request_Parser_PLAIN::parse');
RESTful_Response::register_renderer('application/json', 'RESTful_Response_Renderer_JSON::render');
RESTful_Response::register_renderer('application/php-serialized', 'RESTful_Response_Renderer_PHP::render');
RESTful_Response::register_renderer('application/php-serialized-array', 'RESTful_Response_Renderer_PHP_Array::render');
RESTful_Response::register_renderer('application/php-serialized-object', 'RESTful_Response_Renderer_PHP_Object::render');
RESTful_Response::register_renderer('text/php-printr', 'RESTful_Response_Renderer_PRINTR::render');
RESTful_Response::register_renderer('text/plain', 'RESTful_Response_Renderer_PLAIN::render');