1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Http\Http\Driver\Reader; |
4
|
|
|
|
5
|
|
|
use Dazzle\Throwable\Exception\Runtime\ReadException; |
6
|
|
|
use Dazzle\Throwable\Exception\Logic\InvalidFormatException; |
7
|
|
|
use Dazzle\Http\Http\Driver\Parser\HttpParser; |
8
|
|
|
use Dazzle\Http\Http\Driver\Parser\HttpParserInterface; |
9
|
|
|
use Dazzle\Util\Buffer\BufferInterface; |
10
|
|
|
use GuzzleHttp\Psr7; |
11
|
|
|
use Error; |
12
|
|
|
use Exception; |
13
|
|
|
|
14
|
|
|
class HttpReader implements HttpReaderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
const DEFAULT_MAX_SIZE = 0x4000; // 16 kB |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
const DEFAULT_START_LINE_LENGTH = 0x400; // 1 kB |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const HTTP_EOM = "\r\n\r\n"; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var HttpParserInterface |
33
|
|
|
*/ |
34
|
|
|
protected $parser; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $maxFrameSize; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed[] $options |
43
|
|
|
*/ |
44
|
77 |
|
public function __construct($options = []) |
45
|
|
|
{ |
46
|
77 |
|
$this->parser = new HttpParser(); |
47
|
|
|
|
48
|
77 |
|
$this->maxFrameSize = isset($options['maxFrameSize']) |
49
|
2 |
|
? (int) $options['maxFrameSize'] |
50
|
75 |
|
: self::DEFAULT_MAX_SIZE; |
51
|
77 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
32 |
|
public function __destruct() |
57
|
|
|
{ |
58
|
32 |
|
unset($this->parser); |
59
|
32 |
|
unset($this->maxFrameSize); |
60
|
32 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @override |
64
|
|
|
* @inheritDoc |
65
|
|
|
*/ |
66
|
6 |
View Code Duplication |
public function readRequest(BufferInterface $buffer, $data) |
|
|
|
|
67
|
|
|
{ |
68
|
6 |
|
$buffer->push($data); |
69
|
|
|
|
70
|
6 |
|
if (($position = $buffer->search(self::HTTP_EOM)) === false) |
71
|
|
|
{ |
72
|
1 |
|
if ($buffer->length() > $this->maxFrameSize) |
73
|
|
|
{ |
74
|
1 |
|
throw new ReadException( |
75
|
1 |
|
sprintf('Message start line exceeded maximum size of %d bytes.', $this->maxFrameSize) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try |
83
|
|
|
{ |
84
|
5 |
|
return $this->parser->parseRequest($buffer->drain()); |
85
|
|
|
} |
86
|
1 |
|
catch (Error $ex) |
|
|
|
|
87
|
|
|
{} |
88
|
1 |
|
catch (Exception $ex) |
|
|
|
|
89
|
|
|
{} |
90
|
|
|
|
91
|
1 |
|
throw new InvalidFormatException('Could not parse start line.', 0, $ex); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @override |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
3 |
View Code Duplication |
public function readResponse(BufferInterface $buffer, $data) |
|
|
|
|
99
|
|
|
{ |
100
|
3 |
|
$buffer->push($data); |
101
|
|
|
|
102
|
3 |
|
if (($position = $buffer->search(self::HTTP_EOM)) === false) |
103
|
|
|
{ |
104
|
1 |
|
if ($buffer->length() > $this->maxFrameSize) |
105
|
|
|
{ |
106
|
1 |
|
throw new ReadException( |
107
|
1 |
|
sprintf('Message start line exceeded maximum size of %d bytes.', $this->maxFrameSize) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
try |
115
|
|
|
{ |
116
|
2 |
|
return $this->parser->parseResponse($buffer->drain()); |
117
|
|
|
} |
118
|
1 |
|
catch (Error $ex) |
|
|
|
|
119
|
|
|
{} |
120
|
1 |
|
catch (Exception $ex) |
|
|
|
|
121
|
|
|
{} |
122
|
|
|
|
123
|
1 |
|
throw new InvalidFormatException('Could not parse start line.'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.