123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Polling = void 0;
- const transport_1 = require("../transport");
- const zlib_1 = require("zlib");
- const accepts = require("accepts");
- const debug_1 = require("debug");
- const debug = (0, debug_1.default)("engine:polling");
- const compressionMethods = {
- gzip: zlib_1.createGzip,
- deflate: zlib_1.createDeflate
- };
- class Polling extends transport_1.Transport {
- /**
- * HTTP polling constructor.
- *
- * @api public.
- */
- constructor(req) {
- super(req);
- this.closeTimeout = 30 * 1000;
- }
- /**
- * Transport name
- *
- * @api public
- */
- get name() {
- return "polling";
- }
- get supportsFraming() {
- return false;
- }
- /**
- * Overrides onRequest.
- *
- * @param {http.IncomingMessage}
- * @api private
- */
- onRequest(req) {
- const res = req.res;
- if ("GET" === req.method) {
- this.onPollRequest(req, res);
- }
- else if ("POST" === req.method) {
- this.onDataRequest(req, res);
- }
- else {
- res.writeHead(500);
- res.end();
- }
- }
- /**
- * The client sends a request awaiting for us to send data.
- *
- * @api private
- */
- onPollRequest(req, res) {
- if (this.req) {
- debug("request overlap");
- // assert: this.res, '.req and .res should be (un)set together'
- this.onError("overlap from client");
- res.writeHead(500);
- res.end();
- return;
- }
- debug("setting request");
- this.req = req;
- this.res = res;
- const onClose = () => {
- this.onError("poll connection closed prematurely");
- };
- const cleanup = () => {
- req.removeListener("close", onClose);
- this.req = this.res = null;
- };
- req.cleanup = cleanup;
- req.on("close", onClose);
- this.writable = true;
- this.emit("drain");
- // if we're still writable but had a pending close, trigger an empty send
- if (this.writable && this.shouldClose) {
- debug("triggering empty send to append close packet");
- this.send([{ type: "noop" }]);
- }
- }
- /**
- * The client sends a request with data.
- *
- * @api private
- */
- onDataRequest(req, res) {
- if (this.dataReq) {
- // assert: this.dataRes, '.dataReq and .dataRes should be (un)set together'
- this.onError("data request overlap from client");
- res.writeHead(500);
- res.end();
- return;
- }
- const isBinary = "application/octet-stream" === req.headers["content-type"];
- if (isBinary && this.protocol === 4) {
- return this.onError("invalid content");
- }
- this.dataReq = req;
- this.dataRes = res;
- let chunks = isBinary ? Buffer.concat([]) : "";
- const cleanup = () => {
- req.removeListener("data", onData);
- req.removeListener("end", onEnd);
- req.removeListener("close", onClose);
- this.dataReq = this.dataRes = chunks = null;
- };
- const onClose = () => {
- cleanup();
- this.onError("data request connection closed prematurely");
- };
- const onData = data => {
- let contentLength;
- if (isBinary) {
- chunks = Buffer.concat([chunks, data]);
- contentLength = chunks.length;
- }
- else {
- chunks += data;
- contentLength = Buffer.byteLength(chunks);
- }
- if (contentLength > this.maxHttpBufferSize) {
- res.writeHead(413).end();
- cleanup();
- }
- };
- const onEnd = () => {
- this.onData(chunks);
- const headers = {
- // text/html is required instead of text/plain to avoid an
- // unwanted download dialog on certain user-agents (GH-43)
- "Content-Type": "text/html",
- "Content-Length": 2
- };
- res.writeHead(200, this.headers(req, headers));
- res.end("ok");
- cleanup();
- };
- req.on("close", onClose);
- if (!isBinary)
- req.setEncoding("utf8");
- req.on("data", onData);
- req.on("end", onEnd);
- }
- /**
- * Processes the incoming data payload.
- *
- * @param {String} encoded payload
- * @api private
- */
- onData(data) {
- debug('received "%s"', data);
- const callback = packet => {
- if ("close" === packet.type) {
- debug("got xhr close packet");
- this.onClose();
- return false;
- }
- this.onPacket(packet);
- };
- if (this.protocol === 3) {
- this.parser.decodePayload(data, callback);
- }
- else {
- this.parser.decodePayload(data).forEach(callback);
- }
- }
- /**
- * Overrides onClose.
- *
- * @api private
- */
- onClose() {
- if (this.writable) {
- // close pending poll request
- this.send([{ type: "noop" }]);
- }
- super.onClose();
- }
- /**
- * Writes a packet payload.
- *
- * @param {Object} packet
- * @api private
- */
- send(packets) {
- this.writable = false;
- if (this.shouldClose) {
- debug("appending close packet to payload");
- packets.push({ type: "close" });
- this.shouldClose();
- this.shouldClose = null;
- }
- const doWrite = data => {
- const compress = packets.some(packet => {
- return packet.options && packet.options.compress;
- });
- this.write(data, { compress });
- };
- if (this.protocol === 3) {
- this.parser.encodePayload(packets, this.supportsBinary, doWrite);
- }
- else {
- this.parser.encodePayload(packets, doWrite);
- }
- }
- /**
- * Writes data as response to poll request.
- *
- * @param {String} data
- * @param {Object} options
- * @api private
- */
- write(data, options) {
- debug('writing "%s"', data);
- this.doWrite(data, options, () => {
- this.req.cleanup();
- });
- }
- /**
- * Performs the write.
- *
- * @api private
- */
- doWrite(data, options, callback) {
- // explicit UTF-8 is required for pages not served under utf
- const isString = typeof data === "string";
- const contentType = isString
- ? "text/plain; charset=UTF-8"
- : "application/octet-stream";
- const headers = {
- "Content-Type": contentType
- };
- const respond = data => {
- headers["Content-Length"] =
- "string" === typeof data ? Buffer.byteLength(data) : data.length;
- this.res.writeHead(200, this.headers(this.req, headers));
- this.res.end(data);
- callback();
- };
- if (!this.httpCompression || !options.compress) {
- respond(data);
- return;
- }
- const len = isString ? Buffer.byteLength(data) : data.length;
- if (len < this.httpCompression.threshold) {
- respond(data);
- return;
- }
- const encoding = accepts(this.req).encodings(["gzip", "deflate"]);
- if (!encoding) {
- respond(data);
- return;
- }
- this.compress(data, encoding, (err, data) => {
- if (err) {
- this.res.writeHead(500);
- this.res.end();
- callback(err);
- return;
- }
- headers["Content-Encoding"] = encoding;
- respond(data);
- });
- }
- /**
- * Compresses data.
- *
- * @api private
- */
- compress(data, encoding, callback) {
- debug("compressing");
- const buffers = [];
- let nread = 0;
- compressionMethods[encoding](this.httpCompression)
- .on("error", callback)
- .on("data", function (chunk) {
- buffers.push(chunk);
- nread += chunk.length;
- })
- .on("end", function () {
- callback(null, Buffer.concat(buffers, nread));
- })
- .end(data);
- }
- /**
- * Closes the transport.
- *
- * @api private
- */
- doClose(fn) {
- debug("closing");
- let closeTimeoutTimer;
- if (this.dataReq) {
- debug("aborting ongoing data request");
- this.dataReq.destroy();
- }
- const onClose = () => {
- clearTimeout(closeTimeoutTimer);
- fn();
- this.onClose();
- };
- if (this.writable) {
- debug("transport writable - closing right away");
- this.send([{ type: "close" }]);
- onClose();
- }
- else if (this.discarded) {
- debug("transport discarded - closing right away");
- onClose();
- }
- else {
- debug("transport not writable - buffering orderly close");
- this.shouldClose = onClose;
- closeTimeoutTimer = setTimeout(onClose, this.closeTimeout);
- }
- }
- /**
- * Returns headers for a response.
- *
- * @param {http.IncomingMessage} request
- * @param {Object} extra headers
- * @api private
- */
- headers(req, headers) {
- headers = headers || {};
- // prevent XSS warnings on IE
- // https://github.com/LearnBoost/socket.io/pull/1333
- const ua = req.headers["user-agent"];
- if (ua && (~ua.indexOf(";MSIE") || ~ua.indexOf("Trident/"))) {
- headers["X-XSS-Protection"] = "0";
- }
- this.emit("headers", headers, req);
- return headers;
- }
- }
- exports.Polling = Polling;
|