-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathREPLContentServer.java
More file actions
311 lines (273 loc) · 13.7 KB
/
REPLContentServer.java
File metadata and controls
311 lines (273 loc) · 13.7 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
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
309
310
311
/*
* Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.rascalmpl.repl.http;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CancellationException;
import java.util.function.Function;
import org.rascalmpl.library.lang.json.internal.JsonValueWriter;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.values.ValueFactoryFactory;
import org.rascalmpl.values.functions.IFunction;
import com.google.gson.stream.JsonWriter;
import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Response.Status;
import io.usethesource.vallang.IBool;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IMap;
import io.usethesource.vallang.IMapWriter;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.IWithKeywordParameters;
import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.type.Type;
import io.usethesource.vallang.type.TypeFactory;
import io.usethesource.vallang.type.TypeStore;
public class REPLContentServer extends NanoHTTPD {
private static final IValueFactory vf = ValueFactoryFactory.getValueFactory();
private Function<IValue, IValue> callback;
private long lastServedAt = 0L;
public REPLContentServer(int port, Function<IValue, IValue> callback) {
super(port);
this.callback = callback;
this.lastServedAt = System.currentTimeMillis();
}
public void updateCallback(Function<IValue, IValue> callback) {
this.callback = callback;
lastServedAt = System.currentTimeMillis();
}
/**
* @return the system time at which the last page was served by this server,
* for GC purposes.
*/
public long getLastServedAt() {
return lastServedAt;
}
public Response serve(String uri, Method method, java.util.Map<String,String> headers, java.util.Map<String,String> parms, java.util.Map<String,String> files) {
try {
this.lastServedAt = System.currentTimeMillis();
IConstructor request = makeRequest(uri, method, headers, parms, files);
IValue rascalResponse = callback.apply(request);
return translateResponse(method, rascalResponse);
}
catch (CancellationException e) {
stop();
return newFixedLengthResponse(Status.INTERNAL_ERROR, MIME_HTML, "Shutting down!");
}
catch (Throwable t) {
return handleGeneralThrowable(t);
}
}
private Response handleGeneralThrowable(Throwable e) {
StringWriter str = new StringWriter();
PrintWriter print = new PrintWriter(str);
print.append("Exception while serving content:</br>");
print.append("<pre>");
e.printStackTrace(print);
print.append("</pre>");
print.flush();
return newFixedLengthResponse(Status.NOT_FOUND, MIME_HTML, str.toString());
}
private IConstructor makeRequest(String path, Method method, Map<String, String> headers,
Map<String, String> parms, Map<String, String> files) throws FactTypeUseException, IOException {
Map<String,IValue> kws = new HashMap<>();
kws.put("parameters", makeMap(parms));
kws.put("uploads", makeMap(files));
kws.put("headers", makeMap(headers));
switch (method) {
case HEAD:
return vf.constructor(head, new IValue[]{vf.string(path)}, kws);
case DELETE:
return vf.constructor(delete, new IValue[]{vf.string(path)}, kws);
case GET:
return vf.constructor(get, new IValue[]{vf.string(path)}, kws);
case PUT:
// TODO: PUT
// return vf.constructor(put, new IValue[]{vf.string(path), getContent(files, "content")}, kws);
case POST:
// TODO POST
// return vf.constructor(post, new IValue[]{vf.string(path), getContent(files, "postData")}, kws);
default:
throw new IOException("Unhandled request " + method);
}
}
public static Response translateResponse(Method method, IValue value) throws IOException {
IConstructor cons = (IConstructor) value;
switch (cons.getName()) {
case "fileResponse":
return translateFileResponse(method, cons);
case "jsonResponse":
return translateJsonResponse(method, cons);
case "response":
return translateTextResponse(method, cons);
default:
throw new IOException("Unknown response kind: " + value);
}
}
private static Response translateJsonResponse(Method method, IConstructor cons) {
IMap header = (IMap) cons.get("header");
IValue data = cons.get("val");
Status status = translateStatus((IConstructor) cons.get("status"));
IWithKeywordParameters<? extends IConstructor> kws = cons.asWithKeywordParameters();
IValue dtf = kws.getParameter("dateTimeFormat");
IValue dai = kws.getParameter("dateTimeAsInt");
IValue ras = kws.getParameter("rationalsAsString");
IValue formatters = kws.getParameter("formatter");
IValue ecn = kws.getParameter("explicitConstructorNames");
IValue edt = kws.getParameter("explicitDataTypes");
IValue fpo = kws.getParameter("fileLocationsAsPathOnly");
JsonValueWriter writer = new JsonValueWriter()
.setCalendarFormat(dtf != null ? ((IString) dtf).getValue() : "yyyy-MM-dd\'T\'HH:mm:ss\'Z\'")
.setFormatters((IFunction) formatters)
.setDatesAsInt(dai != null ? ((IBool) dai).getValue() : true)
.setRationalsAsString(ras != null ? ((IBool) ras).getValue() : false)
.setExplicitConstructorNames(ecn != null ? ((IBool) ecn).getValue() : false)
.setExplicitDataTypes(edt != null ? ((IBool) edt).getValue() : false)
.setFileLocationsAsPathOnly(fpo != null ? ((IBool) fpo).getValue() : true);
;
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonWriter out = new JsonWriter(new OutputStreamWriter(baos, Charset.forName("UTF8")));
writer.write(out, data);
out.flush();
out.close();
Response response = newFixedLengthResponse(status, "application/json", new ByteArrayInputStream(baos.toByteArray()), baos.size());
addHeaders(response, header);
return response;
}
catch (IOException e) {
// this should not happen in theory
throw new RuntimeException("Could not create piped inputstream");
}
}
private static Response translateFileResponse(Method method, IConstructor cons) {
ISourceLocation l = (ISourceLocation) cons.get("file");
IString mimeType = (IString) cons.get("mimeType");
IMap header = (IMap) cons.get("header");
Response response;
try {
response = newChunkedResponse(Status.OK, mimeType.getValue(), URIResolverRegistry.getInstance().getInputStream(l));
addHeaders(response, header);
return response;
} catch (IOException e) {
return newFixedLengthResponse(Status.NOT_FOUND, "text/plain", l + " not found.\n" + e);
}
}
private static Response translateTextResponse(Method method, IConstructor cons) {
IString mimeType = (IString) cons.get("mimeType");
IMap header = (IMap) cons.get("header");
IString data = (IString) cons.get("content");
Status status = translateStatus((IConstructor) cons.get("status"));
if (method != Method.HEAD) {
switch (status) {
case BAD_REQUEST:
case UNAUTHORIZED:
case NOT_FOUND:
case FORBIDDEN:
case RANGE_NOT_SATISFIABLE:
case INTERNAL_ERROR:
if (data.length() == 0) {
data = vf.string(status.getDescription());
}
default:
break;
}
}
try {
var fixedContentType = new ContentType(mimeType.getValue()).tryUTF8();
Response response = newChunkedResponse(status, fixedContentType.getContentTypeHeader(), toInputStream(data, Charset.forName(fixedContentType.getEncoding())));
addHeaders(response, header);
return response;
}
catch (IOException e) {
return newFixedLengthResponse(Status.INTERNAL_ERROR, MIME_HTML, e.getMessage());
}
}
private static InputStream toInputStream(IString data, Charset encoding) throws IOException {
return new ByteArrayInputStream(data.getValue().getBytes(encoding));
}
private static void addHeaders(Response response, IMap header) {
// TODO add first class support for cache control on the Rascal side. For
// now we prevent any form of client-side caching with this.. hopefully.
response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.addHeader("Pragma", "no-cache");
response.addHeader("Expires", "0");
for (IValue key : header) {
response.addHeader(((IString) key).getValue(), ((IString) header.get(key)).getValue());
}
}
private static Status translateStatus(IConstructor cons) {
return statusValues.get(cons);
}
private IMap makeMap(Map<String, String> headers) {
IMapWriter writer = vf.mapWriter();
for (Entry<String, String> entry : headers.entrySet()) {
writer.put(vf.string(entry.getKey()), vf.string(entry.getValue()));
}
return writer.done();
}
// these are statics for quick access of and creation of typed Rascal values:
private final static Map<IConstructor,Status> statusValues = new HashMap<>();
public final static Type requestType;
private final static Type get;
private final static Type head;
private final static Type delete;
static {
TypeFactory tf = TypeFactory.getInstance();
TypeStore store = new TypeStore();
Type statusType = tf.abstractDataType(store, "Status");
statusValues.put(vf.constructor(tf.constructor(store, statusType, "ok")), Status.OK);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "created")), Status.CREATED);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "accepted")), Status.ACCEPTED);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "noContent")), Status.NO_CONTENT);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "partialContent")), Status.PARTIAL_CONTENT);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "redirect")), Status.REDIRECT);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "notModified")), Status.NOT_MODIFIED);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "badRequest")), Status.BAD_REQUEST);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "unauthorized")), Status.UNAUTHORIZED);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "forbidden")), Status.FORBIDDEN);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "notFound")), Status.NOT_FOUND);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "rangeNotSatisfiable")), Status.RANGE_NOT_SATISFIABLE);
statusValues.put(vf.constructor(tf.constructor(store, statusType, "internalError")), Status.INTERNAL_ERROR);
requestType = tf.abstractDataType(store, "Request");
get = tf.constructor(store, requestType, "get", tf.stringType(), "path");
delete = tf.constructor(store, requestType, "delete", tf.stringType(), "path");
head = tf.constructor(store, requestType, "head", tf.stringType(), "path");
// TODO: add GET and POST!
}
}