-
Notifications
You must be signed in to change notification settings - Fork 0
/
_functions.php
232 lines (191 loc) · 6.91 KB
/
_functions.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace Poirot\Connection\Http
{
use Poirot\Stream\Interfaces\iStreamable;
use Poirot\Stream\Streamable\STemporary;
/**
* Parse Header line
*
* @param string $headers
*
* @return false|array[string 'label', string 'value']
*/
function parseHeaderLines($headers)
{
if (! preg_match_all('/.*[\n]?/', $headers, $lines) )
throw new \InvalidArgumentException('Error Parsing Request Message.');
$lines = $lines[0];
$headers = array();
foreach ($lines as $line)
$headers += splitLabelValue($line);
return $headers;
}
/**
* Parse Header line
*
* - name MUST be composed of printable US-ASCII characters (i.e.,
* characters that have values between 33 and 126, inclusive),
* except colon.
*
* @param string $line
*
* @return array['label' => 'value_line']
*/
function splitLabelValue($line)
{
if (! preg_match('/^(?P<label>[^()><@,;:\"\\/\[\]?=}{ \t]+):(?P<value>.*)$/', $line, $matches) )
if ($matches === false || !isset($matches['label']) || !isset($matches['value']) )
throw new \InvalidArgumentException(sprintf(
'Invalid Header (%s).'
, $line
));
return array( $matches['label'] => trim($matches['value'], "\r\n") );
}
/**
* Parse Response Headers
*
* @param string $httpMessage
*
* @return array['version'=>string, 'status'=>int, 'reason'=>string, 'headers'=>array(key=>val)]
*/
function parseResponseHeaders($httpMessage)
{
if (! preg_match_all('/.*[\r\n]?/', $httpMessage, $lines) )
throw new \InvalidArgumentException('Error Parsing Request Message.');
$lines = $lines[0];
$firstLine = array_shift($lines);
$regex = '/^HTTP\/(?P<version>1\.[01]) (?P<status>\d{3})(?:[ ]+(?P<reason>.*))?$/';
$matches = array();
if (!preg_match($regex, $firstLine, $matches))
throw new \InvalidArgumentException(
'A valid response status line was not found in the provided string.'
. ' response:'
. $httpMessage
);
// ...
$return = array();
$return['version'] = $matches['version'];
$return['status'] = (int) $matches['status'];
$return['reason'] = (isset($matches['reason']) ? $matches['reason'] : '');
// headers:
$return['headers'] = array();
while ($nextLine = array_shift($lines)) {
if (trim($nextLine) == '')
// headers end
break;
if (! preg_match('/^(?P<label>[^()><@,;:\"\\/\[\]?=}{ \t]+):(?P<value>.*)$/', $nextLine, $matches))
throw new \InvalidArgumentException(
'Valid Header not found: '.$nextLine
);
$return['headers'][$matches['label']] = trim($matches['value']);
}
// sort headers
ksort($return['headers']);
return $return;
}
/**
* Parse Status Line From Request
*
* @param string $httpMessage
*
* @return array['version'=>string, 'status'=>int, 'reason'=>string, 'headers'=>array(key=>val)]
*/
function parseStatusLine($httpMessage)
{
if (! preg_match_all('/.*[\r\n]?/', $httpMessage, $lines) )
throw new \InvalidArgumentException('Error Parsing Request Message.');
$lines = $lines[0];
$firstLine = array_shift($lines);
$regex = '/^HTTP\/(?P<version>1\.[01]) (?P<status>\d{3})(?:[ ]+(?P<reason>.*))?$/';
$matches = array();
if (! preg_match($regex, $firstLine, $matches) )
throw new \InvalidArgumentException(
'A valid response status line was not found in the provided string.'
. ' response:'
. $httpMessage
);
return [
'version' => $matches['version'],
'status' => (int) $matches['status'],
'reason' => $matches['reason'],
];
}
/**
* Parse Request Message Expresssion
*
* @param string $request
*
* @return array
*/
function parseRequest($request)
{
if (! preg_match_all('/.*[\n]?/', $request, $lines) )
throw new \InvalidArgumentException('Error Parsing Request Message.');
$lines = $lines[0];
// request line:
$firstLine = array_shift($lines);
$matches = null;
$methods = '\w+';
$regex = '#^(?P<method>' . $methods . ')\s(?P<uri>[^ ]*)(?:\sHTTP\/(?P<version>\d+\.\d+)){0,1}#';
if (! preg_match($regex, $firstLine, $matches) )
throw new \InvalidArgumentException(
'A valid request line was not found in the provided message.'
);
$return = array();
$return['method'] = $matches['method'];
$return['uri'] = $matches['uri'];
if (isset($matches['version']))
$return['version'] = $matches['version'];
// headers:
while ($nextLine = array_shift($lines)) {
if (trim($nextLine) == '')
// headers end
break;
if (! preg_match('/^(?P<label>[^()><@,;:\"\\/\[\]?=}{ \t]+):(?P<value>.*)$/', $nextLine, $matches))
throw new \InvalidArgumentException(
'Valid Header not found: '.$nextLine
);
$return['headers'][$matches['label']] = trim($matches['value']);
}
// body:
parse_str(urldecode(implode("\r\n", $lines)), $output);
$return['body'] = $output;
return $return;
}
/**
* Read and Skip Headers Of Http Message
*
* @param iStreamable|string $message
*
* @return string Headers of http message
* @throws \Exception
*/
function readAndSkipHeaders($message)
{
if (! $message instanceof iStreamable ) {
$message = (string) $message;
$message = new STemporary($message);
}
$stream = $message;
if ( $stream->getCurrOffset() > 0 ) {
if (! $stream->resource()->isSeekable() )
throw new \Exception(sprintf(
'Reading Headers Must Start From Begining Of Request Stream Or Stream Been Seekable; current offset is: (%s).'
, $stream->getCurrOffset()
));
else
$stream->seek(0);
}
$headers = '';
## 255 can be vary, its each header length.
while(! $stream->isEOF() && ($line = $stream->readLine("\r\n")) !== null ) {
$break = false;
$headers .= $line."\r\n";
if (trim($line) === '')
## http headers part read complete
$break = true;
if ($break) break;
}
return $headers;
}
}