From 1f122cb2cbec7226fa2cc60d9cecd29e57f0fb06 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Mon, 16 Feb 2015 22:09:55 +0000 Subject: [PATCH] fix issue #213 http request broken in canarymod --- src/main/js/modules/http/request.js | 92 ++++++++++++++++++----------- 1 file changed, 58 insertions(+), 34 deletions(-) diff --git a/src/main/js/modules/http/request.js b/src/main/js/modules/http/request.js index 2ff7918..fa65915 100644 --- a/src/main/js/modules/http/request.js +++ b/src/main/js/modules/http/request.js @@ -28,43 +28,66 @@ main thread of execution. The following example illustrates how to use http.request to make a request to a JSON web service and evaluate its response... - var jsResponse; - var http = require('./http/request'); - http.request("http://scriptcraftjs.org/sample.json",function(responseCode, responseBody){ - jsResponse = eval("(" + responseBody + ")"); - }); +```javascript +var jsResponse; +var http = require('request'); +http.request('http://scriptcraftjs.org/sample.json',function(responseCode, responseBody){ + jsResponse = JSON.parse( responseBody ); +}); +``` +The following example illustrates a more complex use-case POSTing parameters to a CGI process on a server... -... The following example illustrates a more complex use-case POSTing parameters to a CGI process on a server... - - var http = require('./http/request'); - http.request( - { - url: 'http://pixenate.com/pixenate/pxn8.pl', - method: 'POST', - params: {script: '[]'} - }, - function( responseCode, responseBody ) { - var jsObj = eval('(' + responseBody + ')'); - }); +```javascript +var http = require('http'); +http.request( { + url: 'http://pixenate.com/pixenate/pxn8.pl', + method: 'POST', + params: {script: '[]'} + }, + function( responseCode, responseBody ) { + var jsObj = JSON.parse( responseBody ); + }); +``` ***/ -exports.request = function( request, callback ) { - var paramsToString = function( params ) { - var result = '', + +/*global exports, encodeURI, server, __plugin*/ +function paramsToString( params ) { + var result = '', paramNames = [], i; - for ( i in params ) { - paramNames.push( i ); - } - for ( i = 0; i < paramNames.length; i++ ) { - result += paramNames[i] + '=' + encodeURI( params[ paramNames[i] ] ); - if ( i < paramNames.length-1 ) - result += '&'; - } - return result; - }; - - server.scheduler.runTaskAsynchronously( __plugin, function() { + for ( i in params ) { + paramNames.push( i ); + } + for ( i = 0; i < paramNames.length; i++ ) { + result += paramNames[i] + '=' + encodeURI( params[ paramNames[i] ] ); + if ( i < paramNames.length-1 ) + result += '&'; + } + return result; +} +function invokeNow( fn ){ + if (__plugin.bukkit){ + server.schedule.runTask( __plugin, fn); + return; + } + if (__plugin.canary){ + fn(); + return; + } +} +function invokeLater( fn ){ + if (__plugin.bukkit){ + server.scheduler.runTaskAsynchronously( __plugin, fn); + return; + } + if (__plugin.canary){ + fn(); + return; + } +} +exports.request = function( request, callback ) { + invokeLater( function() { var url, paramsAsString, conn, requestMethod; if (typeof request === 'string'){ url = request; @@ -94,7 +117,7 @@ exports.request = function( request, callback ) { conn.setRequestProperty('charset', 'utf-8'); conn.setRequestProperty('Content-Length', '' + paramsAsString.length); conn.useCaches =false ; - wr = new java.io.DataOutputStream(conn.getOutputStream ()); + var wr = new java.io.DataOutputStream(conn.getOutputStream ()); wr.writeBytes(paramsAsString); wr.flush(); wr.close(); @@ -106,8 +129,9 @@ exports.request = function( request, callback ) { stream = conn.getInputStream(); response = new java.util.Scanner( stream ).useDelimiter("\\A").next(); } - server.scheduler.runTask( __plugin, function( ) { + invokeNow( function( ) { callback( rc, response ); }); }); + };