From 6f72d59f0c381a4eed945183ed4071da2ad8aa55 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Fri, 28 Nov 2008 20:31:49 -0500 Subject: [PATCH] start using some mime type detection in horde_controller --- .../Controller/lib/Horde/Controller/Mime/Type.php | 134 +++++++++++++++++++++ .../lib/Horde/Controller/Request/Http.php | 66 +++++----- framework/Controller/package.xml | 7 +- 3 files changed, 177 insertions(+), 30 deletions(-) create mode 100644 framework/Controller/lib/Horde/Controller/Mime/Type.php diff --git a/framework/Controller/lib/Horde/Controller/Mime/Type.php b/framework/Controller/lib/Horde/Controller/Mime/Type.php new file mode 100644 index 000000000..f4106c4e2 --- /dev/null +++ b/framework/Controller/lib/Horde/Controller/Mime/Type.php @@ -0,0 +1,134 @@ + + * @author Derek DeVries + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_Controller + * @subpackage Response + */ + +/** + * Handles managing of what types of responses the client can handle and which + * one was requested. + * + * @author Mike Naberezny + * @author Derek DeVries + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_Controller + * @subpackage Response + */ +class Horde_Controller_Mime_Type +{ + public $symbol; + public $synonyms; + public $string; + + public static $set = array(); + public static $lookup = array(); + public static $extensionLookup = array(); + public static $registered = false; + + public function __construct($string, $symbol = null, $synonyms = array()) + { + $this->string = $string; + $this->symbol = $symbol; + $this->synonyms = $synonyms; + } + + public function __toString() + { + return $this->symbol; + } + + public static function lookup($string) + { + if (!empty(self::$lookup[$string])) { + return self::$lookup[$string]; + } else { + return null; + } + } + + public static function lookupByExtension($ext) + { + if (!empty(self::$extensionLookup[$ext])) { + return self::$extensionLookup[$ext]; + } else { + return null; + } + } + + public static function register($string, $symbol, $synonyms = array(), $extSynonyms = array()) + { + $type = new Horde_Controller_Mime_Type($string, $symbol, $synonyms); + self::$set[] = $type; + + // add lookup strings + foreach (array_merge((array)$string, $synonyms) as $string) { + self::$lookup[$string] = $type; + } + + // add extesnsion lookups + foreach (array_merge((array)$symbol, $extSynonyms) as $ext) { + self::$extensionLookup[$ext] = $type; + } + } + + /** + * @todo - actually parse the header. This is simply mocked out + * with common types for now + */ + public static function parse($acceptHeader) + { + $types = array(); + + if (strstr($acceptHeader, 'text/javascript')) { + if (isset(self::$extensionLookup['js'])) { + $types[] = self::$extensionLookup['js']; + } + + } elseif (strstr($acceptHeader, 'text/html')) { + if (isset(self::$extensionLookup['html'])) { + $types[] = self::$extensionLookup['html']; + } + + } elseif (strstr($acceptHeader, 'text/xml')) { + if (isset(self::$extensionLookup['xml'])) { + $types[] = self::$extensionLookup['xml']; + } + + // default to html + } else { + if (isset(self::$extensionLookup['html'])) { + $types[] = self::$extensionLookup['html']; + } + } + return $types; + } + + /** + * Register mime types + * @todo - move this elsewhere? + */ + public static function registerTypes() + { + if (!self::$registered) { + Horde_Controller_Mime_Type::register("*/*", 'all'); + Horde_Controller_Mime_Type::register("text/plain", 'text', array(), array('txt')); + Horde_Controller_Mime_Type::register("text/html", 'html', array('application/xhtml+xml'), array('xhtml')); + Horde_Controller_Mime_Type::register("text/javascript", 'js', array('application/javascript', 'application/x-javascript'), array('xhtml')); + Horde_Controller_Mime_Type::register("application/json", 'json'); + Horde_Controller_Mime_Type::register("text/csv", 'csv'); + Horde_Controller_Mime_Type::register("application/xml", 'xml', array('text/xml', 'application/x-xml')); + self::$registered = true; + } + } + +} diff --git a/framework/Controller/lib/Horde/Controller/Request/Http.php b/framework/Controller/lib/Horde/Controller/Request/Http.php index 8ac026431..952f2d56f 100644 --- a/framework/Controller/lib/Horde/Controller/Request/Http.php +++ b/framework/Controller/lib/Horde/Controller/Request/Http.php @@ -74,34 +74,42 @@ class Horde_Controller_Request_Http extends Horde_Controller_Request_Base */ public function __construct($options = array()) { - $this->_initSessionData(); - - // superglobal data if not passed in thru constructor - $this->_get = isset($options['get']) ? $options['get'] : $_GET; - $this->_post = isset($options['post']) ? $options['post'] : $_POST; - $this->_cookie = isset($options['cookie']) ? $options['cookie'] : $_COOKIE; - $this->_request = isset($options['request']) ? $options['request'] : $_REQUEST; - - parent::__construct($options); - - $this->_pathParams = array(); - // $this->_formattedRequestParams = $this->_parseFormattedRequestParameters(); - - // use FileUpload object to store files - $this->_setFilesSuperglobals(); - - // disable all superglobal data to force us to use correct way - //@TODO - //$_GET = $_POST = $_FILES = $_COOKIE = $_REQUEST = $_SERVER = array(); - - $this->_domain = $this->getServer('SERVER_NAME'); - $this->_uri = trim($this->getServer('REQUEST_URI'), '/'); - $this->_method = $this->getServer('REQUEST_METHOD'); - // @TODO look at HTTP_X_FORWARDED_FOR, handling multiple addresses: http://weblogs.asp.net/james_crowley/archive/2007/06/19/gotcha-http-x-forwarded-for-returns-multiple-ip-addresses.aspx - $this->_remoteIp = $this->getServer('REMOTE_ADDR'); - $this->_port = $this->getServer('SERVER_PORT'); - $this->_https = $this->getServer('HTTPS'); - $this->_isAjax = $this->getServer('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'; + try { + $this->_initSessionData(); + + // register default mime types + Horde_Controller_Mime_Type::registerTypes(); + + // superglobal data if not passed in thru constructor + $this->_get = isset($options['get']) ? $options['get'] : $_GET; + $this->_post = isset($options['post']) ? $options['post'] : $_POST; + $this->_cookie = isset($options['cookie']) ? $options['cookie'] : $_COOKIE; + $this->_request = isset($options['request']) ? $options['request'] : $_REQUEST; + + parent::__construct($options); + + $this->_pathParams = array(); + //$this->_formattedRequestParams = $this->_parseFormattedRequestParameters(); + + // use FileUpload object to store files + $this->_setFilesSuperglobals(); + + // disable all superglobal data to force us to use correct way + //@TODO + //$_GET = $_POST = $_FILES = $_COOKIE = $_REQUEST = $_SERVER = array(); + + $this->_domain = $this->getServer('SERVER_NAME'); + $this->_uri = trim($this->getServer('REQUEST_URI'), '/'); + $this->_method = $this->getServer('REQUEST_METHOD'); + // @TODO look at HTTP_X_FORWARDED_FOR, handling multiple addresses: http://weblogs.asp.net/james_crowley/archive/2007/06/19/gotcha-http-x-forwarded-for-returns-multiple-ip-addresses.aspx + $this->_remoteIp = $this->getServer('REMOTE_ADDR'); + $this->_port = $this->getServer('SERVER_PORT'); + $this->_https = $this->getServer('HTTPS') || $this->getServer('SSL_PROTOCOL') || $this->getServer('HTTP_X_CLUSTER_SSL'); + $this->_isAjax = $this->getServer('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'; + } catch (Exception $e) { + $this->_malformed = true; + $this->_exception = $e; + } } @@ -223,7 +231,7 @@ class Horde_Controller_Request_Http extends Horde_Controller_Request_Base } } - // $this->_contentType = Horde_Controller_Mime_Type::lookup($type); + $this->_contentType = Horde_Controller_Mime_Type::lookup($type); } return $this->_contentType; } diff --git a/framework/Controller/package.xml b/framework/Controller/package.xml index d76628a57..778f9d73c 100644 --- a/framework/Controller/package.xml +++ b/framework/Controller/package.xml @@ -36,11 +36,14 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + + - + @@ -49,6 +52,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -69,6 +73,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0