forked from stomp-php/stomp-php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Frame.php
253 lines (224 loc) · 4.62 KB
/
Frame.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/*
* This file is part of the Stomp package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Stomp\Transport;
use ArrayAccess;
/**
* Stomp Frames are messages that are sent and received on a stomp connection.
*
* @package Stomp
*/
class Frame implements ArrayAccess
{
/**
* Stomp Command
*
* @var string
*/
protected $command;
/**
* Frame Headers
*
* @var array
*/
protected $headers;
/**
* Frame Content
*
* @var mixed
*/
public $body;
/**
* Frame should set an content-length header on transmission
*
* @var bool
*/
private $addLengthHeader = false;
/**
* Frame is in stomp 1.0 mode
*
* @var bool
*/
private $legacyMode = false;
/**
* Constructor
*
* @param string $command
* @param array $headers
* @param string $body
*/
public function __construct($command = null, array $headers = [], $body = null)
{
$this->command = $command;
$this->headers = $headers ?: [];
$this->body = $body;
}
/**
* Add given headers to currently set headers.
*
* Will override existing keys.
*
* @param array $header
* @return Frame
*/
public function addHeaders(array $header)
{
$this->headers += $header;
return $this;
}
/**
* Stomp message Id
*
* @return string
*/
public function getMessageId()
{
return $this['message-id'];
}
/**
* Is error frame.
*
* @return boolean
*/
public function isErrorFrame()
{
return ($this->command == 'ERROR');
}
/**
* Tell the frame that we expect an length header.
*
* @param bool|false $expected
*/
public function expectLengthHeader($expected = false)
{
$this->addLengthHeader = $expected;
}
/**
* Enable legacy mode for this frame
*
* @param bool|false $legacy
*/
public function legacyMode($legacy = false)
{
$this->legacyMode = $legacy;
}
/**
* Frame is in legacy mode.
*
* @return bool
*/
public function isLegacyMode()
{
return $this->legacyMode;
}
/**
* Command
*
* @return string
*/
public function getCommand()
{
return $this->command;
}
/**
* Body
*
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* Headers
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Convert frame to transportable string
*
* @return string
*/
public function __toString()
{
$data = $this->command . "\n";
if (!$this->legacyMode) {
if ($this->body && ($this->addLengthHeader || stripos($this->body, "\x00") !== false)) {
$this['content-length'] = $this->getBodySize();
}
}
foreach ($this->headers as $name => $value) {
$data .= $this->encodeHeaderValue($name) . ':' . $this->encodeHeaderValue($value) . "\n";
}
$data .= "\n";
$data .= $this->body;
return $data . "\x00";
}
/**
* Size of Frame body.
*
* @return int
*/
protected function getBodySize()
{
return strlen($this->body);
}
/**
* Encodes header values.
*
* @param string $value
* @return string
*/
protected function encodeHeaderValue($value)
{
if ($this->legacyMode) {
return str_replace(["\n"], ['\n'], $value);
}
return str_replace(["\\", "\r", "\n", ':'], ["\\\\", '\r', '\n', '\c'], $value);
}
/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->headers[$offset]);
}
/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
if (isset($this->headers[$offset])) {
return $this->headers[$offset];
}
return null;
}
/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if ($value !== null) {
$this->headers[$offset] = $value;
}
}
/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->headers[$offset]);
}
}