📄 nsxmlrpcclient.js
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla XML-RPC Client component. * * The Initial Developer of the Original Code is * Digital Creations 2, Inc. * Portions created by the Initial Developer are Copyright (C) 2000 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Martijn Pieters <mj@digicool.com> (original author) * Samuel Sieb <samuel@sieb.net> * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** *//* * nsXmlRpcClient XPCOM component * Version: $Revision: 1.34.28.3 $ * * $Id: nsXmlRpcClient.js,v 1.34.28.3 2006/11/28 18:18:44 gijskruitbosch%gmail.com Exp $ *//* * Constants */const XMLRPCCLIENT_CONTRACTID = '@mozilla.org/xml-rpc/client;1';const XMLRPCCLIENT_CID = Components.ID('{4d7d15c0-3747-4f7f-b6b3-792a5ea1a9aa}');const XMLRPCCLIENT_IID = Components.interfaces.nsIXmlRpcClient;const XMLRPCFAULT_CONTRACTID = '@mozilla.org/xml-rpc/fault;1';const XMLRPCFAULT_CID = Components.ID('{691cb864-0a7e-448c-98ee-4a7f359cf145}');const XMLRPCFAULT_IID = Components.interfaces.nsIXmlRpcFault;const XMLHTTPREQUEST_CONTRACTID = '@mozilla.org/xmlextras/xmlhttprequest;1';const NSICHANNEL = Components.interfaces.nsIChannel;const DEBUG = false;const DEBUGPARSE = false;const DOMNode = Components.interfaces.nsIDOMNode;/* * Class definitions *//* The nsXmlRpcFault class constructor. */function nsXmlRpcFault() {}/* the nsXmlRpcFault class def */nsXmlRpcFault.prototype = { faultCode: 0, faultString: '', init: function(faultCode, faultString) { this.faultCode = faultCode; this.faultString = faultString; }, toString: function() { return '<XML-RPC Fault: (' + this.faultCode + ') ' + this.faultString + '>'; }, // nsISupports interface QueryInterface: function(iid) { if (!iid.equals(Components.interfaces.nsISupports) && !iid.equals(XMLRPCFAULT_IID)) throw Components.results.NS_ERROR_NO_INTERFACE; return this; }};/* The nsXmlRpcClient class constructor. */function nsXmlRpcClient() {}/* the nsXmlRpcClient class def */nsXmlRpcClient.prototype = { _serverUrl: null, _useAuth: false, init: function(serverURL) { this._serverUrl = serverURL; this._encoding = "UTF-8"; }, setAuthentication: function(username, password){ if ((typeof username == "string") && (typeof password == "string")){ this._useAuth = true; this._username = username; this._password = password; } }, clearAuthentication: function(){ this._useAuth = false; }, setEncoding: function(encoding){ this._encoding = encoding; }, get serverUrl() { return this._serverUrl; }, // Internal copy of the status _status: null, _listener: null, asyncCall: function(listener, context, methodName, methodArgs, count) { debug('asyncCall'); // Check for call in progress. if (this._inProgress) throw Components.Exception('Call in progress!'); // Check for the server URL; if (!this._serverUrl) throw Components.Exception('Not initialized'); this._inProgress = true; // Clear state. this._foundFault = false; this._passwordTried = false; this._result = null; this._fault = null; this._status = null; this._responseStatus = null; this._responseString = null; this._listener = listener; this._seenStart = false; this._context = context; debug('Arguments: ' + methodArgs); // Generate request body var xmlWriter = new XMLWriter(this._encoding); this._generateRequestBody(xmlWriter, methodName, methodArgs); var requestBody = xmlWriter.data; debug('Request: ' + requestBody); this.xmlhttp = Components.classes[XMLHTTPREQUEST_CONTRACTID] .createInstance(Components.interfaces.nsIXMLHttpRequest); if (this._useAuth) { this.xmlhttp.open('POST', this._serverUrl, true, this._username, this._password); } else { this.xmlhttp.open('POST', this._serverUrl); } this.xmlhttp.onload = this._onload; this.xmlhttp.onerror = this._onerror; this.xmlhttp.parent = this; this.xmlhttp.setRequestHeader('Content-Type','text/xml'); this.xmlhttp.send(requestBody); var chan = this.xmlhttp.channel.QueryInterface(NSICHANNEL); chan.notificationCallbacks = this; }, _onload: function(e) { var result; var parent = e.target.parent; parent._inProgress = false; parent._responseStatus = e.target.status; parent._responseString = e.target.statusText; if (!e.target.responseXML) { if (e.target.status) { try { parent._listener.onError(parent, parent._context, Components.results.NS_ERROR_FAILURE, 'Server returned status ' + e.target.status); } catch (ex) { debug('Exception in listener.onError: ' + ex); } } else { try { parent._listener.onError(parent, parent._context, Components.results.NS_ERROR_FAILURE, 'Unknown network error'); } catch (ex) { debug('Exception in listener.onError: ' + ex); } } return; } try { e.target.responseXML.normalize(); result = parent.parse(e.target.responseXML); } catch (ex) { try { parent._listener.onError(parent, parent._context, ex.result, ex.message); } catch (ex) { debug('Exception in listener.onError: ' + ex); } return; } if (parent._foundFault) { parent._fault = result; try { parent._listener.onFault(parent, parent._context, result); } catch(ex) { debug('Exception in listener.onFault: ' + ex); } } else { parent._result = result.value; try { parent._listener.onResult(parent, parent._context, result.value); } catch (ex) { debug('Exception in listener.onResult: ' + ex); } } }, _onerror: function(e) { var parent = e.target.parent; parent._inProgress = false; try { parent._listener.onError(parent, parent._context, Components.results.NS_ERROR_FAILURE, 'Unknown network error'); } catch (ex) { debug('Exception in listener.onError: ' + ex); } }, _foundFault: false, _fault: null, _result: null, _responseStatus: null, _responseString: null, get fault() { return this._fault; }, get result() { return this._result; }, get responseStatus() { return this._responseStatus; }, get responseString() { return this._responseString; }, /* Convenience. Create an appropriate XPCOM object for a given type */ INT: 1, BOOLEAN: 2, STRING: 3, DOUBLE: 4, DATETIME: 5, ARRAY: 6, STRUCT: 7, BASE64: 8, // Not part of nsIXmlRpcClient interface, internal use. createType: function(type, uuid) { const SUPPORTSID = '@mozilla.org/supports-'; switch(type) { case this.INT: uuid.value = Components.interfaces.nsISupportsPRInt32 return createInstance(SUPPORTSID + 'PRInt32;1', 'nsISupportsPRInt32'); case this.BOOLEAN: uuid.value = Components.interfaces.nsISupportsPRBool return createInstance(SUPPORTSID + 'PRBool;1', 'nsISupportsPRBool'); case this.STRING: uuid.value = Components.interfaces.nsISupportsCString return createInstance(SUPPORTSID + 'cstring;1', 'nsISupportsCString'); case this.DOUBLE: uuid.value = Components.interfaces.nsISupportsDouble return createInstance(SUPPORTSID + 'double;1', 'nsISupportsDouble'); case this.DATETIME: uuid.value = Components.interfaces.nsISupportsPRTime return createInstance(SUPPORTSID + 'PRTime;1', 'nsISupportsPRTime'); case this.ARRAY: uuid.value = Components.interfaces.nsISupportsArray return createInstance(SUPPORTSID + 'array;1', 'nsISupportsArray'); case this.STRUCT: uuid.value = Components.interfaces.nsIDictionary return createInstance('@mozilla.org/dictionary;1', 'nsIDictionary'); default: throw Components.Exception('Unsupported type'); } }, // nsISupports interface QueryInterface: function(iid) { if (!iid.equals(Components.interfaces.nsISupports) && !iid.equals(XMLRPCCLIENT_IID) && !iid.equals(Components.interfaces.nsIInterfaceRequestor)) throw Components.results.NS_ERROR_NO_INTERFACE; return this; }, // nsIInterfaceRequester interface getInterface: function(iid, result){ if (iid.equals(Components.interfaces.nsIAuthPrompt)){ return this; } Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE; return null; }, // nsIAuthPrompt interface _passwordTried: false, promptUsernameAndPassword: function(dialogTitle, text, passwordRealm, savePassword, user, pwd){ if (this._useAuth){ if (this._passwordTried){ return false; } user.value = this._username; pwd.value = this._password; this._passwordTried = true; return true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -