Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Revert "Refactor XHR to use HTTP module instead of Java APIs."

This reverts commit 7178d9f.
  • Loading branch information...
commit 7e6eae694b3c9e0cb3420aef34844a2254a66fb4 1 parent 7178d9f
Tom Robinson authored
Showing with 64 additions and 27 deletions.
  1. +64 −27 lib/browser/xhr.js
View
91 lib/browser/xhr.js
@@ -1,5 +1,4 @@
-var FILE = require("file"),
- HTTP = require("http"),
+var File = require("file"),
IO = require("io").IO,
URI = require("uri"),
HashP = require("hashp").HashP,
@@ -92,7 +91,7 @@ XMLHttpRequest.prototype.open = function(method, url, async, user, password)
if (uri.path === null)
uri.path = "/";
else if (uri.path.charAt(0) !== "/")
- uri.path = FILE.cwd() + "/" + uri.path;
+ uri.path = File.cwd() + "/" + uri.path;
// 7. If stored url contains an unsupported scheme raise a NOT_SUPPORTED_ERR and terminate these steps.
if (!(/https?|file/).test(uri.scheme))
@@ -219,16 +218,16 @@ function sendData(entityBody){
// unclear whether plusses are reserved in the URI path
//uri.path = decodeURIComponent(uri.path.replace(/\+/g, " "));
if (this._method === "PUT") {
- if ((FILE.exists(uri.path) && FILE.isWritable(uri.path)) || FILE.path(uri.path).resolve('..').isWritable()) {
- this.output = FILE.write(uri.path, entityBody || new ByteString(), { mode : "b" });
+ if ((File.exists(uri.path) && File.isWritable(uri.path)) || File.path(uri.path).resolve('..').isWritable()) {
+ this.output = File.write(uri.path, entityBody || new ByteString(), { mode : "b" });
this.status = 201;
} else {
this.status = 403;
}
} else if (this._method === "DELETE") {
- if (FILE.exists(uri.path)) {
- if (FILE.path(uri.path).resolve('..').isWritable()) {
- FILE.remove(uri.path);
+ if (File.exists(uri.path)) {
+ if (File.path(uri.path).resolve('..').isWritable()) {
+ File.remove(uri.path);
this.status = 200;
} else {
this.status = 403;
@@ -237,40 +236,78 @@ function sendData(entityBody){
this.status = 404;
}
} else {
- if (FILE.exists(uri.path)) {
- this.responseRaw = FILE.read(uri.path, "b");
+ if (File.exists(uri.path)) {
+ this.responseText = File.read(uri.path, { charset : "UTF-8" }); // FIXME: don't assume UTF-8?
this.status = 200;
} else {
this.status = 404;
}
}
} else {
- var connection = HTTP.open(this._url, "b", {
- method : this._method,
- headers : this._requestHeaders
- });
+ var url = new java.net.URL(this._url),
+ connection = url.openConnection();
- if (entityBody)
- connection.write(entityBody);
+ connection.setDoInput(true);
- this.responseRaw = connection.read();
+ connection.setRequestMethod(this._method);
- this.status = connection.status;
- this.statusText = connection.statusText;
+ for (var header in this._requestHeaders) {
+ var value = this._requestHeaders[header];
+ connection.addRequestProperty(String(header), String(value));
+ }
- connection.close();
- }
+ var input = null;
+ try {
+ if (entityBody) {
+ connection.setDoOutput(true);
- if (this.responseRaw) {
- // FIXME: don't assume UTF-8? Check Content-Type header and also implement overrideMimeType?
- this.responseText = this.responseRaw.decodeToString("UTF-8");
+ var output = new IO(null, connection.getOutputStream());
+ output.write(entityBody);
+ output.close();
+ }
+ connection.connect();
+
+ input = new IO(connection.getInputStream(), null);
+ } catch (e) {
+ // HttpUrlConnection will throw FileNotFoundException on 404 errors. FIXME: others?
+ if (e.javaException instanceof java.io.FileNotFoundException)
+ input = new IO(connection.getErrorStream(), null);
+ else {
+ try {
+ this.status = Number(connection.getResponseCode());
+ this.statusText = String(connection.getResponseMessage());
+ return;
+ } catch (err) {
+ throw e;
+ }
+ }
+ }
+
+ this.status = Number(connection.getResponseCode());
+ this.statusText = String(connection.getResponseMessage() || "");
+
+ for (var i = 0;; i++) {
+ var key = connection.getHeaderFieldKey(i),
+ value = connection.getHeaderField(i);
+ if (!key && !value)
+ break;
+ // returns the HTTP status code with no key, ignore it.
+ if (key)
+ this._responseHeaders[String(key)] = String(value);
+ }
+
+ //this.readyState = HEADERS_RECEIVED;
+ //this.readyState = LOADING;
+
+ this.responseRaw = input.read();
+ this.responseText = this.responseRaw.decodeToString("UTF-8"); // FIXME: don't assume UTF-8?
}
+ system.log.debug("xhr response: " + this._url + " (status="+this.status+" length="+this.responseText.length+")");
}
catch (e) {
- this.readyState = DONE;
- this.status = 0;
+ this.status = 500;
this.responseText = "";
- throw new Error("NETWORK_ERR");
+ system.log.warn("xhr exception: " + this.url + " ("+e+")");
}
this.responseXML = null;
Please sign in to comment.
Something went wrong with that request. Please try again.