add browser finger printing and getElementById

This commit is contained in:
Daniella / Tove 2022-07-03 18:19:52 +02:00
parent 2cc71500fd
commit 9b6f1f8ff0
6 changed files with 48 additions and 3 deletions

View file

@ -34,6 +34,9 @@
<input type="submit" value="Set!">
<button onclick="data.save()" type="button">Save!</button>
</form>
<h2>Your fingerprint</h2>
<div id="fingerprint"></div>
</div>
</body>
</html>

View file

@ -4,6 +4,8 @@ import java.net.Socket;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import de.tudbut.async.Callback;
import de.tudbut.async.ComposeCallback;
import de.tudbut.async.TaskCallable;
@ -12,6 +14,7 @@ import de.tudbut.tryumph.events.EventListener;
import de.tudbut.tryumph.events.GET;
import de.tudbut.tryumph.events.POST;
import de.tudbut.tryumph.events.Path;
import de.tudbut.tryumph.server.HTMLParsing;
import de.tudbut.tryumph.server.Request;
import de.tudbut.tryumph.server.Response;
import tudbut.parsing.TCN;
@ -55,7 +58,13 @@ public class Main implements IRequestCatcher {
@GET
@Path("/")
public void onIndex(Request request, Callback<Response> res, Callback<Throwable> rej) {
res.call(new Response(request, request.context.file("index.html"), 200, "OK"));
Response r = new Response(request, request.context.file("index.html"), 200, "OK");
System.out.println(r.getHTMLData());
Document html = r.getHTML();
Node node = html.createTextNode(request.fingerPrint());
HTMLParsing.getElementById(html, "fingerprint").appendChild(node);
r.updateHTMLData();
res.call(r);
}
@POST
@ -64,7 +73,12 @@ public class Main implements IRequestCatcher {
TCN query = request.bodyURLEncoded();
request.context.data.set(query.getString("name"), query.getString("value"));
request.context.save();
res.call(new Response(request, request.context.file("index.html"), 200, "OK"));
Response r = new Response(request, request.context.file("index.html"), 200, "OK");
Document html = r.getHTML();
Node node = html.createTextNode(request.fingerPrint());
HTMLParsing.getElementById(html, "fingerprint").appendChild(node);
r.updateHTMLData();
res.call(r);
}
}

View file

@ -5,7 +5,10 @@ import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;
import de.tudbut.tryumph.util.Bug;
@ -13,6 +16,19 @@ import de.tudbut.tryumph.util.Bug;
public class HTMLParsing {
private static Tidy tidy = new Tidy();
public static Element getElementById(Document document, String id) {
NodeList list = document.getElementsByTagName("*");
for(int i = 0; i < list.getLength(); i++) {
Element el = (Element) list.item(i);
Attr attr = el.getAttributeNode("id");
if(attr != null && attr.getNodeValue().equals(id)) {
return el;
}
}
return null;
}
public static Document parse(String html) {
return (Document) tidy.parseDOM(new StringReader(html), (Writer) null);
}

View file

@ -3,6 +3,7 @@ package de.tudbut.tryumph.server;
import java.net.Socket;
import java.util.HashMap;
import de.tudbut.tools.Hasher;
import de.tudbut.tryumph.util.ReadOnlyHashMap;
import tudbut.net.http.HTTPUtils;
import tudbut.parsing.TCN;
@ -37,6 +38,16 @@ public class Request {
splitPath = realPath.split("/");
}
public String fingerPrint() {
StringBuilder data = new StringBuilder();
data.append(headers.get("User-Agent"));
data.append(headers.get("Accept"));
data.append(headers.get("Accept-Encoding"));
data.append(headers.get("Connection"));
data.append(headers.get("Accept-Language"));
return Hasher.sha512hex(data.toString());
}
@Override
public String toString() {
return method + " " + path + " " + httpVersion + "\n" +

View file

@ -57,7 +57,7 @@ public class HTTPRequestReader {
assumeCRLF();
String header;
while(!(header = readUntilCRLF()).isEmpty()) {
boolean hasParameters = header.indexOf(';') != -1;
boolean hasParameters = header.indexOf(';') != -1 && header.indexOf('=') != -1;
String name = header.substring(0, header.indexOf(':'));
String value = HTTPUtils.decodeUTF8(header.substring(
header.indexOf(':') + 2,

View file

@ -6,4 +6,5 @@ center {
margin: auto;
display: block;
max-width: 700px;
word-break: break-all;
}