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,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... The following example illustrates how to use http.request to make a request to a JSON web service and evaluate its response...
var jsResponse; ```javascript
var http = require('./http/request'); var jsResponse;
http.request("http://scriptcraftjs.org/sample.json",function(responseCode, responseBody){ var http = require('request');
jsResponse = eval("(" + responseBody + ")"); 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... ```javascript
var http = require('http');
var http = require('./http/request'); http.request( {
http.request( url: 'http://pixenate.com/pixenate/pxn8.pl',
{ method: 'POST',
url: 'http://pixenate.com/pixenate/pxn8.pl', params: {script: '[]'}
method: 'POST', },
params: {script: '[]'} function( responseCode, responseBody ) {
}, var jsObj = JSON.parse( responseBody );
function( responseCode, responseBody ) { });
var jsObj = eval('(' + responseBody + ')'); ```
});
***/ ***/
exports.request = function( request, callback ) {
var paramsToString = function( params ) { /*global exports, encodeURI, server, __plugin*/
var result = '', function paramsToString( params ) {
var result = '',
paramNames = [], paramNames = [],
i; i;
for ( i in params ) { for ( i in params ) {
paramNames.push( i ); paramNames.push( i );
} }
for ( i = 0; i < paramNames.length; i++ ) { for ( i = 0; i < paramNames.length; i++ ) {
result += paramNames[i] + '=' + encodeURI( params[ paramNames[i] ] ); result += paramNames[i] + '=' + encodeURI( params[ paramNames[i] ] );
if ( i < paramNames.length-1 ) if ( i < paramNames.length-1 )
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 );
}); });
}); });
}; };