-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
WebGetCommand.java
308 lines (278 loc) · 12.1 KB
/
WebGetCommand.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.denizenscript.denizencore.scripts.commands.core;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.scheduling.Schedulable;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class WebGetCommand extends AbstractCommand implements Holdable {
public WebGetCommand() {
setName("webget");
setSyntax("webget [<url>] (post:<data>) (headers:<header>/<value>|...) (timeout:<duration>/{10s}) (savefile:<path>)");
setRequiredArguments(1, 6);
}
// <--[command]
// @Name Webget
// @Syntax webget [<url>] (data:<data>) (method:<method>) (headers:<header>/<value>|...) (timeout:<duration>/{10s}) (savefile:<path>)
// @Required 1
// @Maximum 6
// @Short Gets the contents of a web page or API response.
// @Group core
//
// @Description
// Connects to a webpage or API and downloads its contents, to be used via the save argument and corresponding entry tags.
//
// This should almost always be ~waited for.
//
// Note that while this will replace URL spaces to %20, you are responsible for any other necessary URL encoding.
// You may want to use the <@link tag ElementTag.url_encode> tag for this.
//
// Optionally, use "data:<data>" to specify a set of data to send to the server (changes the default method from GET to POST).
//
// Optionally, use "method:<method>" to specify the HTTP method to use in your request.
//
// Optionally, use "headers:" to specify a list of headers using a ListTag of key/value pairs separated by slashes.
//
// Optionally, use "savefile:" to specify a path to save the retrieved file to.
// This will remove the 'result' entry savedata.
// Path is relative to server base directory.
//
// Specify the "timeout:" to set how long the command should wait for a webpage to load before giving up. Defaults to 10 seconds.
//
// @Tags
// <entry[saveName].failed> returns whether the webget failed. A failure occurs when the status is not 2XX/3XX or webget failed to connect.
// <entry[saveName].result> returns the result of the webget. This is null only if webget failed to connect to the url.
// <entry[saveName].status> returns the HTTP status code of the webget. This is null only if webget failed to connect to the url.
// <ElementTag.url_encode>
//
// @Usage
// Use to download the google home page.
// - ~webget https://google.com save:google
// - narrate <entry[google].result>
//
// @Usage
// Use to save a webpage to your server's base directory
// - ~webget https://google.com savefile:google.html
//
// @Usage
// Use to post data to a server.
// - ~webget https://api.mojang.com/orders/statistics 'data:{"metricKeys":["item_sold_minecraft"]}' "headers:Content-type/application/json" save:request
// - narrate <entry[request].result>
//
// @Usage
// Use to retrieve and load an API response into yaml.
// - ~webget https://api.mojang.com/users/profiles/minecraft/<player.name> save:request
// - yaml loadtext:<entry[request].result> id:player_data
//
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("url")) {
scriptEntry.addObject("url", new ElementTag(arg.raw_value));
}
else if (!scriptEntry.hasObject("data")
&& arg.matchesPrefix("data", "post")) {
scriptEntry.addObject("data", arg.asElement());
}
else if (!scriptEntry.hasObject("method")
&& arg.matchesPrefix("method")
&& arg.matches("get", "post", "head", "options", "put", "delete", "trace")) {
scriptEntry.addObject("method", arg.asElement());
}
else if (!scriptEntry.hasObject("timeout")
&& arg.matchesPrefix("timeout", "t")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("timeout", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("headers")
&& arg.matchesPrefix("headers")) {
scriptEntry.addObject("headers", arg.asType(ListTag.class));
}
else if (!scriptEntry.hasObject("savefile")
&& arg.matchesPrefix("savefile")) {
scriptEntry.addObject("savefile", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("url")) {
throw new InvalidArgumentsException("Must have a valid URL!");
}
ElementTag url = scriptEntry.getElement("url");
if (!url.asString().startsWith("http://") && !url.asString().startsWith("https://")) {
throw new InvalidArgumentsException("Must have a valid (HTTP/HTTPS) URL! Attempted: " + url.asString());
}
scriptEntry.defaultObject("timeout", new DurationTag(10));
}
@Override
public void execute(final ScriptEntry scriptEntry) {
if (!DenizenCore.getImplementation().allowedToWebget()) {
Debug.echoError(scriptEntry.getResidingQueue(), "WebGet disabled by config!");
return;
}
final ElementTag url = scriptEntry.getElement("url");
final ElementTag data = scriptEntry.getElement("data");
final ElementTag method = scriptEntry.getElement("method");
final DurationTag timeout = scriptEntry.getObjectTag("timeout");
final ListTag headers = scriptEntry.getObjectTag("headers");
final ElementTag saveFile = scriptEntry.getElement("savefile");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), url.debug()
+ (data != null ? data.debug() : "")
+ (method != null ? method.debug() : "")
+ (timeout != null ? timeout.debug() : "")
+ (saveFile != null ? saveFile.debug() : "")
+ (headers != null ? headers.debug() : ""));
}
Thread thr = new Thread(new Runnable() {
@Override
public void run() {
webGet(scriptEntry, data, method, url, timeout, headers, saveFile);
}
});
thr.start();
}
public void buildResult(BufferedReader buffIn, StringBuilder sb) {
// Probably a better way to do this bit.
while (true) {
try {
String temp = buffIn.readLine();
if (temp == null) {
break;
}
sb.append(temp).append("\n");
}
catch (Exception ex) {
break;
}
}
}
public void writeToFile(InputStream in, ElementTag saveFile) throws Exception {
File file = new File(saveFile.asString());
if (!DenizenCore.getImplementation().canWriteToFile(file)) {
Debug.echoError("Cannot write to that file, as dangerous file paths have been disabled in the Denizen config.");
}
else {
FileOutputStream fout = new FileOutputStream(file);
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = in.read(buffer)) > 0) {
fout.write(buffer, 0, len);
}
fout.flush();
fout.close();
}
}
public void webGet(final ScriptEntry scriptEntry, final ElementTag data, ElementTag method, ElementTag urlp, DurationTag timeout, ListTag headers, ElementTag saveFile) {
BufferedReader buffIn = null;
HttpURLConnection uc = null;
try {
URL url = new URL(urlp.asString().replace(" ", "%20"));
uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
if (method != null) {
uc.setRequestMethod(method.asString().toUpperCase());
}
else if (data != null) {
uc.setRequestMethod("POST");
}
if (headers != null) {
for (String str : headers) {
int ind = str.indexOf('/');
if (ind > 0) {
uc.setRequestProperty(str.substring(0, ind), str.substring(ind + 1));
}
}
}
uc.setConnectTimeout((int) timeout.getMillis());
uc.connect();
if (data != null) {
uc.getOutputStream().write(data.asString().getBytes(StandardCharsets.UTF_8));
}
final int status = uc.getResponseCode();
final StringBuilder sb = new StringBuilder();
if (saveFile != null) {
writeToFile(uc.getInputStream(), saveFile);
}
else {
buffIn = new BufferedReader(new InputStreamReader(uc.getInputStream()));
buildResult(buffIn, sb);
buffIn.close();
buffIn = null;
}
DenizenCore.schedule(new Schedulable() {
@Override
public boolean tick(float seconds) {
scriptEntry.addObject("status", new ElementTag(status));
scriptEntry.addObject("failed", new ElementTag(status >= 200 && status < 400 ? "false" : "true"));
if (saveFile == null) {
scriptEntry.addObject("result", new ElementTag(sb.toString()));
}
scriptEntry.setFinished(true);
return false;
}
});
}
catch (Exception e) {
int tempStatus = -1;
final StringBuilder sb = new StringBuilder();
if (uc != null) {
try {
tempStatus = uc.getResponseCode();
if (saveFile != null) {
writeToFile(uc.getErrorStream(), saveFile);
}
else {
buffIn = new BufferedReader(new InputStreamReader(uc.getErrorStream()));
buildResult(buffIn, sb);
buffIn.close();
buffIn = null;
}
}
catch (Exception e2) {
Debug.echoError(e2);
}
}
else {
Debug.echoError(e);
}
final int status = tempStatus;
DenizenCore.schedule(new Schedulable() {
@Override
public boolean tick(float seconds) {
scriptEntry.addObject("failed", new ElementTag("true"));
if (status != -1) {
scriptEntry.addObject("status", new ElementTag(status));
if (saveFile == null) {
scriptEntry.addObject("result", new ElementTag(sb.toString()));
}
}
scriptEntry.setFinished(true);
return false;
}
});
}
finally {
try {
if (buffIn != null) {
buffIn.close();
}
}
catch (Exception e) {
Debug.echoError(e);
}
}
}
}