fix issue #213 http request broken in canarymod

This commit is contained in:
walterhiggins 2015-02-16 22:09:55 +00:00
parent d083da049a
commit 1f122cb2cb

View file

@ -28,28 +28,31 @@ 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... The following example illustrates how to use http.request to make a request to a JSON web service and evaluate its response...
```javascript
var jsResponse; var jsResponse;
var http = require('./http/request'); var http = require('request');
http.request("http://scriptcraftjs.org/sample.json",function(responseCode, responseBody){ http.request('http://scriptcraftjs.org/sample.json',function(responseCode, responseBody){
jsResponse = eval("(" + 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... ```javascript
var http = require('http');
var http = require('./http/request'); http.request( {
http.request(
{
url: 'http://pixenate.com/pixenate/pxn8.pl', url: 'http://pixenate.com/pixenate/pxn8.pl',
method: 'POST', method: 'POST',
params: {script: '[]'} params: {script: '[]'}
}, },
function( responseCode, responseBody ) { function( responseCode, responseBody ) {
var jsObj = eval('(' + responseBody + ')'); var jsObj = JSON.parse( responseBody );
}); });
```
***/ ***/
exports.request = function( request, callback ) {
var paramsToString = function( params ) { /*global exports, encodeURI, server, __plugin*/
function paramsToString( params ) {
var result = '', var result = '',
paramNames = [], paramNames = [],
i; i;
@ -62,9 +65,29 @@ exports.request = function( request, callback ) {
result += '&'; result += '&';
} }
return result; return result;
}; }
function invokeNow( fn ){
server.scheduler.runTaskAsynchronously( __plugin, function() { 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; var url, paramsAsString, conn, requestMethod;
if (typeof request === 'string'){ if (typeof request === 'string'){
url = request; url = request;
@ -94,7 +117,7 @@ exports.request = function( request, callback ) {
conn.setRequestProperty('charset', 'utf-8'); conn.setRequestProperty('charset', 'utf-8');
conn.setRequestProperty('Content-Length', '' + paramsAsString.length); conn.setRequestProperty('Content-Length', '' + paramsAsString.length);
conn.useCaches =false ; conn.useCaches =false ;
wr = new java.io.DataOutputStream(conn.getOutputStream ()); var wr = new java.io.DataOutputStream(conn.getOutputStream ());
wr.writeBytes(paramsAsString); wr.writeBytes(paramsAsString);
wr.flush(); wr.flush();
wr.close(); wr.close();
@ -106,8 +129,9 @@ exports.request = function( request, callback ) {
stream = conn.getInputStream(); stream = conn.getInputStream();
response = new java.util.Scanner( stream ).useDelimiter("\\A").next(); response = new java.util.Scanner( stream ).useDelimiter("\\A").next();
} }
server.scheduler.runTask( __plugin, function( ) { invokeNow( function( ) {
callback( rc, response ); callback( rc, response );
}); });
}); });
}; };