2013-03-31 14:54:38 +01:00
|
|
|
/*************************************************************************
|
2014-01-05 15:20:29 +00:00
|
|
|
## Http Module
|
|
|
|
|
|
|
|
For handling http requests. Not to be confused with the more robust
|
|
|
|
and functional 'http' module bundled with Node.js.
|
|
|
|
|
|
|
|
### http.request() function
|
2013-12-28 08:44:40 +00:00
|
|
|
|
2013-03-31 14:54:38 +01:00
|
|
|
The http.request() function will fetch a web address asynchronously (on a
|
|
|
|
separate thread)and pass the URL's response to a callback function
|
|
|
|
which will be executed synchronously (on the main thread). In this
|
|
|
|
way, http.request() can be used to fetch web content without blocking the
|
|
|
|
main thread of execution.
|
|
|
|
|
2014-01-05 15:20:29 +00:00
|
|
|
#### Parameters
|
2013-03-31 14:54:38 +01:00
|
|
|
|
|
|
|
* request: The request details either a plain URL e.g. "http://scriptcraft.js/sample.json" or an object with the following properties...
|
|
|
|
|
|
|
|
- url: The URL of the request.
|
|
|
|
- method: Should be one of the standard HTTP methods, GET, POST, PUT, DELETE (defaults to GET).
|
|
|
|
- params: A Javascript object with name-value pairs. This is for supplying parameters to the server.
|
|
|
|
|
|
|
|
* callback: The function to be called when the Web request has completed. This function takes the following parameters...
|
|
|
|
- responseCode: The numeric response code from the server. If the server did not respond with 200 OK then the response parameter will be undefined.
|
|
|
|
- response: A string (if the response is of type text) or object containing the HTTP response body.
|
|
|
|
|
2014-01-05 15:20:29 +00:00
|
|
|
#### Example
|
2013-12-28 08:44:40 +00:00
|
|
|
|
2013-03-31 14:54:38 +01:00
|
|
|
The following example illustrates how to use http.request to make a request to a JSON web service and evaluate its response...
|
|
|
|
|
2015-02-16 22:09:55 +00:00
|
|
|
```javascript
|
|
|
|
var jsResponse;
|
2016-04-07 15:07:05 +02:00
|
|
|
var http = require('http');
|
2015-02-16 22:09:55 +00:00
|
|
|
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...
|
2013-03-31 14:54:38 +01:00
|
|
|
|
2015-02-16 22:09:55 +00:00
|
|
|
```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 );
|
|
|
|
});
|
|
|
|
```
|
2013-03-31 14:54:38 +01:00
|
|
|
|
|
|
|
***/
|
2015-02-16 22:09:55 +00:00
|
|
|
|
2015-02-21 10:43:12 +00:00
|
|
|
/*global exports, encodeURI, server, __plugin, setTimeout*/
|
2015-02-16 22:09:55 +00:00
|
|
|
function paramsToString( params ) {
|
|
|
|
var result = '',
|
2014-01-29 19:49:15 +00:00
|
|
|
paramNames = [],
|
|
|
|
i;
|
2015-02-16 22:09:55 +00:00
|
|
|
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){
|
2015-04-28 20:09:59 +08:00
|
|
|
server.scheduler.runTask( __plugin, fn);
|
2015-02-16 22:09:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (__plugin.canary){
|
|
|
|
fn();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function invokeLater( fn ){
|
|
|
|
if (__plugin.bukkit){
|
|
|
|
server.scheduler.runTaskAsynchronously( __plugin, fn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (__plugin.canary){
|
2015-02-21 10:43:12 +00:00
|
|
|
setTimeout(fn,20);
|
2015-02-16 22:09:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.request = function( request, callback ) {
|
|
|
|
invokeLater( function() {
|
2014-01-29 19:49:15 +00:00
|
|
|
var url, paramsAsString, conn, requestMethod;
|
|
|
|
if (typeof request === 'string'){
|
|
|
|
url = request;
|
|
|
|
requestMethod = 'GET';
|
|
|
|
}else{
|
2014-06-29 16:09:22 +02:00
|
|
|
url = request.url;
|
2014-01-29 19:49:15 +00:00
|
|
|
paramsAsString = paramsToString( request.params );
|
|
|
|
if ( request.method ) {
|
2014-06-29 16:22:37 +02:00
|
|
|
requestMethod = request.method;
|
2014-01-29 19:49:15 +00:00
|
|
|
} else {
|
2014-06-29 16:22:37 +02:00
|
|
|
requestMethod = 'GET';
|
2014-01-29 19:49:15 +00:00
|
|
|
}
|
|
|
|
if ( requestMethod == 'GET' && request.params ) {
|
2014-06-29 16:22:37 +02:00
|
|
|
// append each parameter to the URL
|
|
|
|
url = request.url + '?' + paramsAsString;
|
2014-01-29 19:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
conn = new java.net.URL( url ).openConnection();
|
|
|
|
conn.requestMethod = requestMethod;
|
|
|
|
conn.doOutput = true;
|
|
|
|
conn.instanceFollowRedirects = false;
|
2013-12-08 12:16:41 +00:00
|
|
|
|
2014-01-29 19:49:15 +00:00
|
|
|
if ( conn.requestMethod == 'POST' ) {
|
|
|
|
conn.doInput = true;
|
|
|
|
// put each parameter in the outputstream
|
2014-06-29 16:22:37 +02:00
|
|
|
conn.setRequestProperty('Content-Type', 'application/x-www-form-urlencoded');
|
2014-01-29 19:49:15 +00:00
|
|
|
conn.setRequestProperty('charset', 'utf-8');
|
|
|
|
conn.setRequestProperty('Content-Length', '' + paramsAsString.length);
|
|
|
|
conn.useCaches =false ;
|
2015-02-16 22:09:55 +00:00
|
|
|
var wr = new java.io.DataOutputStream(conn.getOutputStream ());
|
2014-01-29 19:49:15 +00:00
|
|
|
wr.writeBytes(paramsAsString);
|
|
|
|
wr.flush();
|
|
|
|
wr.close();
|
|
|
|
}
|
|
|
|
var rc = conn.responseCode;
|
|
|
|
var response;
|
|
|
|
var stream;
|
|
|
|
if ( rc == 200 ) {
|
|
|
|
stream = conn.getInputStream();
|
|
|
|
response = new java.util.Scanner( stream ).useDelimiter("\\A").next();
|
|
|
|
}
|
2015-02-16 22:09:55 +00:00
|
|
|
invokeNow( function( ) {
|
2014-01-29 19:49:15 +00:00
|
|
|
callback( rc, response );
|
2013-03-31 14:54:38 +01:00
|
|
|
});
|
2014-01-29 19:49:15 +00:00
|
|
|
});
|
2015-02-16 22:09:55 +00:00
|
|
|
|
2013-03-31 14:54:38 +01:00
|
|
|
};
|