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
181 lines (163 loc) · 6.98 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
package space.morphanone.webizen.events;
import com.sun.net.httpserver.HttpExchange;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizencore.events.ScriptEvent;
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 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.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class BasicRequestScriptEvent extends ScriptEvent {
public HttpExchange httpExchange;
public ElementTag contentType;
public ElementTag responseText;
public ElementTag responseCode;
public File responseFile;
public ElementTag parseFile;
private String requestType = getRequestType();
private String lowerRequestType = CoreUtilities.toLowerCase(requestType);
private static final Pattern tagPattern = Pattern.compile("<\\{([^\\}]*)\\}>");
private static final CharsetDecoder utfDecoder = Charset.forName("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;
this.contentType = null;
this.responseText = null;
this.responseCode = null;
this.responseFile = null;
this.parseFile = null;
fire();
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(this.contentType != null ? this.contentType.asString() : "text/html");
if (this.responseCode != null) {
response.setStatus(this.responseCode.asInt());
}
if (this.responseText != null || this.responseFile != null) {
if (this.responseText != null) {
response.write(this.responseText.asString().getBytes(StandardCharsets.UTF_8));
}
else if (this.parseFile.asBoolean()) {
FileInputStream input = new FileInputStream(this.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()) {
String parsed = TagManager.readSingleTag(m.group(1), reusableTagContext);
// If the parsed output is null, allow Denizen to handle the debugging
// and return "null"
if (parsed != null) {
String cleaned = parsed;
m.appendReplacement(s, Matcher.quoteReplacement(cleaned));
}
else {
m.appendReplacement(s, "null");
}
}
m.appendTail(s);
response.write(s.toString().getBytes(StandardCharsets.UTF_8));
}
else {
response.copyFileFrom(this.responseFile.toPath());
}
}
} catch (IOException e) {
Debug.echoError(e);
}
try {
response.send();
} catch (IOException e) {
Debug.echoError(e);
}
}
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith(lowerRequestType + " ");
}
@Override
public boolean matches(ScriptPath path) {
return path.eventArgLowerAt(1).equals("request");
}
@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;
}
responseCode = code;
}
else if (lower.startsWith("file:")) {
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), lower.substring(5));
if (!file.exists()) {
Debug.echoError("File '" + file + "' does not exist.");
return false;
}
responseFile = file;
parseFile = new ElementTag("false");
}
else if (lower.startsWith("parsed_file:")) {
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), lower.substring(12));
if (!file.exists()) {
Debug.echoError("File '" + file + "' does not exist.");
return false;
}
responseFile = file;
parseFile = new ElementTag("true");
}
else if (lower.startsWith("type:")) {
contentType = new ElementTag(lower.substring(5));
}
else {
responseText = new ElementTag(determinationObj.toString());
}
return true;
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(null, null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("address")) {
return new ElementTag(httpExchange.getRemoteAddress().toString());
}
else if (name.equals("query")) {
return new ElementTag(httpExchange.getRequestURI().getQuery());
}
else if (name.equals("request")) {
return new ElementTag(httpExchange.getRequestURI().getPath());
}
else if (name.equals("user_info")) {
return new ElementTag(httpExchange.getRequestURI().getUserInfo());
}
return super.getContext(name);
}
}