fix noscript option in FileServer, add cookies option in FileServer

This commit is contained in:
Daniella / Tove 2022-07-15 23:35:45 +02:00
parent ec74401896
commit 1cea71d3ea
2 changed files with 30 additions and 26 deletions

View file

@ -94,7 +94,9 @@ public class FileServer implements IRequestCatcher, RequestHandler.Listener {
@Override @Override
public Task<BrowserContext> processBrowserContext(BrowserContext context) { public Task<BrowserContext> processBrowserContext(BrowserContext context) {
if(data.getBoolean("noscript") != null) if(data.getBoolean("noscript") != null)
context.useJavaScript = !data.getBoolean("noscript"); context.addJavaScript = !data.getBoolean("noscript");
if(data.getBoolean("cookies") != null)
context.addState = data.getBoolean("cookies");
return IRequestCatcher.super.processBrowserContext(context); return IRequestCatcher.super.processBrowserContext(context);
} }

View file

@ -27,8 +27,9 @@ public class BrowserContext {
public final UUID uuid = UUID.randomUUID(); public final UUID uuid = UUID.randomUUID();
public TCN data; public TCN data;
private final IRequestCatcher requestCatcher; private final IRequestCatcher requestCatcher;
public boolean useJavaScript = false;
private boolean needsChange = false; private boolean needsChange = false;
public boolean addState = true;
public boolean addJavaScript = true;
private BrowserContext(IRequestCatcher requestCatcher) { private BrowserContext(IRequestCatcher requestCatcher) {
this.requestCatcher = requestCatcher; this.requestCatcher = requestCatcher;
@ -65,6 +66,7 @@ public class BrowserContext {
private Task<BrowserContext> init() { private Task<BrowserContext> init() {
return t((res, rej) -> { return t((res, rej) -> {
requestCatcher.processBrowserContext(this).err(rej).then(res).ok(); requestCatcher.processBrowserContext(this).err(rej).then(res).ok();
if(addState)
needsChange = true; needsChange = true;
}); });
} }
@ -78,7 +80,7 @@ public class BrowserContext {
public Task<Response> onSend(Response response) { public Task<Response> onSend(Response response) {
return AsyncJSON.write(data) return AsyncJSON.write(data)
.compose((resp, res, rej) -> { .compose((resp, res, rej) -> {
if(response.isHTML) { if(addJavaScript && response.isHTML) {
Document document = response.getHTML(); Document document = response.getHTML();
Element element = document.createElement("script"); Element element = document.createElement("script");
Node text = document.createTextNode( Node text = document.createTextNode(
@ -110,11 +112,13 @@ public class BrowserContext {
head.appendChild(element); head.appendChild(element);
response.updateHTMLData(); response.updateHTMLData();
} }
if(addState) {
if(needsChange) { if(needsChange) {
response.cookiesToSet.put("tryumph.data", resp); response.cookiesToSet.put("tryumph.data", resp);
needsChange = false; needsChange = false;
} }
response.cookiesToSet.put("tryumph.uuid", uuid.toString()); response.cookiesToSet.put("tryumph.uuid", uuid.toString());
}
res.call(response); res.call(response);
}); });
} }
@ -148,14 +152,13 @@ public class BrowserContext {
return builder.toString(); return builder.toString();
} }
stream = new FileInputStream(file); stream = new FileInputStream(file);
}
int i = 0; int i = 0;
while((i = stream.read()) != -1) { while((i = stream.read()) != -1) {
builder.append((char) i); builder.append((char) i);
} }
stream.close(); stream.close();
}
if(!TryConfig.nocache) if(!TryConfig.nocache)
cache.put(file, builder.toString()); cache.put(file, builder.toString());
@ -184,8 +187,6 @@ public class BrowserContext {
return builder.toString(); return builder.toString();
} }
stream = new FileInputStream(file); stream = new FileInputStream(file);
}
ByteArrayOutputStream s = new ByteArrayOutputStream(); ByteArrayOutputStream s = new ByteArrayOutputStream();
int i = 0; int i = 0;
@ -196,6 +197,7 @@ public class BrowserContext {
stream.close(); stream.close();
s.close(); s.close();
st = new String(s.toByteArray()); st = new String(s.toByteArray());
}
if(!TryConfig.nocache) if(!TryConfig.nocache)
cache.put(file, st); cache.put(file, st);
} catch (IOException e) { } catch (IOException e) {