1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Hyphper\Frame; |
4
|
|
|
|
5
|
|
|
use Hyphper\Frame\Exception\InvalidFrameException; |
6
|
|
|
use Hyphper\Frame\Exception\InvalidPaddingException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The PUSH_PROMISE frame is used to notify the peer endpoint in advance of |
10
|
|
|
* streams the sender intends to initiate. |
11
|
|
|
* |
12
|
|
|
* @package Hyphper\Frame |
13
|
|
|
*/ |
14
|
|
|
class PushPromiseFrame extends \Hyphper\Frame implements PaddingInterface |
15
|
|
|
{ |
16
|
|
|
use PaddingTrait; |
17
|
|
|
|
18
|
|
|
protected $defined_flags = [ |
19
|
|
|
Flag::END_HEADERS, |
20
|
|
|
Flag::PADDED |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected $type = 0x05; |
24
|
|
|
protected $stream_association = self::HAS_STREAM; |
25
|
|
|
/** |
26
|
|
|
* @var int The stream ID that is promised by this frame. |
27
|
|
|
*/ |
28
|
|
|
protected $promised_stream_id; |
29
|
|
|
|
30
|
|
|
protected $data; |
31
|
|
|
|
32
|
6 |
|
public function __construct(array $options = []) |
33
|
|
|
{ |
34
|
6 |
|
parent::__construct($options); |
35
|
|
|
|
36
|
6 |
|
$this->promised_stream_id = (int) ($options['promised_stream_id'] ?? null); |
37
|
6 |
|
$this->data = $options['data'] ?? 0; |
38
|
6 |
|
} |
39
|
|
|
|
40
|
2 |
|
public function serializeBody(): string |
41
|
|
|
{ |
42
|
2 |
|
$padding_data = $this->serializePaddingData(); |
43
|
2 |
|
$padding = ''; |
44
|
2 |
|
if ($this->padding_length) { |
45
|
|
|
$padding = str_repeat("\0", $this->padding_length); |
46
|
|
|
} |
47
|
2 |
|
$data = pack('N', $this->promised_stream_id); |
48
|
|
|
|
49
|
2 |
|
return $padding_data . $data . $this->data . $padding; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Given the body of a frame, parses it into frame data. This populates |
54
|
|
|
* the non-header parts of the frame: that is, it does not populate the |
55
|
|
|
* stream ID or flags. |
56
|
|
|
* |
57
|
|
|
* |
58
|
|
|
* @param string $data |
59
|
|
|
* @throws InvalidFrameException |
60
|
|
|
* @throws InvalidPaddingException |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
4 |
|
public function parseBody(string $data) |
64
|
|
|
{ |
65
|
4 |
|
$padding_data_length = $this->parsePaddingData($data); |
66
|
|
|
|
67
|
4 |
|
if (!$unpack = @unpack('Npromised_stream_id', substr($data, $padding_data_length, $padding_data_length + 4))) { |
68
|
1 |
|
throw new InvalidFrameException('Invalid PUSH_PROMISE body'); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
$this->promised_stream_id = $unpack['promised_stream_id']; |
72
|
|
|
|
73
|
3 |
|
$this->data = substr($data, $padding_data_length + 4); |
74
|
3 |
|
$this->body_len = strlen($data); |
75
|
|
|
|
76
|
3 |
|
if ($this->padding_length && $this->padding_length > $this->body_len) { |
77
|
1 |
|
throw new InvalidPaddingException('Padding is too long'); |
78
|
|
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
1 |
|
public function getPromisedStreamId() |
85
|
|
|
{ |
86
|
1 |
|
return $this->promised_stream_id; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $promised_stream_id |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
1 |
|
public function setPromisedStreamId(int $promised_stream_id) |
94
|
|
|
{ |
95
|
1 |
|
$this->promised_stream_id = $promised_stream_id; |
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return int|mixed |
101
|
|
|
*/ |
102
|
2 |
|
public function getData() |
103
|
|
|
{ |
104
|
2 |
|
return $this->data; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $data |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
2 |
|
public function setData(string $data) |
112
|
|
|
{ |
113
|
2 |
|
$this->data = $data; |
114
|
2 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|