Skip to content
Merged
4 changes: 2 additions & 2 deletions src/Http/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public function __construct(IRequest $request, IResponse $response)

/**
* Attempts to cache the sent entity by its last modification date.
* @param string|int|DateTime last modified time
* @param string|int|\DateTime last modified time
* @param string strong entity tag validator
* @return bool
*/
public function isModified($lastModified = NULL, $etag = NULL)
{
if ($lastModified) {
$this->response->setHeader('Last-Modified', $this->response->date($lastModified));
$this->response->setHeader('Last-Modified', Helpers::formatDate($lastModified));
}
if ($etag) {
$this->response->setHeader('ETag', '"' . addslashes($etag) . '"');
Expand Down
13 changes: 7 additions & 6 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
*
* @property-read string $name
* @property-read string $sanitizedName
* @property-read string $contentType
* @property-read string|NULL $contentType
* @property-read int $size
* @property-read string $temporaryFile
* @property-read int $error
* @property-read bool $ok
* @property-read bool $image
* @property-read array $imageSize
* @property-read string $contents
* @property-read array|NULL $imageSize
* @property-read string|NULL $contents
*/
class FileUpload extends Nette\Object
{
Expand Down Expand Up @@ -81,7 +81,7 @@ public function getSanitizedName()

/**
* Returns the MIME content type of an uploaded file.
* @return string
* @return string|NULL
*/
public function getContentType()
{
Expand Down Expand Up @@ -173,6 +173,7 @@ public function isImage()
/**
* Returns the image.
* @return Nette\Utils\Image
* @throws Nette\Utils\ImageException
*/
public function toImage()
{
Expand All @@ -182,7 +183,7 @@ public function toImage()

/**
* Returns the dimensions of an uploaded image as array.
* @return array
* @return array|NULL
*/
public function getImageSize()
{
Expand All @@ -192,7 +193,7 @@ public function getImageSize()

/**
* Get file contents.
* @return string
* @return string|NULL
*/
public function getContents()
{
Expand Down
16 changes: 15 additions & 1 deletion src/Http/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace Nette\Http;

use Nette;
use Nette,
Nette\Utils\DateTime;


/**
Expand All @@ -18,6 +19,19 @@
class Helpers
{

/**
* Returns HTTP valid date format.
* @param string|int|\DateTime
* @return string
*/
public static function formatDate($time)
{
$time = DateTime::from($time);
$time->setTimezone(new \DateTimeZone('GMT'));
return $time->format('D, d M Y H:i:s \G\M\T');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it better to use $time->format('D, d M Y H:i:s e')?

}


/**
* Is IP address in CIDR block?
* @return bool
Expand Down
8 changes: 4 additions & 4 deletions src/Http/IRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function getPost($key = NULL, $default = NULL);
/**
* Returns uploaded file.
* @param string key (or more keys)
* @return FileUpload
* @return FileUpload|NULL
*/
function getFile($key);

Expand Down Expand Up @@ -122,19 +122,19 @@ function isAjax();

/**
* Returns the IP address of the remote client.
* @return string
* @return string|NULL
*/
function getRemoteAddress();

/**
* Returns the host of the remote client.
* @return string
* @return string|NULL
*/
function getRemoteHost();

/**
* Returns raw content of HTTP request body.
* @return string
* @return string|NULL
*/
function getRawBody();

Expand Down
12 changes: 10 additions & 2 deletions src/Http/IResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function redirect($url, $code = self::S302_FOUND);

/**
* Sets the number of seconds before a page cached on a browser expires.
* @param mixed timestamp or number of seconds
* @param string|int|\DateTime time, value 0 means "until the browser is closed"
* @return void
*/
function setExpiration($seconds);
Expand All @@ -126,9 +126,17 @@ function setExpiration($seconds);
*/
function isSent();

/**
* Returns value of an HTTP header.
* @param string
* @param mixed
* @return mixed
*/
function getHeader($header, $default = NULL);

/**
* Returns a list of headers to sent.
* @return array
* @return array (name => value)
*/
function getHeaders();

Expand Down
63 changes: 26 additions & 37 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
* @author David Grudl
*
* @property-read UrlScript $url
* @property-read mixed $query
* @property-read bool $post
* @property-read array $query
* @property-read array $post
* @property-read array $files
* @property-read array $cookies
* @property-read string $method
* @property-read array $headers
* @property-read Url|NULL $referer
* @property-read bool $secured
* @property-read bool $ajax
* @property-read string $remoteAddress
* @property-read string $remoteHost
* @property-read string $rawBody
* @property-read string|NULL $remoteAddress
* @property-read string|NULL $remoteHost
* @property-read string|NULL $rawBody
*/
class Request extends Nette\Object implements IRequest
{
Expand All @@ -52,32 +52,33 @@ class Request extends Nette\Object implements IRequest
/** @var array */
private $headers;

/** @var string */
/** @var string|NULL */
private $remoteAddress;

/** @var string */
/** @var string|NULL */
private $remoteHost;

/** @var string */
private $rawBody;
/** @var callable|NULL */
private $rawBodyCallback;


public function __construct(UrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
$headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL)
$headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL, $rawBodyCallback = NULL)
{
$this->url = $url;
if ($query === NULL) {
parse_str($url->query, $this->query);
parse_str($url->getQuery(), $this->query);
} else {
$this->query = (array) $query;
}
$this->post = (array) $post;
$this->files = (array) $files;
$this->cookies = (array) $cookies;
$this->headers = array_change_key_case((array) $headers, CASE_LOWER);
$this->method = $method;
$this->method = $method ?: 'GET';
$this->remoteAddress = $remoteAddress;
$this->remoteHost = $remoteHost;
$this->rawBodyCallback = $rawBodyCallback;
}


Expand Down Expand Up @@ -139,7 +140,7 @@ public function getPost($key = NULL, $default = NULL)
/**
* Returns uploaded file.
* @param string key (or more keys)
* @return FileUpload
* @return FileUpload|NULL
*/
public function getFile($key)
{
Expand Down Expand Up @@ -204,8 +205,7 @@ public function isMethod($method)


/**
* Checks if the request method is POST.
* @return bool
* @deprecated
*/
public function isPost()
{
Expand All @@ -223,11 +223,7 @@ public function isPost()
public function getHeader($header, $default = NULL)
{
$header = strtolower($header);
if (isset($this->headers[$header])) {
return $this->headers[$header];
} else {
return $default;
}
return isset($this->headers[$header]) ? $this->headers[$header] : $default;
}


Expand Down Expand Up @@ -257,7 +253,7 @@ public function getReferer()
*/
public function isSecured()
{
return $this->url->scheme === 'https';
return $this->url->getScheme() === 'https';
}


Expand All @@ -273,7 +269,7 @@ public function isAjax()

/**
* Returns the IP address of the remote client.
* @return string
* @return string|NULL
*/
public function getRemoteAddress()
{
Expand All @@ -283,38 +279,31 @@ public function getRemoteAddress()

/**
* Returns the host of the remote client.
* @return string
* @return string|NULL
*/
public function getRemoteHost()
{
if (!$this->remoteHost) {
$this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
if ($this->remoteHost === NULL && $this->remoteAddress !== NULL) {
$this->remoteHost = getHostByAddr($this->remoteAddress);
}
return $this->remoteHost;
}


/**
* Returns raw content of HTTP request body.
* @return string
* @return string|NULL
*/
public function getRawBody()
{
if (PHP_VERSION_ID >= 50600) {
return file_get_contents('php://input');

} elseif ($this->rawBody === NULL) { // can be read only once in PHP < 5.6
$this->rawBody = (string) file_get_contents('php://input');
}

return $this->rawBody;
return $this->rawBodyCallback ? call_user_func($this->rawBodyCallback) : NULL;
}


/**
* Parse Accept-Language header and returns prefered language.
* @param array Supported languages
* @return string|null
* Parse Accept-Language header and returns preferred language.
* @param string[] supported languages
* @return string|NULL
*/
public function detectLanguage(array $langs)
{
Expand Down
Loading