forked from reactphp/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestHeaderParser.php
More file actions
93 lines (77 loc) · 2.65 KB
/
RequestHeaderParser.php
File metadata and controls
93 lines (77 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace React\Http;
use Evenement\EventEmitter;
use Exception;
use RingCentral\Psr7 as g7;
/**
* @event headers
* @event error
*
* @internal
*/
class RequestHeaderParser extends EventEmitter
{
private $buffer = '';
private $maxSize = 4096;
public function feed($data)
{
$this->buffer .= $data;
$endOfHeader = strpos($this->buffer, "\r\n\r\n");
if (false !== $endOfHeader) {
$currentHeaderSize = $endOfHeader;
} else {
$currentHeaderSize = strlen($this->buffer);
}
if ($currentHeaderSize > $this->maxSize) {
$this->emit('error', array(new \OverflowException("Maximum header size of {$this->maxSize} exceeded."), $this));
$this->removeAllListeners();
return;
}
if (false !== $endOfHeader) {
try {
$this->parseAndEmitRequest();
} catch (Exception $exception) {
$this->emit('error', array($exception));
}
$this->removeAllListeners();
}
}
private function parseAndEmitRequest()
{
list($request, $bodyBuffer) = $this->parseRequest($this->buffer);
$this->emit('headers', array($request, $bodyBuffer));
}
private function parseRequest($data)
{
list($headers, $bodyBuffer) = explode("\r\n\r\n", $data, 2);
$originalTarget = null;
if (strpos($headers, 'OPTIONS * ') === 0) {
$originalTarget = '*';
$headers = 'OPTIONS / ' . substr($headers, 10);
} elseif (strpos($headers, 'CONNECT ') === 0) {
$parts = explode(' ', $headers, 3);
$uri = parse_url('tcp://' . $parts[1]);
// check this is a valid authority-form request-target (host:port)
if (isset($uri['scheme'], $uri['host'], $uri['port']) && count($uri) === 3) {
$originalTarget = $parts[1];
$parts[1] = '/';
$headers = implode(' ', $parts);
}
}
$request = g7\parse_request($headers);
// Do not assume this is HTTPS when this happens to be port 443
// detecting HTTPS is left up to the socket layer (TLS detection)
if ($request->getUri()->getScheme() === 'https') {
$request = $request->withUri(
$request->getUri()->withScheme('http')->withPort(443)
);
}
if ($originalTarget !== null) {
$request = $request->withUri(
$request->getUri()->withPath(''),
true
)->withRequestTarget($originalTarget);
}
return array($request, $bodyBuffer);
}
}