-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathrequest.js
More file actions
137 lines (122 loc) · 4.3 KB
/
request.js
File metadata and controls
137 lines (122 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*************************************************************************
## Https Module
For handling https requests. Not to be confused with the more robust
and functional 'https' module bundled with Node.js.
### https.request() function
The https.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, https.request() can be used to fetch web content without blocking the
main thread of execution.
#### Parameters
* request: The request details either a plain URL e.g. "https://scriptcraft.js/sample.json" or an object with the following properties...
- url: The URL of the request.
- method: Should be one of the standard HTTPS 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 HTTPS response body.
#### Example
The following example illustrates how to use https.request to make a request to a JSON web service and evaluate its response...
```javascript
var jsResponse;
var https = require('https');
https.request('https://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...
```javascript
var https = require('https');
https.request( {
url: 'https://pixenate.com/pixenate/pxn8.pl',
method: 'POST',
params: {script: '[]'}
},
function( responseCode, responseBody ) {
var jsObj = JSON.parse( responseBody );
});
```
***/
/*global exports, encodeURI, server, __plugin, setTimeout*/
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;
}
function invokeNow( fn ){
if (__plugin.bukkit){
server.scheduler.runTask( __plugin, fn);
return;
}
if (__plugin.canary){
fn();
return;
}
}
function invokeLater( fn ){
if (__plugin.bukkit){
server.scheduler.runTaskAsynchronously( __plugin, fn);
return;
}
if (__plugin.canary){
setTimeout(fn,20);
return;
}
}
exports.request = function( request, callback ) {
invokeLater( function() {
var url, paramsAsString, conn, requestMethod;
if (typeof request === 'string'){
url = request;
requestMethod = 'GET';
}else{
url = request.url;
paramsAsString = paramsToString( request.params );
if ( request.method ) {
requestMethod = request.method;
} else {
requestMethod = 'GET';
}
if ( requestMethod == 'GET' && request.params ) {
// append each parameter to the URL
url = request.url + '?' + paramsAsString;
}
}
conn = new javax.net.ssl.HttpsURLConnection(new java.net.URL(url)).openConnection();
conn.requestMethod = requestMethod;
conn.doOutput = true;
conn.instanceFollowRedirects = false;
if ( conn.requestMethod == 'POST' ) {
conn.doInput = true;
// put each parameter in the outputstream
conn.setRequestProperty('Content-Type', 'application/x-www-form-urlencoded');
conn.setRequestProperty('charset', 'utf-8');
conn.setRequestProperty('Content-Length', '' + paramsAsString.length);
conn.useCaches =false ;
var wr = new java.io.DataOutputStream(conn.getOutputStream ());
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();
}
invokeNow( function( ) {
callback( rc, response );
});
});
};