diff --git a/lib/jellyproxy.js b/lib/jellyproxy.js index c2f0f97..0b205a9 100644 --- a/lib/jellyproxy.js +++ b/lib/jellyproxy.js @@ -1,12 +1,29 @@ var http = require('http') , url = require('url') , path = require('path') - , paperboy = require('paperboy') , WEBROOT = path.join(path.dirname(__filename), '../static') , h = {accept:'application/json', 'content-type':'application/json'} - , tag = ""; + , tag = "" + , jellyProxy = require('jelly-proxy') + , send = require("send") + +var payload = function(){ + var cache = new Date().getTime(); + var includes = (''); + includes += (''); + return includes +} + +var opts = { + tag: tag +, payload : payload +} + exports.jellyproxy = function(_this) { + // Finish the request var finish = function(req, res, data) { var dataString = JSON.stringify(data); @@ -16,8 +33,9 @@ exports.jellyproxy = function(_this) { res.write(dataString); res.end(); }; - - var server = http.createServer(function (req, res) { + + + var handleJelly = function(req, res){ var ip = req.connection.remoteAddress; var uri = url.parse(req.url, true); @@ -114,83 +132,17 @@ exports.jellyproxy = function(_this) { //name and serve it from the static directory var fname = req.url.split("/"); req.url = req.url.replace(pathname, "/" + fname[fname.length -1]); + send(req, req.url).root(WEBROOT).pipe(res); + } + } - paperboy - .deliver(WEBROOT, req, res) - .otherwise(function() { - res.writeHead(404, {'Content-Type': 'text/plain'}); - res.write('Sorry, no paper this morning!'); - res.close(); - }); - } - else { - // Actual proxying happens here - var c = http.createClient(uri.port, uri.hostname); - - // Stop from requesting gzip - req.headers['accept-encoding'] = "text/html"; - - // allow additional default headers - var headers={},h; - - for(h in req.headers)headers[h]=req.headers[h]; - - if(_this.default_headers)for(h in _this.default_headers)headers[h]=_this.default_headers[h]; - - var proxyRequest = c.request(req.method, pathname, headers); - - var clientError = function (e) { - if (!res._header) { - res.writeHead(502, {}) - res.end(); - } else { - res.end(); - // This should not be debug, it should always display because it's really bad! - _this.log.push(["client error after writeHead was called. "+e.stack]); - } - } - - c.on("error", clientError); - proxyRequest.on("error", clientError); - - proxyRequest.addListener("response", function (response) { - if (response.headers['content-type'] && - response.headers['content-type'].indexOf("text/html") != -1) { - delete response.headers['content-length']; - } - res.writeHead(response.statusCode, response.headers); - response.addListener("data", function (chunk) { - // modify the html content - if (response.headers['content-type'] && - response.headers['content-type'].indexOf("text/html") != -1) { - if (chunk.toString().indexOf(tag)) { - var cache = new Date().getTime(); - var includes = ''; - includes += ''; - includes += tag; - chunk = chunk.toString().replace(tag, includes); - } - } - res.write(chunk, 'binary'); - }) - response.addListener("end", function () { - res.end(); - }) - }) - req.addListener("data", function (chunk) { - proxyRequest.write(chunk, 'binary'); - }) - req.addListener("end", function () { - proxyRequest.end(); - }) - } - }); + var server = jellyProxy(opts, handleJelly, 80)// TODO don't assume port 80 - //needs to be safer - server.listen(_this.port, function() { - _this.port = server.address().port; - }); + //needs to be safer + server.listen(_this.port, function() { + _this.port = server.address().port; + }); - _this.log.push(['output', 'started server', _this.tid+" on port "+_this.port]); - return server; + _this.log.push(['output', 'started server', _this.tid+" on port "+_this.port]); + return server; }; diff --git a/lib/main.js b/lib/main.js index 0815725..7b27a94 100644 --- a/lib/main.js +++ b/lib/main.js @@ -70,6 +70,17 @@ jellyfish = function(b, url, cb) { if (b) { _this.start(b, cb); } + + + process.on('uncaughtException', function (err) { + console.log(' \x1b[33mCaught exception: \x1b[0m :' + err); + console.log(' \x1b[33mException Stack: \x1b[0m :' + err.stack); + _this.log.push(['Caught exception: ' + err]); + _this.log.push([err.stack]); + }); + + + }; jellyfish.prototype.__proto__ = EventEmitter.prototype; @@ -183,9 +194,3 @@ process.on('exit', function () { } }); -process.on('uncaughtException', function (err) { - console.log(' \x1b[33mCaught exception: \x1b[0m :' + err); - console.log(' \x1b[33mException Stack: \x1b[0m :' + err.stack); - _this.log.push(['Caught exception: ' + err]); - _this.log.push([err.stack]); -}); \ No newline at end of file diff --git a/package.json b/package.json index f8d54b4..597dfb7 100644 --- a/package.json +++ b/package.json @@ -12,10 +12,12 @@ , "engines" : ["node"] , "main" : "./lib/main" , "dependencies" : - { "paperboy" : "0.0.x" - , "request" : "1.9.x" + { + "request" : "1.9.x" , "zombie" : "0.x" , "wd" : "0.0.8" + , "jelly-proxy" : "0.2.0" + , "send" : "0.1.1" } , "bin" : { "jellyfish" : "./lib/bin.js" } , "directories" : { "lib" : "./lib" }