Optimisations + bugfixes

This commit is contained in:
Simon Sessingø
2016-11-24 09:36:10 +01:00
parent f1a9a50ee5
commit 258e0e0f13
11 changed files with 243 additions and 143 deletions
+133 -102
View File
@@ -25,48 +25,123 @@ class Input
*/
protected $request;
protected $invalidContentType = false;
protected $invalidContentTypes = [
'text/plain',
'application/x-www-form-urlencoded',
];
public function __construct(Request $request)
{
$this->request = $request;
$this->setGet();
$this->setPost();
$this->setFile();
}
/**
* Get all get/post items
* @param array|null $filter Only take items in filter
* @return array
*/
public function all(array $filter = null)
{
$output = $_POST;
$this->post = new InputCollection();
$this->get = new InputCollection();
$this->file = new InputCollection();
if ($this->request->getMethod() === 'post') {
if ($request->getMethod() !== 'get') {
$contents = file_get_contents('php://input');
$requestContentType = $request->getHeader('http-content-type');
if (stripos(trim($contents), '{') === 0) {
$output = json_decode($contents, true);
if ($output === false) {
$output = array();
$max = count($this->invalidContentTypes) - 1;
for ($i = $max; $i >= 0; $i--) {
$contentType = $this->invalidContentType[$i];
if (stripos($requestContentType, $contentType) === 0) {
$this->invalidContentType = true;
break;
}
}
}
$output = array_merge($_GET, $output);
if ($this->invalidContentType === false) {
$this->parseInputs();
}
}
if ($filter !== null) {
$output = array_filter($output, function ($key) use ($filter) {
if (in_array($key, $filter)) {
return true;
}
protected function parseInputs()
{
/* Parse get requests */
if (count($_GET) > 0) {
$max = count($_GET) - 1;
$keys = array_keys($_GET);
for ($i = $max; $i >= 0; $i--) {
$key = $keys[$i];
$value = $_GET[$key];
$this->get->{$key} = new InputItem($key, $value);
}
return false;
}, ARRAY_FILTER_USE_KEY);
}
return $output;
/* Parse post requests */
$postVars = $_POST;
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete']) === true) {
parse_str(file_get_contents('php://input'), $postVars);
}
if (count($postVars) > 0) {
$max = count($postVars) - 1;
$keys = array_keys($postVars);
for ($i = $max; $i >= 0; $i--) {
$key = $keys[$i];
$value = $postVars[$key];
$this->post->{strtolower($key)} = new InputItem($key, $value);
}
}
/* Parse get requests */
if (count($_FILES) > 0) {
$max = count($_FILES) - 1;
$keys = array_keys($_FILES);
for ($i = $max; $i >= 0; $i--) {
$key = $keys[$i];
$value = $_FILES[$key];
// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $key;
$this->file->{strtolower($key)} = InputFile::createFromArray($values);
continue;
}
$output = new InputCollection();
foreach ($value['name'] as $k => $val) {
$output->{$k} = InputFile::createFromArray([
'index' => $key,
'error' => $value['error'][$k],
'tmp_name' => $value['tmp_name'][$k],
'type' => $value['type'][$k],
'size' => $value['size'][$k],
'name' => $value['name'][$k],
]);
}
$this->file->{strtolower($key)} = $output;
}
}
}
public function getObject($index, $default = null)
@@ -98,9 +173,10 @@ class Input
/**
* Get input element value matching index
*
* @param string $index
* @param string|null $default
* @return string|null
* @return InputItem|string
*/
public function get($index, $default = null)
{
@@ -112,7 +188,7 @@ class Input
return $item;
}
return (trim($item->getValue()) === '') ? $default : $item->getValue();
return (!is_array($item->getValue()) && trim($item->getValue()) === '') ? $default : $item->getValue();
}
return $default;
@@ -123,89 +199,44 @@ class Input
return ($this->getObject($index) !== null);
}
public function setGet()
/**
* Get all get/post items
* @param array|null $filter Only take items in filter
* @return array
*/
public function all(array $filter = null)
{
$this->get = new InputCollection();
if ($this->invalidContentType === true) {
return [];
}
if (count($_GET) > 0) {
foreach ($_GET as $key => $get) {
if (is_array($get) === false) {
$this->get->{$key} = new InputItem($key, $get);
continue;
$output = $_POST;
if ($this->request->getMethod() === 'post') {
$contents = file_get_contents('php://input');
if (stripos(trim($contents), '{') === 0) {
$output = json_decode($contents, true);
if ($output === false) {
$output = [];
}
$output = new InputCollection();
foreach ($get as $k => $g) {
$output->{$k} = new InputItem($k, $g);
}
$this->get->{$key} = $output;
}
}
}
public function setPost()
{
$this->post = new InputCollection();
$output = array_merge($_GET, $output);
$postVars = $_POST;
if ($filter !== null) {
$output = array_filter($output, function ($key) use ($filter) {
if (in_array($key, $filter)) {
return true;
}
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete']) === true) {
parse_str(file_get_contents('php://input'), $postVars);
return false;
}, ARRAY_FILTER_USE_KEY);
}
if (count($postVars) > 0) {
foreach ($postVars as $key => $post) {
if (is_array($post) === false) {
$this->post->{strtolower($key)} = new InputItem($key, $post);
continue;
}
$output = new InputCollection();
foreach ($post as $k => $p) {
$output->{$k} = new InputItem($k, $p);
}
$this->post->{strtolower($key)} = $output;
}
}
}
public function setFile()
{
$this->file = new InputCollection();
if (count($_FILES) > 0) {
foreach ($_FILES as $key => $values) {
// Handle array input
if (is_array($values['name']) === false && trim($values['error']) !== '4') {
$values['index'] = $key;
$this->file->{strtolower($key)} = InputFile::createFromArray($values);
continue;
}
$output = new InputCollection();
foreach ($values['name'] as $k => $val) {
if (trim($val['error'][$k]) !== '4') {
$output->{$k} = InputFile::createFromArray([
'index' => $k,
'error' => $val['error'][$k],
'tmp_name' => $val['tmp_name'][$k],
'type' => $val['type'][$k],
'size' => $val['size'][$k],
'name' => $val['name'][$k]
]);
}
}
$this->file->{strtolower($key)} = $output;
}
}
return $output;
}
}
+3 -6
View File
@@ -3,7 +3,7 @@ namespace Pecee\Http\Input;
class InputCollection implements \IteratorAggregate
{
protected $data = array();
protected $data = [];
/**
* Search for input element matching index.
@@ -41,17 +41,13 @@ class InputCollection implements \IteratorAggregate
{
$input = $this->findFirst($index);
if($input !== null && trim($input->getValue()) !== '') {
if ($input !== null && trim($input->getValue()) !== '') {
return $input->getValue();
}
return $default;
}
public function getValue($index, $default = null) {
}
/**
* @param string $index
* @throws \InvalidArgumentException
@@ -63,6 +59,7 @@ class InputCollection implements \IteratorAggregate
// Ensure that item are always available
if ($item === null) {
$this->data[$index] = new InputItem($index, null);
return $this->data[$index];
}
+19 -8
View File
@@ -104,6 +104,15 @@ class InputFile extends InputItem
return $this;
}
public function getValue()
{
return $this->getTmpName();
}
public function hasError() {
return ($this->getError() !== 0);
}
/**
* Create from array
* @param array $values
@@ -116,21 +125,23 @@ class InputFile extends InputItem
}
$input = new static($values['index']);
$input->setTmpName((isset($values['error']) ? $values['error'] : null));
$input->setError((isset($values['error']) ? $values['error'] : null));
$input->setName((isset($values['name']) ? $values['name'] : null));
$input->setSize((isset($values['size']) ? $values['size'] : null));
$input->setType((isset($values['type']) ? $values['type'] : null));
$input->setError((isset($values['tmp_name']) ? $values['tmp_name'] : null));
$input->setTmpName((isset($values['tmp_name']) ? $values['tmp_name'] : null));
return $input;
}
/**
* @return string
*/
public function getValue()
{
return $this->tmpName;
public function toArray() {
return [
'tmp_name' => $this->tmpName,
'type' => $this->type,
'size' => $this->size,
'name' => $this->name,
'error' => $this->error,
];
}
}
@@ -2,7 +2,7 @@
namespace Pecee\Http\Middleware;
use Pecee\CsrfToken;
use Pecee\Exceptions\TokenMismatchException;
use Pecee\Http\Middleware\Exceptions\TokenMismatchException;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
@@ -1,5 +1,5 @@
<?php
namespace Pecee\Exceptions;
namespace Pecee\Http\Middleware\Exceptions;
class TokenMismatchException extends \Exception
{
+23 -5
View File
@@ -5,7 +5,7 @@ use Pecee\Http\Input\Input;
class Request
{
protected $data = array();
protected $data = [];
protected $headers;
protected $host;
protected $uri;
@@ -15,20 +15,21 @@ class Request
public function __construct()
{
$this->parseHeaders();
$this->input = new Input($this);
$this->host = $this->getHeader('http-host');;
$this->uri = $this->getHeader('request-uri');
$this->method = strtolower($this->input->post->findFirst('_method', $this->getHeader('request-method')));
$this->method = strtolower($this->getHeader('request-method'));
$this->input = new Input($this);
}
protected function parseHeaders()
{
$this->headers = array();
$this->headers = [];
foreach ($_SERVER as $name => $value) {
$this->headers[strtolower($name)] = $value;
$this->headers[strtolower(str_replace('_', '-', $name))] = $value;
}
}
public function isSecure()
@@ -136,7 +137,24 @@ class Request
*/
public function getHeader($name, $defaultValue = null)
{
return isset($this->headers[strtolower($name)]) ? $this->headers[strtolower($name)] : $defaultValue;
if (isset($this->headers[strtolower($name)])) {
return $this->headers[strtolower($name)];
}
$max = count($_SERVER) - 1;
$keys = array_keys($_SERVER);
for ($i = $max; $i >= 0; $i--) {
$key = $keys[$i];
$name = $_SERVER[$key];
if ($key === $name) {
return $name;
}
}
return $defaultValue;
}
/**
+9 -9
View File
@@ -66,13 +66,12 @@ class Response
'Etag: ' . $eTag
]);
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $lastModified ||
isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $eTag
) {
$httpModified = $this->request->getHeader('http-if-modified-since');
$httpIfNoneMatch = $this->request->getHeader('http-if-none-match');
$this->headers([
'HTTP/1.1 304 Not Modified'
]);
if ($httpModified !== null && strtotime($httpModified) == $lastModified || $httpIfNoneMatch !== null && $httpIfNoneMatch === $eTag) {
$this->header('HTTP/1.1 304 Not Modified');
exit();
}
@@ -86,7 +85,7 @@ class Response
*/
public function json(array $value)
{
$this->header('Content-type: application/json');
$this->header('Content-Type: application/json');
echo json_encode($value);
die();
}
@@ -109,8 +108,9 @@ class Response
*/
public function headers(array $headers)
{
foreach ($headers as $header) {
header($header);
$max = count($headers);
for($i = 0; $i < $max; $i++) {
$this->header($headers[$i]);
}
return $this;
}