From de71a046e619884292b122ddcf1401ad9932a5c7 Mon Sep 17 00:00:00 2001 From: John Resig Date: Mon, 9 Jul 2007 00:48:16 +0000 Subject: [PATCH] Touched up the XMLHttpRequest code (fixed some bugs, fixed formatting, etc.). --- build/runtest/env.js | 147 ++++++++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 63 deletions(-) diff --git a/build/runtest/env.js b/build/runtest/env.js index de422f7e5..23ffaeddd 100644 --- a/build/runtest/env.js +++ b/build/runtest/env.js @@ -469,82 +469,103 @@ var window = this; } // XMLHttpRequest + // Originally implemented by Yehuda Katz window.XMLHttpRequest = function(){ - this.headers = {}; - this.responseHeaders = {}; + this.headers = {}; + this.responseHeaders = {}; }; XMLHttpRequest.prototype = { open: function(method, url, async, user, password){ - this.readyState = 1; - if(async) this.async = true; - this.method = method || "GET"; - this.url = url; - this.onreadystatechange(); + this.readyState = 1; + if (async) + this.async = true; + this.method = method || "GET"; + this.url = url; + this.onreadystatechange(); }, setRequestHeader: function(header, value){ - this.headers[header] = value; + this.headers[header] = value; }, getResponseHeader: function(header){ }, send: function(data){ - var makeRequest = function() { - var url = new java.net.URL(this.url); - var connection = url.openConnection(); - // Add headers to Java connection - for(header in this.headers) connection.addRequestProperty(header, this.headers[header]); - connection.connect(); - // Stick the response headers into responseHeaders - for(i=0;;i++) { - headerName = connection.getHeaderFieldKey(i); - headerValue = connection.getHeaderField(i); - if(!headerName && !headerValue) break; - if(headerName) this.responseHeaders[headerName] = headerValue; - } - this.readyState = 4; - this.status = parseInt(connection.responseCode); - this.statusText = connection.responseMessage; - var stream = new java.io.InputStreamReader(connection.getInputStream()); - var buffer = new java.io.BufferedReader(stream); - var line; - while((line = buffer.readLine()) != null) this.responseText += line; - try { - this.responseXML = new DOMDocument(this.responseText); - } catch(e) { - this.responseXML = null; - } - this.onreadystatechange(); - }; - if(this.async) { - var requestThread = (new java.lang.Thread(new java.lang.Runnable({ - run: makeRequest - }))); - requestThread.start(); - } - else makeRequest(); + function makeRequest(){ + var url = new java.net.URL(this.url), + connection = url.openConnection(); + + // Add headers to Java connection + for (var header in this.headers) + connection.addRequestProperty(header, this.headers[header]); + + connection.connect(); + + // Stick the response headers into responseHeaders + for (i=0; ; i++) { + var headerName = connection.getHeaderFieldKey(i); + var headerValue = connection.getHeaderField(i); + if (!headerName && !headerValue) break; + if (headerName) + this.responseHeaders[headerName] = headerValue; + } + + this.readyState = 4; + this.status = parseInt(connection.responseCode); + this.statusText = connection.responseMessage; + + var stream = new java.io.InputStreamReader( + connection.getInputStream()), + buffer = new java.io.BufferedReader(stream), + line; + + while ((line = buffer.readLine()) != null) + this.responseText += line; + + try { + this.responseXML = new DOMDocument(this.responseText); + } catch(e) { + this.responseXML = null; + } + + this.onreadystatechange(); + } + + if (this.async) + (new java.lang.Thread(new java.lang.Runnable({ + run: makeRequest + }))).start(); + else + makeRequest(); }, - abort: function(){ }, - onreadystatechange: function(){ }, - getResponseHeader: function(header) { - if(this.readyState < 3) throw new Error("INVALID_STATE_ERR"); - else { - var returnedHeaders = []; - for(rHeader in this.responseHeaders) { - if(rHeader.match(new Regexp(header, "i"))) returnedHeaders.push(this.responseHeaders[rHeader]); - } - if(returnedHeaders != []) return returnedHeaders.join(", "); - } - return null; + abort: function(){}, + onreadystatechange: function(){}, + getResponseHeader: function(header){ + if (this.readyState < 3) + throw new Error("INVALID_STATE_ERR"); + else { + var returnedHeaders = []; + for (var rHeader in this.responseHeaders) { + if (rHeader.match(new Regexp(header, "i"))) + returnedHeaders.push(this.responseHeaders[rHeader]); + } + + if (returnedHeaders.length) + return returnedHeaders.join(", "); + } + + return null; }, - getAllResponseHeaders: function(header) { - if(this.readyState < 3) throw new Error("INVALID_STATE_ERR"); - else { - returnedHeaders = []; - for(var header in this.responseHeaders) { - returnedHeaders += (header + ": " + this.responseHeaders[header]); - } - return returnedHeaders.join("\r\n"); - } + getAllResponseHeaders: function(header){ + if (this.readyState < 3) + throw new Error("INVALID_STATE_ERR"); + else { + var returnedHeaders = []; + + for (var header in this.responseHeaders) + returnedHeaders.push( header + ": " + this.responseHeaders[header] ); + + return returnedHeaders.join("\r\n"); + } }, async: true, readyState: 0,