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