Skip to content

Instantly share code, notes, and snippets.

@MarkTiedemann
Last active July 22, 2021 20:03
Show Gist options
  • Select an option

  • Save MarkTiedemann/44b07a22f5ab6e5d78368b9a8bb8759b to your computer and use it in GitHub Desktop.

Select an option

Save MarkTiedemann/44b07a22f5ab6e5d78368b9a8bb8759b to your computer and use it in GitHub Desktop.
import java.io.*;
import java.net.*;
import java.util.regex.*;
import com.sun.net.httpserver.*;
class Server {
record Document(String name, String date) {}
static String render_rows(Document... dd) {
var r = "";
for (var d : dd)
r += "<tr>\n\t<td>" + escape_html(d.name()) + "\n\t<td>" + d.date() + "\n";
return r;
}
static String escape_html(String s) {
return Pattern.compile("[\"'<>&]").matcher(s)
.replaceAll(m -> "&#" + (int) m.group().charAt(0) + ";");
}
static String render_html() {
return """
<!DOCTYPE html>
<html lang="de">
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title></title>
<link rel="icon" href="data:;base64,=" />
<style>
body {
margin: 0;
min-height: 100vh;
display: grid;
place-content: center;
};
</style>
<table>
<thead>
<tr>
<th>name
<th>date
<tbody>
""" + render_rows(
new Document("<ä>.pdf", "20.07."),
new Document("b.pdf", "21.07.")
) + """
</table>""";
}
public static void main(String[] args) throws IOException {
var server = HttpServer.create(new InetSocketAddress(80), 0);
server.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange ex) throws IOException {
var buf = render_html().getBytes();
ex.sendResponseHeaders(200, buf.length);
var body = ex.getResponseBody();
body.write(buf);
body.close();
}
});
server.setExecutor(null);
server.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment