PageRenderTime 40ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/dynamictable/public/phpolait/jsolait/lib/urllib.js

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