[Slashhack-cvs] slashhack/resources/js/jslib/network filePostRequest.js,NONE,1.1 getRequest.js,NONE,
Brought to you by:
fletch
Update of /cvsroot/slashhack/slashhack/resources/js/jslib/network In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4907/network Added Files: filePostRequest.js getRequest.js http.js multipartRequest.js postRequest.js remoteFile.js socket.js Log Message: Task #106454 - include jslib. --- NEW FILE: http.js --- /*** -*- Mode: Javascript; tab-width: 2; 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 jslib code. The Initial Developer of the Original Code is jslib team. Portions created by jslib team are Copyright (C) 2000 jslib team. All Rights Reserved. Contributor(s): Rajeev J Sebastian <raj...@ya...)> (original author) *************************************/ if (typeof(JS_LIB_LOADED)=='boolean') { const JS_HTTP_FILE = "http.js"; const JS_HTTP_LOADED = true; function HTTP() { } HTTP.prototype = { response:null, status:null, doOperation: function (req) { p = new XMLHttpRequest(); p.onload = null; p.open(req.getRequestMethod(), req.getRequestUri(), false); var s = req.getBody(p); req.setRequestHeaders(p); p.send(s); // since this is sync request, we get results after send() this.status = p.status; if ( this.status != "200" ) { return false; } else { this.response = p.responseText; return true; } } } jslibDebug('*** load: '+JS_HTTP_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_http);\n\n"); }; --- NEW FILE: remoteFile.js --- /*** -*- Mode: Javascript; tab-width: 2; 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 Mozdev Group, Inc. code. The Initial Developer of the Original Code is Pete Collins. Portions created by Mozdev Group, Inc. are Copyright (C) 2003 Mozdev Group, Inc. All Rights Reserved. Contributor(s): Pete Collins <pe...@mo...)> (original author) ***/ /****************** Globals **********************/ if (typeof(JS_LIB_LOADED)=='boolean') { const JS_REMOTEFILE_FILE = "remoteFile.js"; const JS_REMOTEFILE_LOADED = true; const JS_REMOTEFILE_URI_CID = "@mozilla.org/network/simple-uri;1"; const JS_REMOTEFILE_URI_I_ID = C.interfaces.nsIURI; const JS_REMOTEFILE_IOSERVICE_CID = "@mozilla.org/network/io-service;1"; const JS_REMOTEFILE_I_IOSERVICE = C.interfaces.nsIIOService; const JS_REMOTEFILE_IOSERVICE = C.classes[JS_REMOTEFILE_IOSERVICE_CID]. getService(JS_REMOTEFILE_I_IOSERVICE); /****************** Remote File Object Class *********************/ function RemoteFile(aURL) { if (!aURL) { jslibError (null, "Remote File constructor", "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_REMOTEFILE_FILE); return; } var url = C.classes[JS_REMOTEFILE_URI_CID].createInstance(JS_REMOTEFILE_URI_I_ID); url.spec = aURL; var protocol = url.scheme; if (protocol != "http") { jslibPrint("RemoteFile URL: "+aURL); jslibPrint("RemoteFile: Sorry, only http is implemented! not ["+protocol+"]"); return; } this.mURI = aURL; return; } // constructor RemoteFile.prototype = { mURI : null, mInputStream : null, mOutputStream : null, mContent : null, mContentType : null, open : function () { var rf = new XMLHttpRequest(); rf.open("GET", this.mURI, false); // to prevent leaks see Mozilla bug #206947 rf.overrideMimeType("text/xml"); rf.send(null); if (rf.status!=200) jslibPrint("Status Code: "+rf.status); this.mContent = rf.responseText; this.mContentType = rf.getResponseHeader("Content-type"); return true; }, read : function () { if (!this.mContent) throw "No remote file instance available you must use open first"; return this.mContent; }, get nsIURI () { if (!this.mURI) return ""; return (JS_REMOTEFILE_IOSERVICE.newURI(this.mURI, null, null)); }, get contentType () { if (!this.mURI) return ""; if (this.mContentType) return this.mContentType; var xml = new XMLHttpRequest(); xml.open("HEAD", this.mURI, false); xml.overrideMimeType("text/xml"); xml.send(null); return xml.getResponseHeader("Content-type"); }, exists : function () { if (!this.mURI) return false;; var rv = false; var xml = new XMLHttpRequest(); xml.open("HEAD", this.mURI, false); xml.overrideMimeType("text/xml"); xml.send(null); if (xml.status != 404) rv = true; return rv; }, /** * returns a javascript Date object * for the Last-Modified timestamp of the * remote url */ get lastModified () { if (!this.mURI) return null; var rv = null; var xml = new XMLHttpRequest(); xml.open("HEAD", this.mURI, false); xml.overrideMimeType("text/xml"); xml.send(null); rv = xml.getAllResponseHeaders(); rv = rv.match(/Last-Modified: .*/); if (!rv) return null; rv = rv[0]; return (new Date(rv.substring(15, rv.length))); }, /********************* help ***************************** * void getter help * * Returns the methods in this object * * return values on success and failure * aStr The methods in this object * * useage: * <string> = obj.help; ****************************************************/ get help() { const help = "\n\nFunction and Attribute List:\n" + "nsIURI\n" + "open()\n" + "read()\n" + "contentType\n" + "exists()\n" + "\n"; return help; } } jslibDebug('*** load: '+JS_REMOTEFILE_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_remotefile);\n\n"); }; // END FileSystem Class --- NEW FILE: socket.js --- /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * 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 JSIRC Library * * The Initial Developer of the Original Code is New Dimensions Consulting, * Inc. Portions created by New Dimensions Consulting, Inc. are * Copyright (C) 1999 New Dimenstions Consulting, Inc. All * Rights Reserved. * * Contributor(s): * Robert Ginda, rg...@nd..., original author * Peter Van der Beken, pet...@pa..., necko-only version * Jeremie Miller, je...@je..., generalized into js socket lib (for Jabber) * Modified: Sep-2003 sdk updated to working (more) state. using: chatzilla/connection-xpcom.js Attributes: nsISocketTransport.openInputStream arguments: socket.openInputFlags = 0; open flags socket.openInputSegmentSize = 0; segmentSize socket.openInputSegmentCount = 0; segmentCount nsISocketTransport.openOutputStream arguments: socket.openOutputFlags = 0; open flags socket.openOutputSegmentSize = 0; segmentSize socket.openOutputSegmentCount = 0; segmentCount */ if (typeof(JS_LIB_LOADED)=='boolean') { var jsenv = new Object(); jsenv.HAS_STREAM_PROVIDER = ('nsIStreamProvider' in C.interfaces); /* function: Socket purpose: Constructor for an instance of Socket interface: socket( [ classID ] ) arguments: classID : classesByID class-identifier returns: not applicable Note: by default, Socket uses the local socket-transport-service, however callers looking for a specific implementation can pass in the class identifier as an alternative. */ function Socket () { this.openInputFlags = 0; this.openInputSegmentSize = 0; this.openInputSegmentCount = 0; this.openOutputFlags = 0; this.openOutputSegmentSize = 0; this.openOutputSegmentCount = 0; var defaultContractID = "@mozilla.org/network/socket-transport-service;1"; // var defaultClassID = "{c07e81e0-ef12-11d2-92b6-00105a1b0d64}" var socketServiceClass; switch( arguments.length ) { case 0: socketServiceClass = C.classes[ defaultContractID ]; break; case 1: socketServiceClass = C.classesByID[ arguments[ 1 ] ]; break; default: throw( "Socket: unexpected arguments for constructor" ); break; } if ( arguments.length == 1 ) if (!socketServiceClass) throw ("Socket constructor: Couldn't get socket service class."); var socketService = socketServiceClass.getService(); if (!socketService) throw ("Socket constructor: Couldn't get socket service."); this._socketService = jslibQI(socketService, "nsISocketTransportService"); // to preserve ourselves within necko/async this.wrappedJSObject = this; } /* function: open purpose: opens the socket interface: open( host, port [ binary ] ) Arguments: host : String, host to connect to port : int, port number to use binary : optional, use binary input support, defaults false returns: true. */ Socket.prototype.open = function (host, port) { this.host = host.toLowerCase(); this.port = port; this.binary = ( arguments.length > 2 ) ? arguments[ 2 ] : false; // in theory, we'd look up proxy information here. but we're being // a bare socket so.... // create the transport: // socketTypes = null // typeCount = 0 // host // port // proxy-info = null this._transport = this._socketService.createTransport ( null, 0, host, port, null ); if (!this._transport) throw ("Socket.open: Error opening transport."); var openFlags = ( this.blocking ) && ( typeof document == "object" ) ? 0 : C.interfaces.nsITransport.OPEN_BLOCKING; this._inputStream = this._transport.openInputStream( this.openInputFlags, this.openInputSegmentSize, this.openInputSegmentCount ); if (!this._inputStream) throw ("Socket.open: Error getting input stream."); if ( this.binary ) this._inputInterface = this.toBinaryInputStream( this._inputStream ); else this._inputInterface = this.toScriptableInputStream( this._inputStream ); this._outputStream = this._transport.openOutputStream( this.openOutputFlags, this.openOutputSegmentSize, this.openOutputSegmentCount ); if (!this._outputStream) throw ("Socket.open: Error getting output stream."); // we really should call _transport.isAlive (?) but that always // returns false. attempts to use "available()" don't fail (but will // later if a connection isn't open). this.isConnected = true; return this.isConnected; } /* function: close purpose: closes the socket connection interface: close() returns: nothing. */ Socket.prototype.close = function () { this.isConnected = false; // calls to _inputStream.close() and _outputStream.close() didn't // function. this._transport.close( 0 ); } /* function: write purpose: writes the given string to the socket. interface: write( str ) arguments: str : string to be written. returns: true if no exception is thrown. */ Socket.prototype.write = function (str) { if (!this.isConnected) throw "Socket.write: Not Connected."; var rv = false; try { this._outputStream.write(str, str.length); rv = true; } catch (ex) { if (typeof ex != "undefined") { this.isConnected = false; throw (ex); } else rv = false; } return rv; } /* function: read purpose: reads data from a socket. interface: read( bytes ) arguments: bytes : integer, number of bytes to read in returns: string Note: will only return the smaller of specified vs available bytes. */ Socket.prototype.read = function ( bytes ) { if (!this.isConnected) throw "Socket.read: Not Connected."; var returnValue = new String; try { // attempts to read more bytes than available will throw // an exception. bytes = Math.min( this._inputInterface.available(), bytes ); if ( bytes ) if ( this.binary ) // despite the documentation, this call works returnValue = this._inputInterface.readBytes( bytes ); else returnValue = this._inputInterface.read( bytes ); else returnValue = ""; } catch (ex) { // the assumption is that we lost the connection... if (typeof ex != "undefined") { this.isConnected = false; throw (ex); } else { returnValue = ""; } } return returnValue; } /* function: available purpose: number of bytes waiting to be read interface: available() returns: integer */ Socket.prototype.available = function () { if ( !this.isConnected ) throw "Socket.available: Not Connected."; return this._inputInterface.available(); } /* function: toBinaryInputStream purpose: creates an nsIBinaryInputStream wrapper around the given inputStream. interface: toBinaryInputStream( inputStream ) arguments: inputStream : result of openInputStream returns: nsIBinaryInputStream */ Socket.prototype.toBinaryInputStream = function( inputStream ) { var si = jslibCreateInstance("@mozilla.org/binaryinputstream;1", "nsIBinaryInputStream"); si.setInputStream( inputStream ); return si; } /* function: toScriptableInputStream purpose: creates an nsIScriptableInputStream wrapper around the given inputStream. interface: toScriptableInputStream( inputStream ) arguments: inputStream : result of openInputStream returns: nsIScriptableInputStream */ Socket.prototype.toScriptableInputStream = function( inputStream ) { var si = jslibCreateInstance("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream"); si.init( inputStream ); return si; } // sdk/16-Sep-2003: async is non-functional /* function: async purpose: interface: returns: usage: var aSocket = new Socket(); var observer = { streamStarted: function(socketContext){ }, //onstart action streamStopped: function(socketContext, status){ }, //onstop action receiveData: function(data){alert(data)} } aSocket.open("ftp.mozilla.org", 21); aSocket.async(observer ); */ Socket.prototype.async = function (observer) { this._pump = jslibCreateInstance("@mozilla.org/network/input-stream-pump;1", "nsIInputStreamPump"); this._pump.init(this._inputStream, -1, -1, 0, 0, false); this._pump.asyncRead(new SocketListener(observer), this); } // async callbacks function SocketListener( observer ) { this._observer = observer; } SocketListener.prototype.onStartRequest = function (channel, socketContext) { socketContext = socketContext.wrappedJSObject; this._observer.streamStarted( socketContext ); } SocketListener.prototype.onStopRequest = function (channel, socketContext, status, errorMsg) { socketContext = socketContext.wrappedJSObject; this._observer.streamStopped( socketContext, status ); } SocketListener.prototype.onDataAvailable = function (channel, socketContext, inStr, sourceOffset, count) { socketContext = socketContext.wrappedJSObject; this._observer.receiveData( socketContext.read( count ) ); } }; // END socket.js --- NEW FILE: multipartRequest.js --- /*** -*- Mode: Javascript; tab-width: 2; 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 jslib code. The Initial Developer of the Original Code is jslib team. Portions created by jslib team are Copyright (C) 2000 jslib team. All Rights Reserved. Contributor(s): Rajeev J Sebastian <raj...@ya...)> (original author) MultipartRequest This object is used to represent the MultipartRequest Create any (any number, any combination, any order) of the Parts that you would like to use. Add them to MultipartRequest using mpr.put(part) Pass the MultipartRequest to the HTTP object just like other Request objects FilePart This Part is used to pass a file through the body of the request filepart.setFile(<a name>, <a jslib File obj>, <content type>); TextPart This Part is used to pass a long text through the body of the request. content-type can be used to provide an interpretation of text like text/plain, text/xml, text/html textpart.setPart(<a name>, <a string>, <content type>); URLParametersPart This Part is used to pass urlencoded parameters through the request url of the request urlparampart.put(<parameter key>, <parameter value>); BodyParametersPart This Part is used to pass urlencoded parameters through the body of the request. bodyparampart.put(<parameter key>, <parameter value>); You can also set a name for the part using bodyparampart.setName(<namestring>); *************************************/ if (typeof(JS_LIB_LOADED)=='boolean') { const JS_MULTIPARTREQUEST_FILE = "multipartRequest.js"; const JS_MULTIPARTREQUEST_LOADED = true; const boundary = "--i-N-5-4-N-3-k-4-n-3-------------314159265358979323846"; const delimiter = "\r\n--"+boundary+"\r\n" ; const close_delim = "\r\n--"+boundary+"--" ; //accepts several parameters, which are placed in the body //of the request function Parts() { this._array = new Array(); } Parts.prototype = { _iterind: 0, put: function(part) { this._array.push(part); }, find: function(type) { var list = new Parts(); this.resetIterator(); while( this.hasMoreElements() ) { var param = this.getNext(); if( param.type == type ) list.put(param); } return list; }, //iterator resetIterator: function() { this._iterind = 0; }, hasMoreElements: function() { if( this._iterind < this._array.length - 1 ) return true; else return false; }, next: function() { return this._array[_iterind++]; } } function MultipartRequest(baseuri) { this.baseu = baseuri; this.parts = new Parts(); } MultipartRequest.prototype = { baseu: null, method: "POST", parts: null, put: function(part) { this.parts.put(part); return this; }, getRequestUri: function() { var uri = this.baseu; uri +="?"; while( this.parts.hasMoreElements() ) { var part = this.parts.getNext(); var params = part._getRequestUriParams(); if( params == null ) continue; params.resetIterator(); while(params.hasMoreElements()) { var head = params.getNext(); uri += head.key +"="+head.value+"&"; } } } uri = uri.slice(0,-1); //remove the last & or last ? return uri; }, getRequestMethod: function() { return this.method; }, setRequestHeaders: function(xhr) { while( this.parts.hasMoreElements() ) { var part = this.parts.getNext(); if( typeof(part) == "URLParameterPart" ) { var phead = part._getRequestHeaders(); if( params == null ) continue; phead.resetIterator(); while(phead.hasMoreElements()) { var head = phead.getNext(); xhr.setRequestHeader(head.key,head.value); } } } xhr.setRequestHeader("Content-type","multipart/form-data; boundary="+boundary); }, getBody: function() { var strinsC = Components.classes["@mozilla.org/io/string-input-stream;1"]. var multiplexstrmC = Components.classes["@mozilla.org/io/multiplex-input-stream;1"]; var mimestrmC = Components.classes["@mozilla.org/network/mime-input-stream;1"]. var delimstrm = strinsC.createInstance( Components.interfaces.nsIStringInputStream ); delimstrm.setData(delimiter,-1); var multiplexstrm = multiplexstrmC.createInstance( Components.interfaces.nsIMultiplexInputStream ); multiplexstrm.appendStream(delimstrm); this.parts.resetIterator(); //for each part while( this.parts.hasMoreElements() ) { var part = this.parts.getNext(); var mimestrm = mimestrmC.createInstance( Components.interfaces.nsIMIMEInputStream ); mimestrm.addContentLength = true; var body = part._getBody(); if( body == null ) continue; var phead = part._getRequestHeaders(); phead.resetIterator(); while(phead.hasMoreElements()) { var head = phead.getNext(); mimestrm.addHeader(head.key,head.value); } mimestrm.setData(body); multiplexstrm.appendStream(mimestrm); //is it necessary to create a new stream ? //leave it in anyway delimstrm = strinsC.createInstance( Components.interfaces.nsIStringInputStream ); delimstrm.setData(delimiter,-1); multiplexstrm.appendStream(delimstrm); } var cdelimstrm = strinsC.createInstance( Components.interfaces.nsIStringInputStream ); cdelimstrm.setData(close_delim,-1); multiplexstrm.appendStream(cdelimstrm); return multiplexstrm; } } jslibDebug('*** load: '+JS_MULTIPARTREQUEST_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_multipartrequest);\n\n"); }; --- NEW FILE: filePostRequest.js --- /*** -*- Mode: Javascript; tab-width: 2; 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 jslib code. The Initial Developer of the Original Code is jslib team. Portions created by jslib team are Copyright (C) 2000 jslib team. All Rights Reserved. Contributor(s): Rajeev J Sebastian <raj...@ya...)> (original author) *************************************/ //sends a file as the body. does not accept other parameters if (typeof(JS_LIB_LOADED)=='boolean') { const JS_FILEPOSTREQUEST_FILE = "filePostRequest.js"; const JS_FILEPOSTREQUEST_LOADED = true; function FilePostRequest(baseuri){ this.baseu = baseuri; } FilePostRequest.prototype = { baseu: null, method: "POST", cnttype: null, cntenc: null, filen: null, instrm: null, setFile: function(filename, stream, contenttype) { this.filen = filename; this.instrm = stream; this.cnttype = contenttype; }, setFile: function(filename, jslibFile, contenttype) { this.filen = filename; var nsIFileInStrm = Components.interfaces.nsIFileInputStream; var nsIBufInStrm = Components.interfaces.nsIBufferedInputStream; var finstrm = Components.classes["@mozilla.org/network/file-input-stream;1"]. createInstance( nsIFileInStrm ); finstrm.init(jslibFile.nsIFile, 1, 1, finstrm.CLOSE_ON_EOF); this.instrm = Components.classes["@mozilla.org/network/buffered-input-stream;1"]. createInstance( nsIBufInStrm ); this.instrm.init(finstrm, 4096); this.cnttype = contenttype; }, getRequestUri: function() { var uri = ""; uri += this.baseu; return uri; }, getRequestMethod: function() { return this.method; }, setRequestHeaders: function(p) { return null; }, getBody: function(p) { var boundary = "--i-N-5-4-N-3-k-4-n-3-------------314159265358979323846"; var delimiter = "\r\n--"+boundary+"\r\n" ; var close_delim = "\r\n--"+boundary+"--" ; var cont_dispos_tag = "Content-disposition"; var cont_dispos_val1 = "form-data; name=\""; var cont_dispos_val2 = "\"; filename=\""; var cont_dispos_val3 = "\""; var cont_type_tag = "Content-type"; var cont_type_val = this.cnttype; //set up the multipart stream var delimstrm = Components.classes["@mozilla.org/io/string-input-stream;1"]. createInstance( Components.interfaces.nsIStringInputStream ); delimstrm.setData(delimiter,-1); //the part in a multipart stream var mimestrm = Components.classes["@mozilla.org/network/mime-input-stream;1"]. createInstance( Components.interfaces.nsIMIMEInputStream ); mimestrm.addContentLength = false; mimestrm.addHeader(cont_dispos_tag,cont_dispos_val1+this.filen+cont_dispos_val2+this.filen+cont_dispos_val3); mimestrm.addHeader(cont_type_tag,cont_type_val); mimestrm.setData(this.instrm); var cdelimstrm = Components.classes["@mozilla.org/io/string-input-stream;1"]. createInstance( Components.interfaces.nsIStringInputStream ); cdelimstrm.setData(close_delim,-1); var multiplexstrm = Components.classes["@mozilla.org/io/multiplex-input-stream;1"]. createInstance( Components.interfaces.nsIMultiplexInputStream ); multiplexstrm.appendStream(delimstrm); multiplexstrm.appendStream(mimestrm); multiplexstrm.appendStream(cdelimstrm); p.setRequestHeader("Content-length",multiplexstrm.available()); p.setRequestHeader("Content-type","multipart/form-data; boundary="+boundary); return multiplexstrm; } } jslibDebug('*** load: '+JS_FILEPOSTREQUEST_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_filepostrequest);\n\n"); }; --- NEW FILE: postRequest.js --- /*** -*- Mode: Javascript; tab-width: 2; 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 jslib code. The Initial Developer of the Original Code is jslib team. Portions created by jslib team are Copyright (C) 2000 jslib team. All Rights Reserved. Contributor(s): Rajeev J Sebastian <raj...@ya...)> (original author) *************************************/ //accepts several parameters, which are placed in the body //of the request if (typeof(JS_LIB_LOADED)=='boolean') { const JS_POSTREQUEST_FILE = "postRequest.js"; const JS_POSTREQUEST_LOADED = true; if (typeof(JS_DICTIONARY_LOADED)!='boolean') include(jslib_dictionary); function PostRequest(baseuri){ this.baseu = baseuri; this.parameters = new Dictionary(); } PostRequest.prototype = { baseu: null, method: "POST", parameters: null, cnttype: null, cntenc: null, put: function(key,value) { this.parameters.put(key,value); return this; }, getRequestUri: function() { var uri = ""; uri += this.baseu; return uri; }, getRequestMethod: function() { return this.method; }, setRequestHeaders: function(p) { p.setRequestHeader("Content-type","application/x-www-form-urlencoded"); }, getBody: function() { var uri = ""; this.parameters.resetIterator(); while( this.parameters.hasMoreElements() ) { var param = this.parameters.next(); if( this.parameters.hasMoreElements() ) uri+= escape(param.key)+"="+escape(param.value)+"&"; else { uri+= escape(param.key)+"="+escape(param.value); break; } } return uri; } } jslibDebug('*** load: '+JS_POSTREQUEST_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_postrequest);\n\n"); }; --- NEW FILE: getRequest.js --- /*** -*- Mode: Javascript; tab-width: 2; 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 jslib code. The Initial Developer of the Original Code is jslib team. Portions created by jslib team are Copyright (C) 2000 jslib team. All Rights Reserved. Contributor(s): Rajeev J Sebastian <raj...@ya...)> (original author) *************************************/ //accepts several parameters, which are placed in the url //of the request if (typeof(JS_LIB_LOADED)=='boolean') { const JS_GETREQUEST_FILE = "getRequest.js"; const JS_GETREQUEST_LOADED = true; if (typeof(JS_DICTIONARY_LOADED)!='boolean') include(jslib_dictionary); function GetRequest(baseuri){ this.baseu = baseuri; this.parameters = new Dictionary(); } GetRequest.prototype = { baseu: null, method: "GET", parameters: null, cnttype: null, cntenc: null, put: function(key,value) { this.parameters.put(key,value); return this; }, getRequestUri: function() { var uri = ""; uri += this.baseu + "?"; this.parameters.resetIterator(); while( this.parameters.hasMoreElements() ) { var param = this.parameters.next(); uri+= escape(param.key)+"="+escape(param.value)+"&"; } uri = uri.slice(0,-1) //discard & or ? return uri; }, getRequestMethod: function() { return this.method; }, setRequestHeaders: function(p) { return null; }, getBody: function() { return null; } } jslibDebug('*** load: '+JS_GETREQUEST_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_getrequest);\n\n"); }; |