diff options
author | Andrei Belov <defan@nginx.com> | 2019-08-22 21:33:54 +0300 |
---|---|---|
committer | Andrei Belov <defan@nginx.com> | 2019-08-22 21:33:54 +0300 |
commit | a07c4d30a64f781f93730576b5dced32422a9935 (patch) | |
tree | 06ebfaa66845a057b8069014c5379b2dcfc80861 /src/nodejs/unit-http/http_server.js | |
parent | 8a579acddeae0c0106e15d82aa7220ac01deba84 (diff) | |
parent | c47af243b0e805376c4ec908f21e07dc811b33f0 (diff) | |
download | unit-a07c4d30a64f781f93730576b5dced32422a9935.tar.gz unit-a07c4d30a64f781f93730576b5dced32422a9935.tar.bz2 |
Merged with the default branch.1.10.0-1
Diffstat (limited to '')
-rw-r--r--[-rwxr-xr-x] | src/nodejs/unit-http/http_server.js | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js index ae8e204a..c42149a5 100755..100644 --- a/src/nodejs/unit-http/http_server.js +++ b/src/nodejs/unit-http/http_server.js @@ -8,16 +8,21 @@ const EventEmitter = require('events'); const http = require('http'); const util = require('util'); -const unit_lib = require('unit-http/build/Release/unit-http.node'); -const unit_socket = require('unit-http/socket'); - -const { Socket } = unit_socket; +const unit_lib = require('./build/Release/unit-http'); +const Socket = require('./socket'); +const WebSocketFrame = require('./websocket_frame'); 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; } util.inherits(ServerResponse, EventEmitter); @@ -195,6 +200,8 @@ function writeHead(statusCode, reason, obj) { } } } + + return this; }; /* @@ -205,15 +212,23 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() { this.writeHead(this.statusCode); }; -ServerResponse.prototype._writeBody = function(chunk, encoding, callback) { - var contentLength = 0; +ServerResponse.prototype._send_headers = unit_lib.response_send_headers; +ServerResponse.prototype._sendHeaders = function _sendHeaders() { if (!this.headersSent) { - unit_lib.unit_response_headers(this, this.statusCode, this.headers, - this.headers_count, this.headers_len); + 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; + + this._sendHeaders(); if (typeof chunk === 'function') { callback = chunk; @@ -236,7 +251,7 @@ ServerResponse.prototype._writeBody = function(chunk, encoding, callback) { contentLength = chunk.length; } - unit_lib.unit_response_write(this, chunk, contentLength); + this._write(chunk, contentLength); } if (typeof callback === 'function') { @@ -266,11 +281,13 @@ ServerResponse.prototype.write = function write(chunk, encoding, callback) { return true; }; +ServerResponse.prototype._end = unit_lib.response_end; + ServerResponse.prototype.end = function end(chunk, encoding, callback) { if (!this.finished) { this._writeBody(chunk, encoding, callback); - unit_lib.unit_response_end(this); + this._end(); this.finished = true; } @@ -278,10 +295,12 @@ ServerResponse.prototype.end = function end(chunk, encoding, callback) { return this; }; -function ServerRequest(server) { +function ServerRequest(server, socket) { EventEmitter.call(this); this.server = server; + this.socket = socket; + this.connection = socket; } util.inherits(ServerRequest, EventEmitter); @@ -337,8 +356,8 @@ ServerRequest.prototype.on = function on(ev, fn) { if (ev === "data") { process.nextTick(function () { - if (this.server.buffer.length !== 0) { - this.emit("data", this.server.buffer); + if (this._data.length !== 0) { + this.emit("data", this._data); } }.bind(this)); @@ -355,14 +374,27 @@ function Server(requestListener) { this.unit.createServer(); - this.socket = Socket; - this.request = ServerRequest; - this.response = ServerResponse; + 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--; + } + }); } + util.inherits(Server, EventEmitter); Server.prototype.setTimeout = function setTimeout(msecs, callback) { @@ -379,15 +411,13 @@ Server.prototype.listen = function () { this.unit.listen(); }; -Server.prototype.emit_events = function (server, req, res) { - req.server = server; - res.server = server; - req.res = res; - res.req = req; - - server.buffer = server.unit._read(req.socket.req_pointer); +Server.prototype.emit_request = function (req, res) { + if (req._websocket_handshake && this._upgradeListenerCount > 0) { + this.emit('upgrade', req, req.socket); - server.emit("request", req, res); + } else { + this.emit("request", req, res); + } process.nextTick(() => { req.emit("finish"); |