3 Modification information for LGPL compliance
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
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
19 r4085 - 2005-04-13 17:30:42 -0700 (Wed, 13 Apr 2005) - robert - adding meeting scheduler and accept/decline
25 Copyright (c) 2003 Jan-Klaas Kollhof
27 This file is part of the JavaScript o lait library(jsolait).
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.
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.
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
45 Provides methods for making HTTP requests.
47 Module("urllib","1.1.3", function(mod){
49 Thrown if no request object could be instanciated.
51 mod.NoHTTPRequestObject=Class("NoHTTPRequestObject", mod.Exception, function(publ, supr){
53 Initializes the Exception.
54 @param trace The error causing this exception.
56 publ.init=function(trace){
57 supr(this).init( "Could not create an HTTP request object", trace);
62 Thrown if an HTTP request could not be opened.
64 mod.RequestOpenFailed = Class("RequestOpenFailed", mod.Exception, function(publ, supr){
66 Initializes the Exception.
67 @param trace The error causing this exception.
69 publ.init=function(trace){
70 supr(this).init( "Opening of HTTP request failed.", trace);
75 Thrown is arequest could not be sent to the server.
77 mod.SendFailed=Class("SendFailed", mod.Exception, function(publ, supr){
79 Initializes the Exception.
80 @param trace The error causing this exception.
82 publ.init = function(trace){
83 supr(this).init( "Sending of HTTP request failed.", trace);
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'.
91 var ASVRequest=Class("ASVRequest", function(publ){
93 Initializes the ASVRequest.
95 publ.init = function(){
96 if((getURL==null) || (postURL==null)){
97 throw "getURL and postURL are not available!";
100 this.responseText="";
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.
111 publ.open=function(type,url,async){
113 throw "Can only open asynchronous connections!";
121 @param name The header name. All but "Content-Type" are ignored.
122 @param value The value of the header.
124 publ.setRequestHeader=function(name, value){
125 if (name=="Content-Type"){
126 this.__contType =value;
131 @param data The data to send when doing a post.
133 publ.send=function(data){
135 var cbh=new Object();
136 cbh.operationComplete = function(rsp){
138 self.responseText=rsp.content;
139 if(this.ignoreComplete == false){
140 if(self.onreadystatechange){
141 self.onreadystatechange();
145 cbh.ignoreComplete = false;
147 if(this.__type =="GET"){
148 getURL(this.__url,cbh);
149 }else if (this.__type == "POST"){
150 postURL(this.__url, data, cbh, this.__contType);
153 cbh.ignoreComplete=true;
160 Creates an HTTP request object for retreiving files.
161 @return HTTP request object.
163 var getHTTP=function() {
165 try{ //to get the mozilla httprequest object
166 obj = new XMLHttpRequest();
168 try{ //to get MS HTTP request object
169 obj=new ActiveXObject("Msxml2.XMLHTTP.4.0");
171 try{ //to get MS HTTP request object
172 obj=new ActiveXObject("Msxml2.XMLHTTP")
174 try{// to get the old MS HTTP request object
175 obj = new ActiveXObject("microsoft.XMLHTTP");
177 try{//to create the ASV request object.
178 obj = new ASVRequest();
180 throw new mod.NoHTTPRequestObject("Neither Mozilla, IE nor ASV found. Can't do HTTP request without them.");
189 Sends a request to a server.
190 To explain the way the optional arguments work I will give examples:
192 sendRequest("get", "url")
193 sendRequest("post", "url", "data")
196 sendRequest("get", "url", [["headername","value"]])
197 sendRequest("post", "url", "data", [["headername","value"]])
199 with user information:
200 sendRequest("get", "url", "user", "pass")
201 sendRequest("post", "url", "user", "pass", "data")
203 with headers and user information:
204 sendRequest("get", "url", "user", "pass", [["headername","value"]])
205 sendRequest("post", "url", "user", "pass", "data", [["headername","value"]])
207 To make the request asynchronous just add a callback function as the last argument to the calls above.
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.
218 mod.sendRequest=function(type, url, user, pass, data, headers, callback){
220 //check if the last argument is a function and treat it as callback;
221 if(arguments[arguments.length-1] instanceof Function){
223 callback = arguments[arguments.length-1];
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];
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"){
238 }else if (typeof user == "string"){
246 var xmlhttp= getHTTP();
249 xmlhttp.open(type, url, async, user, pass);
251 xmlhttp.open(type, url, async);
254 throw new mod.RequestOpenFailed(e);
257 for(var i=0;i< headers.length;i++){
258 xmlhttp.setRequestHeader(headers[i][0], headers[i][1]);
261 if(async){//set up a callback
262 xmlhttp.onreadystatechange=function(){
263 if (xmlhttp.readyState==4) {
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
289 callback(xmlhttp, e);
292 throw new mod.SendFailed(e);
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.
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]);
313 return mod.sendRequest.apply(this,a)
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.
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]);
332 return mod.sendRequest.apply(this,a)