start using some mime type detection in horde_controller
authorChuck Hagenbuch <chuck@horde.org>
Sat, 29 Nov 2008 01:31:49 +0000 (20:31 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Sat, 29 Nov 2008 01:31:49 +0000 (20:31 -0500)
framework/Controller/lib/Horde/Controller/Mime/Type.php [new file with mode: 0644]
framework/Controller/lib/Horde/Controller/Request/Http.php
framework/Controller/package.xml

diff --git a/framework/Controller/lib/Horde/Controller/Mime/Type.php b/framework/Controller/lib/Horde/Controller/Mime/Type.php
new file mode 100644 (file)
index 0000000..f4106c4
--- /dev/null
@@ -0,0 +1,134 @@
+<?php
+/**
+ * Copyright 2007 Maintainable Software, LLC
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * @author     Mike Naberezny <mike@maintainable.com>
+ * @author     Derek DeVries <derek@maintainable.com>
+ * @author     Chuck Hagenbuch <chuck@horde.org>
+ * @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 <mike@maintainable.com>
+ * @author     Derek DeVries <derek@maintainable.com>
+ * @author     Chuck Hagenbuch <chuck@horde.org>
+ * @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;
+        }
+    }
+
+}
index 8ac0264..952f2d5 100644 (file)
@@ -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;
     }
index d76628a..778f9d7 100644 (file)
@@ -36,11 +36,14 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <dir name="lib">
     <dir name="Horde">
      <dir name="Controller">
+      <dir name="Mime">
+       <file name="Type.php" role="php" />
+      </dir> <!-- /lib/Horde/Controller/Mime -->
       <dir name="Request">
        <file name="Base.php" role="php" />
        <file name="Http.php" role="php" />
        <file name="Mock.php" role="php" />
-      </dir> <!-- /lib/Horde/Controller/Requeset -->
+      </dir> <!-- /lib/Horde/Controller/Request -->
       <dir name="Response">
        <file name="Base.php" role="php" />
        <file name="Http.php" role="php" />
@@ -49,6 +52,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
       <file name="Base.php" role="php" />
       <file name="Dispatcher.php" role="php" />
       <file name="Exception.php" role="php" />
+      <file name="Mime.php" role="php" />
      </dir> <!-- /lib/Horde/Controller -->
     </dir> <!-- /lib/Horde -->
    </dir> <!-- /lib -->
@@ -69,6 +73,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Controller/Base.php" as="Horde/Controller/Base.php" />
    <install name="lib/Horde/Controller/Dispatcher.php" as="Horde/Controller/Dispatcher.php" />
    <install name="lib/Horde/Controller/Exception.php" as="Horde/Controller/Exception.php" />
+   <install name="lib/Horde/Controller/Mime/Type.php" as="Horde/Controller/Mime/Type.php" />
    <install name="lib/Horde/Controller/Request/Base.php" as="Horde/Controller/Request/Base.php" />
    <install name="lib/Horde/Controller/Request/Http.php" as="Horde/Controller/Request/Http.php" />
    <install name="lib/Horde/Controller/Request/Mock.php" as="Horde/Controller/Request/Mock.php" />