Commit c887a757 authored by indexzero's avatar indexzero

[api] Updated request hashes to use a unique identifier

parent d15bba4c
...@@ -32,6 +32,7 @@ exports.HttpProxy = function () { ...@@ -32,6 +32,7 @@ exports.HttpProxy = function () {
this.emitter = new(events.EventEmitter); this.emitter = new(events.EventEmitter);
this.events = {}; this.events = {};
this.listeners = {}; this.listeners = {};
this.collisions = {};
}; };
exports.createServer = function () { exports.createServer = function () {
...@@ -83,34 +84,55 @@ exports.HttpProxy.prototype = { ...@@ -83,34 +84,55 @@ exports.HttpProxy.prototype = {
watch: function (req, res) { watch: function (req, res) {
var self = this; var self = this;
this.events[req] = []; // Create a unique id for this request so
// we can reference it later.
var id = new Date().getTime().toString();
this.listeners[req] = { // If we get a request in the same tick, we need to
// append to the id so it stays unique.
if(typeof this.collisions[id] === 'undefined') {
this.collisions[id] = 0;
}
else {
this.collisions[id]++;
id += this.collisions[id];
}
req.id = id;
this.events[req.id] = [];
this.listeners[req.id] = {
onData: function () { onData: function () {
self.events[req].push(['data'].concat(self.toArray(arguments))); self.events[req.id].push(['data'].concat(self.toArray(arguments)));
}, },
onEnd: function () { onEnd: function () {
self.events[req].push(['end'].concat(self.toArray(arguments))); self.events[req.id].push(['end'].concat(self.toArray(arguments)));
} }
}; };
req.addListener('data', this.listeners[req].onData); req.addListener('data', this.listeners[req.id].onData);
req.addListener('end', this.listeners[req].onEnd); req.addListener('end', this.listeners[req.id].onEnd);
}, },
unwatch: function (req, res) { unwatch: function (req, res) {
req.removeListener('data', this.listeners[req].onData); req.removeListener('data', this.listeners[req.id].onData);
req.removeListener('end', this.listeners[req].onEnd); req.removeListener('end', this.listeners[req.id].onEnd);
// Rebroadcast any events that have been buffered // Rebroadcast any events that have been buffered
while(this.events[req].length > 0) { while(this.events[req.id].length > 0) {
var args = this.events[req].shift(); var args = this.events[req.id].shift();
req.emit.apply(req, args); req.emit.apply(req, args);
} }
// Remove the data from the event and listeners hashes // Remove the data from the event and listeners hashes
delete this.listeners[req]; delete this.listeners[req.id];
delete this.events[req]; delete this.events[req.id];
// If this request id is a base time, delete it
if (typeof this.collisions[req.id] !== 'undefined') {
delete this.collisions[req.id];
}
}, },
proxyRequest: function (port, server, req, res) { proxyRequest: function (port, server, req, res) {
...@@ -135,11 +157,12 @@ exports.HttpProxy.prototype = { ...@@ -135,11 +157,12 @@ exports.HttpProxy.prototype = {
res.end(); res.end();
}); });
// Add a listener for the reverse_proxy response event // Add a listener for the reverse_proxy response event
reverse_proxy.addListener('response', function (response) { reverse_proxy.addListener('response', function (response) {
// Set the response headers of the client response // Set the response headers of the client response
res.writeHead(response.statusCode, response.headers); res.writeHead(response.statusCode, response.headers);
// Add event handler for the proxied response in chunks // Add event handler for the proxied response in chunks
response.addListener('data', function (chunk) { response.addListener('data', function (chunk) {
if(req.method !== 'HEAD') { if(req.method !== 'HEAD') {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment