Permalink
Browse files

Refactor XHR to use HTTP module instead of Java APIs.

  • Loading branch information...
1 parent 419fb77 commit 7178d9fbcb33ca72ad4f36f11799b035b07acdde Tom Robinson committed Apr 29, 2010
Showing with 27 additions and 64 deletions.
  1. +27 −64 lib/browser/xhr.js
View
@@ -1,4 +1,5 @@
-var File = require("file"),
+var FILE = require("file"),
+ HTTP = require("http"),
IO = require("io").IO,
URI = require("uri"),
HashP = require("hashp").HashP,
@@ -91,7 +92,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))
@@ -218,16 +219,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;
@@ -236,78 +237,40 @@ function sendData(entityBody){
this.status = 404;
}
} else {
- if (File.exists(uri.path)) {
- this.responseText = File.read(uri.path, { charset : "UTF-8" }); // FIXME: don't assume UTF-8?
+ if (FILE.exists(uri.path)) {
+ this.responseRaw = FILE.read(uri.path, "b");
this.status = 200;
} else {
this.status = 404;
}
}
} else {
- var url = new java.net.URL(this._url),
- connection = url.openConnection();
+ var connection = HTTP.open(this._url, "b", {
+ method : this._method,
+ headers : this._requestHeaders
+ });
- connection.setDoInput(true);
+ if (entityBody)
+ connection.write(entityBody);
- connection.setRequestMethod(this._method);
+ this.responseRaw = connection.read();
- for (var header in this._requestHeaders) {
- var value = this._requestHeaders[header];
- connection.addRequestProperty(String(header), String(value));
- }
-
- var input = null;
- try {
- if (entityBody) {
- connection.setDoOutput(true);
+ this.status = connection.status;
+ this.statusText = connection.statusText;
- 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;
+ connection.close();
+ }
- this.responseRaw = input.read();
- this.responseText = this.responseRaw.decodeToString("UTF-8"); // FIXME: don't assume UTF-8?
+ if (this.responseRaw) {
+ // FIXME: don't assume UTF-8? Check Content-Type header and also implement overrideMimeType?
+ this.responseText = this.responseRaw.decodeToString("UTF-8");
}
- system.log.debug("xhr response: " + this._url + " (status="+this.status+" length="+this.responseText.length+")");
}
catch (e) {
- this.status = 500;
+ this.readyState = DONE;
+ this.status = 0;
this.responseText = "";
- system.log.warn("xhr exception: " + this.url + " ("+e+")");
+ throw new Error("NETWORK_ERR");
}
this.responseXML = null;

0 comments on commit 7178d9f

Please sign in to comment.