This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
BasicRequestScriptEvent.java
221 lines (201 loc) · 9.03 KB
/
BasicRequestScriptEvent.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
package space.morphanone.webizen.events;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.sun.net.httpserver.HttpExchange;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import org.bukkit.scheduler.BukkitRunnable;
import space.morphanone.webizen.fake.FakeScriptEntry;
import space.morphanone.webizen.server.ResponseWrapper;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class BasicRequestScriptEvent extends BukkitScriptEvent {
public HttpExchange httpExchange;
public class ResponseOptions {
public ElementTag contentType;
public ElementTag responseText;
public ElementTag responseCode;
public File responseFile;
public ElementTag parseFile;
}
public ResponseOptions scriptResponse;
private String requestType = getRequestType();
private String lowerRequestType = CoreUtilities.toLowerCase(requestType);
private static final Pattern tagPattern = Pattern.compile("<\\{([^\\}]*)\\}>");
private static final CharsetDecoder utfDecoder = StandardCharsets.UTF_8.newDecoder();
private static final FakeScriptEntry reusableScriptEntry = FakeScriptEntry.generate();
private static final BukkitTagContext reusableTagContext = new BukkitTagContext(reusableScriptEntry);
public abstract String getRequestType();
public void fire(HttpExchange httpExchange) {
this.httpExchange = httpExchange;
scriptResponse = new ResponseOptions();
CompletableFuture future = new CompletableFuture();
BukkitScriptEvent altEvent = (BukkitScriptEvent) clone();
new BukkitRunnable() {
@Override
public void run() {
altEvent.fire();
future.complete(null);
}
}.runTask(Denizen.getInstance());
try {
future.get();
}
catch (InterruptedException | ExecutionException ex) {
Debug.echoError(ex);
}
ResponseWrapper response = new ResponseWrapper(httpExchange);
try {
// Add context to fake ScriptQueue for parsed file tags
reusableScriptEntry.getResidingQueue().setContextSource(this);
// Set HTTP response headers before anything else
response.setContentType(scriptResponse.contentType != null ? scriptResponse.contentType.asString() : "text/html");
if (scriptResponse.responseCode != null) {
response.setStatus(scriptResponse.responseCode.asInt());
}
if (scriptResponse.responseText != null || scriptResponse.responseFile != null) {
if (scriptResponse.responseText != null) {
response.write(scriptResponse.responseText.asString().getBytes(StandardCharsets.UTF_8));
}
else if (scriptResponse.parseFile.asBoolean()) {
FileInputStream input = new FileInputStream(scriptResponse.responseFile);
FileChannel channel = input.getChannel();
ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
StringBuffer s = new StringBuffer();
Matcher m = tagPattern.matcher(utfDecoder.decode(bbuf));
while (m.find()) {
ObjectTag parsed = TagManager.parseTextToTag(m.group(1), reusableTagContext).parse(reusableTagContext);
// If the parsed output is null, allow Denizen to handle the debugging
// and return "null"
m.appendReplacement(s, parsed != null ? Matcher.quoteReplacement(parsed.toString()) : "null");
}
m.appendTail(s);
response.write(s.toString().getBytes(StandardCharsets.UTF_8));
}
else {
response.copyFileFrom(scriptResponse.responseFile.toPath());
}
}
} catch (IOException e) {
Debug.echoError(e);
}
try {
response.send();
} catch (IOException e) {
Debug.echoError(e);
}
}
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith(lowerRequestType + " request")) {
return false;
}
return true;
}
@Override
public String getName() {
return requestType + "Request";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String lower = CoreUtilities.toLowerCase(determinationObj.toString());
if (lower.startsWith("code:")) {
ElementTag code = new ElementTag(lower.substring(5));
if (!code.isInt()) {
Debug.echoError("Determination for 'code' must be a valid number.");
return false;
}
scriptResponse.responseCode = code;
}
else if (lower.startsWith("file:")) {
File file = new File(Denizen.getInstance().getDataFolder(), determinationObj.toString().substring(5));
if (!file.exists()) {
Debug.echoError("File '" + file + "' does not exist.");
return false;
}
if (!Utilities.canReadFile(file)) {
Debug.echoError("File '" + file + "' is restricted from access by the Denizen config.");
return false;
}
scriptResponse.responseFile = file;
scriptResponse.parseFile = new ElementTag("false");
}
else if (lower.startsWith("parsed_file:")) {
File file = new File(Denizen.getInstance().getDataFolder(), determinationObj.toString().substring(12));
if (!file.exists()) {
Debug.echoError("File '" + file + "' does not exist.");
return false;
}
if (!Utilities.canReadFile(file)) {
Debug.echoError("File '" + file + "' is restricted from access by the Denizen config.");
return false;
}
scriptResponse.responseFile = file;
scriptResponse.parseFile = new ElementTag("true");
}
else if (lower.startsWith("type:")) {
scriptResponse.contentType = new ElementTag(lower.substring(5));
}
else {
scriptResponse.responseText = new ElementTag(determinationObj.toString());
}
return true;
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(null, null);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "address":
return new ElementTag(httpExchange.getRemoteAddress().toString());
case "query": {
String query = httpExchange.getRequestURI().getQuery();
return new ElementTag(query != null ? query : "");
}
case "query_map": {
MapTag mappedValues = new MapTag();
String query = httpExchange.getRequestURI().getQuery();
if (query != null) {
for (String value : CoreUtilities.split(query, '&')) {
List<String> split = CoreUtilities.split(value, '=', 2);
try {
String split_key = java.net.URLDecoder.decode(split.get(0), "UTF-8");
String split_value = java.net.URLDecoder.decode(split.get(1), "UTF-8");
mappedValues.putObject(split_key, new ElementTag(split_value));
}
catch (UnsupportedEncodingException e) {
Debug.echoError(e);
}
}
}
return mappedValues;
}
case "request":
return new ElementTag(httpExchange.getRequestURI().getPath());
case "user_info":
return new ElementTag(httpExchange.getRequestURI().getUserInfo());
}
return super.getContext(name);
}
}