Permalink
Browse files

Ignore a particular error condition

  • Loading branch information...
1 parent f40d90d commit e7fce4a9e18000c726629640e48f5ea93a82668c @cramforce committed Nov 3, 2011
Showing with 50 additions and 45 deletions.
  1. +50 −45 lib/server.js
View
@@ -28,8 +28,13 @@ if(!host || !key || !secret) {
var scheme = useSSL ? 'https' : 'http';
process.on('uncaughtException', function (err) {
+ var str = '' + err;
console.log('[UNCAUGHT EXCEPTION] ' +err+'\n' + JSON.stringify(err, null, " "));
- process.exit();
+ if(str.match(/ECONNRESET, Connection reset by peer/)) {
+ // ignore
+ } else {
+ process.exit();
+ }
});
// the static file server
@@ -48,19 +53,19 @@ var server = http.createServer(function (request, response) {
error: error
}));
}
-
+
function getSessionToken() {
var match = (request.headers.cookie || "").match(/token=([^;]+)/);
if(!match) return null;
return match[1];
}
-
+
function getTwitterApiClient(cb) {
var token = getSessionToken();
if(!token) {
return respondError("Cant find access token cookie in "+request.headers.cookie, 401)
}
-
+
oauth.reuse(key, secret, token, function (err, requester, info) {
if(err) {
respondError("no_auth", 401)
@@ -69,16 +74,16 @@ var server = http.createServer(function (request, response) {
}
});
}
-
+
// Please somebody put in a middleware/router thing!
var url = require('url').parse(request.url, true);
console.log("Request "+url.pathname);
-
+
// /access
// Prepare oauth negotiation and send user to Twitter
if(url.pathname.match(/^\/access$/)) {
var otherhost = request.headers.host;
-
+
var re = new RegExp(host+"$"); // only allow hosts that are sub domains of the host name from the command line
var targetHost = host;
if(otherhost.match(re)) {
@@ -90,7 +95,7 @@ var server = http.createServer(function (request, response) {
if(error) {
respondError(error);
} else {
-
+
var temp = sha1.HMACSHA1(token,""+Math.random());
TransferURL[temp] = {
url: url,
@@ -99,43 +104,43 @@ var server = http.createServer(function (request, response) {
var transferURL = scheme + "://"+host+"/transfer?token="+encodeURIComponent(temp);
console.log("Transfer via "+transferURL)
-
+
console.log("oauth step 1 success");
Token[token] = cont;
-
+
setTimeout(function () { // clear out memory after 10 minutes
delete Token[token];
}, 60 * 10 * 1000);
-
+
response.writeHead(302, { // onto Twitter
'Location': transferURL
});
response.end()
}
})
}
-
+
// /transfer
// ...
else if(url.pathname.match(/^\/transfer/)) {
var otherhost = request.headers.host; // Cannot be overriden in Ajax. See http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/#dfn-setrequestheader
-
+
// only allow requests to the main host set via command line
if(host != otherhost) {
return respondError("Illegal host");
}
-
+
var transfer = url.query.token;
-
+
if(!transfer) {
return respondError("Cant find transfer token "+transfer);
}
-
+
var info = TransferURL[transfer];
if(!info) {
return respondError("Cant find transfer url "+transfer);
}
-
+
if(info.host == host) {
info.url = info.url.replace(/^http\:/, 'https:');
response.writeHead(302, { // onto Twitter
@@ -144,14 +149,14 @@ var server = http.createServer(function (request, response) {
response.end()
return
}
-
+
response.writeHead(200, { // onto Twitter
'Content-Type': "text/html"
});
response.end('<html>The Streamie client <b>'+info.host+'</b> wants to access your Twitter account.</b><br>'+
'<form><input type="button" value="Cancel" onclick="location=\'http://www.google.com\'"><input type="button" value="OK" onclick="location=\''+info.url+'\'"></form></html>')
}
-
+
// /oauth_callback
// Handle response from Twitter after user allowed us
// After success send user to /oauth_transfer to allow muliple sub domains
@@ -179,7 +184,7 @@ var server = http.createServer(function (request, response) {
})
}
}
-
+
// /oauth_transfer
// set oauth token cookie and send user to homepage
else if(url.pathname.match(/^\/oauth_transfer/)) {
@@ -196,7 +201,7 @@ var server = http.createServer(function (request, response) {
});
response.end()
}
-
+
// /twitter
// Twitter REST API proxy
else if(url.pathname.match(/^\/twitter/)) {
@@ -209,26 +214,26 @@ var server = http.createServer(function (request, response) {
return respondError('Invalid session token');
}
request.url = request.url.replace(/[\?\&]_token=\w+/, '');
-
+
request.url = request.url.replace(/^\/twitter/, ""); // now proxy make request to sub dir
console.log("Twitter API call" + request.url);
-
+
var generic = request.url.replace(/\d+/g, "").replace(/\?.*/, "").replace(/\.\w+$/, "");
stats.inc(generic);
-
+
var data = [];
request.on("data", function (chunk) {
data.push(chunk.toString('ascii')); // no support for streaming request bodies!
});
-
+
request.on("end", function () {
getTwitterApiClient(function (err, requester) {
//console.log("Proxying API call "+request.url);
twitterProxy.proxy(requester, request, response, data.join(""))
});
})
- }
-
+ }
+
// GET info about current user
else if(url.pathname.match(/^\/user_info/)) {
getTwitterApiClient(function (err, requester, info) {
@@ -240,7 +245,7 @@ var server = http.createServer(function (request, response) {
}
});
}
-
+
// /imgur
// imgur.com REST API -> simple proxy
else if(url.pathname.match(/^\/imgur/)) {
@@ -259,10 +264,10 @@ var server = http.createServer(function (request, response) {
}
});
}
-
+
// /untiny
// untiny.com REST API -> simple proxy
- else if(url.pathname.match(/^\/untiny/)) {
+ else if(url.pathname.match(/^\/untiny/)) {
var proxy = new httpProxy.HttpProxy(request, response);
console.log("Onto untiny... ", request);
request.url = request.url.replace(/^\/untiny/, "/api"); // now proxy make request to sub dir
@@ -272,19 +277,19 @@ var server = http.createServer(function (request, response) {
delete request.headers.cookie; // we do not want to pass on session secrets
proxy.proxyRequest(80, domain, request, response);
}
-
+
// less.js server side compilation
else if(url.pathname.match(/css\/\w+\.less\.css/)) {
lessHandler.handle("../public"+url.pathname, request, response);
}
-
+
// All other requests
else {
var curhost = request.headers.host;
var sub = curhost.match(/(.*)\..+\.[\D]+/);
-
+
if(sub && !sub[1].match(/local/)) { // Requests to sub domains and not IP addresses
-
+
sub = sub[1];
if(sub == "www") { // redirect www to host without www
response.writeHead(302, { // onto Twitter
@@ -306,7 +311,7 @@ var server = http.createServer(function (request, response) {
var proxy = new httpProxy.HttpProxy(request, response);
proxy.proxyRequest(80, domain, request, response);
}
- }
+ }
// serve from the local disk
else {
console.log("Static" +url.pathname);
@@ -335,25 +340,25 @@ function now() {
// On Socket.io connections
socket.on('connection', function(client){
console.log("Connection");
-
+
// We always send JSON
function send(data) {
client.send(JSON.stringify(data))
}
-
+
var subscription;
-
+
var last = now();
-
+
client.on('message', function(msg){
var data = JSON.parse(msg);
last = now();
-
+
if(data == "ping") {
send("pong")
}
else if(data.token) { // only interested in messages with tokens
-
+
// try to find the token in the databse and reuse the credentials
oauth.reuse(key, secret, data.token, function (err, requester, info) {
if(err) {
@@ -368,7 +373,7 @@ socket.on('connection', function(client){
action: "auth_ok",
info: info
})
-
+
subscription = stream.subscribe(info.user_id, function (data) {
send({
tweet: data
@@ -378,17 +383,17 @@ socket.on('connection', function(client){
});
}
})
-
+
// send testing data
/*setInterval(function () {var data = dummies[Math.floor(dummies.length * Math.random())];
console.log("Mock "+data)
client.send(JSON.stringify({
tweet: data
}))
}, 2000);*/
-
+
client.send(JSON.stringify("hello world"));
-
+
stats.inc("con");
client.on('disconnect', function(){
stats.dec("con");

0 comments on commit e7fce4a

Please sign in to comment.