Issues

ZF-2723: Support checking other HTTP requests and retrieving raw entity body in Zend_Controller_Request_Http

Description

For some recent RESTful architecture development I was working on, I came across the need to to use Zend_Controller_Request_Http to check for other HTTP requests than POST and to also access the raw entity body of the posted or put request. To achieve this, we extended Zend_Controller_Request_Http and added the needed methods and then set the Zend Framework request to our child class using the front controller's setRequest() method. However, I think these methods should be incorporated back into the Zend Framework itself.

Patch follows...


--- library/Zend/Controller/Request/Http.php    (revision 8365)
+++ library/Zend/Controller/Request/Http.php    (working copy)
@@ -742,6 +742,92 @@
     }
 
     /**
+     * Was the request made by GET?
+     *
+     * @return boolean
+     */
+    public function isGet()
+    {
+        if ('GET' == $this->getMethod()) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Was the request made by PUT?
+     *
+     * @return boolean
+     */
+    public function isPut()
+    {
+        if ('PUT' == $this->getMethod()) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Was the request made by DELETE?
+     *
+     * @return boolean
+     */
+    public function isDelete()
+    {
+        if ('DELETE' == $this->getMethod()) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Was the request made by HEAD?
+     *
+     * @return boolean
+     */
+    public function isHead()
+    {
+        if ('HEAD' == $this->getMethod()) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Was the request made by OPTIONS?
+     *
+     * @return boolean
+     */
+    public function isOptions()
+    {
+        if ('OPTIONS' == $this->getMethod()) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Return the raw entity body of the request, if present
+     *
+     * @return string|false Raw entity body, or false if not present
+     */
+    public function getEntityBody()
+    {
+        $body = file_get_contents('php://input');
+
+        if (strlen(trim($body)) > 0) {
+            return $body;
+        }
+
+        return false;
+    }
+
+    /**
      * Return the value of the given HTTP header. Pass the header name as the
      * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the
      * Accept header, 'Accept-Encoding' to get the Accept-Encoding header.

Comments

Scheduling for 1.5.0 RC2

Committed to trunk and 1.5 release branch, with one change: getEntityBody() was changed to getRawBody() to more closely follow PHP docs and typical descriptions of that particular request usage.

Documentation was commited to the request documentation to cover the new functionality.

Changing the name to getRawBody() is fine since all the PHP documentation already refers to it as the "raw" body. Though, my usage of "entity" more properly reflects the terminology used by RFC 2616, specifically http://tools.ietf.org/html/rfc2616#section-7. There is no distinction in HTTP of "raw" versus "processed" data in the entity body. This distinction occurs in PHP because of the $_POST superglobal array that processes the url-encoded entity body and constructs a PHP array out the parameters.

Just wanted to give my reasoning for using "entity" instead of "raw." :-)