Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved HTTP module for Java. Supports writing, headers, method, etc.

  • Loading branch information...
commit 7265bc173be3018e77d07f05998a4b2c8a6976f3 1 parent d24e85f
Tom Robinson authored
Showing with 78 additions and 0 deletions.
  1. +78 −0 engines/rhino/lib/http.js
View
78 engines/rhino/lib/http.js
@@ -0,0 +1,78 @@
+
+// -- tlrobinson Tom Robinson
+
+var FILE = require("file");
+var IO = require("io").IO;
+
+exports.open = function(url, mode, options) {
+ mode = mode || "b";
+ options = options || {};
+
+ options.method = options.method || "GET";
+ options.headers = options.headers || {};
+
+ var connection = new java.net.URL(url).openConnection();
+ connection.setDoInput(true);
+ connection.setDoOutput(true);
+ connection.setRequestMethod(options.method);
+
+ for (var name in options.headers){
+ connection.addRequestProperty(String(name), String(options.headers[name]));
+ }
+
+ connection.connect();
+
+ var output = new IO(null, connection.getOutputStream());
+ var input = null;
+
+ var request = {
+ status : null,
+ headers : {},
+ read : function() {
+ if (!input) {
+ output.close();
+ input = new IO(connection.getInputStream(), null);
+ 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;
+ if (key)
+ this.headers[String(key)] = String(value);
+ }
+ }
+ return input.read.apply(input, arguments);
+ },
+ write : function() {
+ output.write.apply(output, arguments);
+ return this;
+ },
+ flush : function() {
+ output.flush.apply(output, arguments);
+ return this;
+ },
+ close : function() {
+ if (output)
+ output.close();
+ if (input)
+ input.close();
+ return this;
+ }
+ }
+ return request
+};
+
+exports.read = function(url) {
+ var stream = exports.open(url);
+ try {
+ return stream.read();
+ } finally {
+ stream.close();
+ }
+};
+
+exports.copy = function(source, target, mode) {
+ mode = mode || "b";
+ return FILE.path(target).write(exports.read(source, mode), mode);
+};

0 comments on commit 7265bc1

Please sign in to comment.
Something went wrong with that request. Please try again.