B2G/gaia/apps/email/js/ext/mailapi/activesync/protocollayer.js
/**
   * Perform the actual autodiscovery process for a given URL.
   *
   * @param aHost the host name to attempt autodiscovery for
   * @param aEmailAddress the user's email address
   * @param aPassword the user's password
   * @param aTimeout a timeout (in milliseconds) for the request
   * @param aNoRedirect true if autodiscovery should *not* follow any
   *        specified redirects (typically used when autodiscover has already
   *        told us about a redirect)
   * @param aCallback a callback taking an error status (if any) and the
   *        server's configuration
   */
Perform the actual autodiscovery process for a given URL.
Perform the actual autodiscovery process for a given URL .
function do_autodiscover(aHost, aEmailAddress, aPassword, aTimeout,
                           aNoRedirect, aCallback) {
    var xhr = new XMLHttpRequest({mozSystem: true, mozAnon: true});
    xhr.open('POST', 'https://' + aHost + '/autodiscover/autodiscover.xml',
             true);
    setAuthHeader(xhr, aEmailAddress, aPassword);
    xhr.setRequestHeader('Content-Type', 'text/xml');
    xhr.timeout = aTimeout;
    xhr.upload.onprogress = xhr.upload.onload = function() {
      xhr.timeout = 0;
    };
    xhr.onload = function() {
      if (xhr.status < 200 || xhr.status >= 300)
        return aCallback(new HttpError(xhr.statusText, xhr.status));
      var uid = Math.random();
      self.postMessage({
        uid: uid,
        type: 'configparser',
        cmd: 'accountactivesync',
        args: [xhr.responseText]
      });
      self.addEventListener('message', function onworkerresponse(evt) {
        var data = evt.data;
        if (data.type != 'configparser' || data.cmd != 'accountactivesync' ||
            data.uid != uid) {
          return;
        }
        self.removeEventListener(evt.type, onworkerresponse);
        var args = data.args;
        var config = args[0], error = args[1], redirectedEmail = args[2];
        if (error) {
          aCallback(new AutodiscoverDomainError(error), config);
        } else if (redirectedEmail) {
          autodiscover(redirectedEmail, aPassword, aTimeout, aCallback, true);
        } else {
          aCallback(null, config);
        }
      });
    };
    xhr.ontimeout = xhr.onerror = function() {
      aCallback(new Error('Error getting Autodiscover URL'));
    };
    var postdata =
    '<?xml version="1.0" encoding="utf-8"?>\n' +
    '<Autodiscover xmlns="' + nsResolver('rq') + '">\n' +
    '  <Request>\n' +
    '    <EMailAddress>' + aEmailAddress + '</EMailAddress>\n' +
    '    <AcceptableResponseSchema>' + nsResolver('ms') +
         '</AcceptableResponseSchema>\n' +
    '  </Request>\n' +
    '</Autodiscover>';
    xhr.send(postdata);
  }
function do_autodiscover ( aHost , aEmailAddress , aPassword , aTimeout , aNoRedirect , aCallback ) { var xhr = new XMLHttpRequest ( { mozSystem : true , mozAnon : true } ) ; xhr . open ( 'POST' , 'https://' + aHost + '/autodiscover/autodiscover.xml' , true ) ; setAuthHeader ( xhr , aEmailAddress , aPassword ) ; xhr . setRequestHeader ( 'Content-Type' , 'text/xml' ) ; xhr . timeout = aTimeout ; xhr . upload . onprogress = xhr . upload . onload = function ( ) { xhr . timeout = 0 ; } ; xhr . onload = function ( ) { if ( xhr . status < 200 || xhr . status >= 300 ) return aCallback ( new HttpError ( xhr . statusText , xhr . status ) ) ; var uid = Math . random ( ) ; self . postMessage ( { uid : uid , type : 'configparser' , cmd : 'accountactivesync' , args : [ xhr . responseText ] } ) ; self . addEventListener ( 'message' , function onworkerresponse ( evt ) { var data = evt . data ; if ( data . type != 'configparser' || data . cmd != 'accountactivesync' || data . uid != uid ) { return ; } self . removeEventListener ( evt . type , onworkerresponse ) ; var args = data . args ; var config = args [ 0 ] , error = args [ 1 ] , redirectedEmail = args [ 2 ] ; if ( error ) { aCallback ( new AutodiscoverDomainError ( error ) , config ) ; } else if ( redirectedEmail ) { autodiscover ( redirectedEmail , aPassword , aTimeout , aCallback , true ) ; } else { aCallback ( null , config ) ; } } ) ; } ; xhr . ontimeout = xhr . onerror = function ( ) { aCallback ( new Error ( 'Error getting Autodiscover URL' ) ) ; } ; var postdata = '<?xml version="1.0" encoding="utf-8"?>\n' + '<Autodiscover xmlns="' + nsResolver ( 'rq' ) + '">\n' + '  <Request>\n' + '    <EMailAddress>' + aEmailAddress + '</EMailAddress>\n' + '    <AcceptableResponseSchema>' + nsResolver ( 'ms' ) + '</AcceptableResponseSchema>\n' + '  </Request>\n' + '</Autodiscover>' ; xhr . send ( postdata ) ; }
Perform the actual autodiscovery process for a given URL.
Perform the actual autodiscovery process for a given URL .
// TODO: use something like // http://ejohn.org/blog/javascript-micro-templating/ here?