Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use open/close tags #128

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Note: The **=** sign is important here. Replacing the equal sign with a space wi

* **websocket_ping_interval**: The period, in seconds, between sending WebSocket ping frames to each client. If a client fails to respond with a pong frame twice in a row, the connection will be closed. Set to 0 to disable sending of WebSocket pings. **(default: 30)**

* **use_stream_tags**: Set to 'true' to enable the use of legacy `<stream>` tags instead of `<open>` and `<close>`. You may need this if your client is not up to date with the spec. **(default: false)**

### Architecture

The project itself is divided into 4 main components as of now.
Expand Down Expand Up @@ -238,7 +240,7 @@ if the *callback=* GET query parameter is supplied.

* [http://xmpp.org/extensions/xep-0124.html](http://xmpp.org/extensions/xep-0124.html)
* [http://xmpp.org/extensions/xep-0206.html](http://xmpp.org/extensions/xep-0206.html)
* [http://tools.ietf.org/html/draft-moffitt-xmpp-over-websocket-00](http://tools.ietf.org/html/draft-moffitt-xmpp-over-websocket-00)
* [https://tools.ietf.org/html/draft-ietf-xmpp-websocket-10](https://tools.ietf.org/html/draft-ietf-xmpp-websocket-10)


### Dependencies
Expand Down Expand Up @@ -278,6 +280,7 @@ if the *callback=* GET query parameter is supplied.
4. libpurple (pidgin as a client)
5. [strophe.js websocket client] (https://github.com/superfeedr/strophejs/tree/protocol-ed) [broken link]
6. [node-xmpp] (https://github.com/astro/node-xmpp)
7. [stanza.io] (https://github.com/otalk/stanza.io)


### Tested using
Expand Down
5 changes: 4 additions & 1 deletion bosh.conf.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,8 @@ exports.config = {
// client. If a client fails to respond with a pong frame twice in a row,
// the connection will be closed.
// Set to 0 to disable sending of WebSocket pings.
websocket_ping_interval: 30
websocket_ping_interval: 30,

// Set to true to use legacy <stream> tags instead of <open> and <close>
use_stream_tags: false,
};
28 changes: 20 additions & 8 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var STREAM_OPENED = 2;
var STREAM_CLOSED = 3;

var XML_STREAM_CLOSE = '</stream:stream>';
var XML_CLOSE_TAG = '<close xmlns="urn:ietf:params:xml:ns:xmpp-framing" />';

//
// Important links:
Expand All @@ -61,6 +62,7 @@ exports.createServer = function(bosh_server, options, webSocket) {

// Config options
var ping_interval = options.websocket_ping_interval;
var use_stream_tags = !!options.use_stream_tags;

// State information for XMPP streams
var sn_state = { };
Expand Down Expand Up @@ -104,13 +106,17 @@ exports.createServer = function(bosh_server, options, webSocket) {

wsep.on('stream-added', function(sstate) {
var to = sstate.to || '';
var ss_xml = new ltx.Element('stream:stream', {
'xmlns': 'jabber:client',
'xmlns:stream': 'http://etherx.jabber.org/streams',
var tagName = use_stream_tags ? 'stream:stream' : 'open';
var attributes = {
'xmlns': use_stream_tags ? 'jabber:client' : 'urn:ietf:params:xml:ns:xmpp-framing',
'version': '1.0',
'xml:lang': 'en',
'from': to
}).toString();
}
if(use_stream_tags) {
attributes['xmlns:stream'] = 'http://etherx.jabber.org/streams'
}
var ss_xml = new ltx.Element(tagName, attributes).toString();
if (sstate.has_open_stream_tag) {
ss_xml = ss_xml.replace('/>', '>');
}
Expand All @@ -125,6 +131,12 @@ exports.createServer = function(bosh_server, options, webSocket) {
if (sstate.has_open_stream_tag) {
ss_xml = ss_xml.replace('/>', '>');
}

if(!use_stream_tags) {
ss_xml = ss_xml.replace('<stream:stream', '<open');
ss_xml = ss_xml.replace('xmlns="jabber:client"', 'xmlns="urn:ietf:params:xml:ns:xmpp-framing"');
}

log.trace("%s sending stream:stream tag on stream restart: %s", sstate.name, ss_xml);
wsep.emit('response', ss_xml, sstate);
});
Expand Down Expand Up @@ -234,7 +246,7 @@ exports.createServer = function(bosh_server, options, webSocket) {
message += XML_STREAM_CLOSE;
sstate.has_open_stream_tag = true;
}
} else if (message.indexOf(XML_STREAM_CLOSE) !== -1) {
} else if (message.indexOf(XML_STREAM_CLOSE) !== -1 || message.indexOf('<close') !== -1) {
// Stream close message from a client must appear in a message
// by itself - see draft-moffitt-xmpp-over-websocket-02
if (sstate.stream_state === STREAM_CLOSED) {
Expand All @@ -254,7 +266,7 @@ exports.createServer = function(bosh_server, options, webSocket) {
} else {
// Raise the stream-terminate event on wsep
wsep.emit('stream-terminate', sstate);
wsep.emit('response', XML_STREAM_CLOSE, sstate);
wsep.emit('response', use_stream_tags ? XML_STREAM_CLOSE : XML_CLOSE_TAG, sstate);
sstate.terminated = true;
}
return;
Expand All @@ -280,13 +292,13 @@ exports.createServer = function(bosh_server, options, webSocket) {
// The stream start node is special since we trigger a
// stream-add event when we get it.
var ss_node = nodes.filter(function(node) {
return typeof node.is === 'function' && node.is('stream');
return typeof node.is === 'function' && (node.is('stream') || node.is('open'));
});

ss_node = us.first(ss_node);

nodes = nodes.filter(function(node) {
return typeof node.is === 'function' ? !node.is('stream') : true;
return typeof node.is === 'function' ? !(node.is('stream') || node.is('open')) : true;
});

if (ss_node) {
Expand Down