public static void microHttpServer(String hostname, int port, final F2<String, String, List<String>> handler) {
listen(hostname, port, new F2<Void, BufferedReader, DataOutputStream>() {
@Override
public Void execute(BufferedReader in, DataOutputStream out) throws Exception {
List<String> lines = new ArrayList<String>();
String line;
while ((line = in.readLine()) != null) {
if (line.isEmpty()) {
break;
}
lines.add(line);
}
if (!lines.isEmpty()) {
String req = lines.get(0);
if (req.startsWith("GET /")) {
int pos = req.indexOf(' ', 4);
String path = urlDecode(req.substring(4, pos));
String response = handler.execute(path, lines);
out.writeBytes(response);
} else {
out.writeBytes("Only GET requests are supported!");
}
} else {
out.writeBytes("Invalid HTTP request!");
}
return null;
}
});
}