Skip to content

Commit

Permalink
ostatus.webfinger: use sax instead of o3-xml
Browse files Browse the repository at this point in the history
  • Loading branch information
astro committed Jun 2, 2011
1 parent 4d8b75e commit 4f2cb04
Showing 1 changed file with 58 additions and 35 deletions.
93 changes: 58 additions & 35 deletions lib/ostatus/webfinger.js
Expand Up @@ -24,7 +24,7 @@

var Sys = require('sys')
,Url = require('url')
,Xml = require('o3-xml')
,Sax = require('sax')
,Path = require('path')
,Mu = require('mu')
,Http = require('./http.js');
Expand Down Expand Up @@ -123,7 +123,7 @@ function lookup(identifier, callback) {
if (err) return callback(err);
_fetchUserMeta(template, identifier, function (err, meta) {
if (err) return callback(err);
return callback(null, _parseUserMeta(meta));
return callback(null, meta);
});
});
}
Expand Down Expand Up @@ -172,66 +172,89 @@ function _fetchHostMeta(host, callback) {
Http.get(url, function(err, response, content) {
if (err) return callback(err);
if (response.statusCode == 200) {
var doc = Xml.parseFromString(content);
var nodes = doc.documentElement.selectNodes("descendant-or-self::node()[@rel='lrdd']");
callback(null, nodes[0].getAttribute("template"));
try {
callback(null, _parseHostMeta(content));
} catch (e) {
callback(e);
}
} else {
callback(new Error("Fetching host meta returned HTTP Status " + response.statusCode));
}
});
}

function _parseHostMeta(content) {
var template;
var parser = Sax.parser();
parser.onopentag = function(node) {
/* descendant-or-self::node()[@name='link' and @rel='lrdd']/@template */
if (node.name === 'LINK' &&
node.attributes.rel === 'lrdd' &&
node.attributes.template)
template = node.attributes.template;
};
parser.write(content).close();
return template;
}

/*
* Get a user XRD from a trmplate URI and an identifier.
* Get a user LRDD from a trmplate URI and an identifier.
*/
function _fetchUserMeta(template, identifier, callback) {
var url = template.replace("{uri}", identifier);
console.log("Requesting user meta at " + url);
Http.get(url, function(err, response, content) {
if (err) return callback(err);
if (response.statusCode == 200) {
var links = [];
var doc = Xml.parseFromString(content);
callback(null, doc);
try {
callback(null, _parseUserMeta(content));
} catch(e) {
callback(e);
}
} else {
callback(new Error("Fetching user meta returned HTTP Status " + response.statusCode));
}
});
}

/*
* Parse a user XRD into a JSON object.
* Parse a user LRDD into a JSON object.
*/
function _parseUserMeta(xml) {
function _parseUserMeta(content) {
var result = {
"alias": [],
"links": []
};

// Parse the subject
var subjects = xml.documentElement.getElementsByTagName("Subject");
if (subjects.length > 0) {
result.subject = subjects[0].nodeValue;
var parser = Sax.parser(), state;
parser.onopentag = function(node) {
switch(node.name) {
case 'SUBJECT':
state = 'subject';
break;
case 'LINK':
result.links.push(node.attributes);
break;
case 'ALIAS':
state = 'alias';
break;
}
}

// Parse the links
var links = xml.documentElement.getElementsByTagName("Link");
for (i=0;i<links.length;i++) {
var link = {};
var attributes = links[i].attributes;
if (att = attributes.getNamedItem("href")) link.href = att.nodeValue;
if (att = attributes.getNamedItem("ref")) link.ref = att.nodeValue;
if (att = attributes.getNamedItem("type")) link.type = att.nodeValue;
if (att = attributes.getNamedItem("rel")) link.rel = att.nodeValue;
result.links.push(link);
}

// Parse the aliases
var alias = xml.documentElement.getElementsByTagName("Alias");
for (i=0;i<alias.length;i++) {
result.alias.push(alias[i].nodeValue);
}

/* TODO: for multiple invokations, and w/ cdata */
parser.ontext = function(s) {
switch(state) {
case 'subject':
result.subject = s;
break;
case 'alias':
result.alias.push(s);
break;
}
};
parser.onclosetag = function() {
state = null;
};
parser.write(content).close();

return result;
}

Expand Down

0 comments on commit 4f2cb04

Please sign in to comment.