Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merging develop to master in preparation for 2.2.0 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Nov 12, 2019
2 parents 6e1e657 + 15af09f commit 66eded9
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
26 changes: 24 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,34 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.1.6 - TBD
## 2.2.0 - 2019-11-12

### Added

- [#376](https://github.com/zendframework/zend-diactoros/pull/376) adds support for using the X-Forwarded-Host header for determining the originally requested host name when marshaling the server request.

### Changed

- [#378](https://github.com/zendframework/zend-diactoros/pull/378) updates the `UploadedFile` class to extend `SplFileInfo`, allowing developers to make use of those features in their applications.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 2.2.0 - 2019-11-08

### Added

- [#377](https://github.com/zendframework/zend-diactoros/issues/377) enables UploadedFile to stand in and be used as an SplFileInfo object.

### Changed

- Nothing.
Expand Down Expand Up @@ -619,7 +641,7 @@ All notable changes to this project will be documented in this file, in reverse

- [#293](https://github.com/zendframework/zend-diactoros/pull/293) updates
`Uri::getHost()` to cast the value via `strtolower()` before returning it.
While this represents a change, it is fixing a bug in our implementation:
While this represents a change, it is fixing a bug in our implementation:
the PSR-7 specification for the method, which follows IETF RFC 3986 section
3.2.2, requires that the host name be normalized to lowercase.

Expand Down
2 changes: 2 additions & 0 deletions docs/book/v2/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,6 @@ In most cases, you will not interact with the Stream object directly.
and provides abstraction around a single uploaded file, including behavior for interacting with it
as a stream or moving it to a filesystem location.

Additionally, it extends PHP's build in `SplFileInfo` object in order to provide a compatible interface wherever such an object is needed.

In most cases, you will only use the methods defined in the `UploadedFileInterface`.
7 changes: 6 additions & 1 deletion src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Diactoros;

use SplFileInfo;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;

Expand All @@ -35,7 +36,7 @@
use const UPLOAD_ERR_OK;
use const UPLOAD_ERR_PARTIAL;

class UploadedFile implements UploadedFileInterface
class UploadedFile extends SplFileInfo implements UploadedFileInterface
{
const ERROR_MESSAGES = [
UPLOAD_ERR_OK => 'There is no error, the file uploaded with success',
Expand Down Expand Up @@ -102,9 +103,13 @@ public function __construct(
if ($errorStatus === UPLOAD_ERR_OK) {
if (is_string($streamOrFile)) {
$this->file = $streamOrFile;

parent::__construct($this->file);
}
if (is_resource($streamOrFile)) {
$this->stream = new Stream($streamOrFile);

parent::__construct($this->stream->getMetaData('uri'));
}

if (! $this->file && ! $this->stream) {
Expand Down
14 changes: 10 additions & 4 deletions src/functions/marshal_uri_from_sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function marshalUriFromSapi(array $server, array $headers) : Uri
* @return array Array of two items, host and port, in that order (can be
* passed to a list() operation).
*/
$marshalIpv6HostAndPort = function (array $server, string $host, ?int $port) : array {
$marshalIpv6HostAndPort = function (array $server, ?int $port) : array {
$host = '[' . $server['SERVER_ADDR'] . ']';
$port = $port ?: 80;
if ($port . ']' === substr($host, strrpos($host, ':') + 1)) {
Expand All @@ -93,8 +93,14 @@ function marshalUriFromSapi(array $server, array $headers) : Uri

static $defaults = ['', null];

if ($getHeaderFromArray('host', $headers, false)) {
return $marshalHostAndPortFromHeader($getHeaderFromArray('host', $headers));
$forwardedHost = $getHeaderFromArray('x-forwarded-host', $headers, false);
if ($forwardedHost !== false) {
return $marshalHostAndPortFromHeader($forwardedHost);
}

$host = $getHeaderFromArray('host', $headers, false);
if ($host !== false) {
return $marshalHostAndPortFromHeader($host);
}

if (! isset($server['SERVER_NAME'])) {
Expand All @@ -112,7 +118,7 @@ function marshalUriFromSapi(array $server, array $headers) : Uri

// Misinterpreted IPv6-Address
// Reported for Safari on Windows
return $marshalIpv6HostAndPort($server, $host, $port);
return $marshalIpv6HostAndPort($server, $port);
};

/**
Expand Down
10 changes: 10 additions & 0 deletions test/UploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use RuntimeException;
use SplFileInfo;
use Zend\Diactoros\Stream;
use Zend\Diactoros\UploadedFile;

Expand Down Expand Up @@ -325,4 +326,13 @@ public function testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDe
$this->expectExceptionMessage($message);
$uploadedFile->moveTo('/tmp/foo');
}

/**
* @see https://github.com/zendframework/zend-diactoros/pull/378
*/
public function testExtendsSplFileInfo()
{
$uploaded = new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK);
$this->assertInstanceOf(SplFileInfo::class, $uploaded);
}
}
36 changes: 36 additions & 0 deletions test/functions/MarshalUriFromSapiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,40 @@ public function returnsUrlWithCorrectHttpSchemeFromArraysProvider() : array
'empty' => ['', 'http'],
];
}

/**
* @dataProvider returnsUrlWithCorrectSchemeAndHostFromArrays
*/
public function testReturnsUrlWithCorrectSchemeAndHostFromArrays(
string $expectedScheme,
string $expectedHost,
array $server,
array $headers
) : void {
$uri = marshalUriFromSapi($server, $headers);
self::assertSame($expectedScheme, $uri->getScheme());
self::assertSame($expectedHost, $uri->getHost());
}

public function returnsUrlWithCorrectSchemeAndHostFromArrays() : array
{
return [
'x-forwarded-proto' => [
'https',
'localhost',
[
'SERVER_NAME' => 'localhost',
],
['X-Forwarded-Proto' => 'https'],
],
'x-forwarded-host' => [
'http',
'example.org',
[
'SERVER_NAME' => 'localhost',
],
['X-Forwarded-Host' => 'example.org', 'Host' => 'localhost'],
],
];
}
}

0 comments on commit 66eded9

Please sign in to comment.