2015-08-27 12:03:05 -07:00
< ? php
namespace GuzzleHttp\Psr7 ;
use InvalidArgumentException ;
use Psr\Http\Message\RequestInterface ;
use Psr\Http\Message\StreamInterface ;
use Psr\Http\Message\UriInterface ;
/**
* PSR - 7 request implementation .
*/
class Request implements RequestInterface
{
2016-07-18 09:07:48 -07:00
use MessageTrait ;
2015-08-27 12:03:05 -07:00
/** @var string */
private $method ;
/** @var null|string */
private $requestTarget ;
/** @var null|UriInterface */
private $uri ;
/**
2016-07-18 09:07:48 -07:00
* @ param string $method HTTP method
* @ param string | UriInterface $uri URI
* @ param array $headers Request headers
* @ param string | null | resource | StreamInterface $body Request body
* @ param string $version Protocol version
2015-08-27 12:03:05 -07:00
*/
public function __construct (
$method ,
$uri ,
array $headers = [],
$body = null ,
2016-07-18 09:07:48 -07:00
$version = '1.1'
2015-08-27 12:03:05 -07:00
) {
2016-07-18 09:07:48 -07:00
if ( ! ( $uri instanceof UriInterface )) {
2015-08-27 12:03:05 -07:00
$uri = new Uri ( $uri );
}
$this -> method = strtoupper ( $method );
$this -> uri = $uri ;
$this -> setHeaders ( $headers );
2016-07-18 09:07:48 -07:00
$this -> protocol = $version ;
2015-08-27 12:03:05 -07:00
2016-07-18 09:07:48 -07:00
if ( ! $this -> hasHeader ( 'Host' )) {
$this -> updateHostFromUri ();
2015-08-27 12:03:05 -07:00
}
2016-07-18 09:07:48 -07:00
if ( $body !== '' && $body !== null ) {
2015-08-27 12:03:05 -07:00
$this -> stream = stream_for ( $body );
}
}
public function getRequestTarget ()
{
if ( $this -> requestTarget !== null ) {
return $this -> requestTarget ;
}
$target = $this -> uri -> getPath ();
2016-07-18 09:07:48 -07:00
if ( $target == '' ) {
2015-08-27 12:03:05 -07:00
$target = '/' ;
}
2016-07-18 09:07:48 -07:00
if ( $this -> uri -> getQuery () != '' ) {
2015-08-27 12:03:05 -07:00
$target .= '?' . $this -> uri -> getQuery ();
}
return $target ;
}
public function withRequestTarget ( $requestTarget )
{
if ( preg_match ( '#\s#' , $requestTarget )) {
throw new InvalidArgumentException (
'Invalid request target provided; cannot contain whitespace'
);
}
$new = clone $this ;
$new -> requestTarget = $requestTarget ;
return $new ;
}
public function getMethod ()
{
return $this -> method ;
}
public function withMethod ( $method )
{
$new = clone $this ;
$new -> method = strtoupper ( $method );
return $new ;
}
public function getUri ()
{
return $this -> uri ;
}
public function withUri ( UriInterface $uri , $preserveHost = false )
{
if ( $uri === $this -> uri ) {
return $this ;
}
$new = clone $this ;
$new -> uri = $uri ;
if ( ! $preserveHost ) {
2016-07-18 09:07:48 -07:00
$new -> updateHostFromUri ();
2015-08-27 12:03:05 -07:00
}
return $new ;
}
2016-07-18 09:07:48 -07:00
private function updateHostFromUri ()
2015-08-27 12:03:05 -07:00
{
2016-07-18 09:07:48 -07:00
$host = $this -> uri -> getHost ();
2015-08-27 12:03:05 -07:00
2016-07-18 09:07:48 -07:00
if ( $host == '' ) {
return ;
}
if (( $port = $this -> uri -> getPort ()) !== null ) {
2015-08-27 12:03:05 -07:00
$host .= ':' . $port ;
}
2016-07-18 09:07:48 -07:00
if ( isset ( $this -> headerNames [ 'host' ])) {
$header = $this -> headerNames [ 'host' ];
} else {
$header = 'Host' ;
$this -> headerNames [ 'host' ] = 'Host' ;
}
// Ensure Host is the first header.
// See: http://tools.ietf.org/html/rfc7230#section-5.4
$this -> headers = [ $header => [ $host ]] + $this -> headers ;
2015-08-27 12:03:05 -07:00
}
}