Skip to content

Commit

Permalink
ID#270: added convenience method to retrieve the raw request body for…
Browse files Browse the repository at this point in the history
… e.g. POST requests.
  • Loading branch information
Christian Achatz committed Oct 26, 2015
1 parent 6495c54 commit a393607
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 109 deletions.
10 changes: 10 additions & 0 deletions core/http/Request.php
Expand Up @@ -229,6 +229,16 @@ public function resetGetParameters();
*/
public function resetPostParameters();

/**
* Returns the content of the request body. In case of POST and PUT requests this is
* the raw content posted or put by the browser or user agent.
* <p/>
* Returns an empty string in case of e.g. GET requests.
*
* @return string The current request body.
*/
public function getRequestBody();

/**
* @return string Value of the <em>return $_SERVER['REQUEST_URI']</em> offset.
*/
Expand Down
223 changes: 114 additions & 109 deletions core/http/RequestImpl.php
Expand Up @@ -43,6 +43,21 @@ public function getVersion() {
return isset($parts[1]) ? trim($parts[1]) : null;
}

/**
* Checks whether or not the applied parameter is contained in the current request.
*
* @param string $name The name of the parameter to check.
*
* @return bool <em>True</em> in case the given parameter is contained in the current request, <em>false</em> otherwise.
*/
public function hasParameter($name) {
return $this->getParameter($name) !== null;
}

public function getParameter($name, $default = null) {
return $this->getGenericParameter($name, $default, self::USE_REQUEST_PARAMS);
}

/**
* @param string $name The name of the URL parameter to get.
* @param string $default The default value to return in case the parameter is not present (default: <em>null</em>).
Expand All @@ -61,25 +76,6 @@ protected function getGenericParameter($name, $default, $type) {
: $default;
}

public function getParameter($name, $default = null) {
return $this->getGenericParameter($name, $default, self::USE_REQUEST_PARAMS);
}

/**
* Checks whether or not the applied parameter is contained in the current request.
*
* @param string $name The name of the parameter to check.
*
* @return bool <em>True</em> in case the given parameter is contained in the current request, <em>false</em> otherwise.
*/
public function hasParameter($name) {
return $this->getParameter($name) !== null;
}

public function getGetParameter($name, $default = null) {
return $this->getGenericParameter($name, $default, self::USE_GET_PARAMS);
}

