Fix #5560 - Gitlive - branching wip
[gitlive] / old_seed_version / XMLHttpRequest.js
1 XObject = imports.XObject.XObject;
2
3 Soup = imports.gi.Soup;
4
5 var XMLHttpRequest = function (cfg) {
6     if (typeof(cfg) == 'object') {
7         for(var i in cfg) {
8             this[i] = cfg[i];
9         }
10     }
11     
12 }
13
14 XMLHttpRequest.prototype = { 
15     // event handlers
16     //onreadystatechange;
17     //onloadstart;
18     //onprogress;
19     //onabort;
20     //onerror;
21     //onload;
22     //ontimeout;
23     //onloadend;
24     
25     timeout : 0,
26     withCredentials : false,
27     
28     // states
29     UNSENT : 0,
30     OPENED : 1,
31     HEADERS_RECEIVED : 2,
32     LOADING : 3,
33     DONE : 4,
34     
35     // readonly..
36     readyState : 0,
37     upload: null,
38     
39     
40     _message : false,
41     _session : false,
42     _async   : false,
43     
44     // request
45     open : function ( method,  url, async, user, password)
46     {
47         async = async || false;
48         user = user || false;
49         password = password || false;
50         
51         this._session = async ?  new Soup.SessionAsync() : new Soup.SessionSync();
52
53         var uri = new Soup.URI.c_new(url);
54         this._message = new Soup.Message({method: method, uri:uri});
55         this._async = async;
56
57         if (user || password) {
58             user = user || '';
59             password = password || '';
60             var auth = new Soup.Auth.c_new(
61                     Soup.AuthBasic.type,
62                     this._message,
63                     "Basic realm=\"Test\"");
64  
65             
66
67             auth.authenticate(user,password);
68             var authmsg = auth.get_authorization(this._message);
69             //print(authmsg);
70             this._message.request_headers.append(
71                 'Authorization', authmsg);
72         }
73
74         
75         
76     },
77     
78     setRequestHeader : function ( header,  value)
79     {
80         _this.message.request_headers.append(headers, value)
81
82     },
83     overrideMimeType : function ( mime)
84     {
85         
86     },
87     send  : function(data)
88     {
89         data = data|| false;
90         
91         if (typeof(data) == 'object') {
92             // params..
93             data = this.urlEncode(data);
94         }
95         
96         if (data) {
97             this._message.set_request('application/x-www-form-urlencoded', Soup.MemoryUse.COPY, data, data.length)
98         }
99         var _t = this;
100         if (this._async) {
101             this._session.queue_message(this._message, function(ses, msg) {
102                 // doc's say we get here after we have sent the data..
103                 
104                 //print("got queue callback");
105                 //_t._session.unpause_message(this._message);
106                 //print("queue message");
107                 //print(_t._message.response_body.data)
108                 _t.responseText = _t._message.response_body.data;
109                 _t.status = 4;
110             
111                 if (_t.onreadystatechange) {
112                     _t.onreadystatechange();
113                 }
114                 
115                 
116             });
117             return false;
118             
119         }
120         var status = this._session.send_message(this._message);
121         this.responseText = this._message.response_body.data;
122         this.status = 4;
123         if (_t.onreadystatechange) {
124             _t.onreadystatechange();
125         }
126         return status;
127
128     },
129     abort : function()
130     {
131         
132     },
133     
134     // response (all readonly...)
135     status : false,
136     statusText : false,
137     //readonly attribute any response
138     responseText : false,
139     responseXML : false,
140     responseType : false, 
141     
142     // response - read
143     getResponseHeader : function(  header)
144     {
145         
146     },
147     getAllResponseHeaders : function ()
148     {
149         
150     },
151     urlEncode : function(o){
152         if(!o){
153             return "";
154         }
155         var buf = [];
156         for(var key in o){
157             var ov = o[key], k = encodeURIComponent(key);
158             var type = typeof ov;
159             if(type == 'undefined'){
160                 buf.push(k, "=&");
161             }else if(type != "function" && type != "object"){
162                 buf.push(k, "=", encodeURIComponent(ov), "&");
163             }else if(ov instanceof Array){
164                 if (ov.length) {
165                     for(var i = 0, len = ov.length; i < len; i++) {
166                         buf.push(k, "=", encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
167                     }
168                 } else {
169                     buf.push(k, "=&");
170                 }
171             }
172         }
173         buf.pop();
174         return buf.join("");
175     },
176     
177 };