Skip to content

Commit 40068ad

Browse files
committed
add https module
Signed-off-by: noah the goodra <peterpan0413@live.com>
1 parent 0576344 commit 40068ad

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "https",
3+
"main": "./request.js"
4+
}

src/main/js/modules/https/request.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*************************************************************************
2+
## Https Module
3+
4+
For handling https requests. Not to be confused with the more robust
5+
and functional 'https' module bundled with Node.js.
6+
7+
### https.request() function
8+
9+
The https.request() function will fetch a web address asynchronously (on a
10+
separate thread)and pass the URL's response to a callback function
11+
which will be executed synchronously (on the main thread). In this
12+
way, https.request() can be used to fetch web content without blocking the
13+
main thread of execution.
14+
15+
#### Parameters
16+
17+
* request: The request details either a plain URL e.g. "https://scriptcraft.js/sample.json" or an object with the following properties...
18+
19+
- url: The URL of the request.
20+
- method: Should be one of the standard HTTPS methods, GET, POST, PUT, DELETE (defaults to GET).
21+
- params: A Javascript object with name-value pairs. This is for supplying parameters to the server.
22+
23+
* callback: The function to be called when the Web request has completed. This function takes the following parameters...
24+
- responseCode: The numeric response code from the server. If the server did not respond with 200 OK then the response parameter will be undefined.
25+
- response: A string (if the response is of type text) or object containing the HTTPS response body.
26+
27+
#### Example
28+
29+
The following example illustrates how to use https.request to make a request to a JSON web service and evaluate its response...
30+
31+
```javascript
32+
var jsResponse;
33+
var https = require('https');
34+
https.request('https://scriptcraftjs.org/sample.json',function(responseCode, responseBody){
35+
jsResponse = JSON.parse( responseBody );
36+
});
37+
```
38+
The following example illustrates a more complex use-case POSTing parameters to a CGI process on a server...
39+
40+
```javascript
41+
var https = require('https');
42+
https.request( {
43+
url: 'https://pixenate.com/pixenate/pxn8.pl',
44+
method: 'POST',
45+
params: {script: '[]'}
46+
},
47+
function( responseCode, responseBody ) {
48+
var jsObj = JSON.parse( responseBody );
49+
});
50+
```
51+
52+
***/
53+
54+
/*global exports, encodeURI, server, __plugin, setTimeout*/
55+
function paramsToString( params ) {
56+
var result = '',
57+
paramNames = [],
58+
i;
59+
for ( i in params ) {
60+
paramNames.push( i );
61+
}
62+
for ( i = 0; i < paramNames.length; i++ ) {
63+
result += paramNames[i] + '=' + encodeURI( params[ paramNames[i] ] );
64+
if ( i < paramNames.length-1 )
65+
result += '&';
66+
}
67+
return result;
68+
}
69+
function invokeNow( fn ){
70+
if (__plugin.bukkit){
71+
server.scheduler.runTask( __plugin, fn);
72+
return;
73+
}
74+
if (__plugin.canary){
75+
fn();
76+
return;
77+
}
78+
}
79+
function invokeLater( fn ){
80+
if (__plugin.bukkit){
81+
server.scheduler.runTaskAsynchronously( __plugin, fn);
82+
return;
83+
}
84+
if (__plugin.canary){
85+
setTimeout(fn,20);
86+
return;
87+
}
88+
}
89+
exports.request = function( request, callback ) {
90+
invokeLater( function() {
91+
var url, paramsAsString, conn, requestMethod;
92+
if (typeof request === 'string'){
93+
url = request;
94+
requestMethod = 'GET';
95+
}else{
96+
url = request.url;
97+
paramsAsString = paramsToString( request.params );
98+
if ( request.method ) {
99+
requestMethod = request.method;
100+
} else {
101+
requestMethod = 'GET';
102+
}
103+
if ( requestMethod == 'GET' && request.params ) {
104+
// append each parameter to the URL
105+
url = request.url + '?' + paramsAsString;
106+
}
107+
}
108+
conn = new javax.net.ssl.HttpsURLConnection(new java.net.URL(url)).openConnection();
109+
conn.requestMethod = requestMethod;
110+
conn.doOutput = true;
111+
conn.instanceFollowRedirects = false;
112+
113+
if ( conn.requestMethod == 'POST' ) {
114+
conn.doInput = true;
115+
// put each parameter in the outputstream
116+
conn.setRequestProperty('Content-Type', 'application/x-www-form-urlencoded');
117+
conn.setRequestProperty('charset', 'utf-8');
118+
conn.setRequestProperty('Content-Length', '' + paramsAsString.length);
119+
conn.useCaches =false ;
120+
var wr = new java.io.DataOutputStream(conn.getOutputStream ());
121+
wr.writeBytes(paramsAsString);
122+
wr.flush();
123+
wr.close();
124+
}
125+
var rc = conn.responseCode;
126+
var response;
127+
var stream;
128+
if ( rc == 200 ) {
129+
stream = conn.getInputStream();
130+
response = new java.util.Scanner( stream ).useDelimiter("\\A").next();
131+
}
132+
invokeNow( function( ) {
133+
callback( rc, response );
134+
});
135+
});
136+
137+
};

0 commit comments

Comments
 (0)