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

Commit 66eded9

Browse files
committed
Merging develop to master in preparation for 2.2.0 release.
2 parents 6e1e657 + 15af09f commit 66eded9

File tree

6 files changed

+88
-7
lines changed

6 files changed

+88
-7
lines changed

CHANGELOG.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,34 @@
22

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

5-
## 2.1.6 - TBD
5+
## 2.2.0 - 2019-11-12
66

77
### Added
88

9+
- [#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.
10+
11+
### Changed
12+
13+
- [#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.
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
925
- Nothing.
1026

27+
## 2.2.0 - 2019-11-08
28+
29+
### Added
30+
31+
- [#377](https://github.com/zendframework/zend-diactoros/issues/377) enables UploadedFile to stand in and be used as an SplFileInfo object.
32+
1133
### Changed
1234

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

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

docs/book/v2/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,6 @@ In most cases, you will not interact with the Stream object directly.
194194
and provides abstraction around a single uploaded file, including behavior for interacting with it
195195
as a stream or moving it to a filesystem location.
196196

197+
Additionally, it extends PHP's build in `SplFileInfo` object in order to provide a compatible interface wherever such an object is needed.
198+
197199
In most cases, you will only use the methods defined in the `UploadedFileInterface`.

src/UploadedFile.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Zend\Diactoros;
1111

12+
use SplFileInfo;
1213
use Psr\Http\Message\StreamInterface;
1314
use Psr\Http\Message\UploadedFileInterface;
1415

@@ -35,7 +36,7 @@
3536
use const UPLOAD_ERR_OK;
3637
use const UPLOAD_ERR_PARTIAL;
3738

38-
class UploadedFile implements UploadedFileInterface
39+
class UploadedFile extends SplFileInfo implements UploadedFileInterface
3940
{
4041
const ERROR_MESSAGES = [
4142
UPLOAD_ERR_OK => 'There is no error, the file uploaded with success',
@@ -102,9 +103,13 @@ public function __construct(
102103
if ($errorStatus === UPLOAD_ERR_OK) {
103104
if (is_string($streamOrFile)) {
104105
$this->file = $streamOrFile;
106+
107+
parent::__construct($this->file);
105108
}
106109
if (is_resource($streamOrFile)) {
107110
$this->stream = new Stream($streamOrFile);
111+
112+
parent::__construct($this->stream->getMetaData('uri'));
108113
}
109114

110115
if (! $this->file && ! $this->stream) {

src/functions/marshal_uri_from_sapi.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function marshalUriFromSapi(array $server, array $headers) : Uri
8080
* @return array Array of two items, host and port, in that order (can be
8181
* passed to a list() operation).
8282
*/
83-
$marshalIpv6HostAndPort = function (array $server, string $host, ?int $port) : array {
83+
$marshalIpv6HostAndPort = function (array $server, ?int $port) : array {
8484
$host = '[' . $server['SERVER_ADDR'] . ']';
8585
$port = $port ?: 80;
8686
if ($port . ']' === substr($host, strrpos($host, ':') + 1)) {
@@ -93,8 +93,14 @@ function marshalUriFromSapi(array $server, array $headers) : Uri
9393

9494
static $defaults = ['', null];
9595

96-
if ($getHeaderFromArray('host', $headers, false)) {
97-
return $marshalHostAndPortFromHeader($getHeaderFromArray('host', $headers));
96+
$forwardedHost = $getHeaderFromArray('x-forwarded-host', $headers, false);
97+
if ($forwardedHost !== false) {
98+
return $marshalHostAndPortFromHeader($forwardedHost);
99+
}
100+
101+
$host = $getHeaderFromArray('host', $headers, false);
102+
if ($host !== false) {
103+
return $marshalHostAndPortFromHeader($host);
98104
}
99105

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

113119
// Misinterpreted IPv6-Address
114120
// Reported for Safari on Windows
115-
return $marshalIpv6HostAndPort($server, $host, $port);
121+
return $marshalIpv6HostAndPort($server, $port);
116122
};
117123

118124
/**

test/UploadedFileTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPUnit\Framework\TestCase;
1414
use ReflectionProperty;
1515
use RuntimeException;
16+
use SplFileInfo;
1617
use Zend\Diactoros\Stream;
1718
use Zend\Diactoros\UploadedFile;
1819

@@ -325,4 +326,13 @@ public function testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDe
325326
$this->expectExceptionMessage($message);
326327
$uploadedFile->moveTo('/tmp/foo');
327328
}
329+
330+
/**
331+
* @see https://github.com/zendframework/zend-diactoros/pull/378
332+
*/
333+
public function testExtendsSplFileInfo()
334+
{
335+
$uploaded = new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK);
336+
$this->assertInstanceOf(SplFileInfo::class, $uploaded);
337+
}
328338
}

test/functions/MarshalUriFromSapiTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,40 @@ public function returnsUrlWithCorrectHttpSchemeFromArraysProvider() : array
7474
'empty' => ['', 'http'],
7575
];
7676
}
77+
78+
/**
79+
* @dataProvider returnsUrlWithCorrectSchemeAndHostFromArrays
80+
*/
81+
public function testReturnsUrlWithCorrectSchemeAndHostFromArrays(
82+
string $expectedScheme,
83+
string $expectedHost,
84+
array $server,
85+
array $headers
86+
) : void {
87+
$uri = marshalUriFromSapi($server, $headers);
88+
self::assertSame($expectedScheme, $uri->getScheme());
89+
self::assertSame($expectedHost, $uri->getHost());
90+
}
91+
92+
public function returnsUrlWithCorrectSchemeAndHostFromArrays() : array
93+
{
94+
return [
95+
'x-forwarded-proto' => [
96+
'https',
97+
'localhost',
98+
[
99+
'SERVER_NAME' => 'localhost',
100+
],
101+
['X-Forwarded-Proto' => 'https'],
102+
],
103+
'x-forwarded-host' => [
104+
'http',
105+
'example.org',
106+
[
107+
'SERVER_NAME' => 'localhost',
108+
],
109+
['X-Forwarded-Host' => 'example.org', 'Host' => 'localhost'],
110+
],
111+
];
112+
}
77113
}

0 commit comments

Comments
 (0)