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,7 +66,8 @@ 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();
needsChange = true; if(addState)
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(needsChange) { if(addState) {
response.cookiesToSet.put("tryumph.data", resp); if(needsChange) {
needsChange = false; 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); res.call(response);
}); });
} }
@ -148,15 +152,14 @@ public class BrowserContext {
return builder.toString(); return builder.toString();
} }
stream = new FileInputStream(file); stream = new FileInputStream(file);
} int i = 0;
while((i = stream.read()) != -1) {
builder.append((char) i);
}
int i = 0; stream.close();
while((i = stream.read()) != -1) {
builder.append((char) i);
} }
stream.close();
if(!TryConfig.nocache) if(!TryConfig.nocache)
cache.put(file, builder.toString()); cache.put(file, builder.toString());
} catch (IOException e) { } catch (IOException e) {
@ -184,18 +187,17 @@ public class BrowserContext {
return builder.toString(); return builder.toString();
} }
stream = new FileInputStream(file); 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) if(!TryConfig.nocache)
cache.put(file, st); cache.put(file, st);
} catch (IOException e) { } catch (IOException e) {