[Pkg-javascript-commits] [ltx] 76/469: unify connection ending & errors

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:01:04 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit 9650a9beba0fd39727bdcbc2be8d27fd1c0963ed
Author: Astro <astro at spaceboyz.net>
Date:   Sun Sep 5 23:03:16 2010 +0200

    unify connection ending & errors
---
 lib/xmpp/connection.js | 50 +++++++++++++++++++++++++++++++++++++++++++++-----
 lib/xmpp/router.js     | 35 ++++++++++-------------------------
 lib/xmpp/server.js     | 10 ----------
 3 files changed, 55 insertions(+), 40 deletions(-)

diff --git a/lib/xmpp/connection.js b/lib/xmpp/connection.js
index aed4ac8..c8da07c 100644
--- a/lib/xmpp/connection.js
+++ b/lib/xmpp/connection.js
@@ -41,7 +41,8 @@ function initConnection(conn) {
     conn.charset = 'UTF-8';
 
     conn.addListener('data', conn.onData);
-    conn.addListener('close', conn.onClose);
+    conn.addListener('end', conn.onEnd);
+    conn.addListener('error', conn.onEnd);
 }
 
 /** Climbs the stanza up if a child was passed,
@@ -118,6 +119,11 @@ Connection.prototype.startParser = function() {
     });
 };
 
+Connection.prototype.stopParser = function() {
+    delete this.element;
+    delete this.parser;
+};
+
 Connection.prototype.startStream = function() {
     this.startParser();
 };
@@ -180,9 +186,43 @@ Connection.prototype.rmStreamNs = function(stanza) {
 
 
 /**
- * 
+ * Connection has been ended by remote, we will not get any incoming
+ * 'data' events. Alternatively, used for 'error' event.
+ *
+ * We don't deal with half-closed connections and end our half too.
  */
-Connection.prototype.onClose = function() {
-    delete this.element;
-    delete this.parser;
+Connection.prototype.onEnd = function() {
+    this.stopParser();
+    this.end();
+};
+
+/**
+ * XMPP-style end connection for user
+ */
+Connection.prototype.end = function() {
+    if (this.writable)
+	this.send('</stream:stream>');
+    net.Stream.prototype.end.call(this);
+    // stopParser will called on 'end'/'error' event
+};
+
+/**
+ * End connection with stream error
+ *
+ * @param {String} condition XMPP error condition, see RFC3920 4.7.3. Defined Conditions
+ * @param {String} text Optional error message
+ */
+Connection.prototype.error = function(condition, text) {
+    if (!this.writable)
+	return;
+
+    var e = new xml.Element('stream:error');
+    e.c(condition, { xmlns: NS_XMPP_STREAMS });
+    if (text)
+	e.c('text', { xmlns: NS_XMPP_STREAMS,
+		      'xml:lang': 'en' }).
+	t(text);
+
+    this.send(e);
+    this.end();
 };
diff --git a/lib/xmpp/router.js b/lib/xmpp/router.js
index 89099d0..5c9a84c 100644
--- a/lib/xmpp/router.js
+++ b/lib/xmpp/router.js
@@ -60,7 +60,7 @@ DomainContext.prototype.sendRaw = function(stanza, destDomain) {
 
     var outStream = this.getOutStream(destDomain);
     var send = function() {
-	outStream.send(stanza)
+	outStream.send(stanza);
     };
 
     if (outStream.isOnline)
@@ -108,7 +108,9 @@ DomainContext.prototype.getOutStream = function(domain) {
 		    delete outStream.queue;
 		}
 	    } else {
-		outStream.emit('error', new Error('Dialback failure'));
+		// we cannot do anything else with this stream that
+		// failed dialback
+		outStream.end();
 	    }
 	});
 
@@ -125,11 +127,7 @@ DomainContext.prototype.addInStream = function(domain, stream) {
     if (this.s2sIn.hasOwnProperty(domain)) {
 	// Replace old
 	var oldStream = this.s2sIn[domain];
-	if (oldStream.writable) {
-	    oldStream.send(Server.error('conflict', 'Connection replaced'));
-	    oldStream.send('</stream:stream>');
-	}
-	oldStream.end();
+	oldStream.error('conflict', 'Connection replaced');
     }
 
     this.setupStream(domain, stream);
@@ -152,36 +150,24 @@ DomainContext.prototype.setupStream = function(domain, stream) {
 
 	if (!(typeof stanza.attrs.from === 'string' &&
 	      typeof stanza.attrs.to === 'string')) {
-	    stream.send(Server.error('improper-addressing'));
-	    stream.emit('error', new Error('improper-addressing'));
+	    stream.error('improper-addressing');
 	    return;
 	}
 
 	var fromDomain = (new JID.JID(stanza.attrs.from)).domain;
 	if (fromDomain !== domain) {
-	    stream.send(Server.error('invalid-from'));
-	    stream.emit('error', new Error('invalid-from'));
+	    stream.error('invalid-from');
 	    return;
 	}
 
 	var toDomain = (new JID.JID(stanza.attrs.to)).domain;
 	if (toDomain !== self.domain) {
-	    stream.send(Server.error('improper-addressing'));
-	    stream.emit('error', new Error('improper-addressing'));
+	    stream.error('improper-addressing');
 	    return;
 	}
 
 	self.receive(stanza);
     });
-    // We won't deal with half-closed connections
-    stream.addListener('end', function() {
-	stream.end();
-    });
-    stream.addListener('error', function() {
-	if (stream.writable)
-	    stream.send('</stream:stream>');
-	stream.end();
-    });
 };
 
 DomainContext.prototype.verifyDialback = function(domain, id, key) {
@@ -210,6 +196,7 @@ DomainContext.prototype.receive = function(stanza) {
  * * timeouts
  * * parser errors
  * * keepAlive
+ * * TLS
  */
 function Router(s2sPort) {
     var self = this;
@@ -270,9 +257,7 @@ Router.prototype.acceptConnection = function(inStream) {
 	    outStream.addListener('dialbackVerified', onVerified);
 	    outStream.addListener('close', onClose);
 	} else {
-	    inStream.send(Server.error('host-unknown', to + ' is not served here'));
-	    inStream.send('</stream:stream>');
-	    inStream.end();
+	    inStream.error('host-unknown', to + ' is not served here');
 	}
     });
 };
diff --git a/lib/xmpp/server.js b/lib/xmpp/server.js
index 83bd26c..4fd2a2f 100644
--- a/lib/xmpp/server.js
+++ b/lib/xmpp/server.js
@@ -151,13 +151,3 @@ function generateId() {
     }
     return r.toString();
 };
-
-exports.error = function(condition, text) {
-    var e = new xml.Element('stream:error');
-    e.c(condition, { xmlns: NS_XMPP_STREAMS });
-    if (text)
-	e.c('text', { xmlns: NS_XMPP_STREAMS,
-		      'xml:lang': 'en' }).
-	t(text);
-    return e;
-};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list