This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
transport.js
362 lines (334 loc) · 11.5 KB
/
transport.js
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
define(['exports'], function(exports) {
/**
* This file contains the following classes:
*
* - Pop3Parser: Parses incoming POP3 requests
* - Pop3Protocol: Uses the Pop3Parser to match requests up with responses
* - Request: Encapsulates a request to the server
* - Response: Encapsulates a response from the server
*
* The Pop3Client (in pop3.js) hooks together a socket and an
* instance of Pop3Protocol to form a complete client. See pop3.js
* for a more detailed description of the hierarchy.
*/
var setTimeout = window.setTimeout.bind(window);
var clearTimeout = window.clearTimeout.bind(window);
var MAX_LINE_LENGTH = 512; // per POP3 spec, including CRLF
var CR = '\r'.charCodeAt(0);
var LF = '\n'.charCodeAt(0);
var PERIOD = '.'.charCodeAt(0);
var PLUS = '+'.charCodeAt(0);
var MINUS = '-'.charCodeAt(0);
var SPACE = ' '.charCodeAt(0);
var textEncoder = new TextEncoder('utf-8', { fatal: false });
var textDecoder = new TextDecoder('utf-8', { fatal: false });
function concatBuffers(a, b) {
var buffer = new Uint8Array(a.byteLength + b.byteLength);
buffer.set(a, 0);
buffer.set(b, a.byteLength);
return buffer;
}
/**
* Pop3Parser receives binary data (presumably from a socket) and
* parse it according to the POP3 spec:
*
* var parser = new Pop3Parser();
* parser.push(myBinaryData);
* var rsp = parser.extractResponse(false);
* if (rsp) {
* // do something with the response
* }
*/
function Pop3Parser() {
this.buffer = new Uint8Array(0); // data not yet parsed into lines
this.unprocessedLines = [];
}
/**
* Add new data to be parsed. To actually parse the incoming data
* (to see if there is enough data to extract a full response), call
* `.extractResponse()`.
*
* @param {Uint8Array} data
*/
Pop3Parser.prototype.push = function(data) {
// append the data to be processed
var buffer = this.buffer = concatBuffers(this.buffer, data);
// pull out full lines
for (var i = 0; i < buffer.length - 1; i++) {
if (buffer[i] === CR && buffer[i + 1] === LF) {
var end = i + 1;
if (end > MAX_LINE_LENGTH) {
// Sadly, servers do this, so we can't bail here.
}
this.unprocessedLines.push(buffer.slice(0, end + 1));
buffer = this.buffer = buffer.slice(end + 1);
i = -1;
}
}
}
/**
* Attempt to parse and return a single message from the buffered
* data. Since the POP3 protocol does not provide a foolproof way to
* determine whether a given message is multiline without tracking
* request state, you must specify whether or not the response is
* expected to be multiline.
*
* Multiple responses may be available; you should call
* `.extractResponse()` repeatedly until no more responses are
* available. This method returns null if there was not enough data
* to parse and return a response.
*
* @param {boolean} multiline true to parse a multiline response.
* @return {Response|null}
*/
Pop3Parser.prototype.extractResponse = function(multiline) {
if (!this.unprocessedLines.length) {
return null;
}
if (this.unprocessedLines[0][0] !== PLUS) {
multiline = false; // Negative responses are never multiline.
}
if (!multiline) {
return new Response([this.unprocessedLines.shift()], false);
} else {
var endLineIndex = -1;
for (var i = 1; i < this.unprocessedLines.length; i++) {
var line = this.unprocessedLines[i];
if (line.byteLength === 3 &&
line[0] === PERIOD && line[1] === CR && line[2] === LF) {
endLineIndex = i;
break;
}
}
if (endLineIndex === -1) {
return null;
}
var lines = this.unprocessedLines.splice(0, endLineIndex + 1);
lines.pop(); // remove final ".\r\n" line
// the first line cannot be stuffed (it's the command OK/ERR
// response). Other lines may be period-stuffed.
for (var i = 1; i < endLineIndex; i++) {
if (lines[i][0] === PERIOD) {
lines[i] = lines[i].slice(1);
}
}
return new Response(lines, true);
}
}
/**
* Represent a POP3 response (both success and failure). You should
* not have to instantiate this class directly; Pop3Parser returns
* these objects from `Pop3Parser.extractResponse()`.
*
* @param {UInt8Array[]} lines
* @param {boolean} isMultiline
*/
function Response(lines, isMultiline) {
this.lines = lines; // list of UInt8Arrays
this.isMultiline = isMultiline;
this.ok = (this.lines[0][0] === PLUS);
this.err = !this.ok;
this.request = null;
}
/**
* Return the description text for the status line as a string.
*/
Response.prototype.getStatusLine = function() {
return this.getLineAsString(0).replace(/^(\+OK|-ERR) /, '');
}
/**
* Return the line at `index` as a string.
*
* @param {int} index
* @return {String}
*/
Response.prototype.getLineAsString = function(index) {
return textDecoder.decode(this.lines[index]);
}
/**
* Return an array of strings, one for each line, including CRLFs.
* If you want to parse the data from a response, use
* `.getDataLines()`.
*
* @return {String[]}
*/
Response.prototype.getLinesAsString = function() {
var lines = [];
for (var i = 0; i < this.lines.length; i++) {
lines.push(this.getLineAsString(i));
}
return lines;
}
/**
* Return an array of strings, _excluding_ CRLFs, starting from the
* line after the +OK/-ERR line.
*/
Response.prototype.getDataLines = function() {
var lines = [];
for (var i = 1; i < this.lines.length; i++) {
var line = this.getLineAsString(i);
lines.push(line.slice(0, line.length - 2)); // strip CRLF
}
return lines;
}
/**
* Return the data portion of a multiline response as a string,
* with the lines' CRLFs intact.
*/
Response.prototype.getDataAsString = function() {
var lines = [];
for (var i = 1; i < this.lines.length; i++) {
lines.push(this.getLineAsString(i));
}
return lines.join(''); // lines already have '\r\n'
}
/**
* Return a string representation of the message, primarily for
* debugging purposes.
*/
Response.prototype.toString = function() {
return this.getLinesAsString().join('\r\n');
}
/**
* Represent a POP3 request, with enough data to allow the parser
* to parse out a response and invoke a callback upon receiving a
* response.
*
* @param {string} command The command, like RETR, USER, etc.
* @param {string[]} args Arguments to the command, as an array.
* @param {boolean} expectMultiline Whether or not the response will
* be multiline.
* @param {function(err, rsp)} cb The callback to invoke when a
* response is received.
*/
function Request(command, args, expectMultiline, cb) {
this.command = command;
this.args = args;
this.expectMultiline = expectMultiline;
this.onresponse = cb || null;
}
exports.Request = Request;
/**
* Encode the request into a byte array suitable for transport over
* a socket.
*/
Request.prototype.toByteArray = function() {
return textEncoder.encode(
this.command + (this.args.length ? ' ' + this.args.join(' ') : '') + '\r\n');
}
/**
* Trigger the response callback with '-ERR desc\r\n'.
*/
Request.prototype._respondWithError = function(desc) {
var rsp = new Response([textEncoder.encode(
'-ERR ' + desc + '\r\n')], false);
rsp.request = this;
this.onresponse(rsp, null);
}
/**
* Couple a POP3 parser with a request/response model, such that
* you can easily hook Pop3Protocol up to a socket (or other
* transport) to get proper request/response semantics.
*
* You must attach a handler to `.onsend`, which should fire data
* across the wire. Similarly, you should call `.onreceive(data)` to
* pass data back in from the socket.
*/
function Pop3Protocol() {
this.parser = new Pop3Parser();
this.onsend = function(data) {
throw new Error("You must implement Pop3Protocol.onsend to send data.");
};
this.unsentRequests = []; // if not pipelining, queue requests one at a time
this.pipeline = false;
this.pendingRequests = [];
this.closed = false;
}
exports.Response = Response;
exports.Pop3Protocol = Pop3Protocol;
/**
* Send a request to the server. Upon receiving a response, the
* callback will be invoked, node-style, with an err or a response.
* Negative replies (-ERR) are returned as an error to the callback;
* positive replies (+OK) as a response. Socket errors are returned
* as an error to the callback.
*
* @param {string} cmd The command like USER, RETR, etc.
* @param {string[]} args An array of arguments to the command.
* @param {boolean} expectMultiline Whether or not the response will
* be multiline.
* @param {function(err, rsp)} cb The callback to invoke upon
* receipt of a response.
*/
Pop3Protocol.prototype.sendRequest = function(
cmd, args, expectMultiline, cb) {
var req;
if (cmd instanceof Request) {
req = cmd;
} else {
req = new Request(cmd, args, expectMultiline, cb);
}
if (this.closed) {
req._respondWithError('(request sent after connection closed)');
return;
}
if (this.pipeline || this.pendingRequests.length === 0) {
this.onsend(req.toByteArray());
this.pendingRequests.push(req);
} else {
this.unsentRequests.push(req);
}
}
/**
* Call this function to send received data to the parser. This
* method automatically calls the appropriate response callback for
* its respective request.
*/
Pop3Protocol.prototype.onreceive = function(data) {
this.parser.push(data);
var response;
while (true) {
var req = this.pendingRequests[0];
response = this.parser.extractResponse(req && req.expectMultiline);
if (!response) {
break;
} else if (!req) {
// It's unclear how to handle this in the most nondestructive way;
// if we receive an unsolicited response, something has gone horribly
// wrong, and it's unlikely that we'll be able to recover.
console.error('Unsolicited response from server: ' + response);
break;
}
response.request = req;
this.pendingRequests.shift();
if (this.unsentRequests.length) {
this.sendRequest(this.unsentRequests.shift());
}
if (req.onresponse) {
if (response.err) {
req.onresponse(response, null);
} else {
req.onresponse(null, response);
}
}
}
}
/**
* Call this function when the socket attached to this protocol is
* closed. Any current requests that have been enqueued but not yet
* responded to will be sent a dummy "-ERR" response, indicating
* that the underlying connection closed without actually
* responding. This avoids the case where we hang if we never
* receive a response from the server.
*/
Pop3Protocol.prototype.onclose = function() {
this.closed = true;
var requestsToRespond = this.pendingRequests.concat(this.unsentRequests);
this.pendingRequests = [];
this.unsentRequests = [];
for (var i = 0; i < requestsToRespond.length; i++) {
var req = requestsToRespond[i];
req._respondWithError('(connection closed, no response)');
}
}
});