1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZoiloMora\ElasticAPM\Events\Common; |
4
|
|
|
|
5
|
|
|
use ZoiloMora\ElasticAPM\Events\Common\Request\Socket; |
6
|
|
|
use ZoiloMora\ElasticAPM\Events\Common\Request\Url; |
7
|
|
|
use ZoiloMora\ElasticAPM\Helper\Encoding; |
8
|
|
|
use ZoiloMora\ElasticAPM\Helper\HttpHeaderSanitizer; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* If a log record was generated as a result of a http request, |
12
|
|
|
* the http interface can be used to collect this information. |
13
|
|
|
*/ |
14
|
|
|
class Request implements \JsonSerializable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Data should only contain the request body (not the query string). |
18
|
|
|
* It can either be a dictionary (for standard HTTP requests) or a raw request body. |
19
|
|
|
* |
20
|
|
|
* @var object|string|null |
21
|
|
|
*/ |
22
|
|
|
private $body; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The env variable is a compounded of environment information passed from the webserver. |
26
|
|
|
* |
27
|
|
|
* @var object|null |
28
|
|
|
*/ |
29
|
|
|
private $env; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Should include any headers sent by the requester. Cookies will be taken by headers if supplied. |
33
|
|
|
* |
34
|
|
|
* @var array|null |
35
|
|
|
*/ |
36
|
|
|
private $headers; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* HTTP version. |
40
|
|
|
* |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
private $httpVersion; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* HTTP method. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $method; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Socket|null |
54
|
|
|
*/ |
55
|
|
|
private $socket; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* A complete Url, with scheme, host and path. |
59
|
|
|
* |
60
|
|
|
* @var Url |
61
|
|
|
*/ |
62
|
|
|
private $url; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* A parsed key-value object of cookies |
66
|
|
|
* |
67
|
|
|
* @var array|null |
68
|
|
|
*/ |
69
|
|
|
private $cookies; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $method |
73
|
|
|
* @param Url $url |
74
|
|
|
* @param object|string|null $body |
75
|
|
|
* @param object|null $env |
76
|
|
|
* @param array|null $headers |
77
|
|
|
* @param string|null $httpVersion |
78
|
|
|
* @param Socket|null $socket |
79
|
|
|
* @param array|null $cookies |
80
|
|
|
*/ |
81
|
6 |
|
public function __construct( |
82
|
|
|
$method, |
83
|
|
|
Url $url, |
84
|
|
|
$body = null, |
85
|
|
|
$env = null, |
86
|
|
|
array $headers = null, |
87
|
|
|
$httpVersion = null, |
88
|
|
|
Socket $socket = null, |
89
|
|
|
array $cookies = null |
90
|
|
|
) { |
91
|
6 |
|
$this->body = $body; |
92
|
6 |
|
$this->env = $env; |
93
|
6 |
|
$this->headers = $headers; |
94
|
|
|
|
95
|
6 |
|
$this->httpVersion = $httpVersion; |
96
|
6 |
|
$this->method = $method; |
97
|
6 |
|
$this->socket = $socket; |
98
|
6 |
|
$this->url = $url; |
99
|
6 |
|
$this->cookies = $cookies; |
100
|
6 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return object|string|null |
104
|
|
|
*/ |
105
|
1 |
|
public function body() |
106
|
|
|
{ |
107
|
1 |
|
return $this->body; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return object|null |
112
|
|
|
*/ |
113
|
1 |
|
public function env() |
114
|
|
|
{ |
115
|
1 |
|
return $this->env; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return array|null |
120
|
|
|
*/ |
121
|
2 |
|
public function headers() |
122
|
|
|
{ |
123
|
2 |
|
return $this->headers; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string|null |
128
|
|
|
*/ |
129
|
2 |
|
public function httpVersion() |
130
|
|
|
{ |
131
|
2 |
|
return $this->httpVersion; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
1 |
|
public function method() |
138
|
|
|
{ |
139
|
1 |
|
return $this->method; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return Socket|null |
144
|
|
|
*/ |
145
|
1 |
|
public function socket() |
146
|
|
|
{ |
147
|
1 |
|
return $this->socket; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return Url |
152
|
|
|
*/ |
153
|
1 |
|
public function url() |
154
|
|
|
{ |
155
|
1 |
|
return $this->url; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return array|null |
160
|
|
|
*/ |
161
|
1 |
|
public function cookies() |
162
|
|
|
{ |
163
|
1 |
|
return $this->cookies; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return Request|null |
168
|
|
|
*/ |
169
|
16 |
|
public static function discover() |
170
|
|
|
{ |
171
|
16 |
|
if ('cli' === self::getMethod()) { |
172
|
13 |
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
3 |
|
return new self( |
176
|
3 |
|
self::getMethod(), |
177
|
3 |
|
Url::discover(), |
178
|
3 |
|
self::getBody(), |
179
|
3 |
|
null, |
180
|
3 |
|
self::getHeaders(), |
181
|
3 |
|
self::getHttpVersion(), |
182
|
3 |
|
Socket::discover() |
183
|
3 |
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return string|null |
188
|
|
|
*/ |
189
|
3 |
|
private static function getBody() |
190
|
|
|
{ |
191
|
3 |
|
$input = file_get_contents('php://input'); |
192
|
|
|
|
193
|
|
|
return '' !== $input |
194
|
3 |
|
? (string) $input |
195
|
3 |
|
: null; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return array|null |
200
|
|
|
*/ |
201
|
3 |
|
private static function getHeaders() |
202
|
|
|
{ |
203
|
3 |
|
$headers = []; |
204
|
|
|
|
205
|
3 |
|
foreach ($_SERVER as $key => $value) { |
206
|
3 |
|
if ('HTTP_' !== substr($key, 0, 5)) { |
207
|
3 |
|
continue; |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
$header = str_replace( |
211
|
1 |
|
' ', |
212
|
1 |
|
'-', |
213
|
1 |
|
ucwords( |
214
|
1 |
|
str_replace( |
215
|
1 |
|
'_', |
216
|
1 |
|
' ', |
217
|
1 |
|
strtolower( |
218
|
1 |
|
substr( |
219
|
1 |
|
$key, |
220
|
|
|
5 |
221
|
1 |
|
) |
222
|
1 |
|
) |
223
|
1 |
|
) |
224
|
1 |
|
) |
225
|
1 |
|
); |
226
|
|
|
|
227
|
1 |
|
$headers[$header] = $value; |
228
|
3 |
|
} |
229
|
|
|
|
230
|
3 |
|
$headers = HttpHeaderSanitizer::sanitize($headers); |
231
|
|
|
|
232
|
3 |
|
return 0 !== count($headers) |
233
|
3 |
|
? $headers |
234
|
3 |
|
: null; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return string|null |
239
|
|
|
*/ |
240
|
3 |
|
private static function getHttpVersion() |
241
|
|
|
{ |
242
|
3 |
|
if (false === array_key_exists('SERVER_PROTOCOL', $_SERVER)) { |
243
|
2 |
|
return null; |
244
|
|
|
} |
245
|
|
|
|
246
|
1 |
|
$position = strpos( |
247
|
1 |
|
$_SERVER['SERVER_PROTOCOL'], |
248
|
|
|
'/' |
249
|
1 |
|
); |
250
|
|
|
|
251
|
1 |
|
return substr( |
252
|
1 |
|
$_SERVER['SERVER_PROTOCOL'], |
253
|
|
|
$position + 1 |
254
|
1 |
|
); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
16 |
|
private static function getMethod() |
261
|
|
|
{ |
262
|
16 |
|
if (true === array_key_exists('REQUEST_METHOD', $_SERVER)) { |
263
|
3 |
|
return $_SERVER['REQUEST_METHOD']; |
264
|
|
|
} |
265
|
|
|
|
266
|
13 |
|
return 'cli'; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
1 |
|
public function jsonSerialize() |
273
|
|
|
{ |
274
|
|
|
return [ |
275
|
1 |
|
'body' => $this->body, |
276
|
1 |
|
'env' => $this->env, |
277
|
1 |
|
'headers' => $this->headers, |
278
|
1 |
|
'http_version' => Encoding::keywordField($this->httpVersion), |
279
|
1 |
|
'method' => Encoding::keywordField($this->method), |
280
|
1 |
|
'socket' => $this->socket, |
281
|
1 |
|
'url' => $this->url, |
282
|
1 |
|
'cookies' => $this->cookies, |
283
|
1 |
|
]; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|