]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/jsolait/lib/urllib.js
Release 6.2.1
[Github/sugarcrm.git] / jssource / src_files / include / jsolait / lib / urllib.js
1 /*
2
3 Modification information for LGPL compliance
4
5 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
6
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
8
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
10
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
12
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
14
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
16
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
18
19 r4085 - 2005-04-13 17:30:42 -0700 (Wed, 13 Apr 2005) - robert - adding meeting scheduler and accept/decline
20
21
22 */
23
24 /*
25   Copyright (c) 2003 Jan-Klaas Kollhof
26   
27   This file is part of the JavaScript o lait library(jsolait).
28   
29   jsolait is free software; you can redistribute it and/or modify
30   it under the terms of the GNU Lesser General Public License as published by
31   the Free Software Foundation; either version 2.1 of the License, or
32   (at your option) any later version.
33  
34   This software is distributed in the hope that it will be useful,
35   but WITHOUT ANY WARRANTY; without even the implied warranty of
36   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   GNU Lesser General Public License for more details.
38  
39   You should have received a copy of the GNU Lesser General Public License
40   along with this software; if not, write to the Free Software
41   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
42 */
43
44 /**
45    Provides methods for making HTTP requests.
46 */
47 Module("urllib","1.1.3", function(mod){
48     /**
49         Thrown if no request object could be instanciated.
50     */
51     mod.NoHTTPRequestObject=Class("NoHTTPRequestObject", mod.Exception, function(publ, supr){
52         /**
53             Initializes the Exception.
54             @param trace The error causing this exception.
55         */
56         publ.init=function(trace){
57             supr(this).init( "Could not create an HTTP request object", trace);
58         }
59     })
60     
61     /**
62         Thrown if an HTTP request could not be opened.
63     */
64     mod.RequestOpenFailed = Class("RequestOpenFailed", mod.Exception, function(publ, supr){
65         /**
66             Initializes the Exception.
67             @param trace The error causing this exception.
68         */
69         publ.init=function(trace){
70             supr(this).init( "Opening of HTTP request failed.", trace);
71         }
72     })
73     
74     /**
75         Thrown is arequest could not be sent to the server.
76     */
77     mod.SendFailed=Class("SendFailed", mod.Exception, function(publ, supr){
78          /**
79             Initializes the Exception.
80             @param trace The error causing this exception.
81         */
82         publ.init = function(trace){
83             supr(this).init( "Sending of HTTP request failed.", trace);
84         }
85     })
86     
87     /**
88         Mimics the HTTPRequest object using Adobe's SVG Viewer's postURL and getURL.
89         It can only process asyncronous connection and the only header that's supported is 'Content-Type'.
90     */
91     var ASVRequest=Class("ASVRequest", function(publ){
92         /**
93             Initializes the ASVRequest.
94         */
95         publ.init = function(){
96             if((getURL==null) || (postURL==null)){
97                 throw "getURL and postURL are not available!";
98             }else{
99                 this.readyState=0;
100                 this.responseText="";
101                 this.__contType ="";
102                 this.status=200;
103             }
104         }
105         /**
106             Mimics the open method without actually opening a connection.
107             @param type          "GET" or "POST".
108             @param url             The url to open.
109             @param async=true True for async. connection. Otherwhise an exception is thrown.
110         */
111         publ.open=function(type,url,async){
112             if (async == false){
113                 throw "Can only open asynchronous connections!";
114             }
115             this.__type = type;
116             this.__url = url;
117             this.readyState=0;
118         }
119         /**
120             Sets a header.
121             @param name  The header name. All but "Content-Type" are ignored.
122             @param value  The value of the header.
123         */
124         publ.setRequestHeader=function(name, value){
125             if (name=="Content-Type"){
126                 this.__contType =value;
127             }
128         }
129         /**
130             Sends the request.
131             @param data   The data to send when doing a post.
132         */
133         publ.send=function(data){
134             var self=this;
135             var cbh=new Object();
136             cbh.operationComplete = function(rsp){
137                 self.readyState=4;
138                 self.responseText=rsp.content;
139                 if(this.ignoreComplete == false){
140                     if(self.onreadystatechange){
141                         self.onreadystatechange();
142                     }
143                 }
144             }
145             cbh.ignoreComplete = false;
146             try{
147                 if(this.__type =="GET"){
148                     getURL(this.__url,cbh);
149                 }else if (this.__type == "POST"){
150                     postURL(this.__url, data, cbh, this.__contType);
151                 }
152             }catch(e){
153                 cbh.ignoreComplete=true;
154                 throw e;
155             }
156         }
157     })
158     
159     /**
160         Creates an HTTP request object for retreiving files.
161         @return  HTTP request object.
162     */    
163     var getHTTP=function() {
164         var obj;
165         try{ //to get the mozilla httprequest object
166             obj = new XMLHttpRequest();
167         }catch(e){
168             try{ //to get MS HTTP request object
169                 obj=new ActiveXObject("Msxml2.XMLHTTP.4.0");
170             }catch(e){
171                 try{ //to get MS HTTP request object
172                     obj=new ActiveXObject("Msxml2.XMLHTTP")
173                 }catch(e){
174                     try{// to get the old MS HTTP request object
175                         obj = new ActiveXObject("microsoft.XMLHTTP"); 
176                     }catch(e){
177                         try{//to create the ASV request object.
178                             obj = new ASVRequest();
179                         }catch(e){
180                             throw new mod.NoHTTPRequestObject("Neither Mozilla, IE nor ASV found. Can't do HTTP request without them.");
181                         }
182                     }
183                 }    
184             }
185         }
186         return obj;
187     }
188     /**
189         Sends a request to a server.
190         To explain the way the optional arguments work I will give examples:
191         simple:
192             sendRequest("get", "url")
193             sendRequest("post", "url", "data")
194         
195         with headers:
196             sendRequest("get", "url", [["headername","value"]])
197             sendRequest("post", "url", "data", [["headername","value"]])
198         
199         with user information:
200             sendRequest("get", "url", "user", "pass")
201             sendRequest("post", "url", "user", "pass", "data")
202         
203         with headers and user information:
204             sendRequest("get", "url", "user", "pass", [["headername","value"]])
205             sendRequest("post", "url", "user", "pass", "data", [["headername","value"]])
206         
207         To make the request asynchronous just add a callback function as the last argument to the calls above.
208  
209         @param type              Type of connection (GET, POST, ...).
210         @param url                 The URL to retrieve.
211         @param user=null        The username for auth.
212         @param pass=null        The password. (must be set if user is set!)
213         @param data=""          The data to send with the request.
214         @param headers=[]      Array of headers. Each element in the array should be another array containing [headername,value].
215         @param callback=null   Callback for asynchronous connections. The callback is called after completion and is passed the request object as 1st Parameter.
216         @return                     HTTP request object.
217     */
218     mod.sendRequest=function(type, url, user, pass, data, headers, callback){
219         var async=false;
220         //check if the last argument is a function and treat it as callback;
221         if(arguments[arguments.length-1]  instanceof Function){
222             var async=true;
223             callback = arguments[arguments.length-1];
224         }
225         //treat sencond last(if callback)/last(if no callback) argument as headers
226         var headindex=arguments.length-((async || arguments[arguments.length-1] == null) ?2:1);
227         //is it an array then it's headers
228         if(arguments[headindex] instanceof Array){
229             headers=arguments[headindex];
230         }else{
231             headers=[];
232         }
233         //are user AND password not specified then assume data as 3rd argument.
234         if(typeof user == "string" && typeof pass == "string"){
235             if(typeof data != "string"){
236                 data="";
237             }
238         }else if (typeof user == "string"){
239             data = user;
240             user=null;
241             pass=null;
242         }else{
243             user=null;
244             pass=null;
245         }
246         var xmlhttp= getHTTP();
247         try{
248             if(user!=null){
249                 xmlhttp.open(type, url, async, user, pass);
250             }else{
251                 xmlhttp.open(type, url, async);
252             }
253         }catch(e){
254             throw new mod.RequestOpenFailed(e);
255         }
256         //set headers
257         for(var i=0;i< headers.length;i++){
258             xmlhttp.setRequestHeader(headers[i][0], headers[i][1]);    
259         }
260         
261         if(async){//set up a callback
262             xmlhttp.onreadystatechange=function(){
263                 if (xmlhttp.readyState==4) {
264                     callback(xmlhttp);
265                     xmlhttp = null; //help IE with garbage collection
266                 }else if (xmlhttp.readyState==2){
267                     //status property should be available (MS IXMLHTTPRequest documentation) 
268                     //in Mozilla it is not if the request failed(server not reachable)
269                     //in IE it is not available at all ?!
270                     try{//see if it is mozilla otherwise don't care.
271                         var isNetscape = netscape;
272                         try{//if status is not available the request failed.
273                             var s=xmlhttp.status;
274                         }catch(e){//call the callback because Mozilla will not get to readystate 4
275                             callback(xmlhttp);
276                             xmlhttp = null;
277                         }
278                     }catch(e){
279                         
280                     }
281                 }
282             }
283         }
284         
285         try{
286             xmlhttp.send(data);
287         }catch(e){            
288             if(async){
289                 callback(xmlhttp, e);
290                 xmlhttp=null;
291             }else{
292                 throw new mod.SendFailed(e);
293             }
294         }
295         return xmlhttp;
296     }
297     /**
298         Shorthand for a GET request.
299         It calls sendRequest with "GET" as first argument.
300         See the sendRequest method for more information.
301         @param url                 The URL to retrieve.
302         @param user=null        The username for auth.
303         @param pass=null        The password. (must be set if user is set!)
304         @param headers=[]      Array of headers. Each element in the array should be another array containing [headername,value].
305         @param callback=null   Callback for asynchronous connections. The callback is called after completion and is passed the request object as 1st Parameter.
306         @return                     HTTP request object.
307     */
308     mod.getURL=function(url, user, pass, headers, callback) { 
309         var a=  new Array("GET");
310         for(var i=0;i<arguments.length;i++){
311             a.push(arguments[i]);
312         }
313         return mod.sendRequest.apply(this,a)
314     }
315     /**
316         Shorthand for a POST request.
317         It calls sendRequest with "POST" as first argument.
318         See the sendRequest method for more information.
319         @param url                 The URL to retrieve.
320         @param user=null        The username for auth.
321         @param pass=null        The password. (must be set if user is set!)
322         @param data=""          The data to send with the request.
323         @param headers=[]      Array of headers. Each element in the array should be another array containing [headername,value].
324         @param callback=null   Callback for asynchronous connections. The callback is called after completion and is passed the request object as 1st Parameter.
325         @return                     HTTP request object.
326     */
327     mod.postURL=function(url, user, pass, data, headers, callback) { 
328         var a=  new Array("POST");
329         for(var i=0;i<arguments.length;i++){
330             a.push(arguments[i]);
331         }
332         return mod.sendRequest.apply(this,a)
333     }
334 })
335