2008-04-30 16:55:34 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['HTTPPollingTransport'];
|
|
|
|
|
2008-04-30 16:27:32 -07:00
|
|
|
var Cc = Components.classes;
|
|
|
|
var Ci = Components.interfaces;
|
2008-06-18 14:59:06 -07:00
|
|
|
var Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://weave/log4moz.js");
|
2008-04-30 16:27:32 -07:00
|
|
|
|
2008-06-04 18:34:37 -07:00
|
|
|
/*
|
|
|
|
The interface that should be implemented by any Transport object:
|
|
|
|
|
|
|
|
send( messageXml );
|
|
|
|
setCallbackObject(object with .onIncomingData(aStringData) and .onTransportError(aErrorText) );
|
|
|
|
connect();
|
|
|
|
disconnect();
|
|
|
|
*/
|
|
|
|
|
2008-04-30 16:27:32 -07:00
|
|
|
function InputStreamBuffer() {
|
|
|
|
}
|
|
|
|
InputStreamBuffer.prototype = {
|
2008-06-04 12:14:28 -07:00
|
|
|
_data: "",
|
|
|
|
append: function( stuff ) {
|
2008-04-30 16:27:32 -07:00
|
|
|
this._data = this._data + stuff;
|
|
|
|
},
|
2008-06-04 12:14:28 -07:00
|
|
|
clear: function() {
|
2008-04-30 16:27:32 -07:00
|
|
|
this._data = "";
|
|
|
|
},
|
2008-06-04 12:14:28 -07:00
|
|
|
getData: function() {
|
2008-04-30 16:27:32 -07:00
|
|
|
return this._data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
/**
|
|
|
|
* A transport layer that uses raw sockets.
|
|
|
|
* Not recommended for use; currently fails when trying to negotiate
|
|
|
|
* TLS.
|
|
|
|
* Use HTTPPollingTransport instead.
|
|
|
|
*/
|
2008-04-30 16:27:32 -07:00
|
|
|
function SocketClient( host, port ) {
|
|
|
|
this._init( host, port );
|
|
|
|
}
|
|
|
|
SocketClient.prototype = {
|
|
|
|
__threadManager: null,
|
|
|
|
get _threadManager() {
|
|
|
|
if (!this.__threadManager)
|
|
|
|
this.__threadManager = Cc["@mozilla.org/thread-manager;1"].getService();
|
|
|
|
return this.__threadManager;
|
|
|
|
},
|
2008-06-04 12:14:28 -07:00
|
|
|
|
2008-04-30 16:27:32 -07:00
|
|
|
__transport: null,
|
|
|
|
get _transport() {
|
|
|
|
if (!this.__transport) {
|
|
|
|
var transportService = Cc["@mozilla.org/network/socket-transport-service;1"].getService(Ci.nsISocketTransportService);
|
|
|
|
this.__transport = transportService.createTransport(['starttls'],
|
|
|
|
1, // ssl only
|
|
|
|
this._host,
|
|
|
|
this._port,
|
|
|
|
null); // proxy
|
|
|
|
}
|
|
|
|
return this.__transport;
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
_init: function( host, port ) {
|
2008-04-30 16:27:32 -07:00
|
|
|
this._host = host;
|
|
|
|
this._port = port;
|
|
|
|
this._contentRead = "";
|
|
|
|
this._buffer = null;
|
|
|
|
this.connect();
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
connect: function() {
|
2008-04-30 16:27:32 -07:00
|
|
|
var outstream = this._transport.openOutputStream( 0, // flags
|
|
|
|
0, // buffer size
|
|
|
|
0 ); // number of buffers
|
|
|
|
this._outstream = outstream;
|
|
|
|
|
|
|
|
var buffer = new InputStreamBuffer;
|
|
|
|
this._buffer = buffer;
|
|
|
|
|
|
|
|
// Wrap input stream is C only, nonscriptable, so wrap it in scriptable
|
|
|
|
// stream:
|
|
|
|
var rawInputStream = this._transport.openInputStream( 0, 0, 0 );
|
|
|
|
var scriptablestream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
scriptablestream.init(rawInputStream);
|
|
|
|
|
|
|
|
// input stream pump for asynchronous reading
|
|
|
|
var pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance(Ci.nsIInputStreamPump);
|
|
|
|
pump.init(rawInputStream, -1, -1, 0, 0,
|
|
|
|
false); //automatically close once all data read?
|
|
|
|
|
|
|
|
// create dataListener class for callback:
|
|
|
|
var dataListener = {
|
2008-06-04 12:14:28 -07:00
|
|
|
data : "",
|
|
|
|
onStartRequest: function(request, context){
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
2008-06-04 12:14:28 -07:00
|
|
|
onStopRequest: function(request, context, status){
|
|
|
|
rawInputStream.close();
|
|
|
|
outstream.close();
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
2008-06-04 12:14:28 -07:00
|
|
|
onDataAvailable: function(request, context, inputStream, offset, count){
|
|
|
|
// use scriptable stream wrapper, not "real" stream.
|
|
|
|
// count is number of bytes available, offset is position in stream.
|
|
|
|
// Do stuff with data here!
|
|
|
|
buffer.append( scriptablestream.read( count ));
|
2008-04-30 16:27:32 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// register it:
|
|
|
|
pump.asyncRead(dataListener, null); // second argument is a context
|
|
|
|
//which gets passed in as the context argument to methods of dataListener
|
|
|
|
|
|
|
|
//Should be done connecting now. TODO: Catch and report errors.
|
|
|
|
},
|
|
|
|
|
|
|
|
send: function( messageText ) {
|
|
|
|
this._outstream.write( messageText, messageText.length );
|
|
|
|
},
|
|
|
|
|
|
|
|
getBinaryOutStream: function() {
|
|
|
|
var binaryOutStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIBinaryOutputStream);
|
|
|
|
binaryOutStream.setOutputStream( this._outstream ); // is this right?
|
|
|
|
return binaryOutStream;
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnect: function() {
|
|
|
|
var thread = this._threadManager.currentThread;
|
2008-06-04 12:14:28 -07:00
|
|
|
while ( thread.hasPendingEvents() ) {
|
|
|
|
thread.processNextEvent( true );
|
|
|
|
}
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
checkResponse: function() {
|
2008-04-30 16:27:32 -07:00
|
|
|
return this._getData();
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
waitForResponse: function() {
|
2008-04-30 16:27:32 -07:00
|
|
|
var thread = this._threadManager.currentThread;
|
2008-06-04 12:14:28 -07:00
|
|
|
while( this._buffer.getData().length == 0 ) {
|
|
|
|
thread.processNextEvent( true );
|
|
|
|
}
|
2008-04-30 16:27:32 -07:00
|
|
|
var output = this._buffer.getData();
|
|
|
|
this._buffer.clear();
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
startTLS: function() {
|
|
|
|
this._transport.securityInfo.QueryInterface(Ci.nsISSLSocketControl);
|
|
|
|
this._transport.securityInfo.StartTLS();
|
2008-06-04 12:14:28 -07:00
|
|
|
}
|
2008-04-30 16:27:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
/**
|
|
|
|
* Send HTTP requests periodically to the server using a timer.
|
|
|
|
* HTTP POST requests with content-type application/x-www-form-urlencoded.
|
|
|
|
* responses from the server have content-type text/xml
|
|
|
|
* request and response are UTF-8 encoded (ignore what HTTP header says)
|
|
|
|
* identify session by always using set-cookie header with cookie named ID
|
|
|
|
* first request sets this to 0 to indicate new session.
|
|
|
|
*/
|
2008-04-30 16:27:32 -07:00
|
|
|
function HTTPPollingTransport( serverUrl, useKeys, interval ) {
|
|
|
|
this._init( serverUrl, useKeys, interval );
|
|
|
|
}
|
|
|
|
HTTPPollingTransport.prototype = {
|
2008-06-04 12:14:28 -07:00
|
|
|
_init: function( serverUrl, useKeys, interval ) {
|
2008-06-18 14:50:02 -07:00
|
|
|
this._log = Log4Moz.Service.getLogger("Service.XmppTransportLayer");
|
|
|
|
this._log.info("Initializing transport: serverUrl=" + serverUrl + ", useKeys=" + useKeys + ", interval=" + interval);
|
2008-04-30 16:27:32 -07:00
|
|
|
this._serverUrl = serverUrl
|
|
|
|
this._n = 0;
|
|
|
|
this._key = this._makeSeed();
|
|
|
|
this._latestCookie = "";
|
|
|
|
this._connectionId = 0;
|
|
|
|
this._callbackObject = null;
|
|
|
|
this._useKeys = useKeys;
|
|
|
|
this._interval = interval;
|
2008-05-19 19:40:45 -07:00
|
|
|
this._outgoingRetryBuffer = "";
|
2008-06-04 18:34:37 -07:00
|
|
|
this._retryCount = 0;
|
|
|
|
this._retryCap = 0;
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
get _request() {
|
2008-06-24 17:45:37 -07:00
|
|
|
let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance( Ci.nsIXMLHttpRequest );
|
|
|
|
request.mozBackgroundRequest = true;
|
|
|
|
this.__defineGetter__("_request", function() request);
|
|
|
|
return this._request;
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
__hasher: null,
|
|
|
|
get _hasher() {
|
2008-04-30 16:27:32 -07:00
|
|
|
if (!this.__hasher)
|
|
|
|
this.__hasher = Cc["@mozilla.org/security/hash;1"].createInstance( Ci.nsICryptoHash );
|
|
|
|
return this.__hasher;
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
__timer: null,
|
|
|
|
get _timer() {
|
2008-04-30 16:27:32 -07:00
|
|
|
if (!this.__timer)
|
|
|
|
this.__timer = Cc["@mozilla.org/timer;1"].createInstance( Ci.nsITimer );
|
|
|
|
return this.__timer;
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
_makeSeed: function() {
|
2008-04-30 16:27:32 -07:00
|
|
|
return "foo";//"MyKeyOfHorrors";
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
_advanceKey: function() {
|
|
|
|
var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
|
|
|
createInstance(Ci.nsIScriptableUnicodeConverter);
|
2008-04-30 16:27:32 -07:00
|
|
|
|
|
|
|
// we use UTF-8 here, you can choose other encodings.
|
2008-06-04 12:14:28 -07:00
|
|
|
// TODO make configurable
|
2008-04-30 16:27:32 -07:00
|
|
|
converter.charset = "UTF-8";
|
|
|
|
// result is an out parameter,
|
|
|
|
// result.value will contain the array length
|
|
|
|
var result = {};
|
|
|
|
// data is an array of bytes
|
2008-06-04 12:14:28 -07:00
|
|
|
var data = converter.convertToByteArray(this._key, result);
|
2008-04-30 16:27:32 -07:00
|
|
|
|
|
|
|
this._n += 1;
|
|
|
|
this._hasher.initWithString( "SHA1" );
|
|
|
|
this._hasher.update( data, data.length );
|
|
|
|
this._key = this._hasher.finish( true ); // true means B64encode
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
_setIdFromCookie: function( self, cookie ) {
|
|
|
|
// parse connection ID out of the cookie:
|
2008-04-30 16:27:32 -07:00
|
|
|
var cookieSegments = cookie.split( ";" );
|
|
|
|
cookieSegments = cookieSegments[0].split( "=" );
|
|
|
|
var newConnectionId = cookieSegments[1];
|
|
|
|
switch( newConnectionId) {
|
|
|
|
case "0:0":
|
|
|
|
self._onError( "Unknown error!\n" );
|
|
|
|
break;
|
|
|
|
case "-1:0":
|
|
|
|
self._onError( "Server error!\n" );
|
|
|
|
break;
|
|
|
|
case "-2:0":
|
|
|
|
self._onError( "Bad request!\n" );
|
|
|
|
break;
|
|
|
|
case "-3:0":
|
|
|
|
self._onError( "Key sequence error!\n" );
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
self._connectionId = cookieSegments[1];
|
2008-06-18 17:23:04 -07:00
|
|
|
this._log.debug("Connection ID set to " + self._connectionId);
|
2008-04-30 16:27:32 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
_onError: function( errorText ) {
|
2008-06-18 17:23:04 -07:00
|
|
|
this._log.error( errorText );
|
2008-04-30 16:27:32 -07:00
|
|
|
if ( this._callbackObject != null ) {
|
|
|
|
this._callbackObject.onTransportError( errorText );
|
|
|
|
}
|
|
|
|
this.disconnect();
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
_doPost: function( requestXml ) {
|
|
|
|
var request = this._request;
|
2008-04-30 16:27:32 -07:00
|
|
|
var callbackObj = this._callbackObject;
|
|
|
|
var self = this;
|
|
|
|
var contents = "";
|
|
|
|
|
|
|
|
if ( this._useKey ) {
|
|
|
|
this._advanceKey();
|
|
|
|
contents = this._connectionId + ";" + this._key + "," + requestXml;
|
|
|
|
} else {
|
|
|
|
contents = this._connectionId + "," + requestXml;
|
|
|
|
/* TODO:
|
2008-06-04 12:14:28 -07:00
|
|
|
Currently I get a "-3:0" error (key sequence error) from the 2nd
|
|
|
|
exchange if using the keys is enabled. */
|
2008-04-30 16:27:32 -07:00
|
|
|
}
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
var _processReqChange = function() {
|
|
|
|
// Callback for XMLHTTPRequest object state change messages
|
2008-04-30 16:27:32 -07:00
|
|
|
if ( request.readyState == 4 ) {
|
2008-06-04 12:14:28 -07:00
|
|
|
if ( request.status == 200) {
|
|
|
|
// 200 means success.
|
|
|
|
|
2008-06-18 14:50:02 -07:00
|
|
|
self._log.debug("Server says: " + request.responseText);
|
2008-06-04 12:14:28 -07:00
|
|
|
// Look for a set-cookie header:
|
|
|
|
var latestCookie = request.getResponseHeader( "Set-Cookie" );
|
|
|
|
if ( latestCookie.length > 0 ) {
|
|
|
|
self._setIdFromCookie( self, latestCookie );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond to any text we get back from the server in response
|
|
|
|
if ( callbackObj != null && request.responseText.length > 0 ) {
|
|
|
|
callbackObj.onIncomingData( request.responseText );
|
|
|
|
}
|
|
|
|
} else {
|
2008-06-18 14:50:02 -07:00
|
|
|
self._log.error( "Got HTTP status code " + request.status );
|
2008-06-04 12:14:28 -07:00
|
|
|
if ( request.status == 0 ) {
|
|
|
|
/* Sometimes the server gives us HTTP status code 0 in response
|
|
|
|
to an attempt to POST. I'm not sure why this happens, but
|
|
|
|
if we re-send the POST it seems to usually work the second
|
|
|
|
time. So put the message into a buffer and try again later:
|
|
|
|
*/
|
2008-06-04 18:34:37 -07:00
|
|
|
if (self._retryCount >= self._retryCap) {
|
|
|
|
self._onError("Maximum number of retries reached. Unable to communicate with the server.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self._outgoingRetryBuffer = requestXml;
|
|
|
|
self._retryCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (request.status == 404) {
|
|
|
|
self._onError("Provided URL is not valid.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self._onError("Unable to communicate with the server.");
|
2008-06-04 12:14:28 -07:00
|
|
|
}
|
|
|
|
}
|
2008-04-30 16:27:32 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-06-04 18:34:37 -07:00
|
|
|
try {
|
|
|
|
request.open( "POST", this._serverUrl, true ); //async = true
|
|
|
|
request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded;charset=UTF-8" );
|
|
|
|
request.setRequestHeader( "Content-length", contents.length );
|
|
|
|
request.setRequestHeader( "Connection", "close" );
|
|
|
|
request.onreadystatechange = _processReqChange;
|
2008-06-18 14:50:02 -07:00
|
|
|
this._log.debug("Sending: " + contents);
|
2008-06-04 18:34:37 -07:00
|
|
|
request.send( contents );
|
|
|
|
} catch(ex) {
|
|
|
|
this._onError("Unable to send message to server: " + this._serverUrl);
|
2008-06-18 14:50:02 -07:00
|
|
|
this._log.error("Connection failure: " + ex);
|
2008-06-04 18:34:37 -07:00
|
|
|
}
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
send: function( messageXml ) {
|
2008-04-30 16:27:32 -07:00
|
|
|
this._doPost( messageXml );
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
setCallbackObject: function( callbackObject ) {
|
2008-06-04 17:00:02 -07:00
|
|
|
this._callbackObject = callbackObject;
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
notify: function( timer ) {
|
2008-04-30 16:27:32 -07:00
|
|
|
/* having a notify method makes this object satisfy the nsITimerCallback
|
|
|
|
interface, so the object can be passed to timer.initWithCallback. */
|
|
|
|
|
2008-05-19 19:40:45 -07:00
|
|
|
/* With HTTPPolling, we need to be contacting the server on a regular
|
|
|
|
heartbeat, even if we don't have anything to send, just to see if
|
|
|
|
the server has any data it's waiting to give us.
|
|
|
|
If we have a message waiting in the outgoingRetryBuffer, send that;
|
|
|
|
otherwise send nothing. */
|
|
|
|
var outgoingMsg = this._outgoingRetryBuffer
|
|
|
|
this._outgoingRetryBuffer = "";
|
|
|
|
this._doPost( outgoingMsg );
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
connect: function() {
|
2008-06-04 17:00:02 -07:00
|
|
|
// In case this is a reconnect, make sure to re-initialize.
|
|
|
|
this._init(this._serverUrl, this._useKeys, this._interval);
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
/* Set up a timer to poll the server periodically. */
|
2008-04-30 16:27:32 -07:00
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
// TODO doPost isn't reentrant; don't try to doPost if there's
|
|
|
|
//already a post in progress... or can that never happen?
|
2008-04-30 16:27:32 -07:00
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
this._timer.initWithCallback( this,
|
|
|
|
this._interval,
|
|
|
|
this._timer.TYPE_REPEATING_SLACK );
|
2008-04-30 16:27:32 -07:00
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
disconnect: function () {
|
|
|
|
this._request.abort();
|
2008-04-30 16:27:32 -07:00
|
|
|
this._timer.cancel();
|
|
|
|
},
|
|
|
|
|
2008-06-04 12:14:28 -07:00
|
|
|
testKeys: function () {
|
2008-04-30 16:27:32 -07:00
|
|
|
this._key = "foo";
|
2008-06-18 14:50:02 -07:00
|
|
|
this._log.debug(this._key);
|
2008-04-30 16:27:32 -07:00
|
|
|
for ( var x = 1; x < 7; x++ ) {
|
|
|
|
this._advanceKey();
|
2008-06-18 14:50:02 -07:00
|
|
|
this._log.debug(this._key);
|
2008-04-30 16:27:32 -07:00
|
|
|
}
|
2008-06-04 12:14:28 -07:00
|
|
|
}
|
2008-04-30 16:27:32 -07:00
|
|
|
};
|