//**************************************************************************** // Licence: // 1. Keep this copyright notice. // 2. Keep attribution to any authors listed above, including yourself // 3. Use it for free. //**************************************************************************** // Log of changes: // // 0.5 - 16 Mar 2005: HttpResponse Headers improved // 0.4 - 15 Mar 2005: HttpRequest and OpenEx added by esskar // 0.3 - 15 Mar 2005: Header usage fixed by esskar // 0.2 - 15 Mar 2005: Copyright included by esskar // 0.1 - 14 Mar 2005: Project started by Sascha Kiefer (esskar) // //**************************************************************************** var HTTPERROR_SUCCESS = 0; var HTTPERROR_INVALIDOBJECT = 1; var HTTPERROR_CALLBACK = 2; var HTTPERROR_OPEN = 3; function Http(Interface) { //Properties this.Version = '0.5'; this.IsAsync = false; this.Interface = Interface; this.LastException = ''; if(this.Interface == null) { if(typeof XMLHttpRequest != 'undefined') { /* try this first; maybe MS will implement it someday :) */ this.Interface = new XMLHttpRequest(); } if(this.Interface == null) { var axos = new Array( // newest on top 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ); for(var i = 0; this.Interface == null && i < axos.length; i++ ) { try { this.Interface = new ActiveXObject(axos[i]); } catch(e) { this.LastException = e; this.Interface = null; } } } } //Methods this.IsValid = CallHttpIsValid; this.Get = CallHttpGet; this.Post = CallHttpPost; this.Open = CallHttpOpen; this.OpenEx = CallHttpOpenEx; this.SetHeader = CallHttpSetHeader; this.GetResponse = CallHttpGetResponse; } function CallHttpIsValid() { return this.Interface != null; } function HttpRequest() { this.Method = 'GET'; this.Url = ''; this.Headers = new Array(); this.Body = null; this.Callback = null; } function CallHttpOpenEx(req) { return this.Open( req.Method, req.Url, req.Body, req.Callback, req.Headers ); } function CallHttpOpen(method, url, data, callback, headers) { if(this.IsValid()) { if(!method) method = 'GET'; if(!data) data = null; if(this.IsAsync) { if(typeof callback != 'function') return HTTPERROR_CALLBACK; this.Interface.onreadystatechange = callback; } try { this.Interface.open(method, url, this.IsAsync); } catch(e) { // probably: 'access is denied' this.LastException = e; return HTTPERROR_OPEN; } // Send userdefined headers if(headers != null) { for(var header in headers) { this.SetHeader(header, headers[header]); } } this.Interface.send(data); return HTTPERROR_SUCCESS; } return HTTPERROR_INVALIDOBJECT; } function CallHttpGet(url, callback, headers) { return this.Open('GET', url, null, callback, headers); } function CallHttpPost(url, data, callback, headers) { return this.Open('POST', url, data, callback, headers); } function CallHttpSetHeader(headername, headervalue) { if(!this.IsValid()) return false; var retval = true; try { this.Interface.setRequestHeader(headername, headervalue); } catch(e) { retval = false; this.LastException = e; } return retval; } function HttpResponse() { this.Status = 0; this.StatusText = ''; this.Headers = new Array; this.Body = ''; this.Text = ''; this.Xml = ''; this.SetHeaderString = CallHttpResponseSetHeaderString; } function CallHttpResponseSetHeaderString(string) { if(!string) string = ''; var lines = string.split("\n"); for(var i = 0; i < lines.length; i++) { var header = lines[i].split(": "); if(header.length >= 2) { var headername = header.shift(); var headervalue = header.join(": "); this.Headers[headername] = headervalue; } } } function CallHttpGetResponse() { if(this.Interface.readyState != 4) return null; var res = new HttpResponse(); res.Status = this.Interface.status; res.StatusText = typeof this.Interface.statusText == 'undefined' ? '' : this.Interface.statusText; res.Body = typeof this.Interface.responseBody == 'undefined' ? '' : this.Interface.responseBody; res.Text = typeof this.Interface.responseText == 'undefined' ? '' : this.Interface.responseText; res.Xml = this.Interface.responseXML == null ? '' : this.Interface.responseXML.xml; res.SetHeaderString(this.Interface.getAllResponseHeaders()); return res; }