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

Commit 271ae54

Browse files
committed
Merge pull request #122 from mtymek/parse_protocol_version
Parse protocol version
2 parents 68c015a + cd41db9 commit 271ae54

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/ServerRequestFactory.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Http\Message\MessageInterface;
1515
use Psr\Http\Message\UploadedFileInterface;
1616
use stdClass;
17+
use UnexpectedValueException;
1718

1819
/**
1920
* Class for marshaling a request object from the current PHP environment.
@@ -70,6 +71,7 @@ public static function fromGlobals(
7071
);
7172

7273
return $request
74+
->withProtocolVersion(static::marshalProtocolVersion($_SERVER))
7375
->withCookieParams($cookies ?: $_COOKIE)
7476
->withQueryParams($query ?: $_GET)
7577
->withParsedBody($body ?: $_POST);
@@ -455,4 +457,23 @@ private static function normalizeNestedFileSpec(array $files = [])
455457
}
456458
return $normalizedFiles;
457459
}
460+
461+
/**
462+
* Return HTTP protocol version (X.Y)
463+
*
464+
* @param array $server
465+
* @return string
466+
*/
467+
public static function marshalProtocolVersion(array $server)
468+
{
469+
if (!isset($server['SERVER_PROTOCOL'])) {
470+
return '1.1';
471+
}
472+
473+
if (!preg_match('#^HTTP/(?P<version>[1-9]\d*\.\d)$#', $server['SERVER_PROTOCOL'], $matches)) {
474+
throw new UnexpectedValueException('Unrecognized protocol version.');
475+
}
476+
477+
return $matches['version'];
478+
}
458479
}

test/ServerRequestFactoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
1313
use ReflectionProperty;
14+
use UnexpectedValueException;
1415
use Zend\Diactoros\ServerRequest;
1516
use Zend\Diactoros\ServerRequestFactory;
1617
use Zend\Diactoros\UploadedFile;
@@ -372,6 +373,7 @@ public function testCanCreateServerRequestViaFromGlobalsMethod()
372373
$this->assertEquals($body, $request->getParsedBody());
373374
$this->assertEquals($expectedFiles, $request->getUploadedFiles());
374375
$this->assertEmpty($request->getAttributes());
376+
$this->assertEquals('1.1', $request->getProtocolVersion());
375377
}
376378

377379
public function testNormalizeServerUsesMixedCaseAuthorizationHeaderFromApacheWhenPresent()
@@ -434,4 +436,22 @@ public function testNormalizeFilesReturnsOnlyActualFilesWhenOriginalFilesContain
434436

435437
$this->assertCount(1, $normalizedFiles['fooFiles']);
436438
}
439+
440+
public function testMarshalProtocolVersionReturnsHttpVersion()
441+
{
442+
$version = ServerRequestFactory::marshalProtocolVersion(['SERVER_PROTOCOL' => 'HTTP/1.0']);
443+
$this->assertEquals('1.0', $version);
444+
}
445+
446+
public function testMarshalProtocolVersionRisesExceptionIfVersionIsNotRecognized()
447+
{
448+
$this->setExpectedException('UnexpectedValueException');
449+
ServerRequestFactory::marshalProtocolVersion(['SERVER_PROTOCOL' => 'dadsa/1.0']);
450+
}
451+
452+
public function testMarshalProtocolReturnsDefaultValueIfHeaderIsNotPresent()
453+
{
454+
$version = ServerRequestFactory::marshalProtocolVersion([]);
455+
$this->assertEquals('1.1', $version);
456+
}
437457
}

0 commit comments

Comments
 (0)