forked from reactphp/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestParser.php
More file actions
153 lines (119 loc) · 4.2 KB
/
RequestParser.php
File metadata and controls
153 lines (119 loc) · 4.2 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace React\Http;
use Evenement\EventEmitter;
use GuzzleHttp\Psr7 as gPsr;
/**
* @event headers
* @event error
*/
class RequestParser extends EventEmitter
{
private $buffer = '';
private $maxSize = 4096;
/**
* @var Request
*/
private $request;
private $length = 0;
public function feed($data)
{
$this->buffer .= $data;
if (!$this->request && false !== strpos($this->buffer, "\r\n\r\n")) {
// Extract the header from the buffer
// in case the content isn't complete
list($headers, $this->buffer) = explode("\r\n\r\n", $this->buffer, 2);
// Fail before parsing if the
if (strlen($headers) > $this->maxSize) {
$this->headerSizeExceeded();
return;
}
$this->request = $this->parseHeaders($headers . "\r\n\r\n");
if($this->request->expectsContinue()) {
$this->emit('expects_continue');
}
}
// if there is a request (meaning the headers are parsed) and
// we have the right content size, we can finish the parsing
if ($this->request && $this->isRequestComplete()) {
$this->parseBody(substr($this->buffer, 0, $this->length));
$this->finishParsing();
return;
}
// fail if the header hasn't finished but it is already too large
if (!$this->request && strlen($this->buffer) > $this->maxSize) {
$this->headerSizeExceeded();
return;
}
}
protected function isRequestComplete()
{
$headers = $this->request->getHeaders();
// if there is no content length, there should
// be no content so we can say it's done
if (!isset($headers['Content-Length'])) {
return true;
}
// if the content is present and has the
// right length, we're good to go
if (isset($headers['Content-Length']) && strlen($this->buffer) >= $headers['Content-Length']) {
// store the expected content length
$this->length = $this->request->getHeaders()['Content-Length'];
return true;
}
return false;
}
protected function finishParsing()
{
$this->emit('headers', array($this->request, $this->request->getBody()));
$this->removeAllListeners();
$this->request = null;
}
protected function headerSizeExceeded()
{
$this->emit('error', array(new \OverflowException("Maximum header size of {$this->maxSize} exceeded."), $this));
}
public function parseHeaders($data)
{
$psrRequest = gPsr\parse_request($data);
$parsedQuery = [];
$queryString = $psrRequest->getUri()->getQuery();
if ($queryString) {
parse_str($queryString, $parsedQuery);
}
$headers = array_map(function($val) {
if (1 === count($val)) {
$val = $val[0];
}
return $val;
}, $psrRequest->getHeaders());
return new Request(
$psrRequest->getMethod(),
$psrRequest->getUri(),
$parsedQuery,
$psrRequest->getProtocolVersion(),
$headers
);
}
public function parseBody($content)
{
$headers = $this->request->getHeaders();
if (isset($headers['Content-Type'])) {
if (strpos($headers['Content-Type'], 'multipart/') === 0) {
//TODO :: parse the content while it is streaming
preg_match("/boundary=\"?(.*)\"?$/", $headers['Content-Type'], $matches);
$boundary = $matches[1];
$parser = new MultipartParser($content, $boundary);
$parser->parse();
$this->request->setPost($parser->getPost());
$this->request->setFiles($parser->getFiles());
return;
}
if (strtolower($headers['Content-Type']) == 'application/x-www-form-urlencoded') {
parse_str($content, $result);
$this->request->setPost($result);
return;
}
}
$this->request->setBody($content);
}
}