PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/jsolait/lib/urllib.js

http://pyjamas.googlecode.com/
JavaScript | 177 lines | 176 code | 1 blank | 0 comment | 31 complexity | 3f2dd82365b959778fd586094d0e07a2 MD5 | raw file
  1Module("urllib","1.1.4", function(mod){
  2mod.NoHTTPRequestObject=Class("NoHTTPRequestObject", mod.Exception, function(publ, supr){
  3publ.init=function(trace){
  4supr(this).init( "Could not create an HTTP request object", trace);
  5}
  6})
  7mod.RequestOpenFailed = Class("RequestOpenFailed", mod.Exception, function(publ, supr){
  8publ.init=function(trace){
  9supr(this).init( "Opening of HTTP request failed.", trace);
 10}
 11})
 12mod.SendFailed=Class("SendFailed", mod.Exception, function(publ, supr){
 13publ.init = function(trace){
 14supr(this).init( "Sending of HTTP request failed.", trace);
 15}
 16})
 17var ASVRequest=Class("ASVRequest", function(publ){
 18publ.init = function(){
 19if((getURL==null) || (postURL==null)){
 20throw "getURL and postURL are not available!";
 21}else{
 22this.readyState=0;
 23this.responseText="";
 24this.__contType ="";
 25this.status=200;
 26}
 27}
 28publ.open=function(type,url,async){
 29if (async == false){
 30throw "Can only open asynchronous connections!";
 31}
 32this.__type = type;
 33this.__url = url;
 34this.readyState=0;
 35}
 36publ.setRequestHeader=function(name, value){
 37if (name=="Content-Type"){
 38this.__contType =value;
 39}
 40}
 41publ.send=function(data){
 42var self=this;
 43var cbh=new Object();
 44cbh.operationComplete = function(rsp){
 45self.readyState=4;
 46self.responseText=rsp.content;
 47if(this.ignoreComplete == false){
 48if(self.onreadystatechange){
 49self.onreadystatechange();
 50}
 51}
 52}
 53cbh.ignoreComplete = false;
 54try{
 55if(this.__type =="GET"){
 56getURL(this.__url,cbh);
 57}else if (this.__type == "POST"){
 58postURL(this.__url, data, cbh, this.__contType);
 59}
 60}catch(e){
 61cbh.ignoreComplete=true;
 62throw e;
 63}
 64}
 65})
 66    
 67var getHTTP=function() {
 68var obj;
 69try{ 
 70obj = new XMLHttpRequest();
 71}catch(e){
 72try{ 
 73obj=new ActiveXObject("Msxml2.XMLHTTP.4.0");
 74}catch(e){
 75try{ 
 76obj=new ActiveXObject("Msxml2.XMLHTTP")
 77}catch(e){
 78try{
 79obj = new ActiveXObject("microsoft.XMLHTTP"); 
 80}catch(e){
 81try{
 82obj = new ASVRequest();
 83}catch(e){
 84throw new mod.NoHTTPRequestObject("Neither Mozilla, IE nor ASV found. Can't do HTTP request without them.");
 85}
 86}
 87}    
 88}
 89}
 90return obj;
 91}
 92mod.sendRequest=function(type, url, user, pass, data, headers, callback){
 93var async=false;
 94if(arguments[arguments.length-1]  instanceof Function){
 95var async=true;
 96callback = arguments[arguments.length-1];
 97}
 98var headindex=arguments.length-((async || arguments[arguments.length-1] == null) ?2:1);
 99if(arguments[headindex] instanceof Array){
100headers=arguments[headindex];
101}else{
102headers=[];
103}
104if(typeof user == "string" && typeof pass == "string"){
105if(typeof data != "string"){
106data="";
107}
108}else if (typeof user == "string"){
109data = user;
110user=null;
111pass=null;
112}else{
113user=null;
114pass=null;
115}
116var xmlhttp= getHTTP();
117try{
118if(user!=null){
119xmlhttp.open(type, url, async, user, pass);
120}else{
121xmlhttp.open(type, url, async);
122}
123}catch(e){
124throw new mod.RequestOpenFailed(e);
125}
126for(var i=0;i< headers.length;i++){
127try{
128xmlhttp.setRequestHeader(headers[i][0], headers[i][1]);    
129}catch(e){
130}
131}
132if(async){
133xmlhttp.onreadystatechange=function(){
134if (xmlhttp.readyState==4) {
135callback(xmlhttp);
136xmlhttp = null; 
137}else if (xmlhttp.readyState==2){
138try{
139var isNetscape = netscape;
140try{
141var s=xmlhttp.status;
142}catch(e){
143callback(xmlhttp);
144xmlhttp = null;
145}
146}catch(e){
147}
148}
149}
150}
151try{
152xmlhttp.send(data);
153}catch(e){            
154if(async){
155callback(xmlhttp, e);
156xmlhttp=null;
157}else{
158throw new mod.SendFailed(e);
159}
160}
161return xmlhttp;
162}
163mod.getURL=function(url, user, pass, headers, callback) { 
164var a=  new Array("GET");
165for(var i=0;i<arguments.length;i++){
166a.push(arguments[i]);
167}
168return mod.sendRequest.apply(this,a)
169}
170mod.postURL=function(url, user, pass, data, headers, callback) { 
171var a=  new Array("POST");
172for(var i=0;i<arguments.length;i++){
173a.push(arguments[i]);
174}
175return mod.sendRequest.apply(this,a)
176}
177})