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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 | 1× 18× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 12× 9× 9× 1× 1× 1× 1× 1× 1× 9× 9× 9× 9× 9× 9× 30× 30× 28× 28× 1× 1× 1× 9× 28× 9× 1× 9× 9× 9× 9× 30× 9× 9× 9× 9× 9× 9× 9× 9× 9× 30× 30× 30× 29× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 30× 27× 30× 30× 120× 30× 30× 30× 30× 30× 9× 9× 30× 30× 30× 30× 30× 30× 30× 30× 2× 30× 30× 30× 30× 30× 30× 1× 1× 29× 29× 1× 29× 29× 29× 29× 4× 4× 12× 4× 4× 4× 4× 4× 4× 4× 4× 4× 25× 25× 25× 37× 37× 37× 15× 28× 28× 28× 28× 28× 28× 28× 28× 15× 14× 9× 5× 5× 5× 28× 28× 15× 28× 28× 28× 28× 28× 28× 28× 150× 28× 122× 29× 29× 52× 29× 30× 30× 30× 30× 187× 187× 187× 183× 183× 183× 30× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _qs = require('qs'); var _qs2 = _interopRequireDefault(_qs); var _url = require('url'); var _url2 = _interopRequireDefault(_url); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _zlib = require('zlib'); var _zlib2 = _interopRequireDefault(_zlib); var _etag = require('etag'); var _etag2 = _interopRequireDefault(_etag); var _mime = require('mime'); var _mime2 = _interopRequireDefault(_mime); var _nodeUuid = require('node-uuid'); var _nodeUuid2 = _interopRequireDefault(_nodeUuid); var _utils = require('../utils'); var _utils2 = _interopRequireDefault(_utils); var _formidable = require('formidable'); var _formidable2 = _interopRequireDefault(_formidable); var _genericServer = require('../genericServer'); var _genericServer2 = _interopRequireDefault(_genericServer); var _browser_fingerprint = require('browser_fingerprint'); var _browser_fingerprint2 = _interopRequireDefault(_browser_fingerprint); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { Iif (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // server type var type = 'web'; // server attributes var attributes = { canChat: false, logConnections: false, logExits: false, sendWelcomeMessage: false, verbs: [ // no verbs for connections of this type, as they are to very short-lived ] }; /** * This implements the HTTP web server. */ var Web = function (_GenericServer) { _inherits(Web, _GenericServer); /** * Constructor. * * @param api api instance. * @param options map with the server options. */ function Web(api, options) { _classCallCheck(this, Web); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Web).call(this, api, type, options, attributes)); // call the super constructor _this.server = null; var self = _this; Iif (['api', 'file'].indexOf(self.api.config.servers.web.rootEndpointType) < 0) { throw new Error('api.config.servers.web.rootEndpointType can only be \'api\' or \'file\'.'); } //////////////////// [EVENTS] self.on('connection', function (connection) { self._determineRequestParams(connection, function (requestMode) { switch (requestMode) { case 'api': self.processAction(connection); break; case 'file': self.processFile(connection); break; case 'options': self._respondToOptions(connection); break; case 'client-lib': self.processClientLib(connection); break; case 'trace': self._respondToTrace(connection); } }); }); // event to be executed after the action completion self.on('actionComplete', function (data) { self._completeResponse(data); }); return _this; } //////////////////// [REQUIRED METHODS] /** * Start the server instance. * * @param next Callback function. */ /** * Http server instance. */ _createClass(Web, [{ key: 'start', value: function start(next) { var self = this; // check if id to create a HTTP or a HTTPS server Eif (self.options.secure === false) { var http = require('http'); self.server = http.createServer(function (req, res) { self._handleRequest(req, res); }); } else { var https = require('https'); self.server = https.createServer(self.api.config.servers.web.serverOptions, function (req, res) { self._handleRequest(req, res); }); } var bootAttempts = 0; self.server.on('error', function (e) { bootAttempts++; if (bootAttempts < self.api.config.servers.web.bootAttempts) { self.log('cannot boot web server; trying again [' + String(e) + ']', 'error'); if (bootAttempts === 1) { self._cleanSocket(self.options.bindIP, self.options.port); } setTimeout(function () { self.log('attempting to boot again...'); self.server.listen(self.options.port, self.options.bindIP); }, 1000); } else { return next(new Error('Cannot start web server @ ' + self.options.bindIP + ':' + self.options.port + ' => ' + e.message)); } }); self.server.listen(self.options.port, self.options.bindIP, function () { self.chmodSocket(self.options.bindIP, self.options.port); next(); }); } /** * Stop server. * * @param next Callback function. */ }, { key: 'stop', value: function stop(next) { var self = this; // close the server socket self.server.close(); // execute the callback function process.nextTick(function () { next(); }); } /** * Send a message to the client. * * @param connection Connection object where the message must be sent. * @param message Message to be sent. */ }, { key: 'sendMessage', value: function sendMessage(connection, message) { var self = this; // response string var stringResponse = ''; // if the connection is as 'HEAD' HTTP method we need to // ensure the message is a string if (connection.rawConnection.method !== 'HEAD') { stringResponse = String(message); } // clean HTTP headers self._cleanHeaders(connection); // get the response headers var headers = connection.rawConnection.responseHeaders; // get the response status code var responseHttpCode = parseInt(connection.rawConnection.responseHttpCode); // send the response to the client (use compression if active) self.sendWithCompression(connection, responseHttpCode, headers, stringResponse); } /** * Send a file to the client. * * @param connection Connection object where the file must be sent. * @param error Error object, null if not exists. * @param fileStream FileStream for the requested file. * @param mime File mime type. * @param length File length in bytes. * @param lastModified Timestamp if the last modification. */ }, { key: 'sendFile', value: function sendFile(connection, error, fileStream, mime, length, lastModified) { var self = this; var foundExpires = false; var foundCacheControl = false; var ifModifiedSince = void 0; var reqHeaders = void 0; // check if we should use cache mechanisms connection.rawConnection.responseHeaders.forEach(function (pair) { if (pair[0].toLowerCase() === 'expires') { foundExpires = true; } if (pair[1].toLowerCase() === 'cache-control') { foundCacheControl = true; } }); // get headers from the client request reqHeaders = connection.rawConnection.req.headers; // get the 'if-modified-since' value if exists if (reqHeaders['if-modified-since']) { ifModifiedSince = new Date(reqHeaders['if-modified-since']); } // add mime type to the response headers connection.rawConnection.responseHeaders.push(['Content-Type', mime]); // check if file expires if (foundExpires === false) { connection.rawConnection.responseHeaders.push(['Expires', new Date(new Date().getTime() + self.api.config.servers.web.flatFileCacheDuration * 1000).toUTCString()]); } // check if the client want use cache if (foundCacheControl === false) { connection.rawConnection.responseHeaders.push(['Cache-Control', 'max-age=' + self.api.config.servers.web.flatFileCacheDuration + ', must-revalidate, public']); } // add a header to the response with the last modified timestamp connection.rawConnection.responseHeaders.push(['Last-Modified', new Date(lastModified)]); // clean the connection headers self._cleanHeaders(connection); // get the response headers var headers = connection.rawConnection.responseHeaders; // if an error exists change the status code to 404 if (error) { connection.rawConnection.responseHttpCode = 404; } // if the lastModified is smaller than ifModifiedSince we respond with a 304 (use cache) if (ifModifiedSince && lastModified <= ifModifiedSince) { connection.rawConnection.responseHttpCode = 304; } // check if is to use ETag if (self.api.config.servers.web.enableEtag && fileStream) { (function () { // get a file buffer var fileBuffer = !Buffer.isBuffer(fileStream) ? new Buffer(fileStream.toString(), 'utf8') : fileStream; // build the ETag header var fileEtag = (0, _etag2.default)(fileBuffer, { weak: true }); // push the header to the response connection.rawConnection.responseHeaders.push(['ETag', fileEtag]); var noneMatchHeader = reqHeaders['if-none-match']; var cacheCtrlHeader = reqHeaders['cache-control']; var noCache = false; var etagMatches = void 0; // check for no-cache cache request directive if (cacheCtrlHeader && cacheCtrlHeader.indexOf('no-cache') !== -1) { noCache = true; } // parse if-none-match if (noneMatchHeader) { noneMatchHeader = noneMatchHeader.split(/ *, */); } // if-none-match if (noneMatchHeader) { etagMatches = noneMatchHeader.some(function (match) { return match === '*' || match === fileEtag || match === 'W/' + fileEtag; }); } // use the cached object if (etagMatches && !noCache) { connection.rawConnection.responseHeaders = 304; } })(); } // parse the HTTP status code to int var responseHttpCode = parseInt(connection.rawConnection.responseHttpCode); if (error) { self.sendWithCompression(connection, responseHttpCode, headers, String(error)); } else if (responseHttpCode !== 304) { self.sendWithCompression(connection, responseHttpCode, headers, null, fileStream, length); } else { connection.rawConnection.res.writeHead(responseHttpCode, headers); connection.rawConnection.res.end(); connection.destroy(); } } /** * Send a compressed message to the client. * * @param connection Connection object where the message must be sent. * @param responseHttpCode HTTP Status code. * @param headers HTTP response headers. * @param stringResponse Response body. * @param fileStream FileStream, only needed if to send a file. * @param fileLength File size in bytes, only needed if is to send a file. */ }, { key: 'sendWithCompression', value: function sendWithCompression(connection, responseHttpCode, headers, stringResponse, fileStream, fileLength) { var self = this; var compressor = void 0, stringEncoder = void 0; var acceptEncoding = connection.rawConnection.req.headers['accept-encoding']; // Note: this is not a conformant accept-encoding parser. // https://nodejs.org/api/zlib.html#zlib_zlib_createinflate_options // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3 Iif (self.api.config.servers.web.compress === true) { if (acceptEncoding.match(/\bdeflate\b/)) { headers.push(['Content-Encoding', 'deflate']); compressor = _zlib2.default.createDeflate(); stringEncoder = _zlib2.default.deflate; } else if (acceptEncoding.match(/\bgzip\b/)) { headers.push(['Content-Encoding', 'gzip']); compressor = _zlib2.default.createGzip(); stringEncoder = _zlib2.default.gzip; } } // the 'finish' event deontes a successful transfer connection.rawConnection.res.on('finish', function () { connection.destroy(); }); // the 'close' event deontes a failed transfer, but it is probably the client's fault connection.rawConnection.res.on('close', function () { connection.destroy(); }); Iif (fileStream) { if (compressor) { connection.rawConnection.res.writeHead(responseHttpCode, headers); fileStream.pipe(compressor).pipe(connection.rawConnection.res); } else { headers.push(['Content-Length', fileLength]); connection.rawConnection.res.writeHead(responseHttpCode, headers); fileStream.pipe(connection.rawConnection.res); } } else { Iif (stringEncoder) { stringEncoder(stringResponse, function (error, zippedString) { headers.push(['Content-Length', zippedString.length]); connection.rawConnection.res.writeHead(responseHttpCode, headers); connection.rawConnection.res.end(zippedString); }); } else { headers.push(['Content-Length', Buffer.byteLength(stringResponse)]); connection.rawConnection.res.writeHead(responseHttpCode, headers); connection.rawConnection.res.end(stringResponse); } } } /** * Disconnect a client. * * @param connection */ }, { key: 'goodbye', value: function goodbye(connection) {} // disconnect handlers // --------------------------------------------------------------------------------------------------------- [PRIVATE] /** * Handle the requests. * * @param req Request object. * @param res Response object. * @private */ }, { key: '_handleRequest', value: function _handleRequest(req, res) { var self = this; // get the client fingerprint _browser_fingerprint2.default.fingerprint(req, self.api.config.servers.web.fingerprintOptions, function (fingerprint, elementHash, cookieHash) { var responseHeaders = []; var cookies = _utils2.default.parseCookies(req); var responseHttpCode = 200; var method = req.method.toUpperCase(); var parsedURL = _url2.default.parse(req.url, true); var i = void 0; // push all cookies from the request to the response for (i in cookieHash) { responseHeaders.push([i, cookieHash[i]]); } // set content type to JSON responseHeaders.push(['Content-Type', 'application/json; charset=utf-8']); // push all the default headers to the response object for (i in self.api.config.servers.web.httpHeaders) { responseHeaders.push([i, self.api.config.servers.web.httpHeaders[i]]); } // get the client IP var remoteIP = req.connection.remoteAddress; // get the client port var remotePort = req.connection.remotePort; // helpers for unix socket bindings with no forward Iif (!remoteIP && !remotePort) { remoteIP = '0.0.0.0'; remotePort = '0'; } Iif (req.headers['x-forwarded-for']) { var parts = void 0; var forwardedIp = req.headers['x-forwarded-for'].split(',')[0]; if (forwardedIp.indexOf('.') >= 0 || forwardedIp.indexOf('.') < 0 && forwardedIp.indexOf(':') < 0) { // IPv4 forwardedIp = forwardedIp.replace('::ffff:', ''); // remove any IPv6 information, ie: '::ffff:127.0.0.1' parts = forwardedIp.split(':'); if (parts[0]) { remoteIP = parts[0]; } if (parts[1]) { remotePort = parts[1]; } } else { // IPv6 parts = _utils2.default.parseIPv6URI(forwardedIp); if (parts.host) { remoteIP = parts.host; } if (parts.port) { remotePort = parts.port; } } if (req.headers['x-forwarded-port']) { remotePort = req.headers['x-forwarded-port']; } } self.buildConnection({ // will emit 'connection' rawConnection: { req: req, res: res, params: {}, method: method, cookies: cookies, responseHeaders: responseHeaders, responseHttpCode: responseHttpCode, parsedURL: parsedURL }, id: fingerprint + '-' + _nodeUuid2.default.v4(), fingerprint: fingerprint, remoteAddress: remoteIP, remotePort: remotePort }); }); } /** * Change socket permission. * * @param bindIP * @param port */ }, { key: 'chmodSocket', value: function chmodSocket(bindIP, port) { var self = this; Iif (!bindIP && self.options.port.indexOf('/') >= 0) { _fs2.default.chmodSync(port, 511); } } /** * Determine the request params. * * @param connection Client connection object. * @param callback Callback function. * @private */ }, { key: '_determineRequestParams', value: function _determineRequestParams(connection, callback) { var self = this; // determine if is a file or an api request var requestMode = self.api.config.servers.web.rootEndpointType; var pathname = connection.rawConnection.parsedURL.pathname; var pathParts = pathname.split('/'); var matcherLength = void 0, i = void 0; // remove empty parts from the beginning of the path while (pathParts[0] === '') { pathParts.shift(); } // if exist an empty part on the end of the path, remove it if (pathParts[pathParts.length - 1] === '') { pathParts.pop(); } Eif (pathParts[0] && pathParts[0] === self.api.config.servers.web.urlPathForActions) { requestMode = 'api'; pathParts.shift(); } else if (pathParts[0] && pathParts[0] === self.api.config.servers.websocket.clientJsName) { requestMode = 'client-lib'; pathParts.shift(); } else if (pathParts[0] && pathParts[0] === self.api.config.servers.web.urlPathForFiles) { requestMode = 'file'; pathParts.shift(); } else if (pathParts[0] && pathname.indexOf(self.api.config.servers.web.urlPathForActions) === 0) { requestMode = 'api'; matcherLength = self.api.config.servers.web.urlPathForActions.split('/').length; for (i = 0; i < matcherLength - 1; i++) { pathParts.shift(); } } else if (pathParts[0] && pathname.indexOf(self.api.config.servers.web.urlPathForFiles) === 0) { requestMode = 'file'; matcherLength = self.api.config.servers.web.urlPathForFiles.split('/').length; for (i = 0; i < matcherLength - 1; i++) { pathParts.shift(); } } // split parsed URL by '.' var extensionParts = connection.rawConnection.parsedURL.pathname.split('.'); Iif (extensionParts.length > 1) { connection.extension = extensionParts[extensionParts.length - 1]; } // OPTIONS if (connection.rawConnection.method === 'OPTIONS') { requestMode = 'options'; callback(requestMode); } // API else Eif (requestMode === 'api') { // enable trace mode if (connection.rawConnection.method === 'TRACE') { requestMode = 'trace'; } var search = connection.rawConnection.parsedURL.search.slice(1); self._fillParamsFromWebRequest(connection, _qs2.default.parse(search, self.api.config.servers.web.queryParseOptions)); connection.rawConnection.params.query = connection.rawConnection.parsedURL.query; if (connection.rawConnection.method !== 'GET' && connection.rawConnection.method !== 'HEAD' && (connection.rawConnection.req.headers['content-type'] || connection.rawConnection.req.headers['Content-Type'])) { connection.rawConnection.form = new _formidable2.default.IncomingForm(); for (i in self.api.config.servers.web.formOptions) { connection.rawConnection.form[i] = self.api.config.servers.web.formOptions[i]; } connection.rawConnection.form.parse(connection.rawConnection.req, function (error, fields, files) { Iif (error) { self.log('error processing form: ' + String(error), 'error'); connection.error = new Error('There was an error processing this form.'); } else { connection.rawConnection.params.body = fields; connection.rawConnection.params.files = files; self._fillParamsFromWebRequest(connection, files); self._fillParamsFromWebRequest(connection, fields); } Iif (self.api.config.servers.web.queryRouting !== true) { connection.params.action = null; } // process route self.api.routes.processRoute(connection, pathParts); callback(requestMode); }); } else { Iif (self.api.config.servers.web.queryRouting !== true) { connection.params.action = null; } // process route self.api.routes.processRoute(connection, pathParts); callback(requestMode); } } else if (requestMode === 'file') { if (!connection.params.file) { connection.params.file = pathParts.join(_path2.default.sep); } if (connection.params.file === '' || connection.params.file[connection.params.file.length - 1] === '/') { connection.params.file = connection.params.file + self.api.config.general.directoryFileType; } callback(requestMode); } else if (requestMode === 'client-lib') { callback(requestMode); } } }, { key: 'processClientLib', value: function processClientLib(connection) { var self = this; // client lib var file = _path2.default.normalize(self.api.config.general.paths.temp + _path2.default.sep + self.api.config.servers.websocket.clientJsName + '.js'); // define the file to be loaded connection.params.file = file; // process like a file self.processFile(connection); } /** * Fill the connection with the web request params. * * @param connection Connection object. * @param varsHash Request params. * @private */ }, { key: '_fillParamsFromWebRequest', value: function _fillParamsFromWebRequest(connection, varsHash) { // helper for JSON parts var collapsedVarsHash = _utils2.default.collapseObjectToArray(varsHash); Iif (collapsedVarsHash !== false) { // post was an array, lets call it "payload" varsHash = { payload: collapsedVarsHash }; } // copy requests params to connection object for (var v in varsHash) { connection.params[v] = varsHash[v]; } } /** * Complete the response. * * THis add additional server info to the response message, and * build the final response object. * * @param data Data to be sent to the client. * @private */ }, { key: '_completeResponse', value: function _completeResponse(data) { var self = this; Eif (data.toRender === true) { Eif (self.api.config.servers.web.metadataOptions.serverInformation) { var stopTime = new Date().getTime(); data.response.serverInformation = { serverName: self.api.config.general.serverName, apiVersion: self.api.config.general.apiVersion, requestDuration: stopTime - data.connection.connectedAt, currentTime: stopTime }; } } // check if is to use requester information Eif (self.api.config.servers.web.metadataOptions.requesterInformation) { data.response.requesterInformation = self._buildRequesterInformation(data.connection); } // is an error response? if (data.response.error) { if (self.api.config.servers.web.returnErrorCodes === true && data.connection.rawConnection.responseHttpCode === 200) { if (data.actionStatus === 'unknown_action') { data.connection.rawConnection.responseHttpCode = 404; } else Iif (data.actionStatus === 'missing_params') { data.connection.rawConnection.responseHttpCode = 422; } else Iif (data.actionStatus === 'server_error') { data.connection.rawConnection.responseHttpCode = 500; } else { data.connection.rawConnection.responseHttpCode = 400; } } } Iif (!data.response.error && data.action && data.params.apiVersion && self.api.actions.actions[data.params.action][data.params.apiVersion].matchExtensionMimeType === true && data.connection.extension) { data.connection.rawConnection.responseHeaders.push(['Content-Type', _mime2.default.lookup(data.connection.extension)]); } // if its an error response we need to serialize the error object if (data.response.error) { data.response.error = self.api.config.errors.serializers.servers.web(data.response.error); } var stringResponse = ''; // build the string response Eif (self._extractHeader(data.connection, 'Content-Type').match(/json/)) { stringResponse = JSON.stringify(data.response, null, self.api.config.servers.web.padding); Iif (data.params.callback) { data.connection.rawConnection.responseHeaders.push(['Content-Type', 'application/javascript']); stringResponse = data.connection.params.callback + '(' + stringResponse + ');'; } } else { stringResponse = data.response; } // return the response to the client self.sendMessage(data.connection, stringResponse); } /** * Extract one header from a connection object. * * @param connection Connection object from the header must be extracted. * @param match Header name. * @returns {*} Null if not found, otherwise the header value. * @private */ }, { key: '_extractHeader', value: function _extractHeader(connection, match) { var i = connection.rawConnection.responseHeaders.length - 1; while (i >= 0) { if (connection.rawConnection.responseHeaders[i][0].toLowerCase() === match.toLowerCase()) { return connection.rawConnection.responseHeaders[i][1]; } i--; } return null; } /** * Build the requester information. * * @param connection * @returns {{id: number, fingerprint: (*|browser_fingerprint.fingerprint|null), remoteIP: string, receivedParams: {}}} * @private */ }, { key: '_buildRequesterInformation', value: function _buildRequesterInformation(connection) { // build the request information object var requesterInformation = { id: connection.id, fingerprint: connection.fingerprint, remoteIP: connection.remoteIP, receivedParams: {} }; // copy all the connection params to the request information for (var param in connection.params) { requesterInformation.receivedParams[param] = connection.params[param]; } // return the request information return requesterInformation; } /** * Remove some unnecessary headers from the response. * * @param connection Client connection object. * @private */ }, { key: '_cleanHeaders', value: function _cleanHeaders(connection) { // make a copy of the original headers var originalHeaders = connection.rawConnection.responseHeaders.reverse(); var foundHeaders = []; var cleanedHeaders = []; // iterate all headers and remove duplications and unnecessary headers for (var i in originalHeaders) { // get header name and value var key = originalHeaders[i][0]; var value = originalHeaders[i][1]; if (foundHeaders.indexOf(key.toLowerCase()) >= 0 && key.toLowerCase().indexOf('set-cookie') < 0) { // ignore, it's a duplicate } else Iif (connection.rawConnection.method === 'HEAD' && key === 'Transfer-Encoding') { // ignore, we can't send this header for HEAD requests } else { foundHeaders.push(key.toLowerCase()); cleanedHeaders.push([key, value]); } } // set the clean headers on the connection connection.rawConnection.responseHeaders = cleanedHeaders; } /** * Respond to an option request. * * @param connection Connection object. * @private */ }, { key: '_respondToOptions', value: function _respondToOptions() { var connection = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; var self = this; // inform the allowed methods Iif (!self.api.config.servers.web.httpHeaders['Access-Control-Allow-Methods'] && !self._extractHeader(connection, 'Access-Control-Allow-Methods')) { var methods = 'HEAD, GET, POST, PUT, DELETE, OPTIONS, TRACE'; connection.rawConnection.responseHeaders.push(['Access-Control-Allow-Methods', methods]); } // inform the allowed origins Iif (!self.api.config.servers.web.httpHeaders['Access-Control-Allow-Origin'] && !self._extractHeader(connection, 'Access-Control-Allow-Origin')) { var origin = '*'; connection.rawConnection.responseHeaders.push(['Access-Control-Allow-Origin', origin]); } // send the message to client self.sendMessage(connection, ''); } /** * Respond to a trace request. * * @param connection Client connection object. * @private */ }, { key: '_respondToTrace', value: function _respondToTrace(connection) { var self = this; // build the request information var data = self._buildRequesterInformation(connection); // build the response string and send it to the client var stringResponse = JSON.stringify(data, null, self.api.config.servers.web.padding); self.sendMessage(connection, stringResponse); } /** * Try remove the stale unix socket. * * @param bindIP * @param port * @private */ }, { key: '_cleanSocket', value: function _cleanSocket(bindIP, port) { var self = this; if (!bindIP && port.indexOf('/') >= 0) { _fs2.default.unlink(port, function (error) { if (error) { self.log('cannot remove stale socket @' + port + ':' + error); } else { self.log('removed stale unix socket @' + port); } }); } } }]); return Web; }(_genericServer2.default); exports.default = Web; //# sourceMappingURL=web.js.map |