1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the ReCaptcha Library. |
4
|
|
|
* |
5
|
|
|
* (c) Ilya Pokamestov <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DS\Library\ReCaptcha\Http; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use Psr\Http\Message\UriInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ReCaptcha library, PSR7 request representation. |
19
|
|
|
* |
20
|
|
|
* @author Ilya Pokamestov <[email protected]> |
21
|
|
|
* |
22
|
|
|
* Class Request |
23
|
|
|
* @package DS\Library\ReCaptcha\Http |
24
|
|
|
*/ |
25
|
|
|
class Request extends Message implements RequestInterface |
26
|
|
|
{ |
27
|
|
|
/** @var string */ |
28
|
|
|
protected $method; |
29
|
|
|
/** @var null|UriInterface */ |
30
|
|
|
protected $uri; |
31
|
|
|
/** @var null|string */ |
32
|
|
|
protected $requestTarget; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param null|string $method HTTP method for the request. |
36
|
|
|
* @param null|string $uri URI for the request. |
37
|
|
|
* |
38
|
|
|
* @throws InvalidArgumentException for an invalid URI |
39
|
|
|
*/ |
40
|
9 |
|
public function __construct($method = null, $uri = null) |
41
|
|
|
{ |
42
|
9 |
|
if (is_string($uri)) { |
43
|
6 |
|
$this->uri = new Uri($uri); |
44
|
6 |
|
$this->addHostHeader($this->uri); |
45
|
9 |
|
} elseif ($uri instanceof UriInterface) { |
46
|
3 |
|
$this->uri = $uri; |
47
|
3 |
|
$this->addHostHeader($this->uri); |
48
|
3 |
|
} |
49
|
|
|
|
50
|
9 |
|
if (is_string($method)) { |
51
|
9 |
|
$this->method = strtoupper($method); |
52
|
9 |
|
} |
53
|
9 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Build host. |
57
|
|
|
* |
58
|
|
|
* @param UriInterface $uri |
59
|
|
|
*/ |
60
|
9 |
|
protected function addHostHeader(UriInterface $uri) |
61
|
|
|
{ |
62
|
9 |
|
$host = $uri->getHost(); |
63
|
9 |
|
if ($port = $uri->getPort()) { |
64
|
1 |
|
$host = sprintf('%s:%s', $host, $port); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
9 |
|
$this->headers['host'] = array($host); |
68
|
9 |
|
} |
69
|
|
|
|
70
|
|
|
/** {@inheritdoc} */ |
71
|
1 |
|
public function getRequestTarget() |
72
|
|
|
{ |
73
|
1 |
|
if ($this->requestTarget !== null) { |
74
|
1 |
|
return $this->requestTarget; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$target = $this->uri->getPath(); |
78
|
|
|
|
79
|
1 |
|
if ('' === $target) { |
80
|
1 |
|
$target = '/'; |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
if ($this->uri->getQuery()) { |
84
|
1 |
|
$target .= '?' . $this->uri->getQuery(); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
return $target; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** {@inheritdoc} */ |
91
|
1 |
|
public function withRequestTarget($requestTarget) |
92
|
|
|
{ |
93
|
1 |
|
if (preg_match('#\s#', $requestTarget)) { |
94
|
1 |
|
throw new InvalidArgumentException( |
95
|
|
|
'Invalid request target provided; cannot contain whitespace' |
96
|
1 |
|
); |
97
|
|
|
} |
98
|
1 |
|
$this->requestTarget = $requestTarget; |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** {@inheritdoc} */ |
104
|
3 |
|
public function getMethod() |
105
|
|
|
{ |
106
|
3 |
|
return $this->method; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** {@inheritdoc} */ |
110
|
2 |
|
public function withMethod($method) |
111
|
|
|
{ |
112
|
2 |
|
$this->method = strtoupper($method); |
113
|
|
|
|
114
|
2 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** {@inheritdoc} */ |
118
|
3 |
|
public function getUri() |
119
|
|
|
{ |
120
|
3 |
|
return $this->uri; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** {@inheritdoc} */ |
124
|
2 |
|
public function withUri(UriInterface $uri, $preserveHost = false) |
125
|
|
|
{ |
126
|
2 |
|
$this->uri = $uri; |
127
|
|
|
|
128
|
2 |
|
if (!$preserveHost) { |
129
|
2 |
|
if ($uri->getHost()) { |
130
|
2 |
|
$this->addHostHeader($uri); |
131
|
2 |
|
} |
132
|
2 |
|
} |
133
|
|
|
|
134
|
2 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|