/**
* Checks whether or not the applied parameter is contained in the current GET request.
*
Expand All @@ -91,8 +87,8 @@ public function hasGetParameter($name) {
return $this->getGetParameter($name) !== null;
}

public function getPostParameter($name, $default = null) {
return $this->getGenericParameter($name, $default, self::USE_POST_PARAMS);
public function getGetParameter($name, $default = null) {
return $this->getGenericParameter($name, $default, self::USE_GET_PARAMS);
}

/**
Expand All @@ -106,6 +102,10 @@ public function hasPostParameter($name) {
return $this->getPostParameter($name) !== null;
}

public function getPostParameter($name, $default = null) {
return $this->getGenericParameter($name, $default, self::USE_POST_PARAMS);
}

public function getSession($name) {
return new Session($name);
}
Expand All @@ -123,6 +123,10 @@ public function getSessionName() {
return ini_get('session.name');
}

public function getParameters() {
return $this->getGenericParameters(self::USE_REQUEST_PARAMS);
}

/**
* @param string $type The request parameter type.
*
Expand All @@ -132,10 +136,6 @@ protected function getGenericParameters($type) {
return is_array($GLOBALS['_' . $type]) ? $GLOBALS['_' . $type] : [];
}

public function getParameters() {
return $this->getGenericParameters(self::USE_REQUEST_PARAMS);
}

public function getGetParameters() {
return $this->getGenericParameters(self::USE_GET_PARAMS);
}
Expand All @@ -144,26 +144,6 @@ public function getPostParameters() {
return $this->getGenericParameters(self::USE_POST_PARAMS);
}

public function setParameter($name, $value) {
$_REQUEST[$name] = $value;

return $this;
}

public function setGetParameter($name, $value) {
$_GET[$name] = $value;
$_REQUEST[$name] = $value;

return $this;
}

public function setPostParameter($name, $value) {
$_POST[$name] = $value;
$_REQUEST[$name] = $value;

return $this;
}

/**
* Convenience method to set a list of request parameters. Overwrites existing values without further notice.
*
Expand All @@ -179,6 +159,12 @@ public function setParameters(array $parameters) {
return $this;
}

public function setParameter($name, $value) {
$_REQUEST[$name] = $value;

return $this;
}

/**
* Convenience method to set a list of GET parameters. Overwrites existing values without further notice.
*
Expand All @@ -194,6 +180,13 @@ public function setGetParameters(array $parameters) {
return $this;
}

public function setGetParameter($name, $value) {
$_GET[$name] = $value;
$_REQUEST[$name] = $value;

return $this;
}

/**
* Convenience method to set a list of POST parameters. Overwrites existing values without further notice.
*
Expand All @@ -209,28 +202,9 @@ public function setPostParameters(array $parameters) {
return $this;
}

public function deleteParameter($name) {
unset($_GET[$name]);
unset($_POST[$name]);
unset($_REQUEST[$name]);

return $this;
}

public function deleteGetParameter($name) {
if (isset($_GET[$name])) {
unset($_GET[$name]);
unset($_REQUEST[$name]);
}

return $this;
}

public function deletePostParameter($name) {
if (isset($_POST[$name])) {
unset($_POST[$name]);
unset($_REQUEST[$name]);
}
public function setPostParameter($name, $value) {
$_POST[$name] = $value;
$_REQUEST[$name] = $value;

return $this;
}
Expand All @@ -250,6 +224,14 @@ public function deleteParameters(array $names) {
return $this;
}

public function deleteParameter($name) {
unset($_GET[$name]);
unset($_POST[$name]);
unset($_REQUEST[$name]);

return $this;
}

/**
* Convenience method to delete a list of GET parameters.
* <p/>
Expand All @@ -267,6 +249,15 @@ public function deleteGetParameters(array $names) {
return $this;
}

public function deleteGetParameter($name) {
if (isset($_GET[$name])) {
unset($_GET[$name]);
unset($_REQUEST[$name]);
}

return $this;
}

/**
* Convenience method to delete a list of POST parameters.
* <p/>
Expand All @@ -284,6 +275,15 @@ public function deletePostParameters(array $names) {
return $this;
}

public function deletePostParameter($name) {
if (isset($_POST[$name])) {
unset($_POST[$name]);
unset($_REQUEST[$name]);
}

return $this;
}

public function resetParameters() {
$_REQUEST = [];
$_GET = [];
Expand Down Expand Up @@ -314,12 +314,8 @@ public function resetPostParameters() {
return $this;
}

public function isSecure() {
if ($_SERVER['SERVER_PORT'] === 443) {
return true;
}

return isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == 1);
public function getRequestBody() {
return file_get_contents('php://input');
}

public function getUrl($absolute = false) {
Expand All @@ -338,6 +334,14 @@ public function getUrl($absolute = false) {
return $url;
}

public function isSecure() {
if ($_SERVER['SERVER_PORT'] === 443) {
return true;
}

return isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == 1);
}

public function getReferrerUrl($absolute = false) {

if (isset($_SERVER['HTTP_REFERER'])) {
Expand Down Expand Up @@ -379,10 +383,6 @@ public function hasCookie($name) {
return isset($_COOKIE[$name]);
}

public function getRequestUri() {
return $_SERVER['REQUEST_URI'];
}

public function getHost() {
return $_SERVER['HTTP_HOST'];
}
Expand All @@ -399,24 +399,12 @@ public function getPath() {
return $parts['path'];
}

// we are not using getallheaders() as we would limit the APF to be used with Apache only.
public function getHeaders() {
$headers = [];

foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
$headers[] = new HeaderImpl($name, $value);
} else if ($name == 'CONTENT_TYPE') {
$headers[] = new HeaderImpl('Content-Type', $value);
} else if ($name == 'CONTENT_LENGTH') {
$headers[] = new HeaderImpl('Content-Length', $value);
}
}

return $headers;
public function getRequestUri() {
return $_SERVER['REQUEST_URI'];
}

// we are not using getallheaders() as we would limit the APF to be used with Apache only.

/**
* Returns a HTTP header instance by a given name. In case the header has not been sent
* along with the request, <em>null</em> is returned instead.
Expand All @@ -436,8 +424,21 @@ public function getHeader($name) {
return null;
}

public function getMethod() {
return $_SERVER['REQUEST_METHOD'];
public function getHeaders() {
$headers = [];

foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
$headers[] = new HeaderImpl($name, $value);
} else if ($name == 'CONTENT_TYPE') {
$headers[] = new HeaderImpl('Content-Type', $value);
} else if ($name == 'CONTENT_LENGTH') {
$headers[] = new HeaderImpl('Content-Length', $value);
}
}

return $headers;
}

/**
Expand All @@ -458,6 +459,10 @@ public function isGet() {
return $this->getMethod() === self::METHOD_GET;
}

public function getMethod() {
return $_SERVER['REQUEST_METHOD'];
}

/**
* Convenience Method to determine whether we have a POST request.
*
Expand Down Expand Up @@ -512,6 +517,13 @@ public function isImage() {
return stripos($this->getAcceptHeaderContent(), 'image/') !== false;
}

/**
* @return string The Accept header content.
*/
protected function getAcceptHeaderContent() {
return isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
}

/**
* Convenience Method to determine whether we have an HTML request (e.g. useful for front controller
* actions).
Expand All @@ -531,6 +543,13 @@ public function isGzipSupported() {
return stripos($this->getAcceptedEncodingHeader(), 'gzip') !== false;
}

/**
* @return string The Accept-Encoding header content.
*/
protected function getAcceptedEncodingHeader() {
return isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
}

/**
* Convenience method to determine whether the client supports DEFLATE content encoding or not.
*
Expand All @@ -540,18 +559,4 @@ public function isDeflateSupported() {
return stripos($this->getAcceptedEncodingHeader(), 'deflate') !== false;
}

/**
* @return string The Accept header content.
*/
protected function getAcceptHeaderContent() {
return isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
}

/**
* @return string The Accept-Encoding header content.
*/
protected function getAcceptedEncodingHeader() {
return isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
}

}

0 comments on commit a393607

Please sign in to comment.