1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Hyphper\Frame; |
4
|
|
|
|
5
|
|
|
use Hyphper\Frame\PaddingInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* DATA frames convey arbitrary, variable-length sequences of octets |
9
|
|
|
* associated with a stream. One or more DATA frames are used, for instance, |
10
|
|
|
* to carry HTTP request or response payloads. |
11
|
|
|
* |
12
|
|
|
* @package Hyphper\Frame |
13
|
|
|
*/ |
14
|
|
|
class DataFrame extends \Hyphper\Frame implements PaddingInterface |
15
|
|
|
{ |
16
|
|
|
use PaddingTrait; |
17
|
|
|
|
18
|
|
|
protected $defined_flags = [ |
19
|
|
|
Flag::END_STREAM, |
20
|
|
|
Flag::PADDED |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected $type = 0x0; |
24
|
|
|
protected $stream_association = self::HAS_STREAM; |
25
|
|
|
protected $data; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* DataFrame constructor. |
29
|
|
|
* |
30
|
|
|
* @param array $options |
31
|
|
|
*/ |
32
|
13 |
|
public function __construct(array $options = []) |
33
|
|
|
{ |
34
|
13 |
|
parent::__construct($options); |
35
|
12 |
|
$this->data = $options['data'] ?? ''; |
36
|
12 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
5 |
|
public function serializeBody(): string |
42
|
|
|
{ |
43
|
5 |
|
$padding_data = $this->serializePaddingData(); |
44
|
5 |
|
$padding = str_repeat("\0", $this->padding_length ?? 0); |
45
|
|
|
|
46
|
5 |
|
return $padding_data . $this->data . $padding; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Given the body of a frame, parses it into frame data. This populates |
51
|
|
|
* the non-header parts of the frame: that is, it does not populate the |
52
|
|
|
* stream ID or flags. |
53
|
|
|
* |
54
|
|
|
* |
55
|
|
|
* @param string $data |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
7 |
|
public function parseBody(string $data) |
60
|
|
|
{ |
61
|
7 |
|
$padding_data_length = $this->parsePaddingData($data); |
62
|
6 |
|
$this->data = $data; |
63
|
6 |
|
if ($this->padding_length) { |
64
|
3 |
|
$this->data = substr($data, $padding_data_length, $this->padding_length * -1); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
$this->body_len = strlen($data); |
68
|
6 |
|
if ($this->padding_length && $this->padding_length >= $this->body_len) { |
69
|
1 |
|
throw new \Hyphper\Frame\Exception\InvalidPaddingException("Padding is too long."); |
70
|
|
|
} |
71
|
5 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The length of the frame that needs to be accounted for when considering |
75
|
|
|
* flow control. |
76
|
|
|
*/ |
77
|
2 |
|
public function flowControlledLength() |
78
|
|
|
{ |
79
|
2 |
|
$padding_len = ($this->padding_length) ? $this->padding_length + 1 : 0; |
80
|
2 |
|
return strlen($this->data) + $padding_len; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
3 |
|
public function getData() |
87
|
|
|
{ |
88
|
3 |
|
return $this->data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $data |
93
|
|
|
* |
94
|
|
|
* @return DataFrame |
95
|
|
|
*/ |
96
|
5 |
|
public function setData($data) |
97
|
|
|
{ |
98
|
5 |
|
$this->data = $data; |
99
|
|
|
|
100
|
5 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|