diff options
author | Chad Weider <cweider@oofn.net> | 2012-07-22 23:54:38 -0700 |
---|---|---|
committer | Chad Weider <cweider@oofn.net> | 2012-07-22 23:55:07 -0700 |
commit | cd11717b99d1f7cf1d2e687de7032cdda71b441d (patch) | |
tree | 878ebd092abdd24b2b5e8a8d1b1c09cc7188882e /src/node | |
parent | 4f79f74be5cb7c9e5fc9080f3be2ad6207a26757 (diff) | |
download | etherpad-lite-cd11717b99d1f7cf1d2e687de7032cdda71b441d.zip |
Eliminate the loopback that has been causing so much trouble.
`localhost`, `0.0.0.0`, `127.0.0.1` each works only in some places some of the time, this works around the problem by overriding Yajsml's built-in request mechanism in favor of a hacked together one. TODO: Serve files from another service, or directly from the file system in order to make this unnecessary.
Fixes #747
Diffstat (limited to 'src/node')
-rw-r--r-- | src/node/hooks/express/static.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/node/hooks/express/static.js b/src/node/hooks/express/static.js index f284e478..0d78da2f 100644 --- a/src/node/hooks/express/static.js +++ b/src/node/hooks/express/static.js @@ -7,8 +7,75 @@ var Yajsml = require('yajsml'); var fs = require("fs"); var ERR = require("async-stacktrace"); var _ = require("underscore"); +var urlutil = require('url'); exports.expressCreateServer = function (hook_name, args, cb) { + // What follows is a terrible hack to avoid loop-back within the server. + // TODO: Serve files from another service, or directly from the file system. + function requestURI(url, method, headers, callback, redirectCount) { + var parsedURL = urlutil.parse(url); + + var status = 500, headers = {}, content = []; + + var mockRequest = { + url: url + , method: method + , params: {filename: parsedURL.path.replace(/^\/static\//, '')} + , headers: headers + }; + var mockResponse = { + writeHead: function (_status, _headers) { + status = _status; + for (var header in _headers) { + if (Object.prototype.hasOwnProperty.call(_headers, header)) { + headers[header] = _headers[header]; + } + } + } + , setHeader: function (header, value) { + headers[header.toLowerCase()] = value.toString(); + } + , header: function (header, value) { + headers[header.toLowerCase()] = value.toString(); + } + , write: function (_content) { + _content && content.push(_content); + } + , end: function (_content) { + _content && content.push(_content); + callback(status, headers, content.join('')); + } + }; + + minify.minify(mockRequest, mockResponse); + } + function requestURIs(locations, method, headers, callback) { + var pendingRequests = locations.length; + var responses = []; + + function respondFor(i) { + return function (status, headers, content) { + responses[i] = [status, headers, content]; + if (--pendingRequests == 0) { + completed(); + } + }; + } + + for (var i = 0, ii = locations.length; i < ii; i++) { + requestURI(locations[i], method, headers, respondFor(i)); + } + + function completed() { + var statuss = responses.map(function (x) {return x[0]}); + var headerss = responses.map(function (x) {return x[1]}); + var contentss = responses.map(function (x) {return x[2]}); + callback(statuss, headerss, contentss); + }; + } + + + // Cache both minified and static. var assetCache = new CachingMiddleware; args.app.all('/(javascripts|static)/*', assetCache.handle); @@ -24,6 +91,7 @@ exports.expressCreateServer = function (hook_name, args, cb) { , rootURI: 'http://localhost:' + settings.port + '/static/js/' , libraryPath: 'javascripts/lib/' , libraryURI: 'http://localhost:' + settings.port + '/static/plugins/' + , requestURIs: requestURIs // Loop-back is causing problems, this is a workaround. }); var StaticAssociator = Yajsml.associators.StaticAssociator; |