fix noscript option in FileServer, add cookies option in FileServer

This commit is contained in:
Daniella 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
public Task<BrowserContext> processBrowserContext(BrowserContext context) {
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);
}

View file

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