forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffusionMercurialWireClientSSHProtocolChannel.php
217 lines (179 loc) · 5.68 KB
/
DiffusionMercurialWireClientSSHProtocolChannel.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
<?php
final class DiffusionMercurialWireClientSSHProtocolChannel
extends PhutilProtocolChannel {
private $buffer = '';
private $state = 'command';
private $expectArgumentCount;
private $argumentName;
private $expectBytes;
private $command;
private $arguments;
private $raw;
protected function encodeMessage($message) {
return $message;
}
private function initializeState($last_command = null) {
if ($last_command == 'unbundle') {
$this->command = '<raw-data>';
$this->state = 'data-length';
} else {
$this->state = 'command';
}
$this->expectArgumentCount = null;
$this->expectBytes = null;
$this->command = null;
$this->argumentName = null;
$this->arguments = array();
$this->raw = '';
}
private function readProtocolLine() {
$pos = strpos($this->buffer, "\n");
if ($pos === false) {
return null;
}
$line = substr($this->buffer, 0, $pos);
$this->raw .= $line."\n";
$this->buffer = substr($this->buffer, $pos + 1);
return $line;
}
private function readProtocolBytes() {
if (strlen($this->buffer) < $this->expectBytes) {
return null;
}
$bytes = substr($this->buffer, 0, $this->expectBytes);
$this->raw .= $bytes;
$this->buffer = substr($this->buffer, $this->expectBytes);
return $bytes;
}
private function newMessageAndResetState() {
$message = array(
'command' => $this->command,
'arguments' => $this->arguments,
'raw' => $this->raw,
);
$this->initializeState($this->command);
return $message;
}
private function newDataMessage($bytes) {
$message = array(
'command' => '<raw-data>',
'raw' => strlen($bytes)."\n".$bytes,
);
return $message;
}
protected function decodeStream($data) {
$this->buffer .= $data;
$out = array();
$messages = array();
while (true) {
if ($this->state == 'command') {
$this->initializeState();
// We're reading a command. It looks like:
//
// <command>
$line = $this->readProtocolLine();
if ($line === null) {
break;
}
$this->command = $line;
$this->state = 'arguments';
} else if ($this->state == 'arguments') {
// Check if we're still waiting for arguments.
$args = DiffusionMercurialWireProtocol::getCommandArgs($this->command);
$have = array_select_keys($this->arguments, $args);
if (count($have) == count($args)) {
// We have all the arguments. Emit a message and read the next
// command.
$messages[] = $this->newMessageAndResetState();
} else {
// We're still reading arguments. They can either look like:
//
// <name> <length(value)>
// <value>
// ...
//
// ...or like this:
//
// * <count>
// <name1> <length(value1)>
// <value1>
// ...
$line = $this->readProtocolLine();
if ($line === null) {
break;
}
list($arg, $size) = explode(' ', $line, 2);
$size = (int)$size;
if ($arg != '*') {
$this->expectBytes = $size;
$this->argumentName = $arg;
$this->state = 'value';
} else {
$this->arguments['*'] = array();
$this->expectArgumentCount = $size;
$this->state = 'argv';
}
}
} else if ($this->state == 'value' || $this->state == 'argv-value') {
// We're reading the value of an argument. We just need to wait for
// the right number of bytes to show up.
$bytes = $this->readProtocolBytes();
if ($bytes === null) {
break;
}
if ($this->state == 'argv-value') {
$this->arguments['*'][$this->argumentName] = $bytes;
$this->state = 'argv';
} else {
$this->arguments[$this->argumentName] = $bytes;
$this->state = 'arguments';
}
} else if ($this->state == 'argv') {
// We're reading a variable number of arguments. We need to wait for
// the arguments to arrive.
if ($this->expectArgumentCount) {
$line = $this->readProtocolLine();
if ($line === null) {
break;
}
list($arg, $size) = explode(' ', $line, 2);
$size = (int)$size;
$this->expectBytes = $size;
$this->argumentName = $arg;
$this->state = 'argv-value';
$this->expectArgumentCount--;
} else {
$this->state = 'arguments';
}
} else if ($this->state == 'data-length') {
$line = $this->readProtocolLine();
if ($line === null) {
break;
}
$this->expectBytes = (int)$line;
if (!$this->expectBytes) {
$messages[] = $this->newDataMessage('');
$this->initializeState();
} else {
$this->state = 'data-bytes';
}
} else if ($this->state == 'data-bytes') {
$bytes = substr($this->buffer, 0, $this->expectBytes);
$this->buffer = substr($this->buffer, strlen($bytes));
$this->expectBytes -= strlen($bytes);
$messages[] = $this->newDataMessage($bytes);
if (!$this->expectBytes) {
// We've finished reading this chunk, so go read the next chunk.
$this->state = 'data-length';
} else {
// We're waiting for more data, and have read everything available
// to us so far.
break;
}
} else {
throw new Exception(pht("Bad parser state '%s'!", $this->state));
}
}
return $messages;
}
}