Skip to content

Commit

Permalink
rewriting /aliases request handler for JSONp
Browse files Browse the repository at this point in the history
the code is still a mess and I should modularize this further.

Anyways. Now you can either POST application/json to /aliases or you GET
/aliases with a callback-parameter doing the usual JSONp dance
  • Loading branch information
pilif committed Apr 24, 2010
1 parent cee0715 commit 1a6e8c0
Showing 1 changed file with 84 additions and 54 deletions.
138 changes: 84 additions & 54 deletions lib/tempalias_http.js
Expand Up @@ -12,6 +12,7 @@ var p = tempalias.AliasProvider;

fs.realpath('lib/../public', function(err, public_root){
http.createServer(function(req, res) {

/* Support for google's crawlable ajax specification.
Note that this isn't quite correct in that it returns the content
Expand All @@ -23,38 +24,83 @@ fs.realpath('lib/../public', function(err, public_root){
if (url.query && url.query._escaped_fragment_){
req.url = '/templates/'+url.query._escaped_fragment_+'.html';
}
if (req.url == '/aliases'){
var headers = {"Content-Type": "application/json", 'Date': (new Date()).toUTCString()};
if (!url.query) url.query = {};

if (url.pathname == '/aliases'){

var headers = {
"Content-Type": url.query.callback ? "text/javascript" : "application/json",
'Date': (new Date()).toUTCString()
};

var sendAlias = function(alias){
var body = JSON.stringify(alias);
if (url.query.callback)
body = url.query.callback + '(' + body + ')';
headers['Content-Length'] = body.length;
res.writeHead(200, headers);
res.end(body);
};

var err = function(code, resp){
var body = JSON.stringify(resp);
if (url.query.callback)
body = url.query.callback + '(' + body + ')';
headers['Content-Length'] = body.length;
res.writeHead(code, headers);
res.end(body);
}

if (req.method != 'POST'){
err(405, {
error: "method-not-allowed",
description: '/aliases only takes POST'
});
return;
}
};

if (req.headers['content-type'].split(';')[0] != 'application/json'){
err(400, {
error: "invalid-request-type",
description: "invalid request type. Need application/json, but got "+req.headers['content-type']
});
return;
}
var len = req.headers['content-length'] || 1024;
if (len >= 1024){
err(400 , {
error: "invalid-content-length",
description: "no or too big content length provided"
});
}
var genAlias = function(data){
var alias = p.getNew();
try{
alias.setInfo(data);
}catch(e){
err(400, {
error: "invalid-data",
description: e.message
});
}
try{
p.save(alias, function(id){
try{
alias.currentDate = new Date();
var f = alias.valid_from ? alias.valid_from.getDate() : new Date();
if (alias.valid_until && !alias.valid_from){
alias.days = Math.ceil((alias.valid_until.getTime()-f.getTime())/(1000*60*60*24));
}
sendAlias(alias);
}catch(e){
err(500, {error: "server-exception", description: e.message});
}
});
}catch(e){
err(500, {error: "server-exception", description: e.message});
}
};

if (!url.query.callback){
if (req.method != 'POST'){
err(405, {
error: "method-not-allowed",
description: '/aliases only takes POST'
});
return;
}
if (req.headers['content-type'].split(';')[0] != 'application/json'){
err(400, {
error: "invalid-request-type",
description: "invalid request type. Need application/json, but got "+req.headers['content-type']
});
return;
}
var len = req.headers['content-length'] || 1024;
if (!url.query.callback && len >= 1024){
err(400 , {
error: "invalid-content-length",
description: "no or too big content length provided"
});
}
var reqbody = '';
var error = false;
req.addListener('data', function(chunk){
Expand All @@ -65,39 +111,23 @@ fs.realpath('lib/../public', function(err, public_root){
}
});
req.addListener('end', function(){
if (error) return;
var alias = p.getNew();
try{
alias.setInfo(JSON.parse(reqbody));
}catch(e){
err(400, {
error: "invalid-data",
description: e.message
});
}
try{
p.save(alias, function(id){
try{
alias.currentDate = new Date();
var f = alias.valid_from ? alias.valid_from.getDate() : new Date();
if (alias.valid_until && !alias.valid_from){
alias.days = Math.ceil((alias.valid_until.getTime()-f.getTime())/(1000*60*60*24));
}
var body = JSON.stringify(alias);
headers['Content-Length'] = body.length;
res.writeHead(200, headers);
res.end(body);
}catch(e){
err(500, {error: "server-exception", description: e.message});
}
});
}catch(e){
err(500, {error: "server-exception", description: e.message});
}
genAlias(JSON.parse(reqbody));
});
return;
}else{
if(req.method != 'GET'){
err(400, {
error: "method-not-allowed",
description: 'with callback only GET allowed'
});
return;
}
genAlias(url.query);
}
return; // don't pass to paperboy
}


// other cases: Divert to paperboy
paperboy
.deliver(public_root, req, res)
Expand Down

0 comments on commit 1a6e8c0

Please sign in to comment.