THRIFT-2397 Add CORS and CSP support for JavaScript and Node.js libraries
Patch: Randy Abernethy
diff --git a/lib/js/README b/lib/js/README
index 07b188b..bb65050 100644
--- a/lib/js/README
+++ b/lib/js/README
@@ -25,7 +25,7 @@
Grunt Build
------------
-The is the base directory for the Apache Thrift JavaScript
+This is the base directory for the Apache Thrift JavaScript
library. This directory contains a Gruntfile.js and a
package.json. Many of the build and test tools used here
require a recent version of Node.js to be installed. To
@@ -46,7 +46,7 @@
The following directories are present (some only after the
grunt build):
/src - The JavaScript Apache Thrift source
- /doc - HTML documentation
+ /doc - HTML documentation
/dist - Distribution files (thrift.js and thrift.min.js)
/test - Various tests, this is a good place to look for
example code
@@ -81,8 +81,8 @@
<script src="gen-js/hello_svc.js"></script>
<script>
(function() {
- var transport = new Thrift.Transport("/hello");
- var protocol = new Thrift.Protocol(transport);
+ var transport = new Thrift.TXHRTransport("/hello");
+ var protocol = new Thrift.TJSONProtocol(transport);
var client = new hello_svcClient(protocol);
var nameElement = document.getElementById("name_in");
var outputElement = document.getElementById("output");
@@ -98,9 +98,7 @@
</html>
### hello.js - Node Server
- var Thrift = require('thrift');
- var TBufferedTransport = require('thrift/lib/thrift/transport').TBufferedTransport;
- var TJSONProtocol = require('thrift/lib/thrift/protocol').TJSONProtocol;
+ var thrift = require('thrift');
var hello_svc = require('./gen-nodejs/hello_svc.js');
var hello_handler = {
@@ -111,9 +109,9 @@
}
var hello_svc_opt = {
- transport: TBufferedTransport,
- protocol: TJSONProtocol,
- cls: hello_svc,
+ transport: thrift.TBufferedTransport,
+ protocol: thrift.TJSONProtocol,
+ processor: hello_svc,
handler: hello_handler
};
@@ -124,7 +122,7 @@
}
}
- var server = Thrift.createThriftWebServer(server_opt);
+ var server = Thrift.createWebServer(server_opt);
var port = 9099;
server.listen(port);
console.log("Http/Thrift Server running on port: " + port);
diff --git a/lib/js/src/thrift.js b/lib/js/src/thrift.js
index 411eead..8fc7cd2 100644
--- a/lib/js/src/thrift.js
+++ b/lib/js/src/thrift.js
@@ -286,11 +286,11 @@
* @example
* var transport = new Thrift.TXHRTransport("http://localhost:8585");
*/
-Thrift.Transport = Thrift.TXHRTransport = function(url) {
+Thrift.Transport = Thrift.TXHRTransport = function(url, options) {
this.url = url;
this.wpos = 0;
this.rpos = 0;
-
+ this.useCORS = (options && options.useCORS);
this.send_buf = '';
this.recv_buf = '';
};
@@ -683,7 +683,7 @@
* @example
* var protocol = new Thrift.Protocol(transport);
*/
-Thrift.Protocol = function(transport) {
+Thrift.TJSONProtocol = Thrift.Protocol = function(transport) {
this.transport = transport;
};
@@ -977,16 +977,8 @@
var ch = str.charAt(i); // a single double quote: "
if (ch === '\"') {
escapedString += '\\\"'; // write out as: \"
- } else if (ch === '\\') { // a single backslash: \
- escapedString += '\\\\'; // write out as: \\
- /* Currently escaped forward slashes break TJSONProtocol.
- * As it stands, we can simply pass forward slashes into
- * our strings across the wire without being escaped.
- * I think this is the protocol's bug, not thrift.js
- * } else if(ch === '/') { // a single forward slash: /
- * escapedString += '\\/'; // write out as \/
- * }
- */
+ } else if (ch === '\\') { // a single backslash
+ escapedString += '\\\\'; // write out as double backslash
} else if (ch === '\b') { // a single backspace: invisible
escapedString += '\\b'; // write out as: \b"
} else if (ch === '\f') { // a single formfeed: invisible
@@ -1025,7 +1017,9 @@
this.rstack = [];
this.rpos = [];
- if (typeof jQuery !== 'undefined') {
+ if (typeof JSON !== 'undefined' && typeof JSON.parse === 'function') {
+ this.robj = JSON.parse(this.transport.readAll());
+ } else if (typeof jQuery !== 'undefined') {
this.robj = jQuery.parseJSON(this.transport.readAll());
} else {
this.robj = eval(this.transport.readAll());
diff --git a/lib/js/test/README b/lib/js/test/README
index 6923794..9ad140e 100644
--- a/lib/js/test/README
+++ b/lib/js/test/README
@@ -1,7 +1,8 @@
Thrift Javascript Library
=========================
This browser based Apache Thrift implementation supports
-RPC clients using the JSON protocol over Http[s] with XHR.
+RPC clients using the JSON protocol over Http[s] with XHR
+and WebSocket.
License
-------
@@ -32,14 +33,18 @@
standard Apache Thrift test suite (thrift/test/ThriftTest.thrift).
The server supports Apache Thrift XHR and WebSocket clients.
-server_https.js is the same but uses SSL/TLS. The sec directory
-contains the server key and certificate used by the ssl server.
+server_https.js is the same but uses SSL/TLS. The server key
+and cert are pulled from the thrift/test/keys folder.
+
Both of these servers support WebSocket (the http: supports ws:,
and the https: support wss:).
-To run the test servers use: $ make check (requires
-the Apache Thrift Java branch and make check must have
-been run in thrift/lib/java previously) or run the grunt
+To run the client test with the Java test server use:
+$ make check (requires the Apache Thrift Java branch
+and make check must have been run in thrift/lib/java
+previously).
+
+To run the client tests with the Node servers run the grunt
build in the parent js directory (see README there).
Test Clients
@@ -54,10 +59,10 @@
-rw-r--r-- 1 randy randy 2847 Feb 9 06:31 testws.html
There are three html test driver files, all of which are
-QUnit based. test.html test the Apache Thrift jQuery
+QUnit based. test.html tests the Apache Thrift jQuery
generated code (thrift -gen js:jquery). The test-nojq.html
-Runs almost identical tests against normal JavaScript builds
+runs almost identical tests against normal JavaScript builds
(thrift -gen js). Both of the previous tests use the XHR
transport. The testws.html runs similar tests using the
WebSocket transport. The test*.js files are loaded by the
-html drivers and contain the actualApache Thrift tests.
+html drivers and contain the actual Apache Thrift tests.