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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
/*
* Copyright (C) NGINX, Inc.
*/
'use strict';
const EventEmitter = require('events');
const http = require('http');
const util = require('util');
const unit_lib = require('./build/Release/unit-http');
const Socket = require('./socket');
const WebSocketFrame = require('./websocket_frame');
const Readable = require('stream').Readable;
function ServerResponse(req) {
EventEmitter.call(this);
this.headers = {};
this.server = req.server;
this._request = req;
req._response = this;
this.socket = req.socket;
this.connection = req.connection;
this.writable = true;
}
util.inherits(ServerResponse, EventEmitter);
ServerResponse.prototype.statusCode = 200;
ServerResponse.prototype.statusMessage = undefined;
ServerResponse.prototype.headers_len = 0;
ServerResponse.prototype.headers_count = 0;
ServerResponse.prototype.headersSent = false;
ServerResponse.prototype.destroyed = false;
ServerResponse.prototype.finished = false;
ServerResponse.prototype.destroy = function destroy(error) {
if (!this.destroyed) {
this.destroyed = true;
}
return this;
};
ServerResponse.prototype._finish = function _finish() {
this.headers = {};
this.headers_len = 0;
this.headers_count = 0;
this.finished = true;
};
ServerResponse.prototype.assignSocket = function assignSocket(socket) {
};
ServerResponse.prototype.detachSocket = function detachSocket(socket) {
};
ServerResponse.prototype.writeContinue = function writeContinue(cb) {
};
ServerResponse.prototype.writeProcessing = function writeProcessing(cb) {
};
ServerResponse.prototype.setHeader = function setHeader(name, value) {
if (typeof name !== 'string') {
throw new TypeError('Name argument must be a string');
}
let value_len = 0
let count = 0;
if (Array.isArray(value)) {
count = value.length;
value.forEach(function(val) {
value_len += Buffer.byteLength(val + "", 'latin1');
});
} else {
count = 1;
value_len = Buffer.byteLength(value + "", 'latin1');
}
let lc_name = name.toLowerCase();
if (lc_name in this.headers) {
this._removeHeader(lc_name);
}
let name_len = Buffer.byteLength(name, 'latin1');
this.headers[lc_name] = [name, value];
this.headers_len += value_len + (name_len * count);
this.headers_count += count;
};
ServerResponse.prototype.getHeader = function getHeader(name) {
const entry = this.headers[name.toLowerCase()];
return entry && entry[1];
};
ServerResponse.prototype.getHeaderNames = function getHeaderNames() {
return Object.keys(this.headers);
};
ServerResponse.prototype.getHeaders = function getHeaders() {
const ret = Object.create(null);
if (this.headers) {
const keys = Object.keys(this.headers);
for (var i = 0; i < keys.length; i++) {
const key = keys[i];
ret[key] = this.headers[key][1];
}
}
return ret;
};
ServerResponse.prototype.hasHeader = function hasHeader(name) {
return name.toLowerCase() in this.headers;
};
ServerResponse.prototype.removeHeader = function removeHeader(name) {
if (typeof name !== 'string') {
throw new TypeError('Name argument must be a string');
}
let lc_name = name.toLowerCase();
if (lc_name in this.headers) {
this._removeHeader(lc_name);
}
};
ServerResponse.prototype._removeHeader = function _removeHeader(lc_name) {
let entry = this.headers[lc_name];
let name_len = Buffer.byteLength(entry[0] + "", 'latin1');
let value = entry[1];
delete this.headers[lc_name];
if (Array.isArray(value)) {
this.headers_count -= value.length;
this.headers_len -= value.length * name_len;
value.forEach(function(val) {
this.headers_len -= Buffer.byteLength(val + "", 'latin1');
});
return;
}
this.headers_count--;
this.headers_len -= name_len + Buffer.byteLength(value + "", 'latin1');
};
ServerResponse.prototype.sendDate = function sendDate() {
throw new Error("Not supported");
};
ServerResponse.prototype.setTimeout = function setTimeout(msecs, callback) {
this.timeout = msecs;
if (callback) {
this.on('timeout', callback);
}
return this;
};
ServerResponse.prototype.writeHead = writeHead;
ServerResponse.prototype.writeHeader = ServerResponse.prototype.writeHead;
function writeHead(statusCode, reason, obj) {
var originalStatusCode = statusCode;
statusCode |= 0;
if (statusCode < 100 || statusCode > 999) {
throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
}
if (typeof reason === 'string') {
this.statusMessage = reason;
} else {
if (!this.statusMessage) {
this.statusMessage = http.STATUS_CODES[statusCode] || 'unknown';
}
obj = reason;
}
this.statusCode = statusCode;
if (obj) {
var k;
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
k = keys[i];
if (k) {
this.setHeader(k, obj[k]);
}
}
}
return this;
};
/*
* Some Node.js packages are known to be using this undocumented function,
* notably "compression" middleware.
*/
ServerResponse.prototype._implicitHeader = function _implicitHeader() {
this.writeHead(this.statusCode);
};
ServerResponse.prototype._send_headers = unit_lib.response_send_headers;
ServerResponse.prototype._sendHeaders = function _sendHeaders() {
if (!this.headersSent) {
this._send_headers(this.statusCode, this.headers, this.headers_count,
this.headers_len);
this.headersSent = true;
}
};
ServerResponse.prototype._write = unit_lib.response_write;
ServerResponse.prototype._writeBody = function(chunk, encoding, callback) {
var contentLength = 0;
var res, o;
this._sendHeaders();
if (typeof chunk === 'function') {
callback = chunk;
chunk = null;
} else if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
}
if (chunk) {
if (typeof chunk !== 'string' && !(chunk instanceof Buffer ||
chunk instanceof Uint8Array)) {
throw new TypeError(
'First argument must be a string, Buffer, ' +
'or Uint8Array');
}
if (typeof chunk === 'string') {
contentLength = Buffer.byteLength(chunk, encoding);
if (contentLength > unit_lib.buf_min) {
chunk = Buffer.from(chunk, encoding);
contentLength = chunk.length;
}
} else {
contentLength = chunk.length;
}
if (this.server._output.length > 0 || !this.socket.writable) {
o = new BufferedOutput(this, 0, chunk, encoding, callback);
this.server._output.push(o);
return false;
}
res = this._write(chunk, 0, contentLength);
if (res < contentLength) {
this.socket.writable = false;
this.writable = false;
o = new BufferedOutput(this, res, chunk, encoding, callback);
this.server._output.push(o);
return false;
}
}
if (typeof callback === 'function') {
/*
* The callback must be called only when response.write() caller
* completes. process.nextTick() postpones the callback execution.
*
* process.nextTick() is not technically part of the event loop.
* Instead, the nextTickQueue will be processed after the current
* operation completes, regardless of the current phase of
* the event loop. All callbacks passed to process.nextTick()
* will be resolved before the event loop continues.
*/
process.nextTick(callback);
}
return true;
};
ServerResponse.prototype.write = function write(chunk, encoding, callback) {
if (this.finished) {
if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
}
var err = new Error("Write after end");
process.nextTick(() => {
this.emit('error', err);
if (typeof callback === 'function') {
callback(err);
}
})
}
return this._writeBody(chunk, encoding, callback);
};
ServerResponse.prototype._end = unit_lib.response_end;
ServerResponse.prototype.end = function end(chunk, encoding, callback) {
if (!this.finished) {
if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
}
this._writeBody(chunk, encoding, () => {
this._end();
if (typeof callback === 'function') {
callback();
}
this.emit("finish");
});
this.finished = true;
}
return this;
};
function ServerRequest(server, socket) {
Readable.call(this);
this.server = server;
this.socket = socket;
this.connection = socket;
this._pushed_eofchunk = false;
}
util.inherits(ServerRequest, Readable);
ServerRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
this.timeout = msecs;
if (callback) {
this.on('timeout', callback);
}
return this;
};
ServerRequest.prototype.statusCode = function statusCode() {
/* Only valid for response obtained from http.ClientRequest. */
};
ServerRequest.prototype.statusMessage = function statusMessage() {
/* Only valid for response obtained from http.ClientRequest. */
};
ServerRequest.prototype.trailers = function trailers() {
throw new Error("Not supported");
};
ServerRequest.prototype.METHODS = function METHODS() {
return http.METHODS;
};
ServerRequest.prototype.STATUS_CODES = function STATUS_CODES() {
return http.STATUS_CODES;
};
ServerRequest.prototype._request_read = unit_lib.request_read;
ServerRequest.prototype._read = function _read(n) {
const b = this._request_read(n);
if (b != null) {
this.push(b);
}
if (!this._pushed_eofchunk && (b == null || b.length < n)) {
this._pushed_eofchunk = true;
this.push(null);
}
};
function Server(requestListener) {
EventEmitter.call(this);
this.unit = new unit_lib.Unit();
this.unit.server = this;
this.unit.createServer();
this.Socket = Socket;
this.ServerRequest = ServerRequest;
this.ServerResponse = ServerResponse;
this.WebSocketFrame = WebSocketFrame;
if (requestListener) {
this.on('request', requestListener);
}
this._upgradeListenerCount = 0;
this.on('newListener', function(ev) {
if (ev === 'upgrade'){
this._upgradeListenerCount++;
}
}).on('removeListener', function(ev) {
if (ev === 'upgrade') {
this._upgradeListenerCount--;
}
});
this._output = [];
this._drain_resp = new Set();
}
util.inherits(Server, EventEmitter);
Server.prototype.setTimeout = function setTimeout(msecs, callback) {
this.timeout = msecs;
if (callback) {
this.on('timeout', callback);
}
return this;
};
Server.prototype.listen = function (...args) {
this.unit.listen();
if (typeof args[args.length - 1] === 'function') {
this.once('listening', args[args.length - 1]);
}
/*
* Some express.js apps use the returned server object inside the listening
* callback, so we timeout the listening event to occur after this function
* returns.
*/
setImmediate(function() {
this.emit('listening')
}.bind(this))
return this;
};
Server.prototype.address = function () {
return {
family: "IPv4",
address: "127.0.0.1",
port: 80
}
}
Server.prototype.emit_request = function (req, res) {
if (req._websocket_handshake && this._upgradeListenerCount > 0) {
this.emit('upgrade', req, req.socket);
} else {
this.emit("request", req, res);
}
};
Server.prototype.emit_close = function () {
this.emit('close');
};
Server.prototype.emit_drain = function () {
var res, o, l;
if (this._output.length <= 0) {
return;
}
while (this._output.length > 0) {
o = this._output[0];
if (typeof o.chunk === 'string') {
l = Buffer.byteLength(o.chunk, o.encoding);
} else {
l = o.chunk.length;
}
res = o.resp._write(o.chunk, o.offset, l);
o.offset += res;
if (o.offset < l) {
return;
}
this._drain_resp.add(o.resp);
if (typeof o.callback === 'function') {
process.nextTick(o.callback);
}
this._output.shift();
}
for (var resp of this._drain_resp) {
if (resp.socket.writable) {
continue;
}
resp.socket.writable = true;
resp.writable = true;
process.nextTick(() => {
resp.emit("drain");
});
}
this._drain_resp.clear();
};
function BufferedOutput(resp, offset, chunk, encoding, callback) {
this.resp = resp;
this.offset = offset;
this.chunk = chunk;
this.encoding = encoding;
this.callback = callback;
}
function connectionListener(socket) {
}
module.exports = {
Server,
ServerResponse,
ServerRequest,
_connectionListener: connectionListener
};
